blob: a3ffabf4eca5bbca758fae6a484f5b0845c5399c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103037static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093038/* ignore missing files */
39static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010040/* If set to 1, only warn (instead of error) about missing ns imports */
41static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Jessica Yu93c95e52020-03-06 17:02:05 +010054void __attribute__((format(printf, 2, 3)))
55modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
Jessica Yu93c95e52020-03-06 17:02:05 +010059 switch (loglevel) {
60 case LOG_WARN:
61 fprintf(stderr, "WARNING: ");
62 break;
63 case LOG_ERROR:
64 fprintf(stderr, "ERROR: ");
65 break;
66 case LOG_FATAL:
67 fprintf(stderr, "FATAL: ");
68 break;
69 default: /* invalid loglevel, ignore */
70 break;
71 }
72
73 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78
Jessica Yu93c95e52020-03-06 17:02:05 +010079 if (loglevel == LOG_FATAL)
80 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060081}
82
Rusty Russelld4ef1c32013-04-04 17:37:32 +103083static inline bool strends(const char *str, const char *postfix)
84{
85 if (strlen(str) < strlen(postfix))
86 return false;
87
88 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void *do_nofail(void *ptr, const char *expr)
92{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010093 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +010094 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return ptr;
97}
98
Masahiro Yamadaac5100f2020-06-01 14:57:17 +090099char *read_text_file(const char *filename)
100{
101 struct stat st;
102 size_t nbytes;
103 int fd;
104 char *buf;
105
106 fd = open(filename, O_RDONLY);
107 if (fd < 0) {
108 perror(filename);
109 exit(1);
110 }
111
112 if (fstat(fd, &st) < 0) {
113 perror(filename);
114 exit(1);
115 }
116
117 buf = NOFAIL(malloc(st.st_size + 1));
118
119 nbytes = st.st_size;
120
121 while (nbytes) {
122 ssize_t bytes_read;
123
124 bytes_read = read(fd, buf, nbytes);
125 if (bytes_read < 0) {
126 perror(filename);
127 exit(1);
128 }
129
130 nbytes -= bytes_read;
131 }
132 buf[st.st_size] = '\0';
133
134 close(fd);
135
136 return buf;
137}
138
139char *get_line(char **stringp)
140{
141 /* do not return the unwanted extra line at EOF */
142 if (*stringp && **stringp == '\0')
143 return NULL;
144
145 return strsep(stringp, "\n");
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct module *modules;
150
Masahiro Yamada8b185742018-05-09 18:50:40 +0900151static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct module *mod;
154
155 for (mod = modules; mod; mod = mod->next)
156 if (strcmp(mod->name, modname) == 0)
157 break;
158 return mod;
159}
160
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030161static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100164
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900165 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900169 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900170 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200171 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 mod->next = modules;
173 modules = mod;
174
Masahiro Yamada858b9372020-06-01 14:57:28 +0900175 if (mod->is_vmlinux)
176 have_vmlinux = 1;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return mod;
179}
180
181/* A hash of all exported symbols,
182 * struct symbol is also used for lists of unresolved symbols */
183
184#define SYMBOL_HASH_SIZE 1024
185
186struct symbol {
187 struct symbol *next;
188 struct module *module;
189 unsigned int crc;
190 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900191 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300193 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700194 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500195 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
199
200/* This is based on the hash agorithm from gdbm, via tdb */
201static inline unsigned int tdb_hash(const char *name)
202{
203 unsigned value; /* Used to compute the hash value. */
204 unsigned i; /* Used to cycle through random values. */
205
206 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100207 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
209
210 return (1103515243 * value + 12345);
211}
212
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100213/**
214 * Allocate a new symbols for use in the hash of exported symbols or
215 * the list of unresolved symbols per module
216 **/
217static struct symbol *alloc_symbol(const char *name, unsigned int weak,
218 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
221
222 memset(s, 0, sizeof(*s));
223 strcpy(s->name, name);
224 s->weak = weak;
225 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300226 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return s;
228}
229
230/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700231static struct symbol *new_symbol(const char *name, struct module *module,
232 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900237 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
238
239 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100242static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct symbol *s;
245
246 /* For our purposes, .foo matches foo. PPC64 needs this. */
247 if (name[0] == '.')
248 name++;
249
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100250 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (strcmp(s->name, name) == 0)
252 return s;
253 }
254 return NULL;
255}
256
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100257static bool contains_namespace(struct namespace_list *list,
258 const char *namespace)
259{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900260 for (; list; list = list->next)
261 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100262 return true;
263
264 return false;
265}
266
267static void add_namespace(struct namespace_list **list, const char *namespace)
268{
269 struct namespace_list *ns_entry;
270
271 if (!contains_namespace(*list, namespace)) {
272 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
273 strlen(namespace) + 1));
274 strcpy(ns_entry->namespace, namespace);
275 ns_entry->next = *list;
276 *list = ns_entry;
277 }
278}
279
280static bool module_imports_namespace(struct module *module,
281 const char *namespace)
282{
283 return contains_namespace(module->imported_namespaces, namespace);
284}
285
Mathias Krause7a3ee752014-08-27 20:28:53 +0930286static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700287 const char *str;
288 enum export export;
289} export_list[] = {
290 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200291 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700292 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200293 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700294 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
295 { .str = "(unknown)", .export = export_unknown },
296};
297
298
299static const char *export_str(enum export ex)
300{
301 return export_list[ex].str;
302}
303
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100304static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700305{
306 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100307
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200308 if (!s)
309 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700310 for (i = 0; export_list[i].export != export_unknown; i++) {
311 if (strcmp(export_list[i].str, s) == 0)
312 return export_list[i].export;
313 }
314 return export_unknown;
315}
316
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900317static void *sym_get_data_by_offset(const struct elf_info *info,
318 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900319{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800320 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900321
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900322 if (info->hdr->e_type != ET_REL)
323 offset -= sechdr->sh_addr;
324
325 return (void *)info->hdr + sechdr->sh_offset + offset;
326}
327
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900328static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
329{
330 return sym_get_data_by_offset(info, get_secindex(info, sym),
331 sym->st_value);
332}
333
Masahiro Yamada565587d2020-05-25 14:47:05 +0900334static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
335{
336 return sym_get_data_by_offset(info, info->secindex_strings,
337 sechdr->sh_name);
338}
339
340static const char *sec_name(const struct elf_info *info, int secindex)
341{
342 return sech_name(info, &info->sechdrs[secindex]);
343}
344
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200345#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
346
347static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
348{
349 const char *secname = sec_name(elf, sec);
350
351 if (strstarts(secname, "___ksymtab+"))
352 return export_plain;
353 else if (strstarts(secname, "___ksymtab_unused+"))
354 return export_unused;
355 else if (strstarts(secname, "___ksymtab_gpl+"))
356 return export_gpl;
357 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
358 return export_unused_gpl;
359 else if (strstarts(secname, "___ksymtab_gpl_future+"))
360 return export_gpl_future;
361 else
362 return export_unknown;
363}
364
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200365static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700366{
367 if (sec == elf->export_sec)
368 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200369 else if (sec == elf->export_unused_sec)
370 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700371 else if (sec == elf->export_gpl_sec)
372 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200373 else if (sec == elf->export_unused_gpl_sec)
374 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700375 else if (sec == elf->export_gpl_future_sec)
376 return export_gpl_future;
377 else
378 return export_unknown;
379}
380
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900381static const char *namespace_from_kstrtabns(const struct elf_info *info,
382 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100383{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900384 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100385 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100386}
387
Matthias Maennicha2b11182019-10-18 10:31:40 +0100388static void sym_update_namespace(const char *symname, const char *namespace)
389{
390 struct symbol *s = find_symbol(symname);
391
392 /*
393 * That symbol should have been created earlier and thus this is
394 * actually an assertion.
395 */
396 if (!s) {
397 merror("Could not update namespace(%s) for symbol %s\n",
398 namespace, symname);
399 return;
400 }
401
402 free(s->namespace);
403 s->namespace =
404 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
405}
406
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100407/**
408 * Add an exported symbol - it may have already been added without a
409 * CRC, in this case just update the CRC
410 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100411static struct symbol *sym_add_exported(const char *name, struct module *mod,
412 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct symbol *s = find_symbol(name);
415
416 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700417 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900418 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900419 s->module == mod) {
420 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
421 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900422 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900423 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900425
426 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700427 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100428 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Masahiro Yamada17436942019-11-15 02:42:24 +0900431static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100432{
433 struct symbol *s = find_symbol(name);
434
Masahiro Yamada17436942019-11-15 02:42:24 +0900435 /*
436 * Ignore stand-alone __crc_*, which might be auto-generated symbols
437 * such as __*_veneer in ARM ELF.
438 */
439 if (!s)
440 return;
441
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100442 s->crc = crc;
443 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Masahiro Yamada75893572020-06-01 14:57:21 +0900446static void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930449 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int fd;
451
452 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930453 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930455 if (fstat(fd, &st))
456 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 *size = st.st_size;
459 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930461failed:
462 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (map == MAP_FAILED)
464 return NULL;
465 return map;
466}
467
Masahiro Yamada75893572020-06-01 14:57:21 +0900468static void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 munmap(file, size);
471}
472
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100473static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100476 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 Elf_Shdr *sechdrs;
478 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200479 const char *secstrings;
480 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 hdr = grab_file(filename, &info->size);
483 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930484 if (ignore_missing_files) {
485 fprintf(stderr, "%s: %s (ignored)\n", filename,
486 strerror(errno));
487 return 0;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200490 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100493 if (info->size < sizeof(*hdr)) {
494 /* file too small, assume this is an empty .o file */
495 return 0;
496 }
497 /* Is this a valid ELF file? */
498 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
499 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
500 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
501 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
502 /* Not an ELF file - silently ignore it */
503 return 0;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200506 hdr->e_type = TO_NATIVE(hdr->e_type);
507 hdr->e_machine = TO_NATIVE(hdr->e_machine);
508 hdr->e_version = TO_NATIVE(hdr->e_version);
509 hdr->e_entry = TO_NATIVE(hdr->e_entry);
510 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
511 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
512 hdr->e_flags = TO_NATIVE(hdr->e_flags);
513 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
514 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
515 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
516 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
517 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
518 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 sechdrs = (void *)hdr + hdr->e_shoff;
520 info->sechdrs = sechdrs;
521
Petr Stetiara83710e2007-08-27 12:15:07 +0200522 /* Check if file offset is correct */
523 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100524 fatal("section header offset=%lu in file '%s' is bigger than "
525 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
526 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200527 return 0;
528 }
529
Anders Kaseorg68457562011-05-19 16:55:27 -0600530 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200531 /*
532 * There are more than 64k sections,
533 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200534 */
535 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
536 }
537 else {
538 info->num_sections = hdr->e_shnum;
539 }
540 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600541 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200542 }
543 else {
544 info->secindex_strings = hdr->e_shstrndx;
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200548 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200549 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
550 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
551 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
552 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
553 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
554 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
555 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
556 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
557 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
558 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200561 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
562 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700563 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900564 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Tejun Heo56fc82c2009-02-06 00:48:02 +0900566 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100567 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
568 "sizeof(*hrd)=%zu\n", filename,
569 (unsigned long)sechdrs[i].sh_offset,
570 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100571 return 0;
572 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700573 secname = secstrings + sechdrs[i].sh_name;
574 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900575 if (nobits)
576 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
578 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700579 } else if (strcmp(secname, "__ksymtab") == 0)
580 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200581 else if (strcmp(secname, "__ksymtab_unused") == 0)
582 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700583 else if (strcmp(secname, "__ksymtab_gpl") == 0)
584 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200585 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
586 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700587 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
588 info->export_gpl_future_sec = i;
589
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200590 if (sechdrs[i].sh_type == SHT_SYMTAB) {
591 unsigned int sh_link_idx;
592 symtab_idx = i;
593 info->symtab_start = (void *)hdr +
594 sechdrs[i].sh_offset;
595 info->symtab_stop = (void *)hdr +
596 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600597 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200598 info->strtab = (void *)hdr +
599 sechdrs[sh_link_idx].sh_offset;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200602 /* 32bit section no. table? ("more than 64k sections") */
603 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
604 symtab_shndx_idx = i;
605 info->symtab_shndx_start = (void *)hdr +
606 sechdrs[i].sh_offset;
607 info->symtab_shndx_stop = (void *)hdr +
608 sechdrs[i].sh_offset + sechdrs[i].sh_size;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100611 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100612 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Fix endianness in symbols */
615 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
616 sym->st_shndx = TO_NATIVE(sym->st_shndx);
617 sym->st_name = TO_NATIVE(sym->st_name);
618 sym->st_value = TO_NATIVE(sym->st_value);
619 sym->st_size = TO_NATIVE(sym->st_size);
620 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200621
622 if (symtab_shndx_idx != ~0U) {
623 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600624 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200625 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600626 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200627 symtab_idx);
628 /* Fix endianness */
629 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
630 p++)
631 *p = TO_NATIVE(*p);
632 }
633
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100634 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100637static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 release_file(info->hdr, info->size);
640}
641
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200642static int ignore_undef_symbol(struct elf_info *info, const char *symname)
643{
644 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900645 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200646 return 1;
647 /* ignore global offset table */
648 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
649 return 1;
650 if (info->hdr->e_machine == EM_PPC)
651 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900652 if (strstarts(symname, "_restgpr_") ||
653 strstarts(symname, "_savegpr_") ||
654 strstarts(symname, "_rest32gpr_") ||
655 strstarts(symname, "_save32gpr_") ||
656 strstarts(symname, "_restvr_") ||
657 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200658 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000659 if (info->hdr->e_machine == EM_PPC64)
660 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900661 if (strstarts(symname, "_restgpr0_") ||
662 strstarts(symname, "_savegpr0_") ||
663 strstarts(symname, "_restvr_") ||
664 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100665 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000666 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200667 /* Do not ignore this symbol */
668 return 0;
669}
670
Masahiro Yamada17436942019-11-15 02:42:24 +0900671static void handle_modversion(const struct module *mod,
672 const struct elf_info *info,
673 const Elf_Sym *sym, const char *symname)
674{
675 unsigned int crc;
676
677 if (sym->st_shndx == SHN_UNDEF) {
678 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900679 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900680 return;
681 }
682
683 if (sym->st_shndx == SHN_ABS) {
684 crc = sym->st_value;
685 } else {
686 unsigned int *crcp;
687
688 /* symbol points to the CRC in the ELF object */
689 crcp = sym_get_data(info, sym);
690 crc = TO_NATIVE(*crcp);
691 }
692 sym_set_crc(symname, crc);
693}
694
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900695static void handle_symbol(struct module *mod, struct elf_info *info,
696 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200698 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900699 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200700
Masahiro Yamada33795762020-06-01 14:57:24 +0900701 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200702 export = export_from_secname(info, get_secindex(info, sym));
703 else
704 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 switch (sym->st_shndx) {
707 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900708 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100709 /* Should warn here, but modpost runs before the linker */
710 } else
711 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 case SHN_UNDEF:
714 /* undefined symbol */
715 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
716 ELF_ST_BIND(sym->st_info) != STB_WEAK)
717 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200718 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (info->hdr->e_machine == EM_SPARC ||
721 info->hdr->e_machine == EM_SPARCV9) {
722 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700723 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100725 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700726 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100727 munged[0] = '_';
728 munged[1] = toupper(munged[1]);
729 symname = munged;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100732
Rusty Russellb92021b2013-03-15 15:04:17 +1030733 mod->unres = alloc_symbol(symname,
734 ELF_ST_BIND(sym->st_info) == STB_WEAK,
735 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 default:
738 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900739 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100740 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100741 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900743 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900745 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 mod->has_cleanup = 1;
747 break;
748 }
749}
750
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100751/**
752 * Parse tag=value strings from .modinfo section
753 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static char *next_string(char *string, unsigned long *secsize)
755{
756 /* Skip non-zero chars */
757 while (string[0]) {
758 string++;
759 if ((*secsize)-- <= 1)
760 return NULL;
761 }
762
763 /* Skip any zero padding. */
764 while (!string[0]) {
765 string++;
766 if ((*secsize)-- <= 1)
767 return NULL;
768 }
769 return string;
770}
771
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900772static char *get_next_modinfo(struct elf_info *info, const char *tag,
773 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 char *p;
776 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900777 char *modinfo = info->modinfo;
778 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900780 if (prev) {
781 size -= prev - modinfo;
782 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 for (p = modinfo; p; p = next_string(p, &size)) {
786 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
787 return p + taglen + 1;
788 }
789 return NULL;
790}
791
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900792static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200793
794{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900795 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200796}
797
Sam Ravnborg93684d32006-02-19 11:53:35 +0100798/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100799 * Test if string s ends in string sub
800 * return 0 if match
801 **/
802static int strrcmp(const char *s, const char *sub)
803{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100804 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100805
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100806 if (!s || !sub)
807 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100808
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100809 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100810 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100811
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 if ((slen == 0) || (sublen == 0))
813 return 1;
814
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100815 if (sublen > slen)
816 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100817
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100818 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100819}
820
Sam Ravnborgff13f922008-01-23 19:54:27 +0100821static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
822{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100823 if (sym)
824 return elf->strtab + sym->st_name;
825 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100826 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100827}
828
Sam Ravnborg10668222008-01-13 22:21:31 +0100829/* The pattern is an array of simple patterns.
830 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100831 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100832 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930833 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100834 */
Trevor Keith5c725132009-09-22 16:43:38 -0700835static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100836{
837 const char *p;
838 while (*pat) {
839 p = *pat++;
840 const char *endp = p + strlen(p) - 1;
841
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930842 /* "*foo*" */
843 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300844 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
845 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930846
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930847 free(bare);
848 if (here != NULL)
849 return 1;
850 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100851 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100853 if (strrcmp(sym, p + 1) == 0)
854 return 1;
855 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100856 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100857 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100858 if (strncmp(sym, p, strlen(p) - 1) == 0)
859 return 1;
860 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100861 /* no wildcards */
862 else {
863 if (strcmp(p, sym) == 0)
864 return 1;
865 }
866 }
867 /* no match */
868 return 0;
869}
870
Sam Ravnborg10668222008-01-13 22:21:31 +0100871/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930872static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200873{
874 ".comment*",
875 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930876 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800877 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000878 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200879 ".mdebug*", /* alpha, score, mips etc. */
880 ".pdr", /* alpha, score, mips etc. */
881 ".stab*",
882 ".note*",
883 ".got*",
884 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400885 ".xt.prop", /* xtensa */
886 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030887 ".arcextmap*", /* arc */
888 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030889 ".cmem*", /* EZchip */
890 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100891 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600892 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200893 NULL
894};
Sam Ravnborg10668222008-01-13 22:21:31 +0100895
Sam Ravnborge241a632008-01-28 20:13:13 +0100896/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400897 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100898 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400899 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100900 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400901static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900902 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100903{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400904 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100905
Anders Kaseorgb614a692009-04-23 16:49:33 -0400906 if (sechdr->sh_type == SHT_PROGBITS &&
907 !(sechdr->sh_flags & SHF_ALLOC) &&
908 !match(sec, section_white_list)) {
909 warn("%s (%s): unexpected non-allocatable section.\n"
910 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
911 "Note that for example <linux/init.h> contains\n"
912 "section definitions for use in .S files.\n\n",
913 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100914 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100915}
916
917
918
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100919#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930920 ".init.setup", ".init.rodata", ".meminit.rodata", \
921 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100922#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930923 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100924
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100925#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930926 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100927#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930928 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100929
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200930#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930931 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
932 ".pci_fixup_enable", ".pci_fixup_resume", \
933 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200934
Paul Gortmakere24f6622013-06-19 19:30:48 -0400935#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
936#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100937
938#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
939#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100940
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930942#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700943 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930944#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400945 ".fixup", ".entry.text", ".exception.text", ".text.*", \
946 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100947
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000948#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000949#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100950
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000951#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000952#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100953
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930954#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
955 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
956
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100957/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930958static const char *const init_data_sections[] =
959 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100960
961/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930962static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100963
964/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930965static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100966 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100967
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930968/* all text sections */
969static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
970
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100971/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930972static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100974
975/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100976#define DEFAULT_SYMBOL_WHITE_LIST \
977 "*driver", \
978 "*_template", /* scsi uses *_template a lot */ \
979 "*_timer", /* arm uses ops structures named _timer a lot */ \
980 "*_sht", /* scsi also used *_sht to some extent */ \
981 "*_ops", \
982 "*_probe", \
983 "*_probe_one", \
984 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100985
Mathias Krause7a3ee752014-08-27 20:28:53 +0930986static const char *const head_sections[] = { ".head.text*", NULL };
987static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100988 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930989static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100991enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100992 TEXT_TO_ANY_INIT,
993 DATA_TO_ANY_INIT,
994 TEXT_TO_ANY_EXIT,
995 DATA_TO_ANY_EXIT,
996 XXXINIT_TO_SOME_INIT,
997 XXXEXIT_TO_SOME_EXIT,
998 ANY_INIT_TO_ANY_EXIT,
999 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001000 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301001 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001002};
1003
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301004/**
1005 * Describe how to match sections on different criterias:
1006 *
1007 * @fromsec: Array of sections to be matched.
1008 *
1009 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1010 * this array is forbidden (black-list). Can be empty.
1011 *
1012 * @good_tosec: Relocations applied to a section in @fromsec must be
1013 * targetting sections in this array (white-list). Can be empty.
1014 *
1015 * @mismatch: Type of mismatch.
1016 *
1017 * @symbol_white_list: Do not match a relocation to a symbol in this list
1018 * even if it is targetting a section in @bad_to_sec.
1019 *
1020 * @handler: Specific handler to call when a match is found. If NULL,
1021 * default_mismatch_handler() will be called.
1022 *
1023 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001024struct sectioncheck {
1025 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301026 const char *bad_tosec[20];
1027 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001028 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001029 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301030 void (*handler)(const char *modname, struct elf_info *elf,
1031 const struct sectioncheck* const mismatch,
1032 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1033
Sam Ravnborg10668222008-01-13 22:21:31 +01001034};
1035
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301036static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1037 const struct sectioncheck* const mismatch,
1038 Elf_Rela *r, Elf_Sym *sym,
1039 const char *fromsec);
1040
Mathias Krause7a3ee752014-08-27 20:28:53 +09301041static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001042/* Do not reference init/exit code/data from
1043 * normal code and data
1044 */
1045{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001046 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301047 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001048 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001049 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001050},
1051{
1052 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301053 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001054 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001055 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001056},
1057{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001058 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301059 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001060 .mismatch = DATA_TO_ANY_INIT,
1061 .symbol_white_list = {
1062 "*_template", "*_timer", "*_sht", "*_ops",
1063 "*_probe", "*_probe_one", "*_console", NULL
1064 },
1065},
1066{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001067 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301068 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001069 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001070 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001071},
1072{
1073 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301074 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001075 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001076 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001077},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001078/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001079{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001080 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301081 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001082 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001083 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001084},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001085/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001086{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001087 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301088 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001089 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001090 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001091},
1092/* Do not use exit code/data from init code */
1093{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001094 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301095 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001096 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001097 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001098},
1099/* Do not use init code/data from exit code */
1100{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001101 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301102 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001103 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001104 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001105},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001106{
1107 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001109 .mismatch = ANY_INIT_TO_ANY_EXIT,
1110 .symbol_white_list = { NULL },
1111},
Sam Ravnborg10668222008-01-13 22:21:31 +01001112/* Do not export init/exit functions or data */
1113{
1114 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301115 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001116 .mismatch = EXPORT_TO_INIT_EXIT,
1117 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301118},
1119{
1120 .fromsec = { "__ex_table", NULL },
1121 /* If you're adding any new black-listed sections in here, consider
1122 * adding a special 'printer' for them in scripts/check_extable.
1123 */
1124 .bad_tosec = { ".altinstr_replacement", NULL },
1125 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1126 .mismatch = EXTABLE_TO_NON_TEXT,
1127 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001128}
1129};
1130
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001131static const struct sectioncheck *section_mismatch(
1132 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001133{
1134 int i;
1135 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1136 const struct sectioncheck *check = &sectioncheck[0];
1137
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301138 /*
1139 * The target section could be the SHT_NUL section when we're
1140 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001141 * doesn't make much sense and causes build failures on parisc
1142 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301143 */
1144 if (*tosec == '\0')
1145 return NULL;
1146
Sam Ravnborg10668222008-01-13 22:21:31 +01001147 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301148 if (match(fromsec, check->fromsec)) {
1149 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1150 return check;
1151 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1152 return check;
1153 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001154 check++;
1155 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001156 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001157}
1158
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001159/**
1160 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001161 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001162 * Pattern 1:
1163 * If a module parameter is declared __initdata and permissions=0
1164 * then this is legal despite the warning generated.
1165 * We cannot see value of permissions here, so just ignore
1166 * this pattern.
1167 * The pattern is identified by:
1168 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001169 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001170 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001171 *
Rusty Russell6a841522010-08-11 23:04:16 -06001172 * Pattern 1a:
1173 * module_param_call() ops can refer to __init set function if permissions=0
1174 * The pattern is identified by:
1175 * tosec = .init.text
1176 * fromsec = .data*
1177 * atsym = __param_ops_*
1178 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001179 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001180 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001181 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001182 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001183 * tosec = init or exit section
1184 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001185 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1186 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001187 *
1188 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001189 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001190 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001191 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001192 * Some symbols belong to init section but still it is ok to reference
1193 * these from non-init sections as these symbols don't have any memory
1194 * allocated for them and symbol address and value are same. So even
1195 * if init section is freed, its ok to reference those symbols.
1196 * For ex. symbols marking the init section boundaries.
1197 * This pattern is identified by
1198 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001199 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301200 * Pattern 5:
1201 * GCC may optimize static inlines when fed constant arg(s) resulting
1202 * in functions like cpumask_empty() -- generating an associated symbol
1203 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1204 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1205 * meaningless section warning. May need to add isra symbols too...
1206 * This pattern is identified by
1207 * tosec = init section
1208 * fromsec = text section
1209 * refsymname = *.constprop.*
1210 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001211 * Pattern 6:
1212 * Hide section mismatch warnings for ELF local symbols. The goal
1213 * is to eliminate false positive modpost warnings caused by
1214 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1215 * Autogenerated symbol names bypass modpost's "Pattern 2"
1216 * whitelisting, which relies on pattern-matching against symbol
1217 * names to work. (One situation where gcc can autogenerate ELF
1218 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001219 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001220static int secref_whitelist(const struct sectioncheck *mismatch,
1221 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001222 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001223{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001224 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001225 if (match(tosec, init_data_sections) &&
1226 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001227 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001228 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001229
Rusty Russell6a841522010-08-11 23:04:16 -06001230 /* Check for pattern 1a */
1231 if (strcmp(tosec, ".init.text") == 0 &&
1232 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001233 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001234 return 0;
1235
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001236 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001237 if (match(tosec, init_exit_sections) &&
1238 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001239 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001240 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001241
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001242 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001243 if (match(fromsec, head_sections) &&
1244 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001245 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001246
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001247 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001248 if (match(tosym, linker_symbols))
1249 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001250
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301251 /* Check for pattern 5 */
1252 if (match(fromsec, text_sections) &&
1253 match(tosec, init_sections) &&
1254 match(fromsym, optim_symbols))
1255 return 0;
1256
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001257 /* Check for pattern 6 */
1258 if (strstarts(fromsym, ".L"))
1259 return 0;
1260
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001262}
1263
Sami Tolvanen5818c682018-10-23 15:15:35 -07001264static inline int is_arm_mapping_symbol(const char *str)
1265{
1266 return str[0] == '$' && strchr("axtd", str[1])
1267 && (str[2] == '\0' || str[2] == '.');
1268}
1269
1270/*
1271 * If there's no name there, ignore it; likewise, ignore it if it's
1272 * one of the magic symbols emitted used by current ARM tools.
1273 *
1274 * Otherwise if find_symbols_between() returns those symbols, they'll
1275 * fail the whitelist tests and cause lots of false alarms ... fixable
1276 * only by merging __exit and __init sections into __text, bloating
1277 * the kernel (which is especially evil on embedded platforms).
1278 */
1279static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1280{
1281 const char *name = elf->strtab + sym->st_name;
1282
1283 if (!name || !strlen(name))
1284 return 0;
1285 return !is_arm_mapping_symbol(name);
1286}
1287
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001288/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001289 * Find symbol based on relocation record info.
1290 * In some cases the symbol supplied is a valid symbol so
1291 * return refsym. If st_name != 0 we assume this is a valid symbol.
1292 * In other cases the symbol needs to be looked up in the symbol table
1293 * based on section and address.
1294 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001295static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001296 Elf_Sym *relsym)
1297{
1298 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001299 Elf_Sym *near = NULL;
1300 Elf64_Sword distance = 20;
1301 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001302 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001303
1304 if (relsym->st_name != 0)
1305 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001306
1307 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001309 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001310 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001311 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1312 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001313 if (!is_valid_name(elf, sym))
1314 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001315 if (sym->st_value == addr)
1316 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001317 /* Find a symbol nearby - addr are maybe negative */
1318 d = sym->st_value - addr;
1319 if (d < 0)
1320 d = addr - sym->st_value;
1321 if (d < distance) {
1322 distance = d;
1323 near = sym;
1324 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001325 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001326 /* We need a close match */
1327 if (distance < 20)
1328 return near;
1329 else
1330 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331}
1332
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001333/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001334 * Find symbols before or equal addr and after addr - in the section sec.
1335 * If we find two symbols with equal offset prefer one with a valid name.
1336 * The ELF format may have a better way to detect what type of symbol
1337 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001338 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001339static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1340 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001341{
1342 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001343 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001344 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001345
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1347 const char *symsec;
1348
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001349 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001350 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001351 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001352 if (strcmp(symsec, sec) != 0)
1353 continue;
David Brownellda68d612007-02-20 13:58:16 -08001354 if (!is_valid_name(elf, sym))
1355 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001357 if ((addr - sym->st_value) < distance) {
1358 distance = addr - sym->st_value;
1359 near = sym;
1360 } else if ((addr - sym->st_value) == distance) {
1361 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001362 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001363 }
1364 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001365 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366}
1367
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001368/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001369 * Convert a section name to the function/data attribute
1370 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001371 * .memexitconst => __memconst
1372 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001373 *
1374 * The memory of returned value has been allocated on a heap. The user of this
1375 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001376*/
1377static char *sec2annotation(const char *s)
1378{
1379 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001380 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001381 char *r = p;
1382
1383 *p++ = '_';
1384 *p++ = '_';
1385 if (*s == '.')
1386 s++;
1387 while (*s && *s != '.')
1388 *p++ = *s++;
1389 *p = '\0';
1390 if (*s == '.')
1391 s++;
1392 if (strstr(s, "rodata") != NULL)
1393 strcat(p, "const ");
1394 else if (strstr(s, "data") != NULL)
1395 strcat(p, "data ");
1396 else
1397 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001398 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001399 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001400 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001401 }
1402}
1403
1404static int is_function(Elf_Sym *sym)
1405{
1406 if (sym)
1407 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1408 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001409 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001410}
1411
Randy Dunlap00759c02011-03-15 14:13:47 -07001412static void print_section_list(const char * const list[20])
1413{
1414 const char *const *s = list;
1415
1416 while (*s) {
1417 fprintf(stderr, "%s", *s);
1418 s++;
1419 if (*s)
1420 fprintf(stderr, ", ");
1421 }
1422 fprintf(stderr, "\n");
1423}
1424
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301425static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1426{
1427 switch (is_func) {
1428 case 0: *name = "variable"; *name_p = ""; break;
1429 case 1: *name = "function"; *name_p = "()"; break;
1430 default: *name = "(unknown reference)"; *name_p = ""; break;
1431 }
1432}
1433
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001434/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001435 * Print a warning about a section mismatch.
1436 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001437 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001438 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001439static void report_sec_mismatch(const char *modname,
1440 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001441 const char *fromsec,
1442 unsigned long long fromaddr,
1443 const char *fromsym,
1444 int from_is_func,
1445 const char *tosec, const char *tosym,
1446 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001447{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001448 const char *from, *from_p;
1449 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001450 char *prl_from;
1451 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001452
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001453 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001454
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301455 get_pretty_name(from_is_func, &from, &from_p);
1456 get_pretty_name(to_is_func, &to, &to_p);
1457
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001458 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1459 "to the %s %s:%s%s\n",
1460 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1461 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001462
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001463 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001464 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001465 prl_from = sec2annotation(fromsec);
1466 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001468 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001469 "the %s %s%s%s.\n"
1470 "This is often because %s lacks a %s\n"
1471 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001472 prl_from, fromsym,
1473 to, prl_to, tosym, to_p,
1474 fromsym, prl_to, tosym);
1475 free(prl_from);
1476 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001477 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001478 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001479 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001480 fprintf(stderr,
1481 "The variable %s references\n"
1482 "the %s %s%s%s\n"
1483 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001484 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001486 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001487 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001488 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001490 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001491 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 fprintf(stderr,
1494 "The function %s() references a %s in an exit section.\n"
1495 "Often the %s %s%s has valid usage outside the exit section\n"
1496 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001497 fromsym, to, to, tosym, to_p, prl_to, tosym);
1498 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001499 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001500 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001502 fprintf(stderr,
1503 "The variable %s references\n"
1504 "the %s %s%s%s\n"
1505 "If the reference is valid then annotate the\n"
1506 "variable with __exit* (see linux/init.h) or "
1507 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001509 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001510 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001511 break;
1512 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001513 case XXXINIT_TO_SOME_INIT:
1514 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001515 prl_from = sec2annotation(fromsec);
1516 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 fprintf(stderr,
1518 "The %s %s%s%s references\n"
1519 "a %s %s%s%s.\n"
1520 "If %s is only used by %s then\n"
1521 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 from, prl_from, fromsym, from_p,
1523 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001524 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001525 free(prl_from);
1526 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001527 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001528 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 prl_from = sec2annotation(fromsec);
1530 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 fprintf(stderr,
1532 "The %s %s%s%s references\n"
1533 "a %s %s%s%s.\n"
1534 "This is often seen when error handling "
1535 "in the init function\n"
1536 "uses functionality in the exit path.\n"
1537 "The fix is often to remove the %sannotation of\n"
1538 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001539 from, prl_from, fromsym, from_p,
1540 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001541 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 free(prl_from);
1543 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001544 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001545 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 prl_from = sec2annotation(fromsec);
1547 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 fprintf(stderr,
1549 "The %s %s%s%s references\n"
1550 "a %s %s%s%s.\n"
1551 "This is often seen when error handling "
1552 "in the exit function\n"
1553 "uses functionality in the init path.\n"
1554 "The fix is often to remove the %sannotation of\n"
1555 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001556 from, prl_from, fromsym, from_p,
1557 to, prl_to, tosym, to_p,
1558 prl_to, tosym, to_p);
1559 free(prl_from);
1560 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001561 break;
1562 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001563 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 fprintf(stderr,
1565 "The symbol %s is exported and annotated %s\n"
1566 "Fix this by removing the %sannotation of %s "
1567 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001568 tosym, prl_to, prl_to, tosym);
1569 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001570 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301571 case EXTABLE_TO_NON_TEXT:
1572 fatal("There's a special handler for this mismatch type, "
1573 "we should never get here.");
1574 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001575 }
1576 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001577}
1578
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301579static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1580 const struct sectioncheck* const mismatch,
1581 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1582{
1583 const char *tosec;
1584 Elf_Sym *to;
1585 Elf_Sym *from;
1586 const char *tosym;
1587 const char *fromsym;
1588
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301589 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1590 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301591
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001592 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301593 return;
1594
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301595 tosec = sec_name(elf, get_secindex(elf, sym));
1596 to = find_elf_symbol(elf, r->r_addend, sym);
1597 tosym = sym_name(elf, to);
1598
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301599 /* check whitelist - we may ignore it */
1600 if (secref_whitelist(mismatch,
1601 fromsec, fromsym, tosec, tosym)) {
1602 report_sec_mismatch(modname, mismatch,
1603 fromsec, r->r_offset, fromsym,
1604 is_function(from), tosec, tosym,
1605 is_function(to));
1606 }
1607}
1608
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301609static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1610{
1611 if (section_index > elf->num_sections)
1612 fatal("section_index is outside elf->num_sections!\n");
1613
1614 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1615}
1616
1617/*
1618 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1619 * to know the sizeof(struct exception_table_entry) for the target architecture.
1620 */
1621static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301622static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301623{
1624 /*
1625 * If we're currently checking the second relocation within __ex_table,
1626 * that relocation offset tells us the offsetof(struct
1627 * exception_table_entry, fixup) which is equal to sizeof(struct
1628 * exception_table_entry) divided by two. We use that to our advantage
1629 * since there's no portable way to get that size as every architecture
1630 * seems to go with different sized types. Not pretty but better than
1631 * hard-coding the size for every architecture..
1632 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301633 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301634 extable_entry_size = r->r_offset * 2;
1635}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301636
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301637static inline bool is_extable_fault_address(Elf_Rela *r)
1638{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301639 /*
1640 * extable_entry_size is only discovered after we've handled the
1641 * _second_ relocation in __ex_table, so only abort when we're not
1642 * handling the first reloc and extable_entry_size is zero.
1643 */
1644 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301645 fatal("extable_entry size hasn't been discovered!\n");
1646
1647 return ((r->r_offset == 0) ||
1648 (r->r_offset % extable_entry_size == 0));
1649}
1650
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301651#define is_second_extable_reloc(Start, Cur, Sec) \
1652 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1653
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301654static void report_extable_warnings(const char* modname, struct elf_info* elf,
1655 const struct sectioncheck* const mismatch,
1656 Elf_Rela* r, Elf_Sym* sym,
1657 const char* fromsec, const char* tosec)
1658{
1659 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1660 const char* fromsym_name = sym_name(elf, fromsym);
1661 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1662 const char* tosym_name = sym_name(elf, tosym);
1663 const char* from_pretty_name;
1664 const char* from_pretty_name_p;
1665 const char* to_pretty_name;
1666 const char* to_pretty_name_p;
1667
1668 get_pretty_name(is_function(fromsym),
1669 &from_pretty_name, &from_pretty_name_p);
1670 get_pretty_name(is_function(tosym),
1671 &to_pretty_name, &to_pretty_name_p);
1672
1673 warn("%s(%s+0x%lx): Section mismatch in reference"
1674 " from the %s %s%s to the %s %s:%s%s\n",
1675 modname, fromsec, (long)r->r_offset, from_pretty_name,
1676 fromsym_name, from_pretty_name_p,
1677 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1678
1679 if (!match(tosec, mismatch->bad_tosec) &&
1680 is_executable_section(elf, get_secindex(elf, sym)))
1681 fprintf(stderr,
1682 "The relocation at %s+0x%lx references\n"
1683 "section \"%s\" which is not in the list of\n"
1684 "authorized sections. If you're adding a new section\n"
1685 "and/or if this reference is valid, add \"%s\" to the\n"
1686 "list of authorized sections to jump to on fault.\n"
1687 "This can be achieved by adding \"%s\" to \n"
1688 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1689 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1690}
1691
1692static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1693 const struct sectioncheck* const mismatch,
1694 Elf_Rela* r, Elf_Sym* sym,
1695 const char *fromsec)
1696{
1697 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1698
1699 sec_mismatch_count++;
1700
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001701 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301702
1703 if (match(tosec, mismatch->bad_tosec))
1704 fatal("The relocation at %s+0x%lx references\n"
1705 "section \"%s\" which is black-listed.\n"
1706 "Something is seriously wrong and should be fixed.\n"
1707 "You might get more information about where this is\n"
1708 "coming from by using scripts/check_extable.sh %s\n",
1709 fromsec, (long)r->r_offset, tosec, modname);
1710 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1711 if (is_extable_fault_address(r))
1712 fatal("The relocation at %s+0x%lx references\n"
1713 "section \"%s\" which is not executable, IOW\n"
1714 "it is not possible for the kernel to fault\n"
1715 "at that address. Something is seriously wrong\n"
1716 "and should be fixed.\n",
1717 fromsec, (long)r->r_offset, tosec);
1718 else
1719 fatal("The relocation at %s+0x%lx references\n"
1720 "section \"%s\" which is not executable, IOW\n"
1721 "the kernel will fault if it ever tries to\n"
1722 "jump to it. Something is seriously wrong\n"
1723 "and should be fixed.\n",
1724 fromsec, (long)r->r_offset, tosec);
1725 }
1726}
1727
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001728static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001729 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001730{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001731 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301732 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001733
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001734 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301735 if (mismatch->handler)
1736 mismatch->handler(modname, elf, mismatch,
1737 r, sym, fromsec);
1738 else
1739 default_mismatch_handler(modname, elf, mismatch,
1740 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001741 }
1742}
1743
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001744static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001745 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001746{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001747 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001748}
1749
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001750static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001751{
1752 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001753 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001754
1755 switch (r_typ) {
1756 case R_386_32:
1757 r->r_addend = TO_NATIVE(*location);
1758 break;
1759 case R_386_PC32:
1760 r->r_addend = TO_NATIVE(*location) + 4;
1761 /* For CONFIG_RELOCATABLE=y */
1762 if (elf->hdr->e_type == ET_EXEC)
1763 r->r_addend += r->r_offset;
1764 break;
1765 }
1766 return 0;
1767}
1768
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001769#ifndef R_ARM_CALL
1770#define R_ARM_CALL 28
1771#endif
1772#ifndef R_ARM_JUMP24
1773#define R_ARM_JUMP24 29
1774#endif
1775
David A. Longc9698e52014-02-14 22:41:18 +01001776#ifndef R_ARM_THM_CALL
1777#define R_ARM_THM_CALL 10
1778#endif
1779#ifndef R_ARM_THM_JUMP24
1780#define R_ARM_THM_JUMP24 30
1781#endif
1782#ifndef R_ARM_THM_JUMP19
1783#define R_ARM_THM_JUMP19 51
1784#endif
1785
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001786static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001787{
1788 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1789
1790 switch (r_typ) {
1791 case R_ARM_ABS32:
1792 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001793 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001794 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001795 break;
1796 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001797 case R_ARM_CALL:
1798 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001799 case R_ARM_THM_CALL:
1800 case R_ARM_THM_JUMP24:
1801 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001802 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001803 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001804 sechdr->sh_offset +
1805 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001806 break;
1807 default:
1808 return 1;
1809 }
1810 return 0;
1811}
1812
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001813static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001814{
1815 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001816 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001817 unsigned int inst;
1818
1819 if (r_typ == R_MIPS_HI16)
1820 return 1; /* skip this */
1821 inst = TO_NATIVE(*location);
1822 switch (r_typ) {
1823 case R_MIPS_LO16:
1824 r->r_addend = inst & 0xffff;
1825 break;
1826 case R_MIPS_26:
1827 r->r_addend = (inst & 0x03ffffff) << 2;
1828 break;
1829 case R_MIPS_32:
1830 r->r_addend = inst;
1831 break;
1832 }
1833 return 0;
1834}
1835
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001836static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001837 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001838{
1839 Elf_Sym *sym;
1840 Elf_Rela *rela;
1841 Elf_Rela r;
1842 unsigned int r_sym;
1843 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844
Sam Ravnborgff13f922008-01-23 19:54:27 +01001845 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001846 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1847
Sam Ravnborgff13f922008-01-23 19:54:27 +01001848 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001849 fromsec += strlen(".rela");
1850 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001851 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001853
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854 for (rela = start; rela < stop; rela++) {
1855 r.r_offset = TO_NATIVE(rela->r_offset);
1856#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001857 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 unsigned int r_typ;
1859 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1860 r_sym = TO_NATIVE(r_sym);
1861 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1862 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1863 } else {
1864 r.r_info = TO_NATIVE(rela->r_info);
1865 r_sym = ELF_R_SYM(r.r_info);
1866 }
1867#else
1868 r.r_info = TO_NATIVE(rela->r_info);
1869 r_sym = ELF_R_SYM(r.r_info);
1870#endif
1871 r.r_addend = TO_NATIVE(rela->r_addend);
1872 sym = elf->symtab_start + r_sym;
1873 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001874 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001875 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301876 if (is_second_extable_reloc(start, rela, fromsec))
1877 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001878 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001879 }
1880}
1881
1882static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001883 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001884{
1885 Elf_Sym *sym;
1886 Elf_Rel *rel;
1887 Elf_Rela r;
1888 unsigned int r_sym;
1889 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001890
Sam Ravnborgff13f922008-01-23 19:54:27 +01001891 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001892 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1893
Sam Ravnborgff13f922008-01-23 19:54:27 +01001894 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 fromsec += strlen(".rel");
1896 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001897 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898 return;
1899
1900 for (rel = start; rel < stop; rel++) {
1901 r.r_offset = TO_NATIVE(rel->r_offset);
1902#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001903 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 unsigned int r_typ;
1905 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1906 r_sym = TO_NATIVE(r_sym);
1907 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1908 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1909 } else {
1910 r.r_info = TO_NATIVE(rel->r_info);
1911 r_sym = ELF_R_SYM(r.r_info);
1912 }
1913#else
1914 r.r_info = TO_NATIVE(rel->r_info);
1915 r_sym = ELF_R_SYM(r.r_info);
1916#endif
1917 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001918 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001919 case EM_386:
1920 if (addend_386_rel(elf, sechdr, &r))
1921 continue;
1922 break;
1923 case EM_ARM:
1924 if (addend_arm_rel(elf, sechdr, &r))
1925 continue;
1926 break;
1927 case EM_MIPS:
1928 if (addend_mips_rel(elf, sechdr, &r))
1929 continue;
1930 break;
1931 }
1932 sym = elf->symtab_start + r_sym;
1933 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001934 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001935 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301936 if (is_second_extable_reloc(start, rel, fromsec))
1937 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001938 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001939 }
1940}
1941
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001942/**
1943 * A module includes a number of sections that are discarded
1944 * either when loaded or when used as built-in.
1945 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001946 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001947 * Likewise for modules used built-in the sections marked __exit
1948 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001949 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001950 * The check_sec_ref() function traverses all relocation records
1951 * to find all references to a section that reference a section that will
1952 * be discarded and warns about it.
1953 **/
1954static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001955 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001956{
1957 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001958 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001959
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001960 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001961 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001962 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001964 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001965 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001966 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001967 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968 }
1969}
1970
Andi Kleen7d02b492014-02-08 09:01:12 +01001971static char *remove_dot(char *s)
1972{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301973 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001974
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301975 if (n && s[n]) {
1976 size_t m = strspn(s + n + 1, "0123456789");
1977 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001978 s[n] = 0;
1979 }
1980 return s;
1981}
1982
Masahiro Yamada8b185742018-05-09 18:50:40 +09001983static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 const char *symname;
1986 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001987 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001988 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct module *mod;
1990 struct elf_info info = { };
1991 Elf_Sym *sym;
1992
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001993 if (!parse_elf(&info, modname))
1994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001996 {
1997 char *tmp;
1998
1999 /* strip trailing .o */
2000 tmp = NOFAIL(strdup(modname));
2001 tmp[strlen(tmp) - 2] = '\0';
2002 mod = new_module(tmp);
2003 free(tmp);
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002006 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002007 license = get_modinfo(&info, "license");
2008 if (!license)
2009 warn("missing MODULE_LICENSE() in %s\n", modname);
2010 while (license) {
2011 if (license_is_gpl_compatible(license))
2012 mod->gpl_compatible = 1;
2013 else {
2014 mod->gpl_compatible = 0;
2015 break;
2016 }
2017 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002018 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002019
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002020 namespace = get_modinfo(&info, "import_ns");
2021 while (namespace) {
2022 add_namespace(&mod->imported_namespaces, namespace);
2023 namespace = get_next_modinfo(&info, "import_ns",
2024 namespace);
2025 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002029 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002031 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 handle_moddevtable(mod, &info, sym, symname);
2033 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002034
Matthias Maennich69923202019-10-18 10:31:42 +01002035 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2036 symname = remove_dot(info.strtab + sym->st_name);
2037
Masahiro Yamada17436942019-11-15 02:42:24 +09002038 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002039 if (strstarts(symname, "__kstrtabns_"))
2040 sym_update_namespace(symname + strlen("__kstrtabns_"),
2041 namespace_from_kstrtabns(&info,
2042 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002043
2044 if (strstarts(symname, "__crc_"))
2045 handle_modversion(mod, &info, sym,
2046 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002047 }
2048
Denis Efremov15bfc232019-08-01 09:06:57 +03002049 // check for static EXPORT_SYMBOL_* functions && global vars
2050 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2051 unsigned char bind = ELF_ST_BIND(sym->st_info);
2052
2053 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2054 struct symbol *s =
2055 find_symbol(remove_dot(info.strtab +
2056 sym->st_name));
2057
2058 if (s)
2059 s->is_static = 0;
2060 }
2061 }
2062
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002063 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002065 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002066 version = get_modinfo(&info, "version");
2067 if (version || all_versions)
2068 get_src_version(modname, mod->srcversion,
2069 sizeof(mod->srcversion) - 1);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 parse_elf_finish(&info);
2073
Rusty Russell8c8ef422009-03-31 13:05:34 -06002074 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * never passed as an argument to an exported function, so
2076 * the automatic versioning doesn't pick it up, but it's really
2077 * important anyhow */
2078 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002079 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Rusty Russell712f9b42013-04-04 17:37:38 +10302082static void read_symbols_from_files(const char *filename)
2083{
2084 FILE *in = stdin;
2085 char fname[PATH_MAX];
2086
2087 if (strcmp(filename, "-") != 0) {
2088 in = fopen(filename, "r");
2089 if (!in)
2090 fatal("Can't open filenames file %s: %m", filename);
2091 }
2092
2093 while (fgets(fname, PATH_MAX, in) != NULL) {
2094 if (strends(fname, "\n"))
2095 fname[strlen(fname)-1] = '\0';
2096 read_symbols(fname);
2097 }
2098
2099 if (in != stdin)
2100 fclose(in);
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103#define SZ 500
2104
2105/* We first write the generated file into memory using the
2106 * following helper, then compare to the file on disk and
2107 * only update the later if anything changed */
2108
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002109void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2110 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 char tmp[SZ];
2113 int len;
2114 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 va_start(ap, fmt);
2117 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002118 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 va_end(ap);
2120}
2121
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002122void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002125 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002126 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128 strncpy(buf->p + buf->pos, s, len);
2129 buf->pos += len;
2130}
2131
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002132static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2133{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002134 switch (exp) {
2135 case export_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002136 fatal("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2137 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002138 break;
2139 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002140 fatal("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n",
2141 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002142 break;
2143 case export_gpl_future:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002144 warn("GPL-incompatible module %s.ko uses future GPL-only symbol '%s'\n",
2145 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002146 break;
2147 case export_plain:
2148 case export_unused:
2149 case export_unknown:
2150 /* ignore */
2151 break;
2152 }
2153}
2154
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002155static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002156{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002157 switch (exp) {
2158 case export_unused:
2159 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002160 warn("module %s.ko uses symbol '%s' marked UNUSED\n",
2161 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002162 break;
2163 default:
2164 /* ignore */
2165 break;
2166 }
2167}
2168
Masahiro Yamada3b415282018-11-23 16:57:23 +09002169static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002170{
2171 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002172 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002173
2174 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002175 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002176 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002177 if (!exp || exp->module == mod) {
2178 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002179 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2180 "\"%s\" [%s.ko] undefined!\n",
2181 s->name, mod->name);
2182 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002183 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002184 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002185 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002186 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002187 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002188 if (basename)
2189 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002190 else
2191 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002192
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002193 if (exp->namespace &&
2194 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002195 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2196 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2197 basename, exp->name, exp->namespace);
2198 if (!allow_missing_ns_imports)
2199 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002200 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002201 }
2202
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002203 if (!mod->gpl_compatible)
2204 check_for_gpl_usage(exp->export, basename, exp->name);
2205 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002206 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002207
2208 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002209}
2210
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002211static int check_modname_len(struct module *mod)
2212{
2213 const char *mod_name;
2214
2215 mod_name = strrchr(mod->name, '/');
2216 if (mod_name == NULL)
2217 mod_name = mod->name;
2218 else
2219 mod_name++;
2220 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2221 merror("module name is too long [%s.ko]\n", mod->name);
2222 return 1;
2223 }
2224
2225 return 0;
2226}
2227
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002228/**
2229 * Header for the generated file
2230 **/
2231static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002234 /*
2235 * Include build-salt.h after module.h in order to
2236 * inherit the definitions.
2237 */
2238 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 buf_printf(b, "#include <linux/vermagic.h>\n");
2240 buf_printf(b, "#include <linux/compiler.h>\n");
2241 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002242 buf_printf(b, "BUILD_SALT;\n");
2243 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002245 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302247 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002248 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002249 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002251 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (mod->has_cleanup)
2253 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002254 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002256 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 buf_printf(b, "};\n");
2258}
2259
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002260static void add_intree_flag(struct buffer *b, int is_intree)
2261{
2262 if (is_intree)
2263 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2264}
2265
Andi Kleencaf75012018-01-25 15:50:28 -08002266/* Cannot check for assembler */
2267static void add_retpoline(struct buffer *b)
2268{
WANG Chaoe4f35892018-12-11 00:37:25 +08002269 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002270 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2271 buf_printf(b, "#endif\n");
2272}
2273
Trevor Keith5c725132009-09-22 16:43:38 -07002274static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002275{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002276 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002277 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2278}
2279
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002280/**
2281 * Record CRCs for unresolved symbols
2282 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002283static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002286 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 for (s = mod->unres; s; s = s->next) {
2289 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002290 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 s->module = exp->module;
2293 s->crc_valid = exp->crc_valid;
2294 s->crc = exp->crc;
2295 }
2296
2297 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002298 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 buf_printf(b, "\n");
2301 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002302 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002305 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002308 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 s->name, mod->name);
2310 continue;
2311 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302312 if (strlen(s->name) >= MODULE_NAME_LEN) {
2313 merror("too long symbol \"%s\" [%s.ko]\n",
2314 s->name, mod->name);
2315 err = 1;
2316 break;
2317 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002318 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302319 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321
2322 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002323
2324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002327static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int first = 1;
2331
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002332 /* Clear ->seen flag of modules that own symbols needed by this. */
2333 for (s = mod->unres; s; s = s->next)
2334 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002335 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002338 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002340 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (!s->module)
2342 continue;
2343
2344 if (s->module->seen)
2345 continue;
2346
2347 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002348 p = strrchr(s->module->name, '/');
2349 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002350 p++;
2351 else
2352 p = s->module->name;
2353 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 first = 0;
2355 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002356 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002359static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 if (mod->srcversion[0]) {
2362 buf_printf(b, "\n");
2363 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2364 mod->srcversion);
2365 }
2366}
2367
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002368static void write_buf(struct buffer *b, const char *fname)
2369{
2370 FILE *file;
2371
2372 file = fopen(fname, "w");
2373 if (!file) {
2374 perror(fname);
2375 exit(1);
2376 }
2377 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2378 perror(fname);
2379 exit(1);
2380 }
2381 if (fclose(file) != 0) {
2382 perror(fname);
2383 exit(1);
2384 }
2385}
2386
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002387static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 char *tmp;
2390 FILE *file;
2391 struct stat st;
2392
2393 file = fopen(fname, "r");
2394 if (!file)
2395 goto write;
2396
2397 if (fstat(fileno(file), &st) < 0)
2398 goto close_write;
2399
2400 if (st.st_size != b->pos)
2401 goto close_write;
2402
2403 tmp = NOFAIL(malloc(b->pos));
2404 if (fread(tmp, 1, b->pos, file) != b->pos)
2405 goto free_write;
2406
2407 if (memcmp(tmp, b->p, b->pos) != 0)
2408 goto free_write;
2409
2410 free(tmp);
2411 fclose(file);
2412 return;
2413
2414 free_write:
2415 free(tmp);
2416 close_write:
2417 fclose(file);
2418 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002419 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Ram Paibd5cbce2006-06-08 22:12:53 -07002422/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002423 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002424 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002425static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002427 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002429 buf = read_text_file(fname);
2430 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /* No symbol versions, silently ignore */
2432 return;
2433
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002434 pos = buf;
2435
2436 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002437 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 unsigned int crc;
2439 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002440 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (!(symname = strchr(line, '\t')))
2443 goto fail;
2444 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002445 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto fail;
2447 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002448 if (!(export = strchr(modname, '\t')))
2449 goto fail;
2450 *export++ = '\0';
2451 if (!(namespace = strchr(export, '\t')))
2452 goto fail;
2453 *namespace++ = '\0';
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 crc = strtoul(line, &d, 16);
2456 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2457 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002458 mod = find_module(modname);
2459 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002460 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002461 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002463 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002464 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002465 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002466 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002468 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return;
2470fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002471 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 fatal("parse error in symbol dump file\n");
2473}
2474
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002475/* For normal builds always dump all symbols.
2476 * For external modules only dump symbols
2477 * that are not read from kernel Module.symvers.
2478 **/
2479static int dump_sym(struct symbol *sym)
2480{
2481 if (!external_module)
2482 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002483 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002484 return 0;
2485 return 1;
2486}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002487
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002488static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 struct buffer buf = { };
2491 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002492 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 int n;
2494
2495 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2496 symbol = symbolhash[n];
2497 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002498 if (dump_sym(symbol)) {
2499 namespace = symbol->namespace;
2500 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2501 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002502 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002503 export_str(symbol->export),
2504 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 symbol = symbol->next;
2507 }
2508 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002509 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002510 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002513static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002514{
2515 struct module *mod;
2516 struct namespace_list *ns;
2517 struct buffer ns_deps_buf = {};
2518
2519 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002520
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002521 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002522 continue;
2523
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002524 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002525
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002526 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2527 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002528
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002529 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002530 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002531
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002532 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002533 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002534}
2535
Masahiro Yamada79247992020-06-01 14:57:07 +09002536struct dump_list {
2537 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002538 const char *file;
2539};
2540
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002541int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 struct module *mod;
2544 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002545 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302546 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002548 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002549 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002550 struct dump_list *dump_read_start = NULL;
2551 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002553 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002554 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002555 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002556 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002557 break;
2558 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002559 *dump_read_iter =
2560 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2561 (*dump_read_iter)->file = optarg;
2562 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002563 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002564 case 'm':
2565 modversions = 1;
2566 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302567 case 'n':
2568 ignore_missing_files = 1;
2569 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002570 case 'o':
2571 dump_write = optarg;
2572 break;
2573 case 'a':
2574 all_versions = 1;
2575 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302576 case 'T':
2577 files_source = optarg;
2578 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002579 case 'w':
2580 warn_unresolved = 1;
2581 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302582 case 'E':
2583 sec_mismatch_fatal = 1;
2584 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002585 case 'N':
2586 allow_missing_ns_imports = 1;
2587 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002588 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002589 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002590 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002591 default:
2592 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 }
2595
Masahiro Yamada79247992020-06-01 14:57:07 +09002596 while (dump_read_start) {
2597 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002598
Masahiro Yamada79247992020-06-01 14:57:07 +09002599 read_dump(dump_read_start->file);
2600 tmp = dump_read_start->next;
2601 free(dump_read_start);
2602 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002605 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Rusty Russell712f9b42013-04-04 17:37:38 +10302608 if (files_source)
2609 read_symbols_from_files(files_source);
2610
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002611 /*
2612 * When there's no vmlinux, don't print warnings about
2613 * unresolved symbols (since there'll be too many ;)
2614 */
2615 if (!have_vmlinux)
2616 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2617
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002618 err = 0;
2619
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002620 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302621 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002622
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002623 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002624 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 buf.pos = 0;
2627
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002628 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002629 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002632 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002633 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002634 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002635 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002636 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 add_moddevtable(&buf, mod);
2638 add_srcversion(&buf, mod);
2639
2640 sprintf(fname, "%s.mod.c", mod->name);
2641 write_if_changed(&buf, fname);
2642 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002643
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002644 if (missing_namespace_deps)
2645 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (dump_write)
2648 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002649 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002650 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002651 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002652 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002653 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002654
Masahiro Yamada47346e92019-09-24 21:07:40 +09002655 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002656 if (s->is_static)
2657 warn("\"%s\" [%s] is a static %s\n",
2658 s->name, s->module->name,
2659 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002660 }
2661 }
2662
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002663 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}