blob: 1c22cf7aa732cd358f68861bf598632a3f7264b2 [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) **/
Masahiro Yamada17436942019-11-15 02:42:24 +0900172 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
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
Masahiro Yamada17436942019-11-15 02:42:24 +0900413static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100414{
415 struct symbol *s = find_symbol(name);
416
Masahiro Yamada17436942019-11-15 02:42:24 +0900417 /*
418 * Ignore stand-alone __crc_*, which might be auto-generated symbols
419 * such as __*_veneer in ARM ELF.
420 */
421 if (!s)
422 return;
423
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100424 s->crc = crc;
425 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100428void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930431 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int fd;
433
434 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930435 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930437 if (fstat(fd, &st))
438 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 *size = st.st_size;
441 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930443failed:
444 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (map == MAP_FAILED)
446 return NULL;
447 return map;
448}
449
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100450/**
451 * Return a copy of the next line in a mmap'ed file.
452 * spaces in the beginning of the line is trimmed away.
453 * Return a pointer to a static buffer.
454 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100455char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 static char line[4096];
458 int skip = 1;
459 size_t len = 0;
460 signed char *p = (signed char *)file + *pos;
461 char *s = line;
462
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100463 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (skip && isspace(*p)) {
465 p++;
466 continue;
467 }
468 skip = 0;
469 if (*p != '\n' && (*pos < size)) {
470 len++;
471 *s++ = *p++;
472 if (len > 4095)
473 break; /* Too long, stop */
474 } else {
475 /* End of string */
476 *s = '\0';
477 return line;
478 }
479 }
480 /* End of buffer */
481 return NULL;
482}
483
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100484void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 munmap(file, size);
487}
488
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100489static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100492 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 Elf_Shdr *sechdrs;
494 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200495 const char *secstrings;
496 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 hdr = grab_file(filename, &info->size);
499 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930500 if (ignore_missing_files) {
501 fprintf(stderr, "%s: %s (ignored)\n", filename,
502 strerror(errno));
503 return 0;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200506 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100509 if (info->size < sizeof(*hdr)) {
510 /* file too small, assume this is an empty .o file */
511 return 0;
512 }
513 /* Is this a valid ELF file? */
514 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
515 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
516 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
517 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
518 /* Not an ELF file - silently ignore it */
519 return 0;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200522 hdr->e_type = TO_NATIVE(hdr->e_type);
523 hdr->e_machine = TO_NATIVE(hdr->e_machine);
524 hdr->e_version = TO_NATIVE(hdr->e_version);
525 hdr->e_entry = TO_NATIVE(hdr->e_entry);
526 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
527 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
528 hdr->e_flags = TO_NATIVE(hdr->e_flags);
529 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
530 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
531 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
532 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
533 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
534 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sechdrs = (void *)hdr + hdr->e_shoff;
536 info->sechdrs = sechdrs;
537
Petr Stetiara83710e2007-08-27 12:15:07 +0200538 /* Check if file offset is correct */
539 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100540 fatal("section header offset=%lu in file '%s' is bigger than "
541 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
542 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200543 return 0;
544 }
545
Anders Kaseorg68457562011-05-19 16:55:27 -0600546 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200547 /*
548 * There are more than 64k sections,
549 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200550 */
551 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
552 }
553 else {
554 info->num_sections = hdr->e_shnum;
555 }
556 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600557 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200558 }
559 else {
560 info->secindex_strings = hdr->e_shstrndx;
561 }
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200564 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200565 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
566 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
567 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
568 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
569 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
570 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
571 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
572 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
573 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
574 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200577 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
578 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700579 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900580 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Tejun Heo56fc82c2009-02-06 00:48:02 +0900582 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100583 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
584 "sizeof(*hrd)=%zu\n", filename,
585 (unsigned long)sechdrs[i].sh_offset,
586 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100587 return 0;
588 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700589 secname = secstrings + sechdrs[i].sh_name;
590 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900591 if (nobits)
592 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
594 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700595 } else if (strcmp(secname, "__ksymtab") == 0)
596 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200597 else if (strcmp(secname, "__ksymtab_unused") == 0)
598 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700599 else if (strcmp(secname, "__ksymtab_gpl") == 0)
600 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200601 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
602 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700603 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
604 info->export_gpl_future_sec = i;
605
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200606 if (sechdrs[i].sh_type == SHT_SYMTAB) {
607 unsigned int sh_link_idx;
608 symtab_idx = i;
609 info->symtab_start = (void *)hdr +
610 sechdrs[i].sh_offset;
611 info->symtab_stop = (void *)hdr +
612 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600613 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200614 info->strtab = (void *)hdr +
615 sechdrs[sh_link_idx].sh_offset;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200618 /* 32bit section no. table? ("more than 64k sections") */
619 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
620 symtab_shndx_idx = i;
621 info->symtab_shndx_start = (void *)hdr +
622 sechdrs[i].sh_offset;
623 info->symtab_shndx_stop = (void *)hdr +
624 sechdrs[i].sh_offset + sechdrs[i].sh_size;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100627 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100628 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Fix endianness in symbols */
631 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
632 sym->st_shndx = TO_NATIVE(sym->st_shndx);
633 sym->st_name = TO_NATIVE(sym->st_name);
634 sym->st_value = TO_NATIVE(sym->st_value);
635 sym->st_size = TO_NATIVE(sym->st_size);
636 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200637
638 if (symtab_shndx_idx != ~0U) {
639 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600640 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200641 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600642 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200643 symtab_idx);
644 /* Fix endianness */
645 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
646 p++)
647 *p = TO_NATIVE(*p);
648 }
649
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100650 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100653static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 release_file(info->hdr, info->size);
656}
657
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200658static int ignore_undef_symbol(struct elf_info *info, const char *symname)
659{
660 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900661 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662 return 1;
663 /* ignore global offset table */
664 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
665 return 1;
666 if (info->hdr->e_machine == EM_PPC)
667 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900668 if (strstarts(symname, "_restgpr_") ||
669 strstarts(symname, "_savegpr_") ||
670 strstarts(symname, "_rest32gpr_") ||
671 strstarts(symname, "_save32gpr_") ||
672 strstarts(symname, "_restvr_") ||
673 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200674 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000675 if (info->hdr->e_machine == EM_PPC64)
676 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900677 if (strstarts(symname, "_restgpr0_") ||
678 strstarts(symname, "_savegpr0_") ||
679 strstarts(symname, "_restvr_") ||
680 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100681 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000682 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200683 /* Do not ignore this symbol */
684 return 0;
685}
686
Masahiro Yamada17436942019-11-15 02:42:24 +0900687static void handle_modversion(const struct module *mod,
688 const struct elf_info *info,
689 const Elf_Sym *sym, const char *symname)
690{
691 unsigned int crc;
692
693 if (sym->st_shndx == SHN_UNDEF) {
694 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
695 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
696 return;
697 }
698
699 if (sym->st_shndx == SHN_ABS) {
700 crc = sym->st_value;
701 } else {
702 unsigned int *crcp;
703
704 /* symbol points to the CRC in the ELF object */
705 crcp = sym_get_data(info, sym);
706 crc = TO_NATIVE(*crcp);
707 }
708 sym_set_crc(symname, crc);
709}
710
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900711static void handle_symbol(struct module *mod, struct elf_info *info,
712 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200714 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900715 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200716
Frank Rowand258f7422012-04-09 17:59:03 -0700717 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900718 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200719 export = export_from_secname(info, get_secindex(info, sym));
720 else
721 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 switch (sym->st_shndx) {
724 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900725 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100726 /* Should warn here, but modpost runs before the linker */
727 } else
728 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 case SHN_UNDEF:
731 /* undefined symbol */
732 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
733 ELF_ST_BIND(sym->st_info) != STB_WEAK)
734 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200735 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
Ben Colline8d529012005-08-19 13:44:57 -0700737/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
738#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
739/* add compatibility with older glibc */
740#ifndef STT_SPARC_REGISTER
741#define STT_SPARC_REGISTER STT_REGISTER
742#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (info->hdr->e_machine == EM_SPARC ||
744 info->hdr->e_machine == EM_SPARCV9) {
745 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700746 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100748 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700749 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100750 munged[0] = '_';
751 munged[1] = toupper(munged[1]);
752 symname = munged;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100756
Rusty Russellb92021b2013-03-15 15:04:17 +1030757 mod->unres = alloc_symbol(symname,
758 ELF_ST_BIND(sym->st_info) == STB_WEAK,
759 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 default:
762 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900763 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100764 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100765 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900767 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900769 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 mod->has_cleanup = 1;
771 break;
772 }
773}
774
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100775/**
776 * Parse tag=value strings from .modinfo section
777 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static char *next_string(char *string, unsigned long *secsize)
779{
780 /* Skip non-zero chars */
781 while (string[0]) {
782 string++;
783 if ((*secsize)-- <= 1)
784 return NULL;
785 }
786
787 /* Skip any zero padding. */
788 while (!string[0]) {
789 string++;
790 if ((*secsize)-- <= 1)
791 return NULL;
792 }
793 return string;
794}
795
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796static char *get_next_modinfo(struct elf_info *info, const char *tag,
797 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 char *p;
800 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900801 char *modinfo = info->modinfo;
802 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900804 if (prev) {
805 size -= prev - modinfo;
806 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 for (p = modinfo; p; p = next_string(p, &size)) {
810 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
811 return p + taglen + 1;
812 }
813 return NULL;
814}
815
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900816static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200817
818{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900819 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200820}
821
Sam Ravnborg93684d32006-02-19 11:53:35 +0100822/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100823 * Test if string s ends in string sub
824 * return 0 if match
825 **/
826static int strrcmp(const char *s, const char *sub)
827{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100828 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100829
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830 if (!s || !sub)
831 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100832
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100833 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100834 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100835
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100836 if ((slen == 0) || (sublen == 0))
837 return 1;
838
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100839 if (sublen > slen)
840 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100841
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100842 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100843}
844
Sam Ravnborgff13f922008-01-23 19:54:27 +0100845static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
846{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100847 if (sym)
848 return elf->strtab + sym->st_name;
849 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100850 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100851}
852
Sam Ravnborg10668222008-01-13 22:21:31 +0100853/* The pattern is an array of simple patterns.
854 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100855 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100856 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930857 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100858 */
Trevor Keith5c725132009-09-22 16:43:38 -0700859static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100860{
861 const char *p;
862 while (*pat) {
863 p = *pat++;
864 const char *endp = p + strlen(p) - 1;
865
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930866 /* "*foo*" */
867 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300868 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
869 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930870
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930871 free(bare);
872 if (here != NULL)
873 return 1;
874 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100875 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930876 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100877 if (strrcmp(sym, p + 1) == 0)
878 return 1;
879 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100880 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100881 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100882 if (strncmp(sym, p, strlen(p) - 1) == 0)
883 return 1;
884 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100885 /* no wildcards */
886 else {
887 if (strcmp(p, sym) == 0)
888 return 1;
889 }
890 }
891 /* no match */
892 return 0;
893}
894
Sam Ravnborg10668222008-01-13 22:21:31 +0100895/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930896static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200897{
898 ".comment*",
899 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930900 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800901 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000902 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200903 ".mdebug*", /* alpha, score, mips etc. */
904 ".pdr", /* alpha, score, mips etc. */
905 ".stab*",
906 ".note*",
907 ".got*",
908 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400909 ".xt.prop", /* xtensa */
910 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030911 ".arcextmap*", /* arc */
912 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030913 ".cmem*", /* EZchip */
914 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100915 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600916 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200917 NULL
918};
Sam Ravnborg10668222008-01-13 22:21:31 +0100919
Sam Ravnborge241a632008-01-28 20:13:13 +0100920/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400921 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100922 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400923 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100924 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400925static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900926 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100927{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400928 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100929
Anders Kaseorgb614a692009-04-23 16:49:33 -0400930 if (sechdr->sh_type == SHT_PROGBITS &&
931 !(sechdr->sh_flags & SHF_ALLOC) &&
932 !match(sec, section_white_list)) {
933 warn("%s (%s): unexpected non-allocatable section.\n"
934 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
935 "Note that for example <linux/init.h> contains\n"
936 "section definitions for use in .S files.\n\n",
937 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100938 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100939}
940
941
942
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".init.setup", ".init.rodata", ".meminit.rodata", \
945 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100946#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100948
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100949#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930950 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100951#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930952 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100953
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200954#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930955 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
956 ".pci_fixup_enable", ".pci_fixup_resume", \
957 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200958
Paul Gortmakere24f6622013-06-19 19:30:48 -0400959#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
960#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100961
962#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
963#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100964
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930965#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930966#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700967 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930968#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400969 ".fixup", ".entry.text", ".exception.text", ".text.*", \
970 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100971
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000972#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000973#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100974
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000975#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000976#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100977
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930978#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
979 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
980
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100981/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930982static const char *const init_data_sections[] =
983 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984
985/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930986static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
988/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930989static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100990 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930992/* all text sections */
993static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
994
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100995/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930996static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100997
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100998
999/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001000#define DEFAULT_SYMBOL_WHITE_LIST \
1001 "*driver", \
1002 "*_template", /* scsi uses *_template a lot */ \
1003 "*_timer", /* arm uses ops structures named _timer a lot */ \
1004 "*_sht", /* scsi also used *_sht to some extent */ \
1005 "*_ops", \
1006 "*_probe", \
1007 "*_probe_one", \
1008 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001009
Mathias Krause7a3ee752014-08-27 20:28:53 +09301010static const char *const head_sections[] = { ".head.text*", NULL };
1011static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001012 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301013static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001014
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001015enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001016 TEXT_TO_ANY_INIT,
1017 DATA_TO_ANY_INIT,
1018 TEXT_TO_ANY_EXIT,
1019 DATA_TO_ANY_EXIT,
1020 XXXINIT_TO_SOME_INIT,
1021 XXXEXIT_TO_SOME_EXIT,
1022 ANY_INIT_TO_ANY_EXIT,
1023 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001024 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301025 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001026};
1027
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301028/**
1029 * Describe how to match sections on different criterias:
1030 *
1031 * @fromsec: Array of sections to be matched.
1032 *
1033 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1034 * this array is forbidden (black-list). Can be empty.
1035 *
1036 * @good_tosec: Relocations applied to a section in @fromsec must be
1037 * targetting sections in this array (white-list). Can be empty.
1038 *
1039 * @mismatch: Type of mismatch.
1040 *
1041 * @symbol_white_list: Do not match a relocation to a symbol in this list
1042 * even if it is targetting a section in @bad_to_sec.
1043 *
1044 * @handler: Specific handler to call when a match is found. If NULL,
1045 * default_mismatch_handler() will be called.
1046 *
1047 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001048struct sectioncheck {
1049 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301050 const char *bad_tosec[20];
1051 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001052 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001053 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301054 void (*handler)(const char *modname, struct elf_info *elf,
1055 const struct sectioncheck* const mismatch,
1056 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1057
Sam Ravnborg10668222008-01-13 22:21:31 +01001058};
1059
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301060static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1061 const struct sectioncheck* const mismatch,
1062 Elf_Rela *r, Elf_Sym *sym,
1063 const char *fromsec);
1064
Mathias Krause7a3ee752014-08-27 20:28:53 +09301065static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001066/* Do not reference init/exit code/data from
1067 * normal code and data
1068 */
1069{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001070 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001072 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001074},
1075{
1076 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301077 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001078 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001079 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001080},
1081{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001082 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301083 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001084 .mismatch = DATA_TO_ANY_INIT,
1085 .symbol_white_list = {
1086 "*_template", "*_timer", "*_sht", "*_ops",
1087 "*_probe", "*_probe_one", "*_console", NULL
1088 },
1089},
1090{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001091 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301092 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001093 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001094 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001095},
1096{
1097 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301098 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001099 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001100 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001101},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001102/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001103{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001104 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301105 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001106 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001107 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001108},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001109/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001110{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001111 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301112 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001113 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001114 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001115},
1116/* Do not use exit code/data from init code */
1117{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001118 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301119 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001120 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001121 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001122},
1123/* Do not use init code/data from exit code */
1124{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001125 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301126 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001127 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001128 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001129},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001130{
1131 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301132 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001133 .mismatch = ANY_INIT_TO_ANY_EXIT,
1134 .symbol_white_list = { NULL },
1135},
Sam Ravnborg10668222008-01-13 22:21:31 +01001136/* Do not export init/exit functions or data */
1137{
1138 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301139 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001140 .mismatch = EXPORT_TO_INIT_EXIT,
1141 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301142},
1143{
1144 .fromsec = { "__ex_table", NULL },
1145 /* If you're adding any new black-listed sections in here, consider
1146 * adding a special 'printer' for them in scripts/check_extable.
1147 */
1148 .bad_tosec = { ".altinstr_replacement", NULL },
1149 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1150 .mismatch = EXTABLE_TO_NON_TEXT,
1151 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001152}
1153};
1154
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001155static const struct sectioncheck *section_mismatch(
1156 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001157{
1158 int i;
1159 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1160 const struct sectioncheck *check = &sectioncheck[0];
1161
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301162 /*
1163 * The target section could be the SHT_NUL section when we're
1164 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001165 * doesn't make much sense and causes build failures on parisc
1166 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301167 */
1168 if (*tosec == '\0')
1169 return NULL;
1170
Sam Ravnborg10668222008-01-13 22:21:31 +01001171 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301172 if (match(fromsec, check->fromsec)) {
1173 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1174 return check;
1175 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1176 return check;
1177 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001178 check++;
1179 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001180 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001181}
1182
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183/**
1184 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001185 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * Pattern 1:
1187 * If a module parameter is declared __initdata and permissions=0
1188 * then this is legal despite the warning generated.
1189 * We cannot see value of permissions here, so just ignore
1190 * this pattern.
1191 * The pattern is identified by:
1192 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001193 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001194 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001195 *
Rusty Russell6a841522010-08-11 23:04:16 -06001196 * Pattern 1a:
1197 * module_param_call() ops can refer to __init set function if permissions=0
1198 * The pattern is identified by:
1199 * tosec = .init.text
1200 * fromsec = .data*
1201 * atsym = __param_ops_*
1202 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001203 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001204 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001205 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001206 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001207 * tosec = init or exit section
1208 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001209 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1210 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001211 *
1212 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001213 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001214 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001215 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001216 * Some symbols belong to init section but still it is ok to reference
1217 * these from non-init sections as these symbols don't have any memory
1218 * allocated for them and symbol address and value are same. So even
1219 * if init section is freed, its ok to reference those symbols.
1220 * For ex. symbols marking the init section boundaries.
1221 * This pattern is identified by
1222 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001223 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301224 * Pattern 5:
1225 * GCC may optimize static inlines when fed constant arg(s) resulting
1226 * in functions like cpumask_empty() -- generating an associated symbol
1227 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1228 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1229 * meaningless section warning. May need to add isra symbols too...
1230 * This pattern is identified by
1231 * tosec = init section
1232 * fromsec = text section
1233 * refsymname = *.constprop.*
1234 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001235 * Pattern 6:
1236 * Hide section mismatch warnings for ELF local symbols. The goal
1237 * is to eliminate false positive modpost warnings caused by
1238 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1239 * Autogenerated symbol names bypass modpost's "Pattern 2"
1240 * whitelisting, which relies on pattern-matching against symbol
1241 * names to work. (One situation where gcc can autogenerate ELF
1242 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001243 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001244static int secref_whitelist(const struct sectioncheck *mismatch,
1245 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001246 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001247{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001248 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001249 if (match(tosec, init_data_sections) &&
1250 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001251 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001252 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001253
Rusty Russell6a841522010-08-11 23:04:16 -06001254 /* Check for pattern 1a */
1255 if (strcmp(tosec, ".init.text") == 0 &&
1256 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001257 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001258 return 0;
1259
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001260 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001261 if (match(tosec, init_exit_sections) &&
1262 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001263 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001265
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001266 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001267 if (match(fromsec, head_sections) &&
1268 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001269 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001270
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001271 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001272 if (match(tosym, linker_symbols))
1273 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001274
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301275 /* Check for pattern 5 */
1276 if (match(fromsec, text_sections) &&
1277 match(tosec, init_sections) &&
1278 match(fromsym, optim_symbols))
1279 return 0;
1280
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001281 /* Check for pattern 6 */
1282 if (strstarts(fromsym, ".L"))
1283 return 0;
1284
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001285 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001286}
1287
Sami Tolvanen5818c682018-10-23 15:15:35 -07001288static inline int is_arm_mapping_symbol(const char *str)
1289{
1290 return str[0] == '$' && strchr("axtd", str[1])
1291 && (str[2] == '\0' || str[2] == '.');
1292}
1293
1294/*
1295 * If there's no name there, ignore it; likewise, ignore it if it's
1296 * one of the magic symbols emitted used by current ARM tools.
1297 *
1298 * Otherwise if find_symbols_between() returns those symbols, they'll
1299 * fail the whitelist tests and cause lots of false alarms ... fixable
1300 * only by merging __exit and __init sections into __text, bloating
1301 * the kernel (which is especially evil on embedded platforms).
1302 */
1303static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1304{
1305 const char *name = elf->strtab + sym->st_name;
1306
1307 if (!name || !strlen(name))
1308 return 0;
1309 return !is_arm_mapping_symbol(name);
1310}
1311
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001312/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001313 * Find symbol based on relocation record info.
1314 * In some cases the symbol supplied is a valid symbol so
1315 * return refsym. If st_name != 0 we assume this is a valid symbol.
1316 * In other cases the symbol needs to be looked up in the symbol table
1317 * based on section and address.
1318 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001319static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001320 Elf_Sym *relsym)
1321{
1322 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001323 Elf_Sym *near = NULL;
1324 Elf64_Sword distance = 20;
1325 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001326 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001327
1328 if (relsym->st_name != 0)
1329 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001330
1331 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001332 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001333 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001334 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001335 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1336 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001337 if (!is_valid_name(elf, sym))
1338 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001339 if (sym->st_value == addr)
1340 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001341 /* Find a symbol nearby - addr are maybe negative */
1342 d = sym->st_value - addr;
1343 if (d < 0)
1344 d = addr - sym->st_value;
1345 if (d < distance) {
1346 distance = d;
1347 near = sym;
1348 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001349 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001350 /* We need a close match */
1351 if (distance < 20)
1352 return near;
1353 else
1354 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001355}
1356
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001358 * Find symbols before or equal addr and after addr - in the section sec.
1359 * If we find two symbols with equal offset prefer one with a valid name.
1360 * The ELF format may have a better way to detect what type of symbol
1361 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1364 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001365{
1366 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001367 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001368 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001369
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1371 const char *symsec;
1372
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001373 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001374 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001375 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001376 if (strcmp(symsec, sec) != 0)
1377 continue;
David Brownellda68d612007-02-20 13:58:16 -08001378 if (!is_valid_name(elf, sym))
1379 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001380 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001381 if ((addr - sym->st_value) < distance) {
1382 distance = addr - sym->st_value;
1383 near = sym;
1384 } else if ((addr - sym->st_value) == distance) {
1385 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001386 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001387 }
1388 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001389 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001390}
1391
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001392/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001393 * Convert a section name to the function/data attribute
1394 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001395 * .memexitconst => __memconst
1396 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001397 *
1398 * The memory of returned value has been allocated on a heap. The user of this
1399 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001400*/
1401static char *sec2annotation(const char *s)
1402{
1403 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001404 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 char *r = p;
1406
1407 *p++ = '_';
1408 *p++ = '_';
1409 if (*s == '.')
1410 s++;
1411 while (*s && *s != '.')
1412 *p++ = *s++;
1413 *p = '\0';
1414 if (*s == '.')
1415 s++;
1416 if (strstr(s, "rodata") != NULL)
1417 strcat(p, "const ");
1418 else if (strstr(s, "data") != NULL)
1419 strcat(p, "data ");
1420 else
1421 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001422 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001423 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001424 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001425 }
1426}
1427
1428static int is_function(Elf_Sym *sym)
1429{
1430 if (sym)
1431 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1432 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001433 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001434}
1435
Randy Dunlap00759c02011-03-15 14:13:47 -07001436static void print_section_list(const char * const list[20])
1437{
1438 const char *const *s = list;
1439
1440 while (*s) {
1441 fprintf(stderr, "%s", *s);
1442 s++;
1443 if (*s)
1444 fprintf(stderr, ", ");
1445 }
1446 fprintf(stderr, "\n");
1447}
1448
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301449static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1450{
1451 switch (is_func) {
1452 case 0: *name = "variable"; *name_p = ""; break;
1453 case 1: *name = "function"; *name_p = "()"; break;
1454 default: *name = "(unknown reference)"; *name_p = ""; break;
1455 }
1456}
1457
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001458/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001459 * Print a warning about a section mismatch.
1460 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001461 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001462 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001463static void report_sec_mismatch(const char *modname,
1464 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001465 const char *fromsec,
1466 unsigned long long fromaddr,
1467 const char *fromsym,
1468 int from_is_func,
1469 const char *tosec, const char *tosym,
1470 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001471{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 const char *from, *from_p;
1473 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001474 char *prl_from;
1475 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001476
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001477 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001478
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301479 get_pretty_name(from_is_func, &from, &from_p);
1480 get_pretty_name(to_is_func, &to, &to_p);
1481
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001482 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1483 "to the %s %s:%s%s\n",
1484 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1485 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001487 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001488 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001489 prl_from = sec2annotation(fromsec);
1490 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001491 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001492 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 "the %s %s%s%s.\n"
1494 "This is often because %s lacks a %s\n"
1495 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001496 prl_from, fromsym,
1497 to, prl_to, tosym, to_p,
1498 fromsym, prl_to, tosym);
1499 free(prl_from);
1500 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001501 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001502 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001503 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 fprintf(stderr,
1505 "The variable %s references\n"
1506 "the %s %s%s%s\n"
1507 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001508 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001510 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001511 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001513 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001514 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001515 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001516 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 fprintf(stderr,
1518 "The function %s() references a %s in an exit section.\n"
1519 "Often the %s %s%s has valid usage outside the exit section\n"
1520 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 fromsym, to, to, tosym, to_p, prl_to, tosym);
1522 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001524 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001525 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001526 fprintf(stderr,
1527 "The variable %s references\n"
1528 "the %s %s%s%s\n"
1529 "If the reference is valid then annotate the\n"
1530 "variable with __exit* (see linux/init.h) or "
1531 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001532 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001533 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001535 break;
1536 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001537 case XXXINIT_TO_SOME_INIT:
1538 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001539 prl_from = sec2annotation(fromsec);
1540 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001541 fprintf(stderr,
1542 "The %s %s%s%s references\n"
1543 "a %s %s%s%s.\n"
1544 "If %s is only used by %s then\n"
1545 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 from, prl_from, fromsym, from_p,
1547 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001548 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001549 free(prl_from);
1550 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001551 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001552 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001553 prl_from = sec2annotation(fromsec);
1554 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001555 fprintf(stderr,
1556 "The %s %s%s%s references\n"
1557 "a %s %s%s%s.\n"
1558 "This is often seen when error handling "
1559 "in the init function\n"
1560 "uses functionality in the exit path.\n"
1561 "The fix is often to remove the %sannotation of\n"
1562 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001563 from, prl_from, fromsym, from_p,
1564 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001565 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001566 free(prl_from);
1567 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001568 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001569 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001570 prl_from = sec2annotation(fromsec);
1571 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001572 fprintf(stderr,
1573 "The %s %s%s%s references\n"
1574 "a %s %s%s%s.\n"
1575 "This is often seen when error handling "
1576 "in the exit function\n"
1577 "uses functionality in the init path.\n"
1578 "The fix is often to remove the %sannotation of\n"
1579 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001580 from, prl_from, fromsym, from_p,
1581 to, prl_to, tosym, to_p,
1582 prl_to, tosym, to_p);
1583 free(prl_from);
1584 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001585 break;
1586 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001587 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001588 fprintf(stderr,
1589 "The symbol %s is exported and annotated %s\n"
1590 "Fix this by removing the %sannotation of %s "
1591 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001592 tosym, prl_to, prl_to, tosym);
1593 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001594 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301595 case EXTABLE_TO_NON_TEXT:
1596 fatal("There's a special handler for this mismatch type, "
1597 "we should never get here.");
1598 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001599 }
1600 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001601}
1602
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301603static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1604 const struct sectioncheck* const mismatch,
1605 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1606{
1607 const char *tosec;
1608 Elf_Sym *to;
1609 Elf_Sym *from;
1610 const char *tosym;
1611 const char *fromsym;
1612
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301613 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1614 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301615
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001616 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301617 return;
1618
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301619 tosec = sec_name(elf, get_secindex(elf, sym));
1620 to = find_elf_symbol(elf, r->r_addend, sym);
1621 tosym = sym_name(elf, to);
1622
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301623 /* check whitelist - we may ignore it */
1624 if (secref_whitelist(mismatch,
1625 fromsec, fromsym, tosec, tosym)) {
1626 report_sec_mismatch(modname, mismatch,
1627 fromsec, r->r_offset, fromsym,
1628 is_function(from), tosec, tosym,
1629 is_function(to));
1630 }
1631}
1632
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301633static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1634{
1635 if (section_index > elf->num_sections)
1636 fatal("section_index is outside elf->num_sections!\n");
1637
1638 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1639}
1640
1641/*
1642 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1643 * to know the sizeof(struct exception_table_entry) for the target architecture.
1644 */
1645static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301646static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301647{
1648 /*
1649 * If we're currently checking the second relocation within __ex_table,
1650 * that relocation offset tells us the offsetof(struct
1651 * exception_table_entry, fixup) which is equal to sizeof(struct
1652 * exception_table_entry) divided by two. We use that to our advantage
1653 * since there's no portable way to get that size as every architecture
1654 * seems to go with different sized types. Not pretty but better than
1655 * hard-coding the size for every architecture..
1656 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301657 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658 extable_entry_size = r->r_offset * 2;
1659}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301660
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301661static inline bool is_extable_fault_address(Elf_Rela *r)
1662{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301663 /*
1664 * extable_entry_size is only discovered after we've handled the
1665 * _second_ relocation in __ex_table, so only abort when we're not
1666 * handling the first reloc and extable_entry_size is zero.
1667 */
1668 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301669 fatal("extable_entry size hasn't been discovered!\n");
1670
1671 return ((r->r_offset == 0) ||
1672 (r->r_offset % extable_entry_size == 0));
1673}
1674
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301675#define is_second_extable_reloc(Start, Cur, Sec) \
1676 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1677
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301678static void report_extable_warnings(const char* modname, struct elf_info* elf,
1679 const struct sectioncheck* const mismatch,
1680 Elf_Rela* r, Elf_Sym* sym,
1681 const char* fromsec, const char* tosec)
1682{
1683 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1684 const char* fromsym_name = sym_name(elf, fromsym);
1685 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1686 const char* tosym_name = sym_name(elf, tosym);
1687 const char* from_pretty_name;
1688 const char* from_pretty_name_p;
1689 const char* to_pretty_name;
1690 const char* to_pretty_name_p;
1691
1692 get_pretty_name(is_function(fromsym),
1693 &from_pretty_name, &from_pretty_name_p);
1694 get_pretty_name(is_function(tosym),
1695 &to_pretty_name, &to_pretty_name_p);
1696
1697 warn("%s(%s+0x%lx): Section mismatch in reference"
1698 " from the %s %s%s to the %s %s:%s%s\n",
1699 modname, fromsec, (long)r->r_offset, from_pretty_name,
1700 fromsym_name, from_pretty_name_p,
1701 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1702
1703 if (!match(tosec, mismatch->bad_tosec) &&
1704 is_executable_section(elf, get_secindex(elf, sym)))
1705 fprintf(stderr,
1706 "The relocation at %s+0x%lx references\n"
1707 "section \"%s\" which is not in the list of\n"
1708 "authorized sections. If you're adding a new section\n"
1709 "and/or if this reference is valid, add \"%s\" to the\n"
1710 "list of authorized sections to jump to on fault.\n"
1711 "This can be achieved by adding \"%s\" to \n"
1712 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1713 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1714}
1715
1716static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1717 const struct sectioncheck* const mismatch,
1718 Elf_Rela* r, Elf_Sym* sym,
1719 const char *fromsec)
1720{
1721 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1722
1723 sec_mismatch_count++;
1724
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001725 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301726
1727 if (match(tosec, mismatch->bad_tosec))
1728 fatal("The relocation at %s+0x%lx references\n"
1729 "section \"%s\" which is black-listed.\n"
1730 "Something is seriously wrong and should be fixed.\n"
1731 "You might get more information about where this is\n"
1732 "coming from by using scripts/check_extable.sh %s\n",
1733 fromsec, (long)r->r_offset, tosec, modname);
1734 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1735 if (is_extable_fault_address(r))
1736 fatal("The relocation at %s+0x%lx references\n"
1737 "section \"%s\" which is not executable, IOW\n"
1738 "it is not possible for the kernel to fault\n"
1739 "at that address. Something is seriously wrong\n"
1740 "and should be fixed.\n",
1741 fromsec, (long)r->r_offset, tosec);
1742 else
1743 fatal("The relocation at %s+0x%lx references\n"
1744 "section \"%s\" which is not executable, IOW\n"
1745 "the kernel will fault if it ever tries to\n"
1746 "jump to it. Something is seriously wrong\n"
1747 "and should be fixed.\n",
1748 fromsec, (long)r->r_offset, tosec);
1749 }
1750}
1751
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001752static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001753 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001754{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001755 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301756 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001757
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001758 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301759 if (mismatch->handler)
1760 mismatch->handler(modname, elf, mismatch,
1761 r, sym, fromsec);
1762 else
1763 default_mismatch_handler(modname, elf, mismatch,
1764 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001765 }
1766}
1767
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001768static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001769 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001770{
1771 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001772 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001773
1774 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001775 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001776}
1777
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001778static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001779{
1780 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001781 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001782
1783 switch (r_typ) {
1784 case R_386_32:
1785 r->r_addend = TO_NATIVE(*location);
1786 break;
1787 case R_386_PC32:
1788 r->r_addend = TO_NATIVE(*location) + 4;
1789 /* For CONFIG_RELOCATABLE=y */
1790 if (elf->hdr->e_type == ET_EXEC)
1791 r->r_addend += r->r_offset;
1792 break;
1793 }
1794 return 0;
1795}
1796
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001797#ifndef R_ARM_CALL
1798#define R_ARM_CALL 28
1799#endif
1800#ifndef R_ARM_JUMP24
1801#define R_ARM_JUMP24 29
1802#endif
1803
David A. Longc9698e52014-02-14 22:41:18 +01001804#ifndef R_ARM_THM_CALL
1805#define R_ARM_THM_CALL 10
1806#endif
1807#ifndef R_ARM_THM_JUMP24
1808#define R_ARM_THM_JUMP24 30
1809#endif
1810#ifndef R_ARM_THM_JUMP19
1811#define R_ARM_THM_JUMP19 51
1812#endif
1813
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001814static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001815{
1816 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1817
1818 switch (r_typ) {
1819 case R_ARM_ABS32:
1820 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001821 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001822 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001823 break;
1824 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001825 case R_ARM_CALL:
1826 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001827 case R_ARM_THM_CALL:
1828 case R_ARM_THM_JUMP24:
1829 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001830 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001831 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001832 sechdr->sh_offset +
1833 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001834 break;
1835 default:
1836 return 1;
1837 }
1838 return 0;
1839}
1840
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001842{
1843 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001845 unsigned int inst;
1846
1847 if (r_typ == R_MIPS_HI16)
1848 return 1; /* skip this */
1849 inst = TO_NATIVE(*location);
1850 switch (r_typ) {
1851 case R_MIPS_LO16:
1852 r->r_addend = inst & 0xffff;
1853 break;
1854 case R_MIPS_26:
1855 r->r_addend = (inst & 0x03ffffff) << 2;
1856 break;
1857 case R_MIPS_32:
1858 r->r_addend = inst;
1859 break;
1860 }
1861 return 0;
1862}
1863
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001865 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866{
1867 Elf_Sym *sym;
1868 Elf_Rela *rela;
1869 Elf_Rela r;
1870 unsigned int r_sym;
1871 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001872
Sam Ravnborgff13f922008-01-23 19:54:27 +01001873 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1875
Sam Ravnborgff13f922008-01-23 19:54:27 +01001876 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001877 fromsec += strlen(".rela");
1878 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001879 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001880 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001881
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001882 for (rela = start; rela < stop; rela++) {
1883 r.r_offset = TO_NATIVE(rela->r_offset);
1884#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001885 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001886 unsigned int r_typ;
1887 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1888 r_sym = TO_NATIVE(r_sym);
1889 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1890 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1891 } else {
1892 r.r_info = TO_NATIVE(rela->r_info);
1893 r_sym = ELF_R_SYM(r.r_info);
1894 }
1895#else
1896 r.r_info = TO_NATIVE(rela->r_info);
1897 r_sym = ELF_R_SYM(r.r_info);
1898#endif
1899 r.r_addend = TO_NATIVE(rela->r_addend);
1900 sym = elf->symtab_start + r_sym;
1901 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001902 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301904 if (is_second_extable_reloc(start, rela, fromsec))
1905 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001906 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907 }
1908}
1909
1910static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001911 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912{
1913 Elf_Sym *sym;
1914 Elf_Rel *rel;
1915 Elf_Rela r;
1916 unsigned int r_sym;
1917 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001918
Sam Ravnborgff13f922008-01-23 19:54:27 +01001919 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001920 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1921
Sam Ravnborgff13f922008-01-23 19:54:27 +01001922 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001923 fromsec += strlen(".rel");
1924 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001925 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001926 return;
1927
1928 for (rel = start; rel < stop; rel++) {
1929 r.r_offset = TO_NATIVE(rel->r_offset);
1930#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001931 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001932 unsigned int r_typ;
1933 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1934 r_sym = TO_NATIVE(r_sym);
1935 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1936 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1937 } else {
1938 r.r_info = TO_NATIVE(rel->r_info);
1939 r_sym = ELF_R_SYM(r.r_info);
1940 }
1941#else
1942 r.r_info = TO_NATIVE(rel->r_info);
1943 r_sym = ELF_R_SYM(r.r_info);
1944#endif
1945 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001946 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001947 case EM_386:
1948 if (addend_386_rel(elf, sechdr, &r))
1949 continue;
1950 break;
1951 case EM_ARM:
1952 if (addend_arm_rel(elf, sechdr, &r))
1953 continue;
1954 break;
1955 case EM_MIPS:
1956 if (addend_mips_rel(elf, sechdr, &r))
1957 continue;
1958 break;
1959 }
1960 sym = elf->symtab_start + r_sym;
1961 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001962 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001963 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301964 if (is_second_extable_reloc(start, rel, fromsec))
1965 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001966 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001967 }
1968}
1969
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970/**
1971 * A module includes a number of sections that are discarded
1972 * either when loaded or when used as built-in.
1973 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001974 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001975 * Likewise for modules used built-in the sections marked __exit
1976 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001977 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001978 * The check_sec_ref() function traverses all relocation records
1979 * to find all references to a section that reference a section that will
1980 * be discarded and warns about it.
1981 **/
1982static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001983 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001984{
1985 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001986 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001987
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001988 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001989 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001990 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001991 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001992 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001993 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001994 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001995 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001996 }
1997}
1998
Andi Kleen7d02b492014-02-08 09:01:12 +01001999static char *remove_dot(char *s)
2000{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09302001 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01002002
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09302003 if (n && s[n]) {
2004 size_t m = strspn(s + n + 1, "0123456789");
2005 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002006 s[n] = 0;
2007 }
2008 return s;
2009}
2010
Masahiro Yamada8b185742018-05-09 18:50:40 +09002011static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 const char *symname;
2014 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002015 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002016 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct module *mod;
2018 struct elf_info info = { };
2019 Elf_Sym *sym;
2020
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002021 if (!parse_elf(&info, modname))
2022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 mod = new_module(modname);
2025
2026 /* When there's no vmlinux, don't print warnings about
2027 * unresolved symbols (since there'll be too many ;) */
2028 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 mod->skip = 1;
2031 }
2032
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002033 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002034 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002035 warn("modpost: missing MODULE_LICENSE() in %s\n"
2036 "see include/linux/module.h for "
2037 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002038 while (license) {
2039 if (license_is_gpl_compatible(license))
2040 mod->gpl_compatible = 1;
2041 else {
2042 mod->gpl_compatible = 0;
2043 break;
2044 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002045 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002046 }
2047
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002048 namespace = get_modinfo(&info, "import_ns");
2049 while (namespace) {
2050 add_namespace(&mod->imported_namespaces, namespace);
2051 namespace = get_next_modinfo(&info, "import_ns", namespace);
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002055 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002057 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 handle_moddevtable(mod, &info, sym, symname);
2059 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002060
Matthias Maennich69923202019-10-18 10:31:42 +01002061 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2062 symname = remove_dot(info.strtab + sym->st_name);
2063
Masahiro Yamada17436942019-11-15 02:42:24 +09002064 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002065 if (strstarts(symname, "__kstrtabns_"))
2066 sym_update_namespace(symname + strlen("__kstrtabns_"),
2067 namespace_from_kstrtabns(&info,
2068 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002069
2070 if (strstarts(symname, "__crc_"))
2071 handle_modversion(mod, &info, sym,
2072 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002073 }
2074
Denis Efremov15bfc232019-08-01 09:06:57 +03002075 // check for static EXPORT_SYMBOL_* functions && global vars
2076 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2077 unsigned char bind = ELF_ST_BIND(sym->st_info);
2078
2079 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2080 struct symbol *s =
2081 find_symbol(remove_dot(info.strtab +
2082 sym->st_name));
2083
2084 if (s)
2085 s->is_static = 0;
2086 }
2087 }
2088
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002089 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002090 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002092 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (version)
2094 maybe_frob_rcs_version(modname, version, info.modinfo,
2095 version - (char *)info.hdr);
2096 if (version || (all_versions && !is_vmlinux(modname)))
2097 get_src_version(modname, mod->srcversion,
2098 sizeof(mod->srcversion)-1);
2099
2100 parse_elf_finish(&info);
2101
Rusty Russell8c8ef422009-03-31 13:05:34 -06002102 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 * never passed as an argument to an exported function, so
2104 * the automatic versioning doesn't pick it up, but it's really
2105 * important anyhow */
2106 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002107 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
Rusty Russell712f9b42013-04-04 17:37:38 +10302110static void read_symbols_from_files(const char *filename)
2111{
2112 FILE *in = stdin;
2113 char fname[PATH_MAX];
2114
2115 if (strcmp(filename, "-") != 0) {
2116 in = fopen(filename, "r");
2117 if (!in)
2118 fatal("Can't open filenames file %s: %m", filename);
2119 }
2120
2121 while (fgets(fname, PATH_MAX, in) != NULL) {
2122 if (strends(fname, "\n"))
2123 fname[strlen(fname)-1] = '\0';
2124 read_symbols(fname);
2125 }
2126
2127 if (in != stdin)
2128 fclose(in);
2129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131#define SZ 500
2132
2133/* We first write the generated file into memory using the
2134 * following helper, then compare to the file on disk and
2135 * only update the later if anything changed */
2136
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002137void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2138 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 char tmp[SZ];
2141 int len;
2142 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 va_start(ap, fmt);
2145 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002146 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 va_end(ap);
2148}
2149
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002150void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002153 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002154 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 strncpy(buf->p + buf->pos, s, len);
2157 buf->pos += len;
2158}
2159
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002160static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2161{
2162 const char *e = is_vmlinux(m) ?"":".ko";
2163
2164 switch (exp) {
2165 case export_gpl:
2166 fatal("modpost: GPL-incompatible module %s%s "
2167 "uses GPL-only symbol '%s'\n", m, e, s);
2168 break;
2169 case export_unused_gpl:
2170 fatal("modpost: GPL-incompatible module %s%s "
2171 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2172 break;
2173 case export_gpl_future:
2174 warn("modpost: GPL-incompatible module %s%s "
2175 "uses future GPL-only symbol '%s'\n", m, e, s);
2176 break;
2177 case export_plain:
2178 case export_unused:
2179 case export_unknown:
2180 /* ignore */
2181 break;
2182 }
2183}
2184
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002185static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002186{
2187 const char *e = is_vmlinux(m) ?"":".ko";
2188
2189 switch (exp) {
2190 case export_unused:
2191 case export_unused_gpl:
2192 warn("modpost: module %s%s "
2193 "uses symbol '%s' marked UNUSED\n", m, e, s);
2194 break;
2195 default:
2196 /* ignore */
2197 break;
2198 }
2199}
2200
Masahiro Yamada3b415282018-11-23 16:57:23 +09002201static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002202{
2203 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002204 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002205
2206 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002207 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002208 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002209 if (!exp || exp->module == mod) {
2210 if (have_vmlinux && !s->weak) {
2211 if (warn_unresolved) {
2212 warn("\"%s\" [%s.ko] undefined!\n",
2213 s->name, mod->name);
2214 } else {
2215 merror("\"%s\" [%s.ko] undefined!\n",
2216 s->name, mod->name);
2217 err = 1;
2218 }
2219 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002220 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002221 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002222 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002223 if (basename)
2224 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002225 else
2226 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002227
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002228 if (exp->namespace &&
2229 !module_imports_namespace(mod, exp->namespace)) {
2230 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2231 basename, exp->name, exp->namespace);
2232 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002233 }
2234
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002235 if (!mod->gpl_compatible)
2236 check_for_gpl_usage(exp->export, basename, exp->name);
2237 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002238 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002239
2240 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002241}
2242
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002243static int check_modname_len(struct module *mod)
2244{
2245 const char *mod_name;
2246
2247 mod_name = strrchr(mod->name, '/');
2248 if (mod_name == NULL)
2249 mod_name = mod->name;
2250 else
2251 mod_name++;
2252 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2253 merror("module name is too long [%s.ko]\n", mod->name);
2254 return 1;
2255 }
2256
2257 return 0;
2258}
2259
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002260/**
2261 * Header for the generated file
2262 **/
2263static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Laura Abbott9afb7192018-07-05 17:49:37 -07002265 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 buf_printf(b, "#include <linux/module.h>\n");
2267 buf_printf(b, "#include <linux/vermagic.h>\n");
2268 buf_printf(b, "#include <linux/compiler.h>\n");
2269 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002270 buf_printf(b, "BUILD_SALT;\n");
2271 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002273 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302275 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002276 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002277 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002279 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (mod->has_cleanup)
2281 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002282 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002284 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 buf_printf(b, "};\n");
2286}
2287
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002288static void add_intree_flag(struct buffer *b, int is_intree)
2289{
2290 if (is_intree)
2291 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2292}
2293
Andi Kleencaf75012018-01-25 15:50:28 -08002294/* Cannot check for assembler */
2295static void add_retpoline(struct buffer *b)
2296{
WANG Chaoe4f35892018-12-11 00:37:25 +08002297 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002298 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2299 buf_printf(b, "#endif\n");
2300}
2301
Trevor Keith5c725132009-09-22 16:43:38 -07002302static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002303{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002304 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002305 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2306}
2307
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002308/**
2309 * Record CRCs for unresolved symbols
2310 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002311static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002314 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 for (s = mod->unres; s; s = s->next) {
2317 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002318 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 s->module = exp->module;
2321 s->crc_valid = exp->crc_valid;
2322 s->crc = exp->crc;
2323 }
2324
2325 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 buf_printf(b, "\n");
2329 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002330 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002333 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002336 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 s->name, mod->name);
2338 continue;
2339 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302340 if (strlen(s->name) >= MODULE_NAME_LEN) {
2341 merror("too long symbol \"%s\" [%s.ko]\n",
2342 s->name, mod->name);
2343 err = 1;
2344 break;
2345 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002346 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302347 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
2350 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002351
2352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002355static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 int first = 1;
2359
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002360 /* Clear ->seen flag of modules that own symbols needed by this. */
2361 for (s = mod->unres; s; s = s->next)
2362 if (s->module)
2363 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002366 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002368 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (!s->module)
2370 continue;
2371
2372 if (s->module->seen)
2373 continue;
2374
2375 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002376 p = strrchr(s->module->name, '/');
2377 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002378 p++;
2379 else
2380 p = s->module->name;
2381 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 first = 0;
2383 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002384 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002387static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 if (mod->srcversion[0]) {
2390 buf_printf(b, "\n");
2391 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2392 mod->srcversion);
2393 }
2394}
2395
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002396static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 char *tmp;
2399 FILE *file;
2400 struct stat st;
2401
2402 file = fopen(fname, "r");
2403 if (!file)
2404 goto write;
2405
2406 if (fstat(fileno(file), &st) < 0)
2407 goto close_write;
2408
2409 if (st.st_size != b->pos)
2410 goto close_write;
2411
2412 tmp = NOFAIL(malloc(b->pos));
2413 if (fread(tmp, 1, b->pos, file) != b->pos)
2414 goto free_write;
2415
2416 if (memcmp(tmp, b->p, b->pos) != 0)
2417 goto free_write;
2418
2419 free(tmp);
2420 fclose(file);
2421 return;
2422
2423 free_write:
2424 free(tmp);
2425 close_write:
2426 fclose(file);
2427 write:
2428 file = fopen(fname, "w");
2429 if (!file) {
2430 perror(fname);
2431 exit(1);
2432 }
2433 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2434 perror(fname);
2435 exit(1);
2436 }
2437 fclose(file);
2438}
2439
Ram Paibd5cbce2006-06-08 22:12:53 -07002440/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002441 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002442 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002443static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 unsigned long size, pos = 0;
2446 void *file = grab_file(fname, &size);
2447 char *line;
2448
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002449 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /* No symbol versions, silently ignore */
2451 return;
2452
2453 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002454 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 unsigned int crc;
2456 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002457 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 if (!(symname = strchr(line, '\t')))
2460 goto fail;
2461 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002462 if (!(namespace = strchr(symname, '\t')))
2463 goto fail;
2464 *namespace++ = '\0';
2465 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 goto fail;
2467 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002468 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002469 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002470 if (export && ((end = strchr(export, '\t')) != NULL))
2471 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 crc = strtoul(line, &d, 16);
2473 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2474 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002475 mod = find_module(modname);
2476 if (!mod) {
2477 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002479 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 mod->skip = 1;
2481 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002482 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002483 s->kernel = kernel;
2484 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002485 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002486 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002487 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302489 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return;
2491fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302492 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 fatal("parse error in symbol dump file\n");
2494}
2495
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496/* For normal builds always dump all symbols.
2497 * For external modules only dump symbols
2498 * that are not read from kernel Module.symvers.
2499 **/
2500static int dump_sym(struct symbol *sym)
2501{
2502 if (!external_module)
2503 return 1;
2504 if (sym->vmlinux || sym->kernel)
2505 return 0;
2506 return 1;
2507}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002508
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002509static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 struct buffer buf = { };
2512 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002513 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int n;
2515
2516 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2517 symbol = symbolhash[n];
2518 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002519 if (dump_sym(symbol)) {
2520 namespace = symbol->namespace;
2521 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2522 symbol->crc, symbol->name,
2523 namespace ? namespace : "",
2524 symbol->module->name,
2525 export_str(symbol->export));
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 symbol = symbol->next;
2528 }
2529 }
2530 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002531 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002534static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002535{
2536 struct module *mod;
2537 struct namespace_list *ns;
2538 struct buffer ns_deps_buf = {};
2539
2540 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002541
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002542 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002543 continue;
2544
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002545 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002546
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002547 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2548 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002549
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002550 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002551 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002552
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002553 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002554 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002555}
2556
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002557struct ext_sym_list {
2558 struct ext_sym_list *next;
2559 const char *file;
2560};
2561
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002562int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 struct module *mod;
2565 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002566 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002567 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302568 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002570 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002571 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002572 struct ext_sym_list *extsym_iter;
2573 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002575 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002576 switch (opt) {
2577 case 'i':
2578 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002579 external_module = 1;
2580 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002581 case 'e':
2582 external_module = 1;
2583 extsym_iter =
2584 NOFAIL(malloc(sizeof(*extsym_iter)));
2585 extsym_iter->next = extsym_start;
2586 extsym_iter->file = optarg;
2587 extsym_start = extsym_iter;
2588 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002589 case 'm':
2590 modversions = 1;
2591 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302592 case 'n':
2593 ignore_missing_files = 1;
2594 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002595 case 'o':
2596 dump_write = optarg;
2597 break;
2598 case 'a':
2599 all_versions = 1;
2600 break;
2601 case 's':
2602 vmlinux_section_warnings = 0;
2603 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302604 case 'T':
2605 files_source = optarg;
2606 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002607 case 'w':
2608 warn_unresolved = 1;
2609 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302610 case 'E':
2611 sec_mismatch_fatal = 1;
2612 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002613 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002614 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002615 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002616 default:
2617 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619 }
2620
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002621 if (kernel_read)
2622 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002623 while (extsym_start) {
2624 read_dump(extsym_start->file, 0);
2625 extsym_iter = extsym_start->next;
2626 free(extsym_start);
2627 extsym_start = extsym_iter;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002630 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Rusty Russell712f9b42013-04-04 17:37:38 +10302633 if (files_source)
2634 read_symbols_from_files(files_source);
2635
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002636 err = 0;
2637
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002638 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302639 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002640
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002641 if (mod->skip)
2642 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 buf.pos = 0;
2645
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002646 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002647 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002650 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002651 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002652 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002653 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002654 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 add_moddevtable(&buf, mod);
2656 add_srcversion(&buf, mod);
2657
2658 sprintf(fname, "%s.mod.c", mod->name);
2659 write_if_changed(&buf, fname);
2660 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002661
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002662 if (missing_namespace_deps)
2663 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (dump_write)
2666 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002667 if (sec_mismatch_count && sec_mismatch_fatal)
2668 fatal("modpost: Section mismatches detected.\n"
2669 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002670 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002671 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002672
Masahiro Yamada47346e92019-09-24 21:07:40 +09002673 for (s = symbolhash[n]; s; s = s->next) {
2674 /*
2675 * Do not check "vmlinux". This avoids the same warnings
2676 * shown twice, and false-positives for ARCH=um.
2677 */
2678 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2679 continue;
2680
Denis Efremov15bfc232019-08-01 09:06:57 +03002681 if (s->is_static)
2682 warn("\"%s\" [%s] is a static %s\n",
2683 s->name, s->module->name,
2684 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002685 }
2686 }
2687
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002688 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}