blob: 6735ae3da4c228200aa26ae73153d40551b29e34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Sam Ravnborgc96fca22006-07-01 11:44:23 +020042enum export {
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080047/* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
49 */
50
51#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Rusty Russelld4ef1c32013-04-04 17:37:32 +103090static inline bool strends(const char *str, const char *postfix)
91{
92 if (strlen(str) < strlen(postfix))
93 return false;
94
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96}
97
Sam Ravnborg040fcc82006-01-28 22:15:55 +010098static int is_vmlinux(const char *modname)
99{
100 const char *myname;
101
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100102 myname = strrchr(modname, '/');
103 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100104 myname++;
105 else
106 myname = modname;
107
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void *do_nofail(void *ptr, const char *expr)
113{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100114 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ptr;
118}
119
120/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct module *modules;
122
Masahiro Yamada8b185742018-05-09 18:50:40 +0900123static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct module *mod;
126
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
129 break;
130 return mod;
131}
132
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030133static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
141
142 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
145 mod->is_dot_o = 1;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* add to list */
149 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200150 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mod->next = modules;
152 modules = mod;
153
154 return mod;
155}
156
157/* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
159
160#define SYMBOL_HASH_SIZE 1024
161
162struct symbol {
163 struct symbol *next;
164 struct module *module;
165 unsigned int crc;
166 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900167 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100169 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
170 unsigned int kernel:1; /* 1 if symbol is from kernel
171 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030172 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Denis Efremov15bfc232019-08-01 09:06:57 +0300173 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700174 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 char name[0];
176};
177
178static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
179
180/* This is based on the hash agorithm from gdbm, via tdb */
181static inline unsigned int tdb_hash(const char *name)
182{
183 unsigned value; /* Used to compute the hash value. */
184 unsigned i; /* Used to cycle through random values. */
185
186 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100187 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
189
190 return (1103515243 * value + 12345);
191}
192
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100193/**
194 * Allocate a new symbols for use in the hash of exported symbols or
195 * the list of unresolved symbols per module
196 **/
197static struct symbol *alloc_symbol(const char *name, unsigned int weak,
198 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
201
202 memset(s, 0, sizeof(*s));
203 strcpy(s->name, name);
204 s->weak = weak;
205 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300206 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
215 struct symbol *new;
216
217 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
218 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
219 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100221 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100224static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct symbol *s;
227
228 /* For our purposes, .foo matches foo. PPC64 needs this. */
229 if (name[0] == '.')
230 name++;
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (strcmp(s->name, name) == 0)
234 return s;
235 }
236 return NULL;
237}
238
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100239static bool contains_namespace(struct namespace_list *list,
240 const char *namespace)
241{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900242 for (; list; list = list->next)
243 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100244 return true;
245
246 return false;
247}
248
249static void add_namespace(struct namespace_list **list, const char *namespace)
250{
251 struct namespace_list *ns_entry;
252
253 if (!contains_namespace(*list, namespace)) {
254 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
255 strlen(namespace) + 1));
256 strcpy(ns_entry->namespace, namespace);
257 ns_entry->next = *list;
258 *list = ns_entry;
259 }
260}
261
262static bool module_imports_namespace(struct module *module,
263 const char *namespace)
264{
265 return contains_namespace(module->imported_namespaces, namespace);
266}
267
Mathias Krause7a3ee752014-08-27 20:28:53 +0930268static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700269 const char *str;
270 enum export export;
271} export_list[] = {
272 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200273 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700274 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200275 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700276 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
277 { .str = "(unknown)", .export = export_unknown },
278};
279
280
281static const char *export_str(enum export ex)
282{
283 return export_list[ex].str;
284}
285
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100286static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700287{
288 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100289
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200290 if (!s)
291 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700292 for (i = 0; export_list[i].export != export_unknown; i++) {
293 if (strcmp(export_list[i].str, s) == 0)
294 return export_list[i].export;
295 }
296 return export_unknown;
297}
298
Masahiro Yamada6124c042017-09-06 16:19:05 -0700299static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
300{
301 return (void *)elf->hdr +
302 elf->sechdrs[elf->secindex_strings].sh_offset +
303 sechdr->sh_name;
304}
305
306static const char *sec_name(struct elf_info *elf, int secindex)
307{
308 return sech_name(elf, &elf->sechdrs[secindex]);
309}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200310
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900311static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
312{
313 Elf_Shdr *sechdr = &info->sechdrs[sym->st_shndx];
314 unsigned long offset;
315
316 offset = sym->st_value;
317 if (info->hdr->e_type != ET_REL)
318 offset -= sechdr->sh_addr;
319
320 return (void *)info->hdr + sechdr->sh_offset + offset;
321}
322
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200323#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
324
325static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
326{
327 const char *secname = sec_name(elf, sec);
328
329 if (strstarts(secname, "___ksymtab+"))
330 return export_plain;
331 else if (strstarts(secname, "___ksymtab_unused+"))
332 return export_unused;
333 else if (strstarts(secname, "___ksymtab_gpl+"))
334 return export_gpl;
335 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
336 return export_unused_gpl;
337 else if (strstarts(secname, "___ksymtab_gpl_future+"))
338 return export_gpl_future;
339 else
340 return export_unknown;
341}
342
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200343static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700344{
345 if (sec == elf->export_sec)
346 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200347 else if (sec == elf->export_unused_sec)
348 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700349 else if (sec == elf->export_gpl_sec)
350 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200351 else if (sec == elf->export_unused_gpl_sec)
352 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700353 else if (sec == elf->export_gpl_future_sec)
354 return export_gpl_future;
355 else
356 return export_unknown;
357}
358
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900359static const char *namespace_from_kstrtabns(const struct elf_info *info,
360 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100361{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900362 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100363 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100364}
365
Matthias Maennicha2b11182019-10-18 10:31:40 +0100366static void sym_update_namespace(const char *symname, const char *namespace)
367{
368 struct symbol *s = find_symbol(symname);
369
370 /*
371 * That symbol should have been created earlier and thus this is
372 * actually an assertion.
373 */
374 if (!s) {
375 merror("Could not update namespace(%s) for symbol %s\n",
376 namespace, symname);
377 return;
378 }
379
380 free(s->namespace);
381 s->namespace =
382 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
383}
384
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100385/**
386 * Add an exported symbol - it may have already been added without a
387 * CRC, in this case just update the CRC
388 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100389static struct symbol *sym_add_exported(const char *name, struct module *mod,
390 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct symbol *s = find_symbol(name);
393
394 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700395 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100396 } else {
397 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100398 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
399 mod->name, name, s->module->name,
400 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700401 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200402 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700403 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100406 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100407 s->vmlinux = is_vmlinux(mod->name);
408 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700409 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100410 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100413static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700414 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100415{
416 struct symbol *s = find_symbol(name);
417
Rusty Russellb6568b12013-11-07 12:09:13 +1030418 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700419 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030420 /* Don't complain when we find it later. */
421 s->preloaded = 1;
422 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100423 s->crc = crc;
424 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100427void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930430 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int fd;
432
433 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930434 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930436 if (fstat(fd, &st))
437 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 *size = st.st_size;
440 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930442failed:
443 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (map == MAP_FAILED)
445 return NULL;
446 return map;
447}
448
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100449/**
450 * Return a copy of the next line in a mmap'ed file.
451 * spaces in the beginning of the line is trimmed away.
452 * Return a pointer to a static buffer.
453 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100454char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 static char line[4096];
457 int skip = 1;
458 size_t len = 0;
459 signed char *p = (signed char *)file + *pos;
460 char *s = line;
461
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100462 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (skip && isspace(*p)) {
464 p++;
465 continue;
466 }
467 skip = 0;
468 if (*p != '\n' && (*pos < size)) {
469 len++;
470 *s++ = *p++;
471 if (len > 4095)
472 break; /* Too long, stop */
473 } else {
474 /* End of string */
475 *s = '\0';
476 return line;
477 }
478 }
479 /* End of buffer */
480 return NULL;
481}
482
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100483void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 munmap(file, size);
486}
487
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100488static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100491 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 Elf_Shdr *sechdrs;
493 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200494 const char *secstrings;
495 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 hdr = grab_file(filename, &info->size);
498 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930499 if (ignore_missing_files) {
500 fprintf(stderr, "%s: %s (ignored)\n", filename,
501 strerror(errno));
502 return 0;
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200505 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100508 if (info->size < sizeof(*hdr)) {
509 /* file too small, assume this is an empty .o file */
510 return 0;
511 }
512 /* Is this a valid ELF file? */
513 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
514 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
515 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
516 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
517 /* Not an ELF file - silently ignore it */
518 return 0;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200521 hdr->e_type = TO_NATIVE(hdr->e_type);
522 hdr->e_machine = TO_NATIVE(hdr->e_machine);
523 hdr->e_version = TO_NATIVE(hdr->e_version);
524 hdr->e_entry = TO_NATIVE(hdr->e_entry);
525 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
526 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
527 hdr->e_flags = TO_NATIVE(hdr->e_flags);
528 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
529 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
530 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
531 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
532 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
533 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sechdrs = (void *)hdr + hdr->e_shoff;
535 info->sechdrs = sechdrs;
536
Petr Stetiara83710e2007-08-27 12:15:07 +0200537 /* Check if file offset is correct */
538 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100539 fatal("section header offset=%lu in file '%s' is bigger than "
540 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
541 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200542 return 0;
543 }
544
Anders Kaseorg68457562011-05-19 16:55:27 -0600545 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 /*
547 * There are more than 64k sections,
548 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200549 */
550 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
551 }
552 else {
553 info->num_sections = hdr->e_shnum;
554 }
555 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600556 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200557 }
558 else {
559 info->secindex_strings = hdr->e_shstrndx;
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200563 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200564 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
565 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
566 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
567 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
568 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
569 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
570 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
571 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
572 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
573 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200576 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
577 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700578 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900579 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Tejun Heo56fc82c2009-02-06 00:48:02 +0900581 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100582 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
583 "sizeof(*hrd)=%zu\n", filename,
584 (unsigned long)sechdrs[i].sh_offset,
585 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100586 return 0;
587 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700588 secname = secstrings + sechdrs[i].sh_name;
589 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900590 if (nobits)
591 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
593 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700594 } else if (strcmp(secname, "__ksymtab") == 0)
595 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200596 else if (strcmp(secname, "__ksymtab_unused") == 0)
597 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700598 else if (strcmp(secname, "__ksymtab_gpl") == 0)
599 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200600 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
601 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700602 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
603 info->export_gpl_future_sec = i;
604
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200605 if (sechdrs[i].sh_type == SHT_SYMTAB) {
606 unsigned int sh_link_idx;
607 symtab_idx = i;
608 info->symtab_start = (void *)hdr +
609 sechdrs[i].sh_offset;
610 info->symtab_stop = (void *)hdr +
611 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600612 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200613 info->strtab = (void *)hdr +
614 sechdrs[sh_link_idx].sh_offset;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200617 /* 32bit section no. table? ("more than 64k sections") */
618 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
619 symtab_shndx_idx = i;
620 info->symtab_shndx_start = (void *)hdr +
621 sechdrs[i].sh_offset;
622 info->symtab_shndx_stop = (void *)hdr +
623 sechdrs[i].sh_offset + sechdrs[i].sh_size;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100626 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100627 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Fix endianness in symbols */
630 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
631 sym->st_shndx = TO_NATIVE(sym->st_shndx);
632 sym->st_name = TO_NATIVE(sym->st_name);
633 sym->st_value = TO_NATIVE(sym->st_value);
634 sym->st_size = TO_NATIVE(sym->st_size);
635 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200636
637 if (symtab_shndx_idx != ~0U) {
638 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600639 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200640 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600641 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200642 symtab_idx);
643 /* Fix endianness */
644 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
645 p++)
646 *p = TO_NATIVE(*p);
647 }
648
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100649 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100652static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 release_file(info->hdr, info->size);
655}
656
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200657static int ignore_undef_symbol(struct elf_info *info, const char *symname)
658{
659 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900660 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200661 return 1;
662 /* ignore global offset table */
663 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
664 return 1;
665 if (info->hdr->e_machine == EM_PPC)
666 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900667 if (strstarts(symname, "_restgpr_") ||
668 strstarts(symname, "_savegpr_") ||
669 strstarts(symname, "_rest32gpr_") ||
670 strstarts(symname, "_save32gpr_") ||
671 strstarts(symname, "_restvr_") ||
672 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200673 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000674 if (info->hdr->e_machine == EM_PPC64)
675 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900676 if (strstarts(symname, "_restgpr0_") ||
677 strstarts(symname, "_savegpr0_") ||
678 strstarts(symname, "_restvr_") ||
679 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100680 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000681 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200682 /* Do not ignore this symbol */
683 return 0;
684}
685
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900686static void handle_symbol(struct module *mod, struct elf_info *info,
687 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200690 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100691 bool is_crc = false;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900692 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200693
Frank Rowand258f7422012-04-09 17:59:03 -0700694 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900695 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200696 export = export_from_secname(info, get_secindex(info, sym));
697 else
698 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Andi Kleenb5064652013-11-12 15:08:38 -0800700 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900701 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100702 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800703 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000704 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
705 unsigned int *crcp;
706
707 /* symbol points to the CRC in the ELF object */
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900708 crcp = sym_get_data(info, sym);
Fredrik Noring54a71512019-03-27 19:12:50 +0100709 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000710 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900711 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800712 export);
713 }
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 switch (sym->st_shndx) {
716 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900717 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100718 /* Should warn here, but modpost runs before the linker */
719 } else
720 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 case SHN_UNDEF:
723 /* undefined symbol */
724 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
725 ELF_ST_BIND(sym->st_info) != STB_WEAK)
726 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200727 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
Ben Colline8d529012005-08-19 13:44:57 -0700729/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
730#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
731/* add compatibility with older glibc */
732#ifndef STT_SPARC_REGISTER
733#define STT_SPARC_REGISTER STT_REGISTER
734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (info->hdr->e_machine == EM_SPARC ||
736 info->hdr->e_machine == EM_SPARCV9) {
737 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700738 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100740 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700741 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100742 munged[0] = '_';
743 munged[1] = toupper(munged[1]);
744 symname = munged;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100748
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100749 if (is_crc) {
750 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900751 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
752 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100753 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030754 mod->unres = alloc_symbol(symname,
755 ELF_ST_BIND(sym->st_info) == STB_WEAK,
756 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 default:
759 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900760 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100761 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100762 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900764 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900766 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 mod->has_cleanup = 1;
768 break;
769 }
770}
771
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100772/**
773 * Parse tag=value strings from .modinfo section
774 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static char *next_string(char *string, unsigned long *secsize)
776{
777 /* Skip non-zero chars */
778 while (string[0]) {
779 string++;
780 if ((*secsize)-- <= 1)
781 return NULL;
782 }
783
784 /* Skip any zero padding. */
785 while (!string[0]) {
786 string++;
787 if ((*secsize)-- <= 1)
788 return NULL;
789 }
790 return string;
791}
792
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793static char *get_next_modinfo(struct elf_info *info, const char *tag,
794 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 char *p;
797 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798 char *modinfo = info->modinfo;
799 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900801 if (prev) {
802 size -= prev - modinfo;
803 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 for (p = modinfo; p; p = next_string(p, &size)) {
807 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
808 return p + taglen + 1;
809 }
810 return NULL;
811}
812
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900813static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200814
815{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900816 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200817}
818
Sam Ravnborg93684d32006-02-19 11:53:35 +0100819/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100820 * Test if string s ends in string sub
821 * return 0 if match
822 **/
823static int strrcmp(const char *s, const char *sub)
824{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100825 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100826
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100827 if (!s || !sub)
828 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100829
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100832
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100833 if ((slen == 0) || (sublen == 0))
834 return 1;
835
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100836 if (sublen > slen)
837 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100838
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100839 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100840}
841
Sam Ravnborgff13f922008-01-23 19:54:27 +0100842static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
843{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100844 if (sym)
845 return elf->strtab + sym->st_name;
846 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100847 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100848}
849
Sam Ravnborg10668222008-01-13 22:21:31 +0100850/* The pattern is an array of simple patterns.
851 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100852 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930854 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100855 */
Trevor Keith5c725132009-09-22 16:43:38 -0700856static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100857{
858 const char *p;
859 while (*pat) {
860 p = *pat++;
861 const char *endp = p + strlen(p) - 1;
862
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930863 /* "*foo*" */
864 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300865 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
866 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930867
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930868 free(bare);
869 if (here != NULL)
870 return 1;
871 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100872 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930873 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100874 if (strrcmp(sym, p + 1) == 0)
875 return 1;
876 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100878 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100879 if (strncmp(sym, p, strlen(p) - 1) == 0)
880 return 1;
881 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100882 /* no wildcards */
883 else {
884 if (strcmp(p, sym) == 0)
885 return 1;
886 }
887 }
888 /* no match */
889 return 0;
890}
891
Sam Ravnborg10668222008-01-13 22:21:31 +0100892/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930893static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200894{
895 ".comment*",
896 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930897 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800898 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000899 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200900 ".mdebug*", /* alpha, score, mips etc. */
901 ".pdr", /* alpha, score, mips etc. */
902 ".stab*",
903 ".note*",
904 ".got*",
905 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400906 ".xt.prop", /* xtensa */
907 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030908 ".arcextmap*", /* arc */
909 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030910 ".cmem*", /* EZchip */
911 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100912 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600913 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200914 NULL
915};
Sam Ravnborg10668222008-01-13 22:21:31 +0100916
Sam Ravnborge241a632008-01-28 20:13:13 +0100917/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400918 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100921 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400922static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900923 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100924{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400925 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100926
Anders Kaseorgb614a692009-04-23 16:49:33 -0400927 if (sechdr->sh_type == SHT_PROGBITS &&
928 !(sechdr->sh_flags & SHF_ALLOC) &&
929 !match(sec, section_white_list)) {
930 warn("%s (%s): unexpected non-allocatable section.\n"
931 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
932 "Note that for example <linux/init.h> contains\n"
933 "section definitions for use in .S files.\n\n",
934 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100935 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100936}
937
938
939
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100940#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941 ".init.setup", ".init.rodata", ".meminit.rodata", \
942 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100946#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100948#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930949 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100950
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200951#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930952 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
953 ".pci_fixup_enable", ".pci_fixup_resume", \
954 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200955
Paul Gortmakere24f6622013-06-19 19:30:48 -0400956#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
957#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100958
959#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
960#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100961
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930962#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930963#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700964 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930965#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400966 ".fixup", ".entry.text", ".exception.text", ".text.*", \
967 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100968
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000969#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000970#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100971
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000972#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000973#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100974
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930975#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
976 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
977
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930979static const char *const init_data_sections[] =
980 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100981
982/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930983static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984
985/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930986static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100987 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100988
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930989/* all text sections */
990static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
991
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930993static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100995
996/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100997#define DEFAULT_SYMBOL_WHITE_LIST \
998 "*driver", \
999 "*_template", /* scsi uses *_template a lot */ \
1000 "*_timer", /* arm uses ops structures named _timer a lot */ \
1001 "*_sht", /* scsi also used *_sht to some extent */ \
1002 "*_ops", \
1003 "*_probe", \
1004 "*_probe_one", \
1005 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001006
Mathias Krause7a3ee752014-08-27 20:28:53 +09301007static const char *const head_sections[] = { ".head.text*", NULL };
1008static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001009 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301010static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001011
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001012enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001013 TEXT_TO_ANY_INIT,
1014 DATA_TO_ANY_INIT,
1015 TEXT_TO_ANY_EXIT,
1016 DATA_TO_ANY_EXIT,
1017 XXXINIT_TO_SOME_INIT,
1018 XXXEXIT_TO_SOME_EXIT,
1019 ANY_INIT_TO_ANY_EXIT,
1020 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001021 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301022 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001023};
1024
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301025/**
1026 * Describe how to match sections on different criterias:
1027 *
1028 * @fromsec: Array of sections to be matched.
1029 *
1030 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1031 * this array is forbidden (black-list). Can be empty.
1032 *
1033 * @good_tosec: Relocations applied to a section in @fromsec must be
1034 * targetting sections in this array (white-list). Can be empty.
1035 *
1036 * @mismatch: Type of mismatch.
1037 *
1038 * @symbol_white_list: Do not match a relocation to a symbol in this list
1039 * even if it is targetting a section in @bad_to_sec.
1040 *
1041 * @handler: Specific handler to call when a match is found. If NULL,
1042 * default_mismatch_handler() will be called.
1043 *
1044 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001045struct sectioncheck {
1046 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301047 const char *bad_tosec[20];
1048 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001049 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001050 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301051 void (*handler)(const char *modname, struct elf_info *elf,
1052 const struct sectioncheck* const mismatch,
1053 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1054
Sam Ravnborg10668222008-01-13 22:21:31 +01001055};
1056
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301057static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1058 const struct sectioncheck* const mismatch,
1059 Elf_Rela *r, Elf_Sym *sym,
1060 const char *fromsec);
1061
Mathias Krause7a3ee752014-08-27 20:28:53 +09301062static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001063/* Do not reference init/exit code/data from
1064 * normal code and data
1065 */
1066{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001067 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301068 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001069 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001070 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001071},
1072{
1073 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301074 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001075 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001076 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001077},
1078{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001079 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001081 .mismatch = DATA_TO_ANY_INIT,
1082 .symbol_white_list = {
1083 "*_template", "*_timer", "*_sht", "*_ops",
1084 "*_probe", "*_probe_one", "*_console", NULL
1085 },
1086},
1087{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001088 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301089 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001090 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001091 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001092},
1093{
1094 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301095 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001096 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001097 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001099/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001101 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301102 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001103 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001104 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001105},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001106/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001108 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301109 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001110 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001111 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001112},
1113/* Do not use exit code/data from init code */
1114{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001115 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301116 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001117 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001118 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001119},
1120/* Do not use init code/data from exit code */
1121{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001122 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301123 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001124 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001125 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001126},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001127{
1128 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301129 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001130 .mismatch = ANY_INIT_TO_ANY_EXIT,
1131 .symbol_white_list = { NULL },
1132},
Sam Ravnborg10668222008-01-13 22:21:31 +01001133/* Do not export init/exit functions or data */
1134{
1135 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301136 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001137 .mismatch = EXPORT_TO_INIT_EXIT,
1138 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301139},
1140{
1141 .fromsec = { "__ex_table", NULL },
1142 /* If you're adding any new black-listed sections in here, consider
1143 * adding a special 'printer' for them in scripts/check_extable.
1144 */
1145 .bad_tosec = { ".altinstr_replacement", NULL },
1146 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1147 .mismatch = EXTABLE_TO_NON_TEXT,
1148 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001149}
1150};
1151
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001152static const struct sectioncheck *section_mismatch(
1153 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001154{
1155 int i;
1156 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1157 const struct sectioncheck *check = &sectioncheck[0];
1158
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301159 /*
1160 * The target section could be the SHT_NUL section when we're
1161 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001162 * doesn't make much sense and causes build failures on parisc
1163 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301164 */
1165 if (*tosec == '\0')
1166 return NULL;
1167
Sam Ravnborg10668222008-01-13 22:21:31 +01001168 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301169 if (match(fromsec, check->fromsec)) {
1170 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1171 return check;
1172 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1173 return check;
1174 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001175 check++;
1176 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001177 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001178}
1179
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001180/**
1181 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001182 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183 * Pattern 1:
1184 * If a module parameter is declared __initdata and permissions=0
1185 * then this is legal despite the warning generated.
1186 * We cannot see value of permissions here, so just ignore
1187 * this pattern.
1188 * The pattern is identified by:
1189 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001190 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001191 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001192 *
Rusty Russell6a841522010-08-11 23:04:16 -06001193 * Pattern 1a:
1194 * module_param_call() ops can refer to __init set function if permissions=0
1195 * The pattern is identified by:
1196 * tosec = .init.text
1197 * fromsec = .data*
1198 * atsym = __param_ops_*
1199 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001200 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001201 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001202 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001203 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001204 * tosec = init or exit section
1205 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001206 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1207 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001208 *
1209 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001210 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001211 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001212 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001213 * Some symbols belong to init section but still it is ok to reference
1214 * these from non-init sections as these symbols don't have any memory
1215 * allocated for them and symbol address and value are same. So even
1216 * if init section is freed, its ok to reference those symbols.
1217 * For ex. symbols marking the init section boundaries.
1218 * This pattern is identified by
1219 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001220 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301221 * Pattern 5:
1222 * GCC may optimize static inlines when fed constant arg(s) resulting
1223 * in functions like cpumask_empty() -- generating an associated symbol
1224 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1225 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1226 * meaningless section warning. May need to add isra symbols too...
1227 * This pattern is identified by
1228 * tosec = init section
1229 * fromsec = text section
1230 * refsymname = *.constprop.*
1231 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001232 * Pattern 6:
1233 * Hide section mismatch warnings for ELF local symbols. The goal
1234 * is to eliminate false positive modpost warnings caused by
1235 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1236 * Autogenerated symbol names bypass modpost's "Pattern 2"
1237 * whitelisting, which relies on pattern-matching against symbol
1238 * names to work. (One situation where gcc can autogenerate ELF
1239 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001241static int secref_whitelist(const struct sectioncheck *mismatch,
1242 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001243 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001246 if (match(tosec, init_data_sections) &&
1247 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001248 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001249 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001250
Rusty Russell6a841522010-08-11 23:04:16 -06001251 /* Check for pattern 1a */
1252 if (strcmp(tosec, ".init.text") == 0 &&
1253 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001254 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001255 return 0;
1256
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001257 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001258 if (match(tosec, init_exit_sections) &&
1259 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001260 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001262
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001263 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001264 if (match(fromsec, head_sections) &&
1265 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001266 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001267
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001268 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001269 if (match(tosym, linker_symbols))
1270 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001271
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301272 /* Check for pattern 5 */
1273 if (match(fromsec, text_sections) &&
1274 match(tosec, init_sections) &&
1275 match(fromsym, optim_symbols))
1276 return 0;
1277
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001278 /* Check for pattern 6 */
1279 if (strstarts(fromsym, ".L"))
1280 return 0;
1281
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001282 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001283}
1284
Sami Tolvanen5818c682018-10-23 15:15:35 -07001285static inline int is_arm_mapping_symbol(const char *str)
1286{
1287 return str[0] == '$' && strchr("axtd", str[1])
1288 && (str[2] == '\0' || str[2] == '.');
1289}
1290
1291/*
1292 * If there's no name there, ignore it; likewise, ignore it if it's
1293 * one of the magic symbols emitted used by current ARM tools.
1294 *
1295 * Otherwise if find_symbols_between() returns those symbols, they'll
1296 * fail the whitelist tests and cause lots of false alarms ... fixable
1297 * only by merging __exit and __init sections into __text, bloating
1298 * the kernel (which is especially evil on embedded platforms).
1299 */
1300static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1301{
1302 const char *name = elf->strtab + sym->st_name;
1303
1304 if (!name || !strlen(name))
1305 return 0;
1306 return !is_arm_mapping_symbol(name);
1307}
1308
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001309/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001310 * Find symbol based on relocation record info.
1311 * In some cases the symbol supplied is a valid symbol so
1312 * return refsym. If st_name != 0 we assume this is a valid symbol.
1313 * In other cases the symbol needs to be looked up in the symbol table
1314 * based on section and address.
1315 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001316static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001317 Elf_Sym *relsym)
1318{
1319 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001320 Elf_Sym *near = NULL;
1321 Elf64_Sword distance = 20;
1322 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001323 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001324
1325 if (relsym->st_name != 0)
1326 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001327
1328 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001330 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001332 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1333 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001334 if (!is_valid_name(elf, sym))
1335 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001336 if (sym->st_value == addr)
1337 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001338 /* Find a symbol nearby - addr are maybe negative */
1339 d = sym->st_value - addr;
1340 if (d < 0)
1341 d = addr - sym->st_value;
1342 if (d < distance) {
1343 distance = d;
1344 near = sym;
1345 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001346 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001347 /* We need a close match */
1348 if (distance < 20)
1349 return near;
1350 else
1351 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001352}
1353
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001355 * Find symbols before or equal addr and after addr - in the section sec.
1356 * If we find two symbols with equal offset prefer one with a valid name.
1357 * The ELF format may have a better way to detect what type of symbol
1358 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001360static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1361 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362{
1363 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001364 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001365 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001366
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001367 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1368 const char *symsec;
1369
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001370 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001372 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001373 if (strcmp(symsec, sec) != 0)
1374 continue;
David Brownellda68d612007-02-20 13:58:16 -08001375 if (!is_valid_name(elf, sym))
1376 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001377 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001378 if ((addr - sym->st_value) < distance) {
1379 distance = addr - sym->st_value;
1380 near = sym;
1381 } else if ((addr - sym->st_value) == distance) {
1382 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001383 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001384 }
1385 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001386 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001387}
1388
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001389/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001390 * Convert a section name to the function/data attribute
1391 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001392 * .memexitconst => __memconst
1393 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001394 *
1395 * The memory of returned value has been allocated on a heap. The user of this
1396 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001397*/
1398static char *sec2annotation(const char *s)
1399{
1400 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001401 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001402 char *r = p;
1403
1404 *p++ = '_';
1405 *p++ = '_';
1406 if (*s == '.')
1407 s++;
1408 while (*s && *s != '.')
1409 *p++ = *s++;
1410 *p = '\0';
1411 if (*s == '.')
1412 s++;
1413 if (strstr(s, "rodata") != NULL)
1414 strcat(p, "const ");
1415 else if (strstr(s, "data") != NULL)
1416 strcat(p, "data ");
1417 else
1418 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001419 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001420 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001421 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001422 }
1423}
1424
1425static int is_function(Elf_Sym *sym)
1426{
1427 if (sym)
1428 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1429 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001430 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001431}
1432
Randy Dunlap00759c02011-03-15 14:13:47 -07001433static void print_section_list(const char * const list[20])
1434{
1435 const char *const *s = list;
1436
1437 while (*s) {
1438 fprintf(stderr, "%s", *s);
1439 s++;
1440 if (*s)
1441 fprintf(stderr, ", ");
1442 }
1443 fprintf(stderr, "\n");
1444}
1445
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301446static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1447{
1448 switch (is_func) {
1449 case 0: *name = "variable"; *name_p = ""; break;
1450 case 1: *name = "function"; *name_p = "()"; break;
1451 default: *name = "(unknown reference)"; *name_p = ""; break;
1452 }
1453}
1454
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001455/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001456 * Print a warning about a section mismatch.
1457 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001458 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001459 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001460static void report_sec_mismatch(const char *modname,
1461 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001462 const char *fromsec,
1463 unsigned long long fromaddr,
1464 const char *fromsym,
1465 int from_is_func,
1466 const char *tosec, const char *tosym,
1467 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001468{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001469 const char *from, *from_p;
1470 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 char *prl_from;
1472 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001473
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001474 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001475
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301476 get_pretty_name(from_is_func, &from, &from_p);
1477 get_pretty_name(to_is_func, &to, &to_p);
1478
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001479 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1480 "to the %s %s:%s%s\n",
1481 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1482 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001484 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001485 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001486 prl_from = sec2annotation(fromsec);
1487 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001488 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001489 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 "the %s %s%s%s.\n"
1491 "This is often because %s lacks a %s\n"
1492 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 prl_from, fromsym,
1494 to, prl_to, tosym, to_p,
1495 fromsym, prl_to, tosym);
1496 free(prl_from);
1497 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001499 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001500 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001501 fprintf(stderr,
1502 "The variable %s references\n"
1503 "the %s %s%s%s\n"
1504 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001505 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001508 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001509 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001510 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001511 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001512 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001514 fprintf(stderr,
1515 "The function %s() references a %s in an exit section.\n"
1516 "Often the %s %s%s has valid usage outside the exit section\n"
1517 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001518 fromsym, to, to, tosym, to_p, prl_to, tosym);
1519 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001521 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The variable %s references\n"
1525 "the %s %s%s%s\n"
1526 "If the reference is valid then annotate the\n"
1527 "variable with __exit* (see linux/init.h) or "
1528 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001530 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001532 break;
1533 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001534 case XXXINIT_TO_SOME_INIT:
1535 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001536 prl_from = sec2annotation(fromsec);
1537 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001538 fprintf(stderr,
1539 "The %s %s%s%s references\n"
1540 "a %s %s%s%s.\n"
1541 "If %s is only used by %s then\n"
1542 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 from, prl_from, fromsym, from_p,
1544 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001545 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 free(prl_from);
1547 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001549 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001550 prl_from = sec2annotation(fromsec);
1551 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001552 fprintf(stderr,
1553 "The %s %s%s%s references\n"
1554 "a %s %s%s%s.\n"
1555 "This is often seen when error handling "
1556 "in the init function\n"
1557 "uses functionality in the exit path.\n"
1558 "The fix is often to remove the %sannotation of\n"
1559 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 from, prl_from, fromsym, from_p,
1561 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001562 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001563 free(prl_from);
1564 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001565 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001566 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001567 prl_from = sec2annotation(fromsec);
1568 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001569 fprintf(stderr,
1570 "The %s %s%s%s references\n"
1571 "a %s %s%s%s.\n"
1572 "This is often seen when error handling "
1573 "in the exit function\n"
1574 "uses functionality in the init path.\n"
1575 "The fix is often to remove the %sannotation of\n"
1576 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001577 from, prl_from, fromsym, from_p,
1578 to, prl_to, tosym, to_p,
1579 prl_to, tosym, to_p);
1580 free(prl_from);
1581 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001582 break;
1583 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001584 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001585 fprintf(stderr,
1586 "The symbol %s is exported and annotated %s\n"
1587 "Fix this by removing the %sannotation of %s "
1588 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001589 tosym, prl_to, prl_to, tosym);
1590 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001591 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301592 case EXTABLE_TO_NON_TEXT:
1593 fatal("There's a special handler for this mismatch type, "
1594 "we should never get here.");
1595 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001596 }
1597 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001598}
1599
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301600static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1601 const struct sectioncheck* const mismatch,
1602 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1603{
1604 const char *tosec;
1605 Elf_Sym *to;
1606 Elf_Sym *from;
1607 const char *tosym;
1608 const char *fromsym;
1609
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301610 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1611 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001613 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301614 return;
1615
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301616 tosec = sec_name(elf, get_secindex(elf, sym));
1617 to = find_elf_symbol(elf, r->r_addend, sym);
1618 tosym = sym_name(elf, to);
1619
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301620 /* check whitelist - we may ignore it */
1621 if (secref_whitelist(mismatch,
1622 fromsec, fromsym, tosec, tosym)) {
1623 report_sec_mismatch(modname, mismatch,
1624 fromsec, r->r_offset, fromsym,
1625 is_function(from), tosec, tosym,
1626 is_function(to));
1627 }
1628}
1629
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301630static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1631{
1632 if (section_index > elf->num_sections)
1633 fatal("section_index is outside elf->num_sections!\n");
1634
1635 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1636}
1637
1638/*
1639 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1640 * to know the sizeof(struct exception_table_entry) for the target architecture.
1641 */
1642static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301643static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301644{
1645 /*
1646 * If we're currently checking the second relocation within __ex_table,
1647 * that relocation offset tells us the offsetof(struct
1648 * exception_table_entry, fixup) which is equal to sizeof(struct
1649 * exception_table_entry) divided by two. We use that to our advantage
1650 * since there's no portable way to get that size as every architecture
1651 * seems to go with different sized types. Not pretty but better than
1652 * hard-coding the size for every architecture..
1653 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301654 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301655 extable_entry_size = r->r_offset * 2;
1656}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301657
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658static inline bool is_extable_fault_address(Elf_Rela *r)
1659{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301660 /*
1661 * extable_entry_size is only discovered after we've handled the
1662 * _second_ relocation in __ex_table, so only abort when we're not
1663 * handling the first reloc and extable_entry_size is zero.
1664 */
1665 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301666 fatal("extable_entry size hasn't been discovered!\n");
1667
1668 return ((r->r_offset == 0) ||
1669 (r->r_offset % extable_entry_size == 0));
1670}
1671
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301672#define is_second_extable_reloc(Start, Cur, Sec) \
1673 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1674
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301675static void report_extable_warnings(const char* modname, struct elf_info* elf,
1676 const struct sectioncheck* const mismatch,
1677 Elf_Rela* r, Elf_Sym* sym,
1678 const char* fromsec, const char* tosec)
1679{
1680 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1681 const char* fromsym_name = sym_name(elf, fromsym);
1682 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1683 const char* tosym_name = sym_name(elf, tosym);
1684 const char* from_pretty_name;
1685 const char* from_pretty_name_p;
1686 const char* to_pretty_name;
1687 const char* to_pretty_name_p;
1688
1689 get_pretty_name(is_function(fromsym),
1690 &from_pretty_name, &from_pretty_name_p);
1691 get_pretty_name(is_function(tosym),
1692 &to_pretty_name, &to_pretty_name_p);
1693
1694 warn("%s(%s+0x%lx): Section mismatch in reference"
1695 " from the %s %s%s to the %s %s:%s%s\n",
1696 modname, fromsec, (long)r->r_offset, from_pretty_name,
1697 fromsym_name, from_pretty_name_p,
1698 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1699
1700 if (!match(tosec, mismatch->bad_tosec) &&
1701 is_executable_section(elf, get_secindex(elf, sym)))
1702 fprintf(stderr,
1703 "The relocation at %s+0x%lx references\n"
1704 "section \"%s\" which is not in the list of\n"
1705 "authorized sections. If you're adding a new section\n"
1706 "and/or if this reference is valid, add \"%s\" to the\n"
1707 "list of authorized sections to jump to on fault.\n"
1708 "This can be achieved by adding \"%s\" to \n"
1709 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1710 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1711}
1712
1713static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1714 const struct sectioncheck* const mismatch,
1715 Elf_Rela* r, Elf_Sym* sym,
1716 const char *fromsec)
1717{
1718 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1719
1720 sec_mismatch_count++;
1721
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001722 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301723
1724 if (match(tosec, mismatch->bad_tosec))
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is black-listed.\n"
1727 "Something is seriously wrong and should be fixed.\n"
1728 "You might get more information about where this is\n"
1729 "coming from by using scripts/check_extable.sh %s\n",
1730 fromsec, (long)r->r_offset, tosec, modname);
1731 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1732 if (is_extable_fault_address(r))
1733 fatal("The relocation at %s+0x%lx references\n"
1734 "section \"%s\" which is not executable, IOW\n"
1735 "it is not possible for the kernel to fault\n"
1736 "at that address. Something is seriously wrong\n"
1737 "and should be fixed.\n",
1738 fromsec, (long)r->r_offset, tosec);
1739 else
1740 fatal("The relocation at %s+0x%lx references\n"
1741 "section \"%s\" which is not executable, IOW\n"
1742 "the kernel will fault if it ever tries to\n"
1743 "jump to it. Something is seriously wrong\n"
1744 "and should be fixed.\n",
1745 fromsec, (long)r->r_offset, tosec);
1746 }
1747}
1748
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001750 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001751{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001752 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301753 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001754
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001755 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301756 if (mismatch->handler)
1757 mismatch->handler(modname, elf, mismatch,
1758 r, sym, fromsec);
1759 else
1760 default_mismatch_handler(modname, elf, mismatch,
1761 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001762 }
1763}
1764
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001766 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767{
1768 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001769 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001770
1771 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001772 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001773}
1774
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001775static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001776{
1777 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001778 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001779
1780 switch (r_typ) {
1781 case R_386_32:
1782 r->r_addend = TO_NATIVE(*location);
1783 break;
1784 case R_386_PC32:
1785 r->r_addend = TO_NATIVE(*location) + 4;
1786 /* For CONFIG_RELOCATABLE=y */
1787 if (elf->hdr->e_type == ET_EXEC)
1788 r->r_addend += r->r_offset;
1789 break;
1790 }
1791 return 0;
1792}
1793
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001794#ifndef R_ARM_CALL
1795#define R_ARM_CALL 28
1796#endif
1797#ifndef R_ARM_JUMP24
1798#define R_ARM_JUMP24 29
1799#endif
1800
David A. Longc9698e52014-02-14 22:41:18 +01001801#ifndef R_ARM_THM_CALL
1802#define R_ARM_THM_CALL 10
1803#endif
1804#ifndef R_ARM_THM_JUMP24
1805#define R_ARM_THM_JUMP24 30
1806#endif
1807#ifndef R_ARM_THM_JUMP19
1808#define R_ARM_THM_JUMP19 51
1809#endif
1810
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001811static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001812{
1813 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1814
1815 switch (r_typ) {
1816 case R_ARM_ABS32:
1817 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001818 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001819 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001820 break;
1821 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001822 case R_ARM_CALL:
1823 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001824 case R_ARM_THM_CALL:
1825 case R_ARM_THM_JUMP24:
1826 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001827 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001828 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001829 sechdr->sh_offset +
1830 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001831 break;
1832 default:
1833 return 1;
1834 }
1835 return 0;
1836}
1837
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001838static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001839{
1840 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001842 unsigned int inst;
1843
1844 if (r_typ == R_MIPS_HI16)
1845 return 1; /* skip this */
1846 inst = TO_NATIVE(*location);
1847 switch (r_typ) {
1848 case R_MIPS_LO16:
1849 r->r_addend = inst & 0xffff;
1850 break;
1851 case R_MIPS_26:
1852 r->r_addend = (inst & 0x03ffffff) << 2;
1853 break;
1854 case R_MIPS_32:
1855 r->r_addend = inst;
1856 break;
1857 }
1858 return 0;
1859}
1860
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001862 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863{
1864 Elf_Sym *sym;
1865 Elf_Rela *rela;
1866 Elf_Rela r;
1867 unsigned int r_sym;
1868 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001869
Sam Ravnborgff13f922008-01-23 19:54:27 +01001870 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1872
Sam Ravnborgff13f922008-01-23 19:54:27 +01001873 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 fromsec += strlen(".rela");
1875 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001876 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001877 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001878
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001879 for (rela = start; rela < stop; rela++) {
1880 r.r_offset = TO_NATIVE(rela->r_offset);
1881#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001882 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001883 unsigned int r_typ;
1884 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1885 r_sym = TO_NATIVE(r_sym);
1886 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1887 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1888 } else {
1889 r.r_info = TO_NATIVE(rela->r_info);
1890 r_sym = ELF_R_SYM(r.r_info);
1891 }
1892#else
1893 r.r_info = TO_NATIVE(rela->r_info);
1894 r_sym = ELF_R_SYM(r.r_info);
1895#endif
1896 r.r_addend = TO_NATIVE(rela->r_addend);
1897 sym = elf->symtab_start + r_sym;
1898 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001899 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301901 if (is_second_extable_reloc(start, rela, fromsec))
1902 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001903 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 }
1905}
1906
1907static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001908 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001909{
1910 Elf_Sym *sym;
1911 Elf_Rel *rel;
1912 Elf_Rela r;
1913 unsigned int r_sym;
1914 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001915
Sam Ravnborgff13f922008-01-23 19:54:27 +01001916 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001917 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1918
Sam Ravnborgff13f922008-01-23 19:54:27 +01001919 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001920 fromsec += strlen(".rel");
1921 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001922 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001923 return;
1924
1925 for (rel = start; rel < stop; rel++) {
1926 r.r_offset = TO_NATIVE(rel->r_offset);
1927#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001928 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001929 unsigned int r_typ;
1930 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1931 r_sym = TO_NATIVE(r_sym);
1932 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1933 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1934 } else {
1935 r.r_info = TO_NATIVE(rel->r_info);
1936 r_sym = ELF_R_SYM(r.r_info);
1937 }
1938#else
1939 r.r_info = TO_NATIVE(rel->r_info);
1940 r_sym = ELF_R_SYM(r.r_info);
1941#endif
1942 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001943 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001944 case EM_386:
1945 if (addend_386_rel(elf, sechdr, &r))
1946 continue;
1947 break;
1948 case EM_ARM:
1949 if (addend_arm_rel(elf, sechdr, &r))
1950 continue;
1951 break;
1952 case EM_MIPS:
1953 if (addend_mips_rel(elf, sechdr, &r))
1954 continue;
1955 break;
1956 }
1957 sym = elf->symtab_start + r_sym;
1958 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001959 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001960 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301961 if (is_second_extable_reloc(start, rel, fromsec))
1962 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001963 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001964 }
1965}
1966
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001967/**
1968 * A module includes a number of sections that are discarded
1969 * either when loaded or when used as built-in.
1970 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001971 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001972 * Likewise for modules used built-in the sections marked __exit
1973 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001974 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001975 * The check_sec_ref() function traverses all relocation records
1976 * to find all references to a section that reference a section that will
1977 * be discarded and warns about it.
1978 **/
1979static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001980 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001981{
1982 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001983 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001984
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001985 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001986 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001987 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001988 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001989 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001990 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001991 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001992 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001993 }
1994}
1995
Andi Kleen7d02b492014-02-08 09:01:12 +01001996static char *remove_dot(char *s)
1997{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301998 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001999
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09302000 if (n && s[n]) {
2001 size_t m = strspn(s + n + 1, "0123456789");
2002 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002003 s[n] = 0;
2004 }
2005 return s;
2006}
2007
Masahiro Yamada8b185742018-05-09 18:50:40 +09002008static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 const char *symname;
2011 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002012 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002013 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct module *mod;
2015 struct elf_info info = { };
2016 Elf_Sym *sym;
2017
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002018 if (!parse_elf(&info, modname))
2019 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 mod = new_module(modname);
2022
2023 /* When there's no vmlinux, don't print warnings about
2024 * unresolved symbols (since there'll be too many ;) */
2025 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 mod->skip = 1;
2028 }
2029
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002030 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002031 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002032 warn("modpost: missing MODULE_LICENSE() in %s\n"
2033 "see include/linux/module.h for "
2034 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002035 while (license) {
2036 if (license_is_gpl_compatible(license))
2037 mod->gpl_compatible = 1;
2038 else {
2039 mod->gpl_compatible = 0;
2040 break;
2041 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002042 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002043 }
2044
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002045 namespace = get_modinfo(&info, "import_ns");
2046 while (namespace) {
2047 add_namespace(&mod->imported_namespaces, namespace);
2048 namespace = get_next_modinfo(&info, "import_ns", namespace);
2049 }
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002052 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002054 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 handle_moddevtable(mod, &info, sym, symname);
2056 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002057
Matthias Maennich69923202019-10-18 10:31:42 +01002058 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2059 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2060 symname = remove_dot(info.strtab + sym->st_name);
2061
2062 if (strstarts(symname, "__kstrtabns_"))
2063 sym_update_namespace(symname + strlen("__kstrtabns_"),
2064 namespace_from_kstrtabns(&info,
2065 sym));
2066 }
2067
Denis Efremov15bfc232019-08-01 09:06:57 +03002068 // check for static EXPORT_SYMBOL_* functions && global vars
2069 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2070 unsigned char bind = ELF_ST_BIND(sym->st_info);
2071
2072 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2073 struct symbol *s =
2074 find_symbol(remove_dot(info.strtab +
2075 sym->st_name));
2076
2077 if (s)
2078 s->is_static = 0;
2079 }
2080 }
2081
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002082 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002083 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002085 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (version)
2087 maybe_frob_rcs_version(modname, version, info.modinfo,
2088 version - (char *)info.hdr);
2089 if (version || (all_versions && !is_vmlinux(modname)))
2090 get_src_version(modname, mod->srcversion,
2091 sizeof(mod->srcversion)-1);
2092
2093 parse_elf_finish(&info);
2094
Rusty Russell8c8ef422009-03-31 13:05:34 -06002095 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * never passed as an argument to an exported function, so
2097 * the automatic versioning doesn't pick it up, but it's really
2098 * important anyhow */
2099 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002100 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Rusty Russell712f9b42013-04-04 17:37:38 +10302103static void read_symbols_from_files(const char *filename)
2104{
2105 FILE *in = stdin;
2106 char fname[PATH_MAX];
2107
2108 if (strcmp(filename, "-") != 0) {
2109 in = fopen(filename, "r");
2110 if (!in)
2111 fatal("Can't open filenames file %s: %m", filename);
2112 }
2113
2114 while (fgets(fname, PATH_MAX, in) != NULL) {
2115 if (strends(fname, "\n"))
2116 fname[strlen(fname)-1] = '\0';
2117 read_symbols(fname);
2118 }
2119
2120 if (in != stdin)
2121 fclose(in);
2122}
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#define SZ 500
2125
2126/* We first write the generated file into memory using the
2127 * following helper, then compare to the file on disk and
2128 * only update the later if anything changed */
2129
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002130void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2131 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 char tmp[SZ];
2134 int len;
2135 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 va_start(ap, fmt);
2138 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08002139 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 va_end(ap);
2141}
2142
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002143void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08002146 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002147 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 strncpy(buf->p + buf->pos, s, len);
2150 buf->pos += len;
2151}
2152
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002153static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2154{
2155 const char *e = is_vmlinux(m) ?"":".ko";
2156
2157 switch (exp) {
2158 case export_gpl:
2159 fatal("modpost: GPL-incompatible module %s%s "
2160 "uses GPL-only symbol '%s'\n", m, e, s);
2161 break;
2162 case export_unused_gpl:
2163 fatal("modpost: GPL-incompatible module %s%s "
2164 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2165 break;
2166 case export_gpl_future:
2167 warn("modpost: GPL-incompatible module %s%s "
2168 "uses future GPL-only symbol '%s'\n", m, e, s);
2169 break;
2170 case export_plain:
2171 case export_unused:
2172 case export_unknown:
2173 /* ignore */
2174 break;
2175 }
2176}
2177
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002178static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002179{
2180 const char *e = is_vmlinux(m) ?"":".ko";
2181
2182 switch (exp) {
2183 case export_unused:
2184 case export_unused_gpl:
2185 warn("modpost: module %s%s "
2186 "uses symbol '%s' marked UNUSED\n", m, e, s);
2187 break;
2188 default:
2189 /* ignore */
2190 break;
2191 }
2192}
2193
Masahiro Yamada3b415282018-11-23 16:57:23 +09002194static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002195{
2196 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002197 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002198
2199 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002200 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002201 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002202 if (!exp || exp->module == mod) {
2203 if (have_vmlinux && !s->weak) {
2204 if (warn_unresolved) {
2205 warn("\"%s\" [%s.ko] undefined!\n",
2206 s->name, mod->name);
2207 } else {
2208 merror("\"%s\" [%s.ko] undefined!\n",
2209 s->name, mod->name);
2210 err = 1;
2211 }
2212 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002213 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002214 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002215 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002216 if (basename)
2217 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002218 else
2219 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002220
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002221 if (exp->namespace &&
2222 !module_imports_namespace(mod, exp->namespace)) {
2223 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2224 basename, exp->name, exp->namespace);
2225 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002226 }
2227
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002228 if (!mod->gpl_compatible)
2229 check_for_gpl_usage(exp->export, basename, exp->name);
2230 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002231 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002232
2233 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002234}
2235
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002236static int check_modname_len(struct module *mod)
2237{
2238 const char *mod_name;
2239
2240 mod_name = strrchr(mod->name, '/');
2241 if (mod_name == NULL)
2242 mod_name = mod->name;
2243 else
2244 mod_name++;
2245 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2246 merror("module name is too long [%s.ko]\n", mod->name);
2247 return 1;
2248 }
2249
2250 return 0;
2251}
2252
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002253/**
2254 * Header for the generated file
2255 **/
2256static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Laura Abbott9afb7192018-07-05 17:49:37 -07002258 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 buf_printf(b, "#include <linux/module.h>\n");
2260 buf_printf(b, "#include <linux/vermagic.h>\n");
2261 buf_printf(b, "#include <linux/compiler.h>\n");
2262 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002263 buf_printf(b, "BUILD_SALT;\n");
2264 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002266 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302268 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002269 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002270 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (mod->has_cleanup)
2274 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002275 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002277 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 buf_printf(b, "};\n");
2279}
2280
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002281static void add_intree_flag(struct buffer *b, int is_intree)
2282{
2283 if (is_intree)
2284 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2285}
2286
Andi Kleencaf75012018-01-25 15:50:28 -08002287/* Cannot check for assembler */
2288static void add_retpoline(struct buffer *b)
2289{
WANG Chaoe4f35892018-12-11 00:37:25 +08002290 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002291 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2292 buf_printf(b, "#endif\n");
2293}
2294
Trevor Keith5c725132009-09-22 16:43:38 -07002295static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002296{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002297 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002298 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2299}
2300
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002301/**
2302 * Record CRCs for unresolved symbols
2303 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002304static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002307 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 for (s = mod->unres; s; s = s->next) {
2310 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002311 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 s->module = exp->module;
2314 s->crc_valid = exp->crc_valid;
2315 s->crc = exp->crc;
2316 }
2317
2318 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 buf_printf(b, "\n");
2322 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002323 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002326 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002329 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 s->name, mod->name);
2331 continue;
2332 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302333 if (strlen(s->name) >= MODULE_NAME_LEN) {
2334 merror("too long symbol \"%s\" [%s.ko]\n",
2335 s->name, mod->name);
2336 err = 1;
2337 break;
2338 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002339 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302340 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342
2343 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002344
2345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002348static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int first = 1;
2352
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002353 /* Clear ->seen flag of modules that own symbols needed by this. */
2354 for (s = mod->unres; s; s = s->next)
2355 if (s->module)
2356 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002359 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002361 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (!s->module)
2363 continue;
2364
2365 if (s->module->seen)
2366 continue;
2367
2368 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002369 p = strrchr(s->module->name, '/');
2370 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002371 p++;
2372 else
2373 p = s->module->name;
2374 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 first = 0;
2376 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002377 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002380static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 if (mod->srcversion[0]) {
2383 buf_printf(b, "\n");
2384 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2385 mod->srcversion);
2386 }
2387}
2388
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002389static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 char *tmp;
2392 FILE *file;
2393 struct stat st;
2394
2395 file = fopen(fname, "r");
2396 if (!file)
2397 goto write;
2398
2399 if (fstat(fileno(file), &st) < 0)
2400 goto close_write;
2401
2402 if (st.st_size != b->pos)
2403 goto close_write;
2404
2405 tmp = NOFAIL(malloc(b->pos));
2406 if (fread(tmp, 1, b->pos, file) != b->pos)
2407 goto free_write;
2408
2409 if (memcmp(tmp, b->p, b->pos) != 0)
2410 goto free_write;
2411
2412 free(tmp);
2413 fclose(file);
2414 return;
2415
2416 free_write:
2417 free(tmp);
2418 close_write:
2419 fclose(file);
2420 write:
2421 file = fopen(fname, "w");
2422 if (!file) {
2423 perror(fname);
2424 exit(1);
2425 }
2426 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2427 perror(fname);
2428 exit(1);
2429 }
2430 fclose(file);
2431}
2432
Ram Paibd5cbce2006-06-08 22:12:53 -07002433/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002434 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002435 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002436static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 unsigned long size, pos = 0;
2439 void *file = grab_file(fname, &size);
2440 char *line;
2441
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002442 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* No symbol versions, silently ignore */
2444 return;
2445
2446 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002447 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 unsigned int crc;
2449 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002450 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 if (!(symname = strchr(line, '\t')))
2453 goto fail;
2454 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002455 if (!(namespace = strchr(symname, '\t')))
2456 goto fail;
2457 *namespace++ = '\0';
2458 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto fail;
2460 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002461 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002462 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002463 if (export && ((end = strchr(export, '\t')) != NULL))
2464 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 crc = strtoul(line, &d, 16);
2466 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2467 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002468 mod = find_module(modname);
2469 if (!mod) {
2470 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002472 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 mod->skip = 1;
2474 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002476 s->kernel = kernel;
2477 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002478 s->is_static = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -07002479 sym_update_crc(symname, mod, crc, export_no(export));
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002480 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302482 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return;
2484fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302485 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 fatal("parse error in symbol dump file\n");
2487}
2488
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002489/* For normal builds always dump all symbols.
2490 * For external modules only dump symbols
2491 * that are not read from kernel Module.symvers.
2492 **/
2493static int dump_sym(struct symbol *sym)
2494{
2495 if (!external_module)
2496 return 1;
2497 if (sym->vmlinux || sym->kernel)
2498 return 0;
2499 return 1;
2500}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002501
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002502static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 struct buffer buf = { };
2505 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002506 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 int n;
2508
2509 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2510 symbol = symbolhash[n];
2511 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002512 if (dump_sym(symbol)) {
2513 namespace = symbol->namespace;
2514 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2515 symbol->crc, symbol->name,
2516 namespace ? namespace : "",
2517 symbol->module->name,
2518 export_str(symbol->export));
2519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 symbol = symbol->next;
2521 }
2522 }
2523 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002524 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002527static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002528{
2529 struct module *mod;
2530 struct namespace_list *ns;
2531 struct buffer ns_deps_buf = {};
2532
2533 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002534
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002535 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002536 continue;
2537
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002538 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002539
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002540 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2541 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002542
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002543 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002544 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002545
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002546 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002547 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002548}
2549
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002550struct ext_sym_list {
2551 struct ext_sym_list *next;
2552 const char *file;
2553};
2554
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002555int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 struct module *mod;
2558 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002559 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002560 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302561 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002563 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002564 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002565 struct ext_sym_list *extsym_iter;
2566 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002568 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002569 switch (opt) {
2570 case 'i':
2571 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002572 external_module = 1;
2573 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002574 case 'e':
2575 external_module = 1;
2576 extsym_iter =
2577 NOFAIL(malloc(sizeof(*extsym_iter)));
2578 extsym_iter->next = extsym_start;
2579 extsym_iter->file = optarg;
2580 extsym_start = extsym_iter;
2581 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002582 case 'm':
2583 modversions = 1;
2584 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302585 case 'n':
2586 ignore_missing_files = 1;
2587 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002588 case 'o':
2589 dump_write = optarg;
2590 break;
2591 case 'a':
2592 all_versions = 1;
2593 break;
2594 case 's':
2595 vmlinux_section_warnings = 0;
2596 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302597 case 'T':
2598 files_source = optarg;
2599 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002600 case 'w':
2601 warn_unresolved = 1;
2602 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302603 case 'E':
2604 sec_mismatch_fatal = 1;
2605 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002606 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002607 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002608 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002609 default:
2610 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 }
2613
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002614 if (kernel_read)
2615 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002616 while (extsym_start) {
2617 read_dump(extsym_start->file, 0);
2618 extsym_iter = extsym_start->next;
2619 free(extsym_start);
2620 extsym_start = extsym_iter;
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002623 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Rusty Russell712f9b42013-04-04 17:37:38 +10302626 if (files_source)
2627 read_symbols_from_files(files_source);
2628
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002629 err = 0;
2630
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002631 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302632 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002633
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002634 if (mod->skip)
2635 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 buf.pos = 0;
2638
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002639 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002640 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002643 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002644 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002645 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002646 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002647 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 add_moddevtable(&buf, mod);
2649 add_srcversion(&buf, mod);
2650
2651 sprintf(fname, "%s.mod.c", mod->name);
2652 write_if_changed(&buf, fname);
2653 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002654
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002655 if (missing_namespace_deps)
2656 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (dump_write)
2659 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002660 if (sec_mismatch_count && sec_mismatch_fatal)
2661 fatal("modpost: Section mismatches detected.\n"
2662 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002663 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002664 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002665
Masahiro Yamada47346e92019-09-24 21:07:40 +09002666 for (s = symbolhash[n]; s; s = s->next) {
2667 /*
2668 * Do not check "vmlinux". This avoids the same warnings
2669 * shown twice, and false-positives for ARCH=um.
2670 */
2671 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2672 continue;
2673
Denis Efremov15bfc232019-08-01 09:06:57 +03002674 if (s->is_static)
2675 warn("\"%s\" [%s] is a static %s\n",
2676 s->name, s->module->name,
2677 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002678 }
2679 }
2680
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002681 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}