blob: d2a30a7b3f07cc884889555948054acb911e1fc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Matthias Maennich1d082772019-09-06 11:32:31 +010041/* write namespace dependencies */
42static int write_namespace_deps;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010043
Sam Ravnborgc96fca22006-07-01 11:44:23 +020044enum export {
45 export_plain, export_unused, export_gpl,
46 export_unused_gpl, export_gpl_future, export_unknown
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080049/* In kernel, this size is defined in linux/module.h;
50 * here we use Elf_Addr instead of long for covering cross-compile
51 */
52
53#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
54
Andi Kleen6d9a89e2007-11-22 03:43:08 +010055#define PRINTF __attribute__ ((format (printf, 1, 2)))
56
57PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 va_list arglist;
60
61 fprintf(stderr, "FATAL: ");
62
63 va_start(arglist, fmt);
64 vfprintf(stderr, fmt, arglist);
65 va_end(arglist);
66
67 exit(1);
68}
69
Andi Kleen6d9a89e2007-11-22 03:43:08 +010070PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 va_list arglist;
73
74 fprintf(stderr, "WARNING: ");
75
76 va_start(arglist, fmt);
77 vfprintf(stderr, fmt, arglist);
78 va_end(arglist);
79}
80
Andi Kleen6d9a89e2007-11-22 03:43:08 +010081PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060082{
83 va_list arglist;
84
85 fprintf(stderr, "ERROR: ");
86
87 va_start(arglist, fmt);
88 vfprintf(stderr, fmt, arglist);
89 va_end(arglist);
90}
91
Rusty Russelld4ef1c32013-04-04 17:37:32 +103092static inline bool strends(const char *str, const char *postfix)
93{
94 if (strlen(str) < strlen(postfix))
95 return false;
96
97 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
98}
99
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100100static int is_vmlinux(const char *modname)
101{
102 const char *myname;
103
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100104 myname = strrchr(modname, '/');
105 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100106 myname++;
107 else
108 myname = modname;
109
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200110 return (strcmp(myname, "vmlinux") == 0) ||
111 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void *do_nofail(void *ptr, const char *expr)
115{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ptr;
120}
121
122/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static struct module *modules;
124
Masahiro Yamada8b185742018-05-09 18:50:40 +0900125static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct module *mod;
128
129 for (mod = modules; mod; mod = mod->next)
130 if (strcmp(mod->name, modname) == 0)
131 break;
132 return mod;
133}
134
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030135static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030138 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 mod = NOFAIL(malloc(sizeof(*mod)));
141 memset(mod, 0, sizeof(*mod));
142 p = NOFAIL(strdup(modname));
143
144 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030145 if (strends(p, ".o")) {
146 p[strlen(p) - 2] = '\0';
147 mod->is_dot_o = 1;
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* add to list */
151 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200152 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 mod->next = modules;
154 modules = mod;
155
156 return mod;
157}
158
159/* A hash of all exported symbols,
160 * struct symbol is also used for lists of unresolved symbols */
161
162#define SYMBOL_HASH_SIZE 1024
163
164struct symbol {
165 struct symbol *next;
166 struct module *module;
167 unsigned int crc;
168 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900169 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100171 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
172 unsigned int kernel:1; /* 1 if symbol is from kernel
173 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030174 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Denis Efremov15bfc232019-08-01 09:06:57 +0300175 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700176 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 char name[0];
178};
179
180static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
181
182/* This is based on the hash agorithm from gdbm, via tdb */
183static inline unsigned int tdb_hash(const char *name)
184{
185 unsigned value; /* Used to compute the hash value. */
186 unsigned i; /* Used to cycle through random values. */
187
188 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100189 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
191
192 return (1103515243 * value + 12345);
193}
194
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100195/**
196 * Allocate a new symbols for use in the hash of exported symbols or
197 * the list of unresolved symbols per module
198 **/
199static struct symbol *alloc_symbol(const char *name, unsigned int weak,
200 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
203
204 memset(s, 0, sizeof(*s));
205 strcpy(s->name, name);
206 s->weak = weak;
207 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300208 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return s;
210}
211
212/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700213static struct symbol *new_symbol(const char *name, struct module *module,
214 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned int hash;
217 struct symbol *new;
218
219 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
220 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
221 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700222 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100223 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100226static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct symbol *s;
229
230 /* For our purposes, .foo matches foo. PPC64 needs this. */
231 if (name[0] == '.')
232 name++;
233
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100234 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (strcmp(s->name, name) == 0)
236 return s;
237 }
238 return NULL;
239}
240
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100241static bool contains_namespace(struct namespace_list *list,
242 const char *namespace)
243{
244 struct namespace_list *ns_entry;
245
246 for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
247 if (strcmp(ns_entry->namespace, namespace) == 0)
248 return true;
249
250 return false;
251}
252
253static void add_namespace(struct namespace_list **list, const char *namespace)
254{
255 struct namespace_list *ns_entry;
256
257 if (!contains_namespace(*list, namespace)) {
258 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
259 strlen(namespace) + 1));
260 strcpy(ns_entry->namespace, namespace);
261 ns_entry->next = *list;
262 *list = ns_entry;
263 }
264}
265
266static bool module_imports_namespace(struct module *module,
267 const char *namespace)
268{
269 return contains_namespace(module->imported_namespaces, namespace);
270}
271
Mathias Krause7a3ee752014-08-27 20:28:53 +0930272static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700273 const char *str;
274 enum export export;
275} export_list[] = {
276 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200277 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700278 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200279 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700280 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
281 { .str = "(unknown)", .export = export_unknown },
282};
283
284
285static const char *export_str(enum export ex)
286{
287 return export_list[ex].str;
288}
289
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100290static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700291{
292 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100293
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200294 if (!s)
295 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700296 for (i = 0; export_list[i].export != export_unknown; i++) {
297 if (strcmp(export_list[i].str, s) == 0)
298 return export_list[i].export;
299 }
300 return export_unknown;
301}
302
Masahiro Yamada6124c042017-09-06 16:19:05 -0700303static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
304{
305 return (void *)elf->hdr +
306 elf->sechdrs[elf->secindex_strings].sh_offset +
307 sechdr->sh_name;
308}
309
310static const char *sec_name(struct elf_info *elf, int secindex)
311{
312 return sech_name(elf, &elf->sechdrs[secindex]);
313}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200314
315#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
316
317static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
318{
319 const char *secname = sec_name(elf, sec);
320
321 if (strstarts(secname, "___ksymtab+"))
322 return export_plain;
323 else if (strstarts(secname, "___ksymtab_unused+"))
324 return export_unused;
325 else if (strstarts(secname, "___ksymtab_gpl+"))
326 return export_gpl;
327 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
328 return export_unused_gpl;
329 else if (strstarts(secname, "___ksymtab_gpl_future+"))
330 return export_gpl_future;
331 else
332 return export_unknown;
333}
334
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200335static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700336{
337 if (sec == elf->export_sec)
338 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200339 else if (sec == elf->export_unused_sec)
340 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700341 else if (sec == elf->export_gpl_sec)
342 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200343 else if (sec == elf->export_unused_gpl_sec)
344 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700345 else if (sec == elf->export_gpl_future_sec)
346 return export_gpl_future;
347 else
348 return export_unknown;
349}
350
Matthias Maennich69923202019-10-18 10:31:42 +0100351static const char *namespace_from_kstrtabns(struct elf_info *info,
352 Elf_Sym *kstrtabns)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100353{
Matthias Maennich69923202019-10-18 10:31:42 +0100354 char *value = info->ksymtab_strings + kstrtabns->st_value;
355 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100356}
357
Matthias Maennicha2b11182019-10-18 10:31:40 +0100358static void sym_update_namespace(const char *symname, const char *namespace)
359{
360 struct symbol *s = find_symbol(symname);
361
362 /*
363 * That symbol should have been created earlier and thus this is
364 * actually an assertion.
365 */
366 if (!s) {
367 merror("Could not update namespace(%s) for symbol %s\n",
368 namespace, symname);
369 return;
370 }
371
372 free(s->namespace);
373 s->namespace =
374 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
375}
376
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100377/**
378 * Add an exported symbol - it may have already been added without a
379 * CRC, in this case just update the CRC
380 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100381static struct symbol *sym_add_exported(const char *name, struct module *mod,
382 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct symbol *s = find_symbol(name);
385
386 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700387 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100388 } else {
389 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100390 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
391 mod->name, name, s->module->name,
392 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700393 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200394 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700395 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100398 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100399 s->vmlinux = is_vmlinux(mod->name);
400 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700401 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100402 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100405static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700406 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100407{
408 struct symbol *s = find_symbol(name);
409
Rusty Russellb6568b12013-11-07 12:09:13 +1030410 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700411 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030412 /* Don't complain when we find it later. */
413 s->preloaded = 1;
414 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100415 s->crc = crc;
416 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100419void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930422 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int fd;
424
425 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930426 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930428 if (fstat(fd, &st))
429 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 *size = st.st_size;
432 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930434failed:
435 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (map == MAP_FAILED)
437 return NULL;
438 return map;
439}
440
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100441/**
442 * Return a copy of the next line in a mmap'ed file.
443 * spaces in the beginning of the line is trimmed away.
444 * Return a pointer to a static buffer.
445 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100446char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 static char line[4096];
449 int skip = 1;
450 size_t len = 0;
451 signed char *p = (signed char *)file + *pos;
452 char *s = line;
453
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100454 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (skip && isspace(*p)) {
456 p++;
457 continue;
458 }
459 skip = 0;
460 if (*p != '\n' && (*pos < size)) {
461 len++;
462 *s++ = *p++;
463 if (len > 4095)
464 break; /* Too long, stop */
465 } else {
466 /* End of string */
467 *s = '\0';
468 return line;
469 }
470 }
471 /* End of buffer */
472 return NULL;
473}
474
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100475void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 munmap(file, size);
478}
479
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100480static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100483 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 Elf_Shdr *sechdrs;
485 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200486 const char *secstrings;
487 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 hdr = grab_file(filename, &info->size);
490 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930491 if (ignore_missing_files) {
492 fprintf(stderr, "%s: %s (ignored)\n", filename,
493 strerror(errno));
494 return 0;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200497 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100500 if (info->size < sizeof(*hdr)) {
501 /* file too small, assume this is an empty .o file */
502 return 0;
503 }
504 /* Is this a valid ELF file? */
505 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
506 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
507 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
508 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
509 /* Not an ELF file - silently ignore it */
510 return 0;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200513 hdr->e_type = TO_NATIVE(hdr->e_type);
514 hdr->e_machine = TO_NATIVE(hdr->e_machine);
515 hdr->e_version = TO_NATIVE(hdr->e_version);
516 hdr->e_entry = TO_NATIVE(hdr->e_entry);
517 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
518 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
519 hdr->e_flags = TO_NATIVE(hdr->e_flags);
520 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
521 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
522 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
523 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
524 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
525 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sechdrs = (void *)hdr + hdr->e_shoff;
527 info->sechdrs = sechdrs;
528
Petr Stetiara83710e2007-08-27 12:15:07 +0200529 /* Check if file offset is correct */
530 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100531 fatal("section header offset=%lu in file '%s' is bigger than "
532 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
533 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200534 return 0;
535 }
536
Anders Kaseorg68457562011-05-19 16:55:27 -0600537 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 /*
539 * There are more than 64k sections,
540 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200541 */
542 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
543 }
544 else {
545 info->num_sections = hdr->e_shnum;
546 }
547 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600548 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200549 }
550 else {
551 info->secindex_strings = hdr->e_shstrndx;
552 }
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200555 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200556 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
557 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
558 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
559 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
560 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
561 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
562 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
563 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
564 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
565 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200568 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
569 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700570 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900571 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Tejun Heo56fc82c2009-02-06 00:48:02 +0900573 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100574 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
575 "sizeof(*hrd)=%zu\n", filename,
576 (unsigned long)sechdrs[i].sh_offset,
577 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100578 return 0;
579 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700580 secname = secstrings + sechdrs[i].sh_name;
581 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900582 if (nobits)
583 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
585 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700586 } else if (strcmp(secname, "__ksymtab") == 0)
587 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200588 else if (strcmp(secname, "__ksymtab_unused") == 0)
589 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700590 else if (strcmp(secname, "__ksymtab_gpl") == 0)
591 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200592 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
593 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700594 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
595 info->export_gpl_future_sec = i;
Matthias Maennich69923202019-10-18 10:31:42 +0100596 else if (strcmp(secname, "__ksymtab_strings") == 0)
597 info->ksymtab_strings = (void *)hdr +
598 sechdrs[i].sh_offset -
599 sechdrs[i].sh_addr;
Ram Paibd5cbce2006-06-08 22:12:53 -0700600
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200601 if (sechdrs[i].sh_type == SHT_SYMTAB) {
602 unsigned int sh_link_idx;
603 symtab_idx = i;
604 info->symtab_start = (void *)hdr +
605 sechdrs[i].sh_offset;
606 info->symtab_stop = (void *)hdr +
607 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600608 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200609 info->strtab = (void *)hdr +
610 sechdrs[sh_link_idx].sh_offset;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200613 /* 32bit section no. table? ("more than 64k sections") */
614 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
615 symtab_shndx_idx = i;
616 info->symtab_shndx_start = (void *)hdr +
617 sechdrs[i].sh_offset;
618 info->symtab_shndx_stop = (void *)hdr +
619 sechdrs[i].sh_offset + sechdrs[i].sh_size;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100622 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100623 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Fix endianness in symbols */
626 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
627 sym->st_shndx = TO_NATIVE(sym->st_shndx);
628 sym->st_name = TO_NATIVE(sym->st_name);
629 sym->st_value = TO_NATIVE(sym->st_value);
630 sym->st_size = TO_NATIVE(sym->st_size);
631 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200632
633 if (symtab_shndx_idx != ~0U) {
634 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600635 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200636 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600637 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200638 symtab_idx);
639 /* Fix endianness */
640 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
641 p++)
642 *p = TO_NATIVE(*p);
643 }
644
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100645 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100648static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 release_file(info->hdr, info->size);
651}
652
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200653static int ignore_undef_symbol(struct elf_info *info, const char *symname)
654{
655 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900656 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200657 return 1;
658 /* ignore global offset table */
659 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
660 return 1;
661 if (info->hdr->e_machine == EM_PPC)
662 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900663 if (strstarts(symname, "_restgpr_") ||
664 strstarts(symname, "_savegpr_") ||
665 strstarts(symname, "_rest32gpr_") ||
666 strstarts(symname, "_save32gpr_") ||
667 strstarts(symname, "_restvr_") ||
668 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200669 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000670 if (info->hdr->e_machine == EM_PPC64)
671 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900672 if (strstarts(symname, "_restgpr0_") ||
673 strstarts(symname, "_savegpr0_") ||
674 strstarts(symname, "_restvr_") ||
675 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100676 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000677 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200678 /* Do not ignore this symbol */
679 return 0;
680}
681
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100682static void handle_modversions(struct module *mod, struct elf_info *info,
683 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200686 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100687 bool is_crc = false;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900688 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200689
Frank Rowand258f7422012-04-09 17:59:03 -0700690 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900691 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200692 export = export_from_secname(info, get_secindex(info, sym));
693 else
694 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Andi Kleenb5064652013-11-12 15:08:38 -0800696 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900697 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100698 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800699 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000700 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
701 unsigned int *crcp;
702
703 /* symbol points to the CRC in the ELF object */
704 crcp = (void *)info->hdr + sym->st_value +
705 info->sechdrs[sym->st_shndx].sh_offset -
706 (info->hdr->e_type != ET_REL ?
707 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100708 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000709 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900710 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800711 export);
712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 switch (sym->st_shndx) {
715 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900716 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100717 /* Should warn here, but modpost runs before the linker */
718 } else
719 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 case SHN_UNDEF:
722 /* undefined symbol */
723 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
724 ELF_ST_BIND(sym->st_info) != STB_WEAK)
725 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200726 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
Ben Colline8d529012005-08-19 13:44:57 -0700728/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
729#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
730/* add compatibility with older glibc */
731#ifndef STT_SPARC_REGISTER
732#define STT_SPARC_REGISTER STT_REGISTER
733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (info->hdr->e_machine == EM_SPARC ||
735 info->hdr->e_machine == EM_SPARCV9) {
736 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700737 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100739 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700740 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100741 munged[0] = '_';
742 munged[1] = toupper(munged[1]);
743 symname = munged;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100747
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100748 if (is_crc) {
749 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900750 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
751 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100752 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030753 mod->unres = alloc_symbol(symname,
754 ELF_ST_BIND(sym->st_info) == STB_WEAK,
755 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757 default:
758 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900759 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100760 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100761 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900763 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900765 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 mod->has_cleanup = 1;
767 break;
768 }
769}
770
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100771/**
772 * Parse tag=value strings from .modinfo section
773 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static char *next_string(char *string, unsigned long *secsize)
775{
776 /* Skip non-zero chars */
777 while (string[0]) {
778 string++;
779 if ((*secsize)-- <= 1)
780 return NULL;
781 }
782
783 /* Skip any zero padding. */
784 while (!string[0]) {
785 string++;
786 if ((*secsize)-- <= 1)
787 return NULL;
788 }
789 return string;
790}
791
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900792static char *get_next_modinfo(struct elf_info *info, const char *tag,
793 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 char *p;
796 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900797 char *modinfo = info->modinfo;
798 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900800 if (prev) {
801 size -= prev - modinfo;
802 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 for (p = modinfo; p; p = next_string(p, &size)) {
806 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
807 return p + taglen + 1;
808 }
809 return NULL;
810}
811
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900812static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200813
814{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900815 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200816}
817
Sam Ravnborg93684d32006-02-19 11:53:35 +0100818/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100819 * Test if string s ends in string sub
820 * return 0 if match
821 **/
822static int strrcmp(const char *s, const char *sub)
823{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100824 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100825
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100826 if (!s || !sub)
827 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100828
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100829 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100830 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100831
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100832 if ((slen == 0) || (sublen == 0))
833 return 1;
834
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100835 if (sublen > slen)
836 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100837
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100838 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100839}
840
Sam Ravnborgff13f922008-01-23 19:54:27 +0100841static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
842{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100843 if (sym)
844 return elf->strtab + sym->st_name;
845 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100846 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100847}
848
Sam Ravnborg10668222008-01-13 22:21:31 +0100849/* The pattern is an array of simple patterns.
850 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100851 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100852 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930853 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100854 */
Trevor Keith5c725132009-09-22 16:43:38 -0700855static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100856{
857 const char *p;
858 while (*pat) {
859 p = *pat++;
860 const char *endp = p + strlen(p) - 1;
861
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930862 /* "*foo*" */
863 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300864 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
865 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930866
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930867 free(bare);
868 if (here != NULL)
869 return 1;
870 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100871 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930872 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100873 if (strrcmp(sym, p + 1) == 0)
874 return 1;
875 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100876 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100877 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100878 if (strncmp(sym, p, strlen(p) - 1) == 0)
879 return 1;
880 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100881 /* no wildcards */
882 else {
883 if (strcmp(p, sym) == 0)
884 return 1;
885 }
886 }
887 /* no match */
888 return 0;
889}
890
Sam Ravnborg10668222008-01-13 22:21:31 +0100891/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930892static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200893{
894 ".comment*",
895 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930896 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800897 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000898 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200899 ".mdebug*", /* alpha, score, mips etc. */
900 ".pdr", /* alpha, score, mips etc. */
901 ".stab*",
902 ".note*",
903 ".got*",
904 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400905 ".xt.prop", /* xtensa */
906 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030907 ".arcextmap*", /* arc */
908 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030909 ".cmem*", /* EZchip */
910 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100911 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600912 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200913 NULL
914};
Sam Ravnborg10668222008-01-13 22:21:31 +0100915
Sam Ravnborge241a632008-01-28 20:13:13 +0100916/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100918 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400919 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100920 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400921static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900922 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100923{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400924 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100925
Anders Kaseorgb614a692009-04-23 16:49:33 -0400926 if (sechdr->sh_type == SHT_PROGBITS &&
927 !(sechdr->sh_flags & SHF_ALLOC) &&
928 !match(sec, section_white_list)) {
929 warn("%s (%s): unexpected non-allocatable section.\n"
930 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
931 "Note that for example <linux/init.h> contains\n"
932 "section definitions for use in .S files.\n\n",
933 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100934 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100935}
936
937
938
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100939#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930940 ".init.setup", ".init.rodata", ".meminit.rodata", \
941 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100942#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930943 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100944
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100945#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930946 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100947#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930948 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100949
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200950#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930951 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
952 ".pci_fixup_enable", ".pci_fixup_resume", \
953 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200954
Paul Gortmakere24f6622013-06-19 19:30:48 -0400955#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
956#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100957
958#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
959#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100960
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930961#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930962#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700963 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930964#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400965 ".fixup", ".entry.text", ".exception.text", ".text.*", \
966 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100967
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000969#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100970
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000971#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000972#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100973
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930974#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
975 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
976
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_data_sections[] =
979 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
981/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930982static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100983
984/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930985static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100986 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930988/* all text sections */
989static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
990
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930992static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994
995/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100996#define DEFAULT_SYMBOL_WHITE_LIST \
997 "*driver", \
998 "*_template", /* scsi uses *_template a lot */ \
999 "*_timer", /* arm uses ops structures named _timer a lot */ \
1000 "*_sht", /* scsi also used *_sht to some extent */ \
1001 "*_ops", \
1002 "*_probe", \
1003 "*_probe_one", \
1004 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001005
Mathias Krause7a3ee752014-08-27 20:28:53 +09301006static const char *const head_sections[] = { ".head.text*", NULL };
1007static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001008 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301009static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001010
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001011enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001012 TEXT_TO_ANY_INIT,
1013 DATA_TO_ANY_INIT,
1014 TEXT_TO_ANY_EXIT,
1015 DATA_TO_ANY_EXIT,
1016 XXXINIT_TO_SOME_INIT,
1017 XXXEXIT_TO_SOME_EXIT,
1018 ANY_INIT_TO_ANY_EXIT,
1019 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001020 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301021 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001022};
1023
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301024/**
1025 * Describe how to match sections on different criterias:
1026 *
1027 * @fromsec: Array of sections to be matched.
1028 *
1029 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1030 * this array is forbidden (black-list). Can be empty.
1031 *
1032 * @good_tosec: Relocations applied to a section in @fromsec must be
1033 * targetting sections in this array (white-list). Can be empty.
1034 *
1035 * @mismatch: Type of mismatch.
1036 *
1037 * @symbol_white_list: Do not match a relocation to a symbol in this list
1038 * even if it is targetting a section in @bad_to_sec.
1039 *
1040 * @handler: Specific handler to call when a match is found. If NULL,
1041 * default_mismatch_handler() will be called.
1042 *
1043 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001044struct sectioncheck {
1045 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301046 const char *bad_tosec[20];
1047 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001048 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001049 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301050 void (*handler)(const char *modname, struct elf_info *elf,
1051 const struct sectioncheck* const mismatch,
1052 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1053
Sam Ravnborg10668222008-01-13 22:21:31 +01001054};
1055
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301056static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1057 const struct sectioncheck* const mismatch,
1058 Elf_Rela *r, Elf_Sym *sym,
1059 const char *fromsec);
1060
Mathias Krause7a3ee752014-08-27 20:28:53 +09301061static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001062/* Do not reference init/exit code/data from
1063 * normal code and data
1064 */
1065{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001066 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301067 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001068 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001069 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001070},
1071{
1072 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001076},
1077{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001078 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301079 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001080 .mismatch = DATA_TO_ANY_INIT,
1081 .symbol_white_list = {
1082 "*_template", "*_timer", "*_sht", "*_ops",
1083 "*_probe", "*_probe_one", "*_console", NULL
1084 },
1085},
1086{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001087 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301088 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001089 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001090 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001091},
1092{
1093 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = DATA_TO_ANY_EXIT,
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 init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001100 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001104},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001105/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001106{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001107 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = XXXEXIT_TO_SOME_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 exit code/data from init code */
1113{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001114 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301115 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001116 .mismatch = ANY_INIT_TO_ANY_EXIT,
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},
1119/* Do not use init code/data from exit code */
1120{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001121 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301122 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001123 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001124 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001125},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001126{
1127 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301128 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001129 .mismatch = ANY_INIT_TO_ANY_EXIT,
1130 .symbol_white_list = { NULL },
1131},
Sam Ravnborg10668222008-01-13 22:21:31 +01001132/* Do not export init/exit functions or data */
1133{
1134 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301135 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001136 .mismatch = EXPORT_TO_INIT_EXIT,
1137 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301138},
1139{
1140 .fromsec = { "__ex_table", NULL },
1141 /* If you're adding any new black-listed sections in here, consider
1142 * adding a special 'printer' for them in scripts/check_extable.
1143 */
1144 .bad_tosec = { ".altinstr_replacement", NULL },
1145 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1146 .mismatch = EXTABLE_TO_NON_TEXT,
1147 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001148}
1149};
1150
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001151static const struct sectioncheck *section_mismatch(
1152 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001153{
1154 int i;
1155 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1156 const struct sectioncheck *check = &sectioncheck[0];
1157
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301158 /*
1159 * The target section could be the SHT_NUL section when we're
1160 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001161 * doesn't make much sense and causes build failures on parisc
1162 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301163 */
1164 if (*tosec == '\0')
1165 return NULL;
1166
Sam Ravnborg10668222008-01-13 22:21:31 +01001167 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301168 if (match(fromsec, check->fromsec)) {
1169 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1170 return check;
1171 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1172 return check;
1173 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001174 check++;
1175 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001176 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001177}
1178
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001179/**
1180 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001181 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001182 * Pattern 1:
1183 * If a module parameter is declared __initdata and permissions=0
1184 * then this is legal despite the warning generated.
1185 * We cannot see value of permissions here, so just ignore
1186 * this pattern.
1187 * The pattern is identified by:
1188 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001189 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001190 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001191 *
Rusty Russell6a841522010-08-11 23:04:16 -06001192 * Pattern 1a:
1193 * module_param_call() ops can refer to __init set function if permissions=0
1194 * The pattern is identified by:
1195 * tosec = .init.text
1196 * fromsec = .data*
1197 * atsym = __param_ops_*
1198 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001199 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001200 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001201 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001202 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001203 * tosec = init or exit section
1204 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001205 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1206 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001207 *
1208 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001209 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001210 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001211 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001212 * Some symbols belong to init section but still it is ok to reference
1213 * these from non-init sections as these symbols don't have any memory
1214 * allocated for them and symbol address and value are same. So even
1215 * if init section is freed, its ok to reference those symbols.
1216 * For ex. symbols marking the init section boundaries.
1217 * This pattern is identified by
1218 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001219 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301220 * Pattern 5:
1221 * GCC may optimize static inlines when fed constant arg(s) resulting
1222 * in functions like cpumask_empty() -- generating an associated symbol
1223 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1224 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1225 * meaningless section warning. May need to add isra symbols too...
1226 * This pattern is identified by
1227 * tosec = init section
1228 * fromsec = text section
1229 * refsymname = *.constprop.*
1230 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001231 * Pattern 6:
1232 * Hide section mismatch warnings for ELF local symbols. The goal
1233 * is to eliminate false positive modpost warnings caused by
1234 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1235 * Autogenerated symbol names bypass modpost's "Pattern 2"
1236 * whitelisting, which relies on pattern-matching against symbol
1237 * names to work. (One situation where gcc can autogenerate ELF
1238 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001239 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001240static int secref_whitelist(const struct sectioncheck *mismatch,
1241 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001242 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001243{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001245 if (match(tosec, init_data_sections) &&
1246 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001247 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001248 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001249
Rusty Russell6a841522010-08-11 23:04:16 -06001250 /* Check for pattern 1a */
1251 if (strcmp(tosec, ".init.text") == 0 &&
1252 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001253 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001254 return 0;
1255
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001256 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001257 if (match(tosec, init_exit_sections) &&
1258 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001259 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001260 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001261
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001262 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001263 if (match(fromsec, head_sections) &&
1264 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001265 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001266
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001267 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001268 if (match(tosym, linker_symbols))
1269 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001270
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301271 /* Check for pattern 5 */
1272 if (match(fromsec, text_sections) &&
1273 match(tosec, init_sections) &&
1274 match(fromsym, optim_symbols))
1275 return 0;
1276
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001277 /* Check for pattern 6 */
1278 if (strstarts(fromsym, ".L"))
1279 return 0;
1280
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001281 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001282}
1283
Sami Tolvanen5818c682018-10-23 15:15:35 -07001284static inline int is_arm_mapping_symbol(const char *str)
1285{
1286 return str[0] == '$' && strchr("axtd", str[1])
1287 && (str[2] == '\0' || str[2] == '.');
1288}
1289
1290/*
1291 * If there's no name there, ignore it; likewise, ignore it if it's
1292 * one of the magic symbols emitted used by current ARM tools.
1293 *
1294 * Otherwise if find_symbols_between() returns those symbols, they'll
1295 * fail the whitelist tests and cause lots of false alarms ... fixable
1296 * only by merging __exit and __init sections into __text, bloating
1297 * the kernel (which is especially evil on embedded platforms).
1298 */
1299static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1300{
1301 const char *name = elf->strtab + sym->st_name;
1302
1303 if (!name || !strlen(name))
1304 return 0;
1305 return !is_arm_mapping_symbol(name);
1306}
1307
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001308/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309 * Find symbol based on relocation record info.
1310 * In some cases the symbol supplied is a valid symbol so
1311 * return refsym. If st_name != 0 we assume this is a valid symbol.
1312 * In other cases the symbol needs to be looked up in the symbol table
1313 * based on section and address.
1314 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001315static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316 Elf_Sym *relsym)
1317{
1318 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001319 Elf_Sym *near = NULL;
1320 Elf64_Sword distance = 20;
1321 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001323
1324 if (relsym->st_name != 0)
1325 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001326
1327 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001329 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001330 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001331 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1332 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001333 if (!is_valid_name(elf, sym))
1334 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001335 if (sym->st_value == addr)
1336 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001337 /* Find a symbol nearby - addr are maybe negative */
1338 d = sym->st_value - addr;
1339 if (d < 0)
1340 d = addr - sym->st_value;
1341 if (d < distance) {
1342 distance = d;
1343 near = sym;
1344 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001345 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001346 /* We need a close match */
1347 if (distance < 20)
1348 return near;
1349 else
1350 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001351}
1352
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001353/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001354 * Find symbols before or equal addr and after addr - in the section sec.
1355 * If we find two symbols with equal offset prefer one with a valid name.
1356 * The ELF format may have a better way to detect what type of symbol
1357 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001358 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001359static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1360 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001361{
1362 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001364 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001365
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1367 const char *symsec;
1368
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001369 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001371 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372 if (strcmp(symsec, sec) != 0)
1373 continue;
David Brownellda68d612007-02-20 13:58:16 -08001374 if (!is_valid_name(elf, sym))
1375 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001376 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001377 if ((addr - sym->st_value) < distance) {
1378 distance = addr - sym->st_value;
1379 near = sym;
1380 } else if ((addr - sym->st_value) == distance) {
1381 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001382 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001383 }
1384 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001385 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001386}
1387
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001388/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389 * Convert a section name to the function/data attribute
1390 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001391 * .memexitconst => __memconst
1392 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001393 *
1394 * The memory of returned value has been allocated on a heap. The user of this
1395 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001396*/
1397static char *sec2annotation(const char *s)
1398{
1399 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001400 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001401 char *r = p;
1402
1403 *p++ = '_';
1404 *p++ = '_';
1405 if (*s == '.')
1406 s++;
1407 while (*s && *s != '.')
1408 *p++ = *s++;
1409 *p = '\0';
1410 if (*s == '.')
1411 s++;
1412 if (strstr(s, "rodata") != NULL)
1413 strcat(p, "const ");
1414 else if (strstr(s, "data") != NULL)
1415 strcat(p, "data ");
1416 else
1417 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001418 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001419 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001420 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001421 }
1422}
1423
1424static int is_function(Elf_Sym *sym)
1425{
1426 if (sym)
1427 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1428 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001429 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001430}
1431
Randy Dunlap00759c02011-03-15 14:13:47 -07001432static void print_section_list(const char * const list[20])
1433{
1434 const char *const *s = list;
1435
1436 while (*s) {
1437 fprintf(stderr, "%s", *s);
1438 s++;
1439 if (*s)
1440 fprintf(stderr, ", ");
1441 }
1442 fprintf(stderr, "\n");
1443}
1444
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301445static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1446{
1447 switch (is_func) {
1448 case 0: *name = "variable"; *name_p = ""; break;
1449 case 1: *name = "function"; *name_p = "()"; break;
1450 default: *name = "(unknown reference)"; *name_p = ""; break;
1451 }
1452}
1453
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001455 * Print a warning about a section mismatch.
1456 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001457 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001458 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001459static void report_sec_mismatch(const char *modname,
1460 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001461 const char *fromsec,
1462 unsigned long long fromaddr,
1463 const char *fromsym,
1464 int from_is_func,
1465 const char *tosec, const char *tosym,
1466 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001467{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001468 const char *from, *from_p;
1469 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001470 char *prl_from;
1471 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001472
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001473 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001474
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301475 get_pretty_name(from_is_func, &from, &from_p);
1476 get_pretty_name(to_is_func, &to, &to_p);
1477
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001478 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1479 "to the %s %s:%s%s\n",
1480 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1481 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001483 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001484 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_from = sec2annotation(fromsec);
1486 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001487 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001488 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489 "the %s %s%s%s.\n"
1490 "This is often because %s lacks a %s\n"
1491 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 prl_from, fromsym,
1493 to, prl_to, tosym, to_p,
1494 fromsym, prl_to, tosym);
1495 free(prl_from);
1496 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001497 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001498 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001499 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001500 fprintf(stderr,
1501 "The variable %s references\n"
1502 "the %s %s%s%s\n"
1503 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001504 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001506 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001507 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001510 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001511 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001513 fprintf(stderr,
1514 "The function %s() references a %s in an exit section.\n"
1515 "Often the %s %s%s has valid usage outside the exit section\n"
1516 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001517 fromsym, to, to, tosym, to_p, prl_to, tosym);
1518 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001519 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001520 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001522 fprintf(stderr,
1523 "The variable %s references\n"
1524 "the %s %s%s%s\n"
1525 "If the reference is valid then annotate the\n"
1526 "variable with __exit* (see linux/init.h) or "
1527 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001529 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001530 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 break;
1532 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001533 case XXXINIT_TO_SOME_INIT:
1534 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 prl_from = sec2annotation(fromsec);
1536 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001537 fprintf(stderr,
1538 "The %s %s%s%s references\n"
1539 "a %s %s%s%s.\n"
1540 "If %s is only used by %s then\n"
1541 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 from, prl_from, fromsym, from_p,
1543 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001544 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 free(prl_from);
1546 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001548 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001549 prl_from = sec2annotation(fromsec);
1550 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001551 fprintf(stderr,
1552 "The %s %s%s%s references\n"
1553 "a %s %s%s%s.\n"
1554 "This is often seen when error handling "
1555 "in the init function\n"
1556 "uses functionality in the exit path.\n"
1557 "The fix is often to remove the %sannotation of\n"
1558 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001559 from, prl_from, fromsym, from_p,
1560 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001561 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 free(prl_from);
1563 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001565 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001566 prl_from = sec2annotation(fromsec);
1567 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001568 fprintf(stderr,
1569 "The %s %s%s%s references\n"
1570 "a %s %s%s%s.\n"
1571 "This is often seen when error handling "
1572 "in the exit function\n"
1573 "uses functionality in the init path.\n"
1574 "The fix is often to remove the %sannotation of\n"
1575 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001576 from, prl_from, fromsym, from_p,
1577 to, prl_to, tosym, to_p,
1578 prl_to, tosym, to_p);
1579 free(prl_from);
1580 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001581 break;
1582 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001583 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001584 fprintf(stderr,
1585 "The symbol %s is exported and annotated %s\n"
1586 "Fix this by removing the %sannotation of %s "
1587 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001588 tosym, prl_to, prl_to, tosym);
1589 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001590 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301591 case EXTABLE_TO_NON_TEXT:
1592 fatal("There's a special handler for this mismatch type, "
1593 "we should never get here.");
1594 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001595 }
1596 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001597}
1598
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301599static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1600 const struct sectioncheck* const mismatch,
1601 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1602{
1603 const char *tosec;
1604 Elf_Sym *to;
1605 Elf_Sym *from;
1606 const char *tosym;
1607 const char *fromsym;
1608
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301609 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1610 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301611
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001612 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301613 return;
1614
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301615 tosec = sec_name(elf, get_secindex(elf, sym));
1616 to = find_elf_symbol(elf, r->r_addend, sym);
1617 tosym = sym_name(elf, to);
1618
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301619 /* check whitelist - we may ignore it */
1620 if (secref_whitelist(mismatch,
1621 fromsec, fromsym, tosec, tosym)) {
1622 report_sec_mismatch(modname, mismatch,
1623 fromsec, r->r_offset, fromsym,
1624 is_function(from), tosec, tosym,
1625 is_function(to));
1626 }
1627}
1628
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301629static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1630{
1631 if (section_index > elf->num_sections)
1632 fatal("section_index is outside elf->num_sections!\n");
1633
1634 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1635}
1636
1637/*
1638 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1639 * to know the sizeof(struct exception_table_entry) for the target architecture.
1640 */
1641static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301642static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301643{
1644 /*
1645 * If we're currently checking the second relocation within __ex_table,
1646 * that relocation offset tells us the offsetof(struct
1647 * exception_table_entry, fixup) which is equal to sizeof(struct
1648 * exception_table_entry) divided by two. We use that to our advantage
1649 * since there's no portable way to get that size as every architecture
1650 * seems to go with different sized types. Not pretty but better than
1651 * hard-coding the size for every architecture..
1652 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301653 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301654 extable_entry_size = r->r_offset * 2;
1655}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301656
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301657static inline bool is_extable_fault_address(Elf_Rela *r)
1658{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301659 /*
1660 * extable_entry_size is only discovered after we've handled the
1661 * _second_ relocation in __ex_table, so only abort when we're not
1662 * handling the first reloc and extable_entry_size is zero.
1663 */
1664 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301665 fatal("extable_entry size hasn't been discovered!\n");
1666
1667 return ((r->r_offset == 0) ||
1668 (r->r_offset % extable_entry_size == 0));
1669}
1670
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301671#define is_second_extable_reloc(Start, Cur, Sec) \
1672 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1673
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301674static void report_extable_warnings(const char* modname, struct elf_info* elf,
1675 const struct sectioncheck* const mismatch,
1676 Elf_Rela* r, Elf_Sym* sym,
1677 const char* fromsec, const char* tosec)
1678{
1679 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1680 const char* fromsym_name = sym_name(elf, fromsym);
1681 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1682 const char* tosym_name = sym_name(elf, tosym);
1683 const char* from_pretty_name;
1684 const char* from_pretty_name_p;
1685 const char* to_pretty_name;
1686 const char* to_pretty_name_p;
1687
1688 get_pretty_name(is_function(fromsym),
1689 &from_pretty_name, &from_pretty_name_p);
1690 get_pretty_name(is_function(tosym),
1691 &to_pretty_name, &to_pretty_name_p);
1692
1693 warn("%s(%s+0x%lx): Section mismatch in reference"
1694 " from the %s %s%s to the %s %s:%s%s\n",
1695 modname, fromsec, (long)r->r_offset, from_pretty_name,
1696 fromsym_name, from_pretty_name_p,
1697 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1698
1699 if (!match(tosec, mismatch->bad_tosec) &&
1700 is_executable_section(elf, get_secindex(elf, sym)))
1701 fprintf(stderr,
1702 "The relocation at %s+0x%lx references\n"
1703 "section \"%s\" which is not in the list of\n"
1704 "authorized sections. If you're adding a new section\n"
1705 "and/or if this reference is valid, add \"%s\" to the\n"
1706 "list of authorized sections to jump to on fault.\n"
1707 "This can be achieved by adding \"%s\" to \n"
1708 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1709 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1710}
1711
1712static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1713 const struct sectioncheck* const mismatch,
1714 Elf_Rela* r, Elf_Sym* sym,
1715 const char *fromsec)
1716{
1717 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1718
1719 sec_mismatch_count++;
1720
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001721 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301722
1723 if (match(tosec, mismatch->bad_tosec))
1724 fatal("The relocation at %s+0x%lx references\n"
1725 "section \"%s\" which is black-listed.\n"
1726 "Something is seriously wrong and should be fixed.\n"
1727 "You might get more information about where this is\n"
1728 "coming from by using scripts/check_extable.sh %s\n",
1729 fromsec, (long)r->r_offset, tosec, modname);
1730 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1731 if (is_extable_fault_address(r))
1732 fatal("The relocation at %s+0x%lx references\n"
1733 "section \"%s\" which is not executable, IOW\n"
1734 "it is not possible for the kernel to fault\n"
1735 "at that address. Something is seriously wrong\n"
1736 "and should be fixed.\n",
1737 fromsec, (long)r->r_offset, tosec);
1738 else
1739 fatal("The relocation at %s+0x%lx references\n"
1740 "section \"%s\" which is not executable, IOW\n"
1741 "the kernel will fault if it ever tries to\n"
1742 "jump to it. Something is seriously wrong\n"
1743 "and should be fixed.\n",
1744 fromsec, (long)r->r_offset, tosec);
1745 }
1746}
1747
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001748static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001749 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001750{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001751 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301752 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001753
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001754 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301755 if (mismatch->handler)
1756 mismatch->handler(modname, elf, mismatch,
1757 r, sym, fromsec);
1758 else
1759 default_mismatch_handler(modname, elf, mismatch,
1760 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001761 }
1762}
1763
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001765 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001766{
1767 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001768 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001769
1770 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001771 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001772}
1773
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001774static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001775{
1776 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001777 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001778
1779 switch (r_typ) {
1780 case R_386_32:
1781 r->r_addend = TO_NATIVE(*location);
1782 break;
1783 case R_386_PC32:
1784 r->r_addend = TO_NATIVE(*location) + 4;
1785 /* For CONFIG_RELOCATABLE=y */
1786 if (elf->hdr->e_type == ET_EXEC)
1787 r->r_addend += r->r_offset;
1788 break;
1789 }
1790 return 0;
1791}
1792
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001793#ifndef R_ARM_CALL
1794#define R_ARM_CALL 28
1795#endif
1796#ifndef R_ARM_JUMP24
1797#define R_ARM_JUMP24 29
1798#endif
1799
David A. Longc9698e52014-02-14 22:41:18 +01001800#ifndef R_ARM_THM_CALL
1801#define R_ARM_THM_CALL 10
1802#endif
1803#ifndef R_ARM_THM_JUMP24
1804#define R_ARM_THM_JUMP24 30
1805#endif
1806#ifndef R_ARM_THM_JUMP19
1807#define R_ARM_THM_JUMP19 51
1808#endif
1809
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001810static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001811{
1812 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1813
1814 switch (r_typ) {
1815 case R_ARM_ABS32:
1816 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001817 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001818 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001819 break;
1820 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001821 case R_ARM_CALL:
1822 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001823 case R_ARM_THM_CALL:
1824 case R_ARM_THM_JUMP24:
1825 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001826 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001827 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001828 sechdr->sh_offset +
1829 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001830 break;
1831 default:
1832 return 1;
1833 }
1834 return 0;
1835}
1836
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001837static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001838{
1839 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001840 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001841 unsigned int inst;
1842
1843 if (r_typ == R_MIPS_HI16)
1844 return 1; /* skip this */
1845 inst = TO_NATIVE(*location);
1846 switch (r_typ) {
1847 case R_MIPS_LO16:
1848 r->r_addend = inst & 0xffff;
1849 break;
1850 case R_MIPS_26:
1851 r->r_addend = (inst & 0x03ffffff) << 2;
1852 break;
1853 case R_MIPS_32:
1854 r->r_addend = inst;
1855 break;
1856 }
1857 return 0;
1858}
1859
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001860static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001861 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862{
1863 Elf_Sym *sym;
1864 Elf_Rela *rela;
1865 Elf_Rela r;
1866 unsigned int r_sym;
1867 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868
Sam Ravnborgff13f922008-01-23 19:54:27 +01001869 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001870 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1871
Sam Ravnborgff13f922008-01-23 19:54:27 +01001872 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001873 fromsec += strlen(".rela");
1874 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001875 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001876 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001877
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001878 for (rela = start; rela < stop; rela++) {
1879 r.r_offset = TO_NATIVE(rela->r_offset);
1880#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001881 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001882 unsigned int r_typ;
1883 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1884 r_sym = TO_NATIVE(r_sym);
1885 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1886 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1887 } else {
1888 r.r_info = TO_NATIVE(rela->r_info);
1889 r_sym = ELF_R_SYM(r.r_info);
1890 }
1891#else
1892 r.r_info = TO_NATIVE(rela->r_info);
1893 r_sym = ELF_R_SYM(r.r_info);
1894#endif
1895 r.r_addend = TO_NATIVE(rela->r_addend);
1896 sym = elf->symtab_start + r_sym;
1897 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001898 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301900 if (is_second_extable_reloc(start, rela, fromsec))
1901 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001902 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903 }
1904}
1905
1906static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001907 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001908{
1909 Elf_Sym *sym;
1910 Elf_Rel *rel;
1911 Elf_Rela r;
1912 unsigned int r_sym;
1913 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001914
Sam Ravnborgff13f922008-01-23 19:54:27 +01001915 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001916 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1917
Sam Ravnborgff13f922008-01-23 19:54:27 +01001918 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001919 fromsec += strlen(".rel");
1920 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001921 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001922 return;
1923
1924 for (rel = start; rel < stop; rel++) {
1925 r.r_offset = TO_NATIVE(rel->r_offset);
1926#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001927 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001928 unsigned int r_typ;
1929 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1930 r_sym = TO_NATIVE(r_sym);
1931 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1932 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1933 } else {
1934 r.r_info = TO_NATIVE(rel->r_info);
1935 r_sym = ELF_R_SYM(r.r_info);
1936 }
1937#else
1938 r.r_info = TO_NATIVE(rel->r_info);
1939 r_sym = ELF_R_SYM(r.r_info);
1940#endif
1941 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001942 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001943 case EM_386:
1944 if (addend_386_rel(elf, sechdr, &r))
1945 continue;
1946 break;
1947 case EM_ARM:
1948 if (addend_arm_rel(elf, sechdr, &r))
1949 continue;
1950 break;
1951 case EM_MIPS:
1952 if (addend_mips_rel(elf, sechdr, &r))
1953 continue;
1954 break;
1955 }
1956 sym = elf->symtab_start + r_sym;
1957 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001958 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001959 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301960 if (is_second_extable_reloc(start, rel, fromsec))
1961 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001962 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001963 }
1964}
1965
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966/**
1967 * A module includes a number of sections that are discarded
1968 * either when loaded or when used as built-in.
1969 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001970 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001971 * Likewise for modules used built-in the sections marked __exit
1972 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001973 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001974 * The check_sec_ref() function traverses all relocation records
1975 * to find all references to a section that reference a section that will
1976 * be discarded and warns about it.
1977 **/
1978static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001979 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980{
1981 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001982 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001983
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001984 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001985 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001986 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001987 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001988 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001989 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001990 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001991 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001992 }
1993}
1994
Andi Kleen7d02b492014-02-08 09:01:12 +01001995static char *remove_dot(char *s)
1996{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301997 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001998
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301999 if (n && s[n]) {
2000 size_t m = strspn(s + n + 1, "0123456789");
2001 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002002 s[n] = 0;
2003 }
2004 return s;
2005}
2006
Masahiro Yamada8b185742018-05-09 18:50:40 +09002007static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 const char *symname;
2010 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002011 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002012 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct module *mod;
2014 struct elf_info info = { };
2015 Elf_Sym *sym;
2016
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002017 if (!parse_elf(&info, modname))
2018 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 mod = new_module(modname);
2021
2022 /* When there's no vmlinux, don't print warnings about
2023 * unresolved symbols (since there'll be too many ;) */
2024 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 mod->skip = 1;
2027 }
2028
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002029 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002030 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002031 warn("modpost: missing MODULE_LICENSE() in %s\n"
2032 "see include/linux/module.h for "
2033 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002034 while (license) {
2035 if (license_is_gpl_compatible(license))
2036 mod->gpl_compatible = 1;
2037 else {
2038 mod->gpl_compatible = 0;
2039 break;
2040 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002041 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002042 }
2043
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002044 namespace = get_modinfo(&info, "import_ns");
2045 while (namespace) {
2046 add_namespace(&mod->imported_namespaces, namespace);
2047 namespace = get_next_modinfo(&info, "import_ns", namespace);
2048 }
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002051 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 handle_modversions(mod, &info, sym, symname);
2054 handle_moddevtable(mod, &info, sym, symname);
2055 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002056
Matthias Maennich69923202019-10-18 10:31:42 +01002057 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2058 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2059 symname = remove_dot(info.strtab + sym->st_name);
2060
2061 if (strstarts(symname, "__kstrtabns_"))
2062 sym_update_namespace(symname + strlen("__kstrtabns_"),
2063 namespace_from_kstrtabns(&info,
2064 sym));
2065 }
2066
Denis Efremov15bfc232019-08-01 09:06:57 +03002067 // check for static EXPORT_SYMBOL_* functions && global vars
2068 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2069 unsigned char bind = ELF_ST_BIND(sym->st_info);
2070
2071 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2072 struct symbol *s =
2073 find_symbol(remove_dot(info.strtab +
2074 sym->st_name));
2075
2076 if (s)
2077 s->is_static = 0;
2078 }
2079 }
2080
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002081 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002082 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002084 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (version)
2086 maybe_frob_rcs_version(modname, version, info.modinfo,
2087 version - (char *)info.hdr);
2088 if (version || (all_versions && !is_vmlinux(modname)))
2089 get_src_version(modname, mod->srcversion,
2090 sizeof(mod->srcversion)-1);
2091
2092 parse_elf_finish(&info);
2093
Rusty Russell8c8ef422009-03-31 13:05:34 -06002094 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * never passed as an argument to an exported function, so
2096 * the automatic versioning doesn't pick it up, but it's really
2097 * important anyhow */
2098 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002099 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Rusty Russell712f9b42013-04-04 17:37:38 +10302102static void read_symbols_from_files(const char *filename)
2103{
2104 FILE *in = stdin;
2105 char fname[PATH_MAX];
2106
2107 if (strcmp(filename, "-") != 0) {
2108 in = fopen(filename, "r");
2109 if (!in)
2110 fatal("Can't open filenames file %s: %m", filename);
2111 }
2112
2113 while (fgets(fname, PATH_MAX, in) != NULL) {
2114 if (strends(fname, "\n"))
2115 fname[strlen(fname)-1] = '\0';
2116 read_symbols(fname);
2117 }
2118
2119 if (in != stdin)
2120 fclose(in);
2121}
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123#define SZ 500
2124
2125/* We first write the generated file into memory using the
2126 * following helper, then compare to the file on disk and
2127 * only update the later if anything changed */
2128
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002129void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2130 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 char tmp[SZ];
2133 int len;
2134 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 va_start(ap, fmt);
2137 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002138 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 va_end(ap);
2140}
2141
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002142void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002145 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002146 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 strncpy(buf->p + buf->pos, s, len);
2149 buf->pos += len;
2150}
2151
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002152static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2153{
2154 const char *e = is_vmlinux(m) ?"":".ko";
2155
2156 switch (exp) {
2157 case export_gpl:
2158 fatal("modpost: GPL-incompatible module %s%s "
2159 "uses GPL-only symbol '%s'\n", m, e, s);
2160 break;
2161 case export_unused_gpl:
2162 fatal("modpost: GPL-incompatible module %s%s "
2163 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2164 break;
2165 case export_gpl_future:
2166 warn("modpost: GPL-incompatible module %s%s "
2167 "uses future GPL-only symbol '%s'\n", m, e, s);
2168 break;
2169 case export_plain:
2170 case export_unused:
2171 case export_unknown:
2172 /* ignore */
2173 break;
2174 }
2175}
2176
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002177static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002178{
2179 const char *e = is_vmlinux(m) ?"":".ko";
2180
2181 switch (exp) {
2182 case export_unused:
2183 case export_unused_gpl:
2184 warn("modpost: module %s%s "
2185 "uses symbol '%s' marked UNUSED\n", m, e, s);
2186 break;
2187 default:
2188 /* ignore */
2189 break;
2190 }
2191}
2192
Masahiro Yamada3b415282018-11-23 16:57:23 +09002193static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002194{
2195 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002197
2198 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002199 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002200 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002201 if (!exp || exp->module == mod) {
2202 if (have_vmlinux && !s->weak) {
2203 if (warn_unresolved) {
2204 warn("\"%s\" [%s.ko] undefined!\n",
2205 s->name, mod->name);
2206 } else {
2207 merror("\"%s\" [%s.ko] undefined!\n",
2208 s->name, mod->name);
2209 err = 1;
2210 }
2211 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002212 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002213 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002214 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002215 if (basename)
2216 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002217 else
2218 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002219
Matthias Maennicha2b11182019-10-18 10:31:40 +01002220 if (exp->namespace) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002221 add_namespace(&mod->required_namespaces,
2222 exp->namespace);
2223
2224 if (!write_namespace_deps &&
2225 !module_imports_namespace(mod, exp->namespace)) {
2226 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2227 basename, exp->name, exp->namespace);
2228 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002229 }
2230
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002231 if (!mod->gpl_compatible)
2232 check_for_gpl_usage(exp->export, basename, exp->name);
2233 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002234 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002235
2236 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002237}
2238
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002239static int check_modname_len(struct module *mod)
2240{
2241 const char *mod_name;
2242
2243 mod_name = strrchr(mod->name, '/');
2244 if (mod_name == NULL)
2245 mod_name = mod->name;
2246 else
2247 mod_name++;
2248 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2249 merror("module name is too long [%s.ko]\n", mod->name);
2250 return 1;
2251 }
2252
2253 return 0;
2254}
2255
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002256/**
2257 * Header for the generated file
2258 **/
2259static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Laura Abbott9afb7192018-07-05 17:49:37 -07002261 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 buf_printf(b, "#include <linux/module.h>\n");
2263 buf_printf(b, "#include <linux/vermagic.h>\n");
2264 buf_printf(b, "#include <linux/compiler.h>\n");
2265 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002266 buf_printf(b, "BUILD_SALT;\n");
2267 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002269 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302271 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002272 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002273 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002275 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (mod->has_cleanup)
2277 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002278 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002280 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 buf_printf(b, "};\n");
2282}
2283
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002284static void add_intree_flag(struct buffer *b, int is_intree)
2285{
2286 if (is_intree)
2287 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2288}
2289
Andi Kleencaf75012018-01-25 15:50:28 -08002290/* Cannot check for assembler */
2291static void add_retpoline(struct buffer *b)
2292{
WANG Chaoe4f35892018-12-11 00:37:25 +08002293 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002294 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2295 buf_printf(b, "#endif\n");
2296}
2297
Trevor Keith5c725132009-09-22 16:43:38 -07002298static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002299{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002300 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002301 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2302}
2303
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002304/**
2305 * Record CRCs for unresolved symbols
2306 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002307static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002310 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 for (s = mod->unres; s; s = s->next) {
2313 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002314 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 s->module = exp->module;
2317 s->crc_valid = exp->crc_valid;
2318 s->crc = exp->crc;
2319 }
2320
2321 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 buf_printf(b, "\n");
2325 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002326 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002329 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002332 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 s->name, mod->name);
2334 continue;
2335 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302336 if (strlen(s->name) >= MODULE_NAME_LEN) {
2337 merror("too long symbol \"%s\" [%s.ko]\n",
2338 s->name, mod->name);
2339 err = 1;
2340 break;
2341 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002342 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302343 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345
2346 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002347
2348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002351static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 int first = 1;
2355
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002356 /* Clear ->seen flag of modules that own symbols needed by this. */
2357 for (s = mod->unres; s; s = s->next)
2358 if (s->module)
2359 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002362 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002364 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!s->module)
2366 continue;
2367
2368 if (s->module->seen)
2369 continue;
2370
2371 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002372 p = strrchr(s->module->name, '/');
2373 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002374 p++;
2375 else
2376 p = s->module->name;
2377 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 first = 0;
2379 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002380 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002383static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 if (mod->srcversion[0]) {
2386 buf_printf(b, "\n");
2387 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2388 mod->srcversion);
2389 }
2390}
2391
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002392static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 char *tmp;
2395 FILE *file;
2396 struct stat st;
2397
2398 file = fopen(fname, "r");
2399 if (!file)
2400 goto write;
2401
2402 if (fstat(fileno(file), &st) < 0)
2403 goto close_write;
2404
2405 if (st.st_size != b->pos)
2406 goto close_write;
2407
2408 tmp = NOFAIL(malloc(b->pos));
2409 if (fread(tmp, 1, b->pos, file) != b->pos)
2410 goto free_write;
2411
2412 if (memcmp(tmp, b->p, b->pos) != 0)
2413 goto free_write;
2414
2415 free(tmp);
2416 fclose(file);
2417 return;
2418
2419 free_write:
2420 free(tmp);
2421 close_write:
2422 fclose(file);
2423 write:
2424 file = fopen(fname, "w");
2425 if (!file) {
2426 perror(fname);
2427 exit(1);
2428 }
2429 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2430 perror(fname);
2431 exit(1);
2432 }
2433 fclose(file);
2434}
2435
Ram Paibd5cbce2006-06-08 22:12:53 -07002436/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002437 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002438 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002439static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 unsigned long size, pos = 0;
2442 void *file = grab_file(fname, &size);
2443 char *line;
2444
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002445 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* No symbol versions, silently ignore */
2447 return;
2448
2449 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002450 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 unsigned int crc;
2452 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002453 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 if (!(symname = strchr(line, '\t')))
2456 goto fail;
2457 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002458 if (!(namespace = strchr(symname, '\t')))
2459 goto fail;
2460 *namespace++ = '\0';
2461 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 goto fail;
2463 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002464 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002465 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002466 if (export && ((end = strchr(export, '\t')) != NULL))
2467 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 crc = strtoul(line, &d, 16);
2469 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2470 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002471 mod = find_module(modname);
2472 if (!mod) {
2473 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002475 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 mod->skip = 1;
2477 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002478 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002479 s->kernel = kernel;
2480 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002481 s->is_static = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -07002482 sym_update_crc(symname, mod, crc, export_no(export));
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002483 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302485 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return;
2487fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302488 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 fatal("parse error in symbol dump file\n");
2490}
2491
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002492/* For normal builds always dump all symbols.
2493 * For external modules only dump symbols
2494 * that are not read from kernel Module.symvers.
2495 **/
2496static int dump_sym(struct symbol *sym)
2497{
2498 if (!external_module)
2499 return 1;
2500 if (sym->vmlinux || sym->kernel)
2501 return 0;
2502 return 1;
2503}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002504
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002505static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 struct buffer buf = { };
2508 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002509 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 int n;
2511
2512 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2513 symbol = symbolhash[n];
2514 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002515 if (dump_sym(symbol)) {
2516 namespace = symbol->namespace;
2517 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2518 symbol->crc, symbol->name,
2519 namespace ? namespace : "",
2520 symbol->module->name,
2521 export_str(symbol->export));
2522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 symbol = symbol->next;
2524 }
2525 }
2526 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002527 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Matthias Maennich1d082772019-09-06 11:32:31 +01002530static void write_namespace_deps_files(void)
2531{
2532 struct module *mod;
2533 struct namespace_list *ns;
2534 struct buffer ns_deps_buf = {};
2535
2536 for (mod = modules; mod; mod = mod->next) {
2537 char fname[PATH_MAX];
2538
2539 if (mod->skip)
2540 continue;
2541
2542 ns_deps_buf.pos = 0;
2543
2544 for (ns = mod->required_namespaces; ns; ns = ns->next)
2545 buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
2546
2547 if (ns_deps_buf.pos == 0)
2548 continue;
2549
2550 sprintf(fname, "%s.ns_deps", mod->name);
2551 write_if_changed(&ns_deps_buf, fname);
2552 }
2553}
2554
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002555struct ext_sym_list {
2556 struct ext_sym_list *next;
2557 const char *file;
2558};
2559
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002560int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 struct module *mod;
2563 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002564 char *kernel_read = NULL, *module_read = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302565 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002567 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002568 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002569 struct ext_sym_list *extsym_iter;
2570 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Matthias Maennich1d082772019-09-06 11:32:31 +01002572 while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002573 switch (opt) {
2574 case 'i':
2575 kernel_read = optarg;
2576 break;
2577 case 'I':
2578 module_read = optarg;
2579 external_module = 1;
2580 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002581 case 'e':
2582 external_module = 1;
2583 extsym_iter =
2584 NOFAIL(malloc(sizeof(*extsym_iter)));
2585 extsym_iter->next = extsym_start;
2586 extsym_iter->file = optarg;
2587 extsym_start = extsym_iter;
2588 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002589 case 'm':
2590 modversions = 1;
2591 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302592 case 'n':
2593 ignore_missing_files = 1;
2594 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002595 case 'o':
2596 dump_write = optarg;
2597 break;
2598 case 'a':
2599 all_versions = 1;
2600 break;
2601 case 's':
2602 vmlinux_section_warnings = 0;
2603 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302604 case 'T':
2605 files_source = optarg;
2606 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002607 case 'w':
2608 warn_unresolved = 1;
2609 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302610 case 'E':
2611 sec_mismatch_fatal = 1;
2612 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002613 case 'd':
2614 write_namespace_deps = 1;
2615 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002616 default:
2617 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619 }
2620
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002621 if (kernel_read)
2622 read_dump(kernel_read, 1);
2623 if (module_read)
2624 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002625 while (extsym_start) {
2626 read_dump(extsym_start->file, 0);
2627 extsym_iter = extsym_start->next;
2628 free(extsym_start);
2629 extsym_start = extsym_iter;
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002632 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Rusty Russell712f9b42013-04-04 17:37:38 +10302635 if (files_source)
2636 read_symbols_from_files(files_source);
2637
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002638 err = 0;
2639
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002640 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302641 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002642
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002643 if (mod->skip)
2644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 buf.pos = 0;
2647
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002648 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002649 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002650 if (write_namespace_deps)
2651 continue;
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002654 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002655 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002656 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002657 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002658 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 add_moddevtable(&buf, mod);
2660 add_srcversion(&buf, mod);
2661
2662 sprintf(fname, "%s.mod.c", mod->name);
2663 write_if_changed(&buf, fname);
2664 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002665
2666 if (write_namespace_deps) {
2667 write_namespace_deps_files();
2668 return 0;
2669 }
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (dump_write)
2672 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002673 if (sec_mismatch_count && sec_mismatch_fatal)
2674 fatal("modpost: Section mismatches detected.\n"
2675 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002676 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002677 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002678
Masahiro Yamada47346e92019-09-24 21:07:40 +09002679 for (s = symbolhash[n]; s; s = s->next) {
2680 /*
2681 * Do not check "vmlinux". This avoids the same warnings
2682 * shown twice, and false-positives for ARCH=um.
2683 */
2684 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2685 continue;
2686
Denis Efremov15bfc232019-08-01 09:06:57 +03002687 if (s->is_static)
2688 warn("\"%s\" [%s] is a static %s\n",
2689 s->name, s->module->name,
2690 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002691 }
2692 }
2693
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002694 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}