blob: 1ee257d084552cd68d4009c221db50f8a0090f83 [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>
Masahiro Yamadae54dd932021-08-28 18:50:59 +090020#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/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010029/* If we are modposting external module set to 1 */
30static int external_module = 0;
Will McVicker4b9c11a2020-11-19 13:46:37 -080031#define MODULE_SCMVERSION_SIZE 64
32static char module_scmversion[MODULE_SCMVERSION_SIZE];
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;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090037static int sec_mismatch_warn_only = true;
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
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090043static bool error_occurred;
44
Masahiro Yamada4475dff2021-03-26 03:54:11 +090045/*
46 * Cut off the warnings when there are too many. This typically occurs when
47 * vmlinux is missing. ('make modules' without building vmlinux.)
48 */
49#define MAX_UNRESOLVED_REPORTS 10
50static unsigned int nr_unresolved;
51
Sam Ravnborgc96fca22006-07-01 11:44:23 +020052enum export {
Christoph Hellwig36794822021-02-02 13:13:34 +010053 export_plain,
54 export_gpl,
55 export_unknown
Sam Ravnborgc96fca22006-07-01 11:44:23 +020056};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080058/* In kernel, this size is defined in linux/module.h;
59 * here we use Elf_Addr instead of long for covering cross-compile
60 */
61
62#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
63
Jessica Yu93c95e52020-03-06 17:02:05 +010064void __attribute__((format(printf, 2, 3)))
65modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 va_list arglist;
68
Jessica Yu93c95e52020-03-06 17:02:05 +010069 switch (loglevel) {
70 case LOG_WARN:
71 fprintf(stderr, "WARNING: ");
72 break;
73 case LOG_ERROR:
74 fprintf(stderr, "ERROR: ");
75 break;
76 case LOG_FATAL:
77 fprintf(stderr, "FATAL: ");
78 break;
79 default: /* invalid loglevel, ignore */
80 break;
81 }
82
83 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88
Jessica Yu93c95e52020-03-06 17:02:05 +010089 if (loglevel == LOG_FATAL)
90 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090091 if (loglevel == LOG_ERROR)
92 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060093}
94
Masahiro Yamadae54dd932021-08-28 18:50:59 +090095static inline bool strends(const char *str, const char *postfix)
96{
97 if (strlen(str) < strlen(postfix))
98 return false;
99
100 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void *do_nofail(void *ptr, const char *expr)
104{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100105 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100106 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return ptr;
109}
110
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900111char *read_text_file(const char *filename)
112{
113 struct stat st;
114 size_t nbytes;
115 int fd;
116 char *buf;
117
118 fd = open(filename, O_RDONLY);
119 if (fd < 0) {
120 perror(filename);
121 exit(1);
122 }
123
124 if (fstat(fd, &st) < 0) {
125 perror(filename);
126 exit(1);
127 }
128
129 buf = NOFAIL(malloc(st.st_size + 1));
130
131 nbytes = st.st_size;
132
133 while (nbytes) {
134 ssize_t bytes_read;
135
136 bytes_read = read(fd, buf, nbytes);
137 if (bytes_read < 0) {
138 perror(filename);
139 exit(1);
140 }
141
142 nbytes -= bytes_read;
143 }
144 buf[st.st_size] = '\0';
145
146 close(fd);
147
148 return buf;
149}
150
151char *get_line(char **stringp)
152{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200153 char *orig = *stringp, *next;
154
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900155 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200156 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900157 return NULL;
158
Wolfram Sang6020db52020-07-26 23:44:19 +0200159 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200160 next = strchr(orig, '\n');
161 if (next)
162 *next++ = '\0';
163
164 *stringp = next;
165
166 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static struct module *modules;
171
Masahiro Yamada8b185742018-05-09 18:50:40 +0900172static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct module *mod;
175
176 for (mod = modules; mod; mod = mod->next)
177 if (strcmp(mod->name, modname) == 0)
178 break;
179 return mod;
180}
181
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030182static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100185
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900186 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900190 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900191 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200192 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 mod->next = modules;
194 modules = mod;
195
196 return mod;
197}
198
199/* A hash of all exported symbols,
200 * struct symbol is also used for lists of unresolved symbols */
201
202#define SYMBOL_HASH_SIZE 1024
203
204struct symbol {
205 struct symbol *next;
206 struct module *module;
207 unsigned int crc;
208 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900209 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300211 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700212 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500213 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
217
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +0530218/* This is based on the hash algorithm from gdbm, via tdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static inline unsigned int tdb_hash(const char *name)
220{
221 unsigned value; /* Used to compute the hash value. */
222 unsigned i; /* Used to cycle through random values. */
223
224 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100225 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
227
228 return (1103515243 * value + 12345);
229}
230
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100231/**
232 * Allocate a new symbols for use in the hash of exported symbols or
233 * the list of unresolved symbols per module
234 **/
235static struct symbol *alloc_symbol(const char *name, unsigned int weak,
236 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
239
240 memset(s, 0, sizeof(*s));
241 strcpy(s->name, name);
242 s->weak = weak;
243 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300244 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return s;
246}
247
248/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700249static struct symbol *new_symbol(const char *name, struct module *module,
250 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900255 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
256
257 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100260static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct symbol *s;
263
264 /* For our purposes, .foo matches foo. PPC64 needs this. */
265 if (name[0] == '.')
266 name++;
267
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100268 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (strcmp(s->name, name) == 0)
270 return s;
271 }
272 return NULL;
273}
274
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100275static bool contains_namespace(struct namespace_list *list,
276 const char *namespace)
277{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900278 for (; list; list = list->next)
279 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100280 return true;
281
282 return false;
283}
284
285static void add_namespace(struct namespace_list **list, const char *namespace)
286{
287 struct namespace_list *ns_entry;
288
289 if (!contains_namespace(*list, namespace)) {
290 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
291 strlen(namespace) + 1));
292 strcpy(ns_entry->namespace, namespace);
293 ns_entry->next = *list;
294 *list = ns_entry;
295 }
296}
297
298static bool module_imports_namespace(struct module *module,
299 const char *namespace)
300{
301 return contains_namespace(module->imported_namespaces, namespace);
302}
303
Mathias Krause7a3ee752014-08-27 20:28:53 +0930304static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700305 const char *str;
306 enum export export;
307} export_list[] = {
308 { .str = "EXPORT_SYMBOL", .export = export_plain },
309 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700310 { .str = "(unknown)", .export = export_unknown },
311};
312
313
314static const char *export_str(enum export ex)
315{
316 return export_list[ex].str;
317}
318
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100319static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700320{
321 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100322
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200323 if (!s)
324 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700325 for (i = 0; export_list[i].export != export_unknown; i++) {
326 if (strcmp(export_list[i].str, s) == 0)
327 return export_list[i].export;
328 }
329 return export_unknown;
330}
331
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900332static void *sym_get_data_by_offset(const struct elf_info *info,
333 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700334{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800335 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900336
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900337 if (info->hdr->e_type != ET_REL)
338 offset -= sechdr->sh_addr;
339
340 return (void *)info->hdr + sechdr->sh_offset + offset;
341}
342
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900343static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
344{
345 return sym_get_data_by_offset(info, get_secindex(info, sym),
346 sym->st_value);
347}
348
Masahiro Yamada565587d2020-05-25 14:47:05 +0900349static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
350{
351 return sym_get_data_by_offset(info, info->secindex_strings,
352 sechdr->sh_name);
353}
354
355static const char *sec_name(const struct elf_info *info, int secindex)
356{
357 return sech_name(info, &info->sechdrs[secindex]);
358}
359
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200360#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
361
362static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
363{
364 const char *secname = sec_name(elf, sec);
365
366 if (strstarts(secname, "___ksymtab+"))
367 return export_plain;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200368 else if (strstarts(secname, "___ksymtab_gpl+"))
369 return export_gpl;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200370 else
371 return export_unknown;
372}
373
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200374static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700375{
376 if (sec == elf->export_sec)
377 return export_plain;
378 else if (sec == elf->export_gpl_sec)
379 return export_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700380 else
381 return export_unknown;
382}
383
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900384static const char *namespace_from_kstrtabns(const struct elf_info *info,
385 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100386{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900387 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100388 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100389}
390
Matthias Maennicha2b11182019-10-18 10:31:40 +0100391static void sym_update_namespace(const char *symname, const char *namespace)
392{
393 struct symbol *s = find_symbol(symname);
394
395 /*
396 * That symbol should have been created earlier and thus this is
397 * actually an assertion.
398 */
399 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900400 error("Could not update namespace(%s) for symbol %s\n",
401 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100402 return;
403 }
404
405 free(s->namespace);
406 s->namespace =
407 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
408}
409
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100410/**
411 * Add an exported symbol - it may have already been added without a
412 * CRC, in this case just update the CRC
413 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100414static struct symbol *sym_add_exported(const char *name, struct module *mod,
415 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct symbol *s = find_symbol(name);
418
419 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700420 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900421 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900422 s->module == mod) {
Quentin Perret479488f2020-11-24 14:40:13 +0000423 fatal("%s: '%s' exported twice. Previous export was in %s%s\n",
424 mod->name, name, s->module->name,
425 s->module->is_vmlinux ? "" : ".ko");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900427
428 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700429 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100430 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Masahiro Yamada17436942019-11-15 02:42:24 +0900433static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100434{
435 struct symbol *s = find_symbol(name);
436
Masahiro Yamada17436942019-11-15 02:42:24 +0900437 /*
438 * Ignore stand-alone __crc_*, which might be auto-generated symbols
439 * such as __*_veneer in ARM ELF.
440 */
441 if (!s)
442 return;
443
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100444 s->crc = crc;
445 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900448static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930451 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int fd;
453
454 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930455 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930457 if (fstat(fd, &st))
458 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 *size = st.st_size;
461 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930463failed:
464 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (map == MAP_FAILED)
466 return NULL;
467 return map;
468}
469
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900470static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 munmap(file, size);
473}
474
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100475static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100478 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 Elf_Shdr *sechdrs;
480 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200481 const char *secstrings;
482 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 hdr = grab_file(filename, &info->size);
485 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930486 if (ignore_missing_files) {
487 fprintf(stderr, "%s: %s (ignored)\n", filename,
488 strerror(errno));
489 return 0;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200492 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100495 if (info->size < sizeof(*hdr)) {
496 /* file too small, assume this is an empty .o file */
497 return 0;
498 }
499 /* Is this a valid ELF file? */
500 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
501 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
502 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
503 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
504 /* Not an ELF file - silently ignore it */
505 return 0;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200508 hdr->e_type = TO_NATIVE(hdr->e_type);
509 hdr->e_machine = TO_NATIVE(hdr->e_machine);
510 hdr->e_version = TO_NATIVE(hdr->e_version);
511 hdr->e_entry = TO_NATIVE(hdr->e_entry);
512 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
513 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
514 hdr->e_flags = TO_NATIVE(hdr->e_flags);
515 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
516 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
517 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
518 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
519 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
520 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sechdrs = (void *)hdr + hdr->e_shoff;
522 info->sechdrs = sechdrs;
523
Petr Stetiara83710e2007-08-27 12:15:07 +0200524 /* Check if file offset is correct */
525 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900526 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
527 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200528 return 0;
529 }
530
Anders Kaseorg68457562011-05-19 16:55:27 -0600531 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200532 /*
533 * There are more than 64k sections,
534 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200535 */
536 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
537 }
538 else {
539 info->num_sections = hdr->e_shnum;
540 }
541 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600542 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200543 }
544 else {
545 info->secindex_strings = hdr->e_shstrndx;
546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200549 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200550 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
551 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
552 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
553 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
554 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
555 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
556 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
557 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
558 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
559 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200562 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
563 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700564 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900565 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Tejun Heo56fc82c2009-02-06 00:48:02 +0900567 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100568 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
569 "sizeof(*hrd)=%zu\n", filename,
570 (unsigned long)sechdrs[i].sh_offset,
571 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100572 return 0;
573 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700574 secname = secstrings + sechdrs[i].sh_name;
575 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900576 if (nobits)
577 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
579 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700580 } else if (strcmp(secname, "__ksymtab") == 0)
581 info->export_sec = i;
582 else if (strcmp(secname, "__ksymtab_gpl") == 0)
583 info->export_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700584
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200585 if (sechdrs[i].sh_type == SHT_SYMTAB) {
586 unsigned int sh_link_idx;
587 symtab_idx = i;
588 info->symtab_start = (void *)hdr +
589 sechdrs[i].sh_offset;
590 info->symtab_stop = (void *)hdr +
591 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600592 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200593 info->strtab = (void *)hdr +
594 sechdrs[sh_link_idx].sh_offset;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200597 /* 32bit section no. table? ("more than 64k sections") */
598 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
599 symtab_shndx_idx = i;
600 info->symtab_shndx_start = (void *)hdr +
601 sechdrs[i].sh_offset;
602 info->symtab_shndx_stop = (void *)hdr +
603 sechdrs[i].sh_offset + sechdrs[i].sh_size;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100606 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100607 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Fix endianness in symbols */
610 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
611 sym->st_shndx = TO_NATIVE(sym->st_shndx);
612 sym->st_name = TO_NATIVE(sym->st_name);
613 sym->st_value = TO_NATIVE(sym->st_value);
614 sym->st_size = TO_NATIVE(sym->st_size);
615 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200616
617 if (symtab_shndx_idx != ~0U) {
618 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600619 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200620 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600621 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200622 symtab_idx);
623 /* Fix endianness */
624 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
625 p++)
626 *p = TO_NATIVE(*p);
627 }
628
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100632static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 release_file(info->hdr, info->size);
635}
636
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200637static int ignore_undef_symbol(struct elf_info *info, const char *symname)
638{
639 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900640 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200641 return 1;
642 /* ignore global offset table */
643 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
644 return 1;
645 if (info->hdr->e_machine == EM_PPC)
646 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900647 if (strstarts(symname, "_restgpr_") ||
648 strstarts(symname, "_savegpr_") ||
649 strstarts(symname, "_rest32gpr_") ||
650 strstarts(symname, "_save32gpr_") ||
651 strstarts(symname, "_restvr_") ||
652 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200653 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000654 if (info->hdr->e_machine == EM_PPC64)
655 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900656 if (strstarts(symname, "_restgpr0_") ||
657 strstarts(symname, "_savegpr0_") ||
658 strstarts(symname, "_restvr_") ||
659 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100660 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000661 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662 /* Do not ignore this symbol */
663 return 0;
664}
665
Masahiro Yamada17436942019-11-15 02:42:24 +0900666static void handle_modversion(const struct module *mod,
667 const struct elf_info *info,
668 const Elf_Sym *sym, const char *symname)
669{
670 unsigned int crc;
671
672 if (sym->st_shndx == SHN_UNDEF) {
Masahiro Yamada9f20ce92022-04-02 00:56:10 +0900673 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
Mark Brown4a679592021-06-07 15:02:06 +0100674 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
675 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
676 symname);
677
Masahiro Yamada17436942019-11-15 02:42:24 +0900678 return;
679 }
680
681 if (sym->st_shndx == SHN_ABS) {
682 crc = sym->st_value;
683 } else {
684 unsigned int *crcp;
685
686 /* symbol points to the CRC in the ELF object */
687 crcp = sym_get_data(info, sym);
688 crc = TO_NATIVE(*crcp);
689 }
690 sym_set_crc(symname, crc);
691}
692
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900693static void handle_symbol(struct module *mod, struct elf_info *info,
694 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200696 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900697 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200698
Masahiro Yamada33795762020-06-01 14:57:24 +0900699 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200700 export = export_from_secname(info, get_secindex(info, sym));
701 else
702 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 switch (sym->st_shndx) {
705 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900706 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100707 /* Should warn here, but modpost runs before the linker */
708 } else
709 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case SHN_UNDEF:
712 /* undefined symbol */
713 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
714 ELF_ST_BIND(sym->st_info) != STB_WEAK)
715 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200716 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (info->hdr->e_machine == EM_SPARC ||
719 info->hdr->e_machine == EM_SPARCV9) {
720 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700721 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100723 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700724 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100725 munged[0] = '_';
726 munged[1] = toupper(munged[1]);
727 symname = munged;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100730
Rusty Russellb92021b2013-03-15 15:04:17 +1030731 mod->unres = alloc_symbol(symname,
732 ELF_ST_BIND(sym->st_info) == STB_WEAK,
733 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
735 default:
736 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900737 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100738 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100739 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900741 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900743 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 mod->has_cleanup = 1;
745 break;
746 }
747}
748
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100749/**
750 * Parse tag=value strings from .modinfo section
751 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static char *next_string(char *string, unsigned long *secsize)
753{
754 /* Skip non-zero chars */
755 while (string[0]) {
756 string++;
757 if ((*secsize)-- <= 1)
758 return NULL;
759 }
760
761 /* Skip any zero padding. */
762 while (!string[0]) {
763 string++;
764 if ((*secsize)-- <= 1)
765 return NULL;
766 }
767 return string;
768}
769
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900770static char *get_next_modinfo(struct elf_info *info, const char *tag,
771 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 char *p;
774 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900775 char *modinfo = info->modinfo;
776 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900778 if (prev) {
779 size -= prev - modinfo;
780 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 for (p = modinfo; p; p = next_string(p, &size)) {
784 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
785 return p + taglen + 1;
786 }
787 return NULL;
788}
789
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900790static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200791
792{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200794}
795
Sam Ravnborg93684d32006-02-19 11:53:35 +0100796/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100797 * Test if string s ends in string sub
798 * return 0 if match
799 **/
800static int strrcmp(const char *s, const char *sub)
801{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100802 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100803
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100804 if (!s || !sub)
805 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100806
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100807 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100808 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100809
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100810 if ((slen == 0) || (sublen == 0))
811 return 1;
812
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100813 if (sublen > slen)
814 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100816 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100817}
818
Sam Ravnborgff13f922008-01-23 19:54:27 +0100819static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
820{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100821 if (sym)
822 return elf->strtab + sym->st_name;
823 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100824 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100825}
826
Sam Ravnborg10668222008-01-13 22:21:31 +0100827/* The pattern is an array of simple patterns.
828 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100829 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100830 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930831 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100832 */
Trevor Keith5c725132009-09-22 16:43:38 -0700833static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100834{
835 const char *p;
836 while (*pat) {
837 p = *pat++;
838 const char *endp = p + strlen(p) - 1;
839
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930840 /* "*foo*" */
841 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300842 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
843 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930844
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930845 free(bare);
846 if (here != NULL)
847 return 1;
848 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100849 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930850 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100851 if (strrcmp(sym, p + 1) == 0)
852 return 1;
853 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100854 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100855 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100856 if (strncmp(sym, p, strlen(p) - 1) == 0)
857 return 1;
858 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100859 /* no wildcards */
860 else {
861 if (strcmp(p, sym) == 0)
862 return 1;
863 }
864 }
865 /* no match */
866 return 0;
867}
868
Sam Ravnborg10668222008-01-13 22:21:31 +0100869/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930870static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200871{
872 ".comment*",
873 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930874 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800875 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000876 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200877 ".mdebug*", /* alpha, score, mips etc. */
878 ".pdr", /* alpha, score, mips etc. */
879 ".stab*",
880 ".note*",
881 ".got*",
882 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400883 ".xt.prop", /* xtensa */
884 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030885 ".arcextmap*", /* arc */
886 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030887 ".cmem*", /* EZchip */
888 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100889 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600890 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200891 NULL
892};
Sam Ravnborg10668222008-01-13 22:21:31 +0100893
Sam Ravnborge241a632008-01-28 20:13:13 +0100894/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400895 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100896 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400897 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100898 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400899static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900900 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100901{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400902 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100903
Anders Kaseorgb614a692009-04-23 16:49:33 -0400904 if (sechdr->sh_type == SHT_PROGBITS &&
Will Deacon88c2c382023-02-16 19:08:26 +0000905 sechdr->sh_size > 0 &&
Anders Kaseorgb614a692009-04-23 16:49:33 -0400906 !(sechdr->sh_flags & SHF_ALLOC) &&
907 !match(sec, section_white_list)) {
908 warn("%s (%s): unexpected non-allocatable section.\n"
909 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
910 "Note that for example <linux/init.h> contains\n"
911 "section definitions for use in .S files.\n\n",
912 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100913 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100914}
915
916
917
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100918#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930919 ".init.setup", ".init.rodata", ".meminit.rodata", \
920 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100921#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930922 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100923
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100924#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930925 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100926#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930927 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100928
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200929#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930930 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
931 ".pci_fixup_enable", ".pci_fixup_resume", \
932 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200933
Paul Gortmakere24f6622013-06-19 19:30:48 -0400934#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
935#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100936
937#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
938#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100939
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930940#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930941#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100942 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930943#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400944 ".fixup", ".entry.text", ".exception.text", ".text.*", \
Christophe Leroy1e688dd22021-04-13 16:38:10 +0000945 ".coldtext", ".softirqentry.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100946
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000947#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000948#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100949
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000950#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000951#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100952
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930953#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
954 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
955
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100956/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930957static const char *const init_data_sections[] =
958 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100959
960/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930961static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100962
963/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930964static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100965 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100966
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930967/* all text sections */
968static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
969
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100970/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930971static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100972
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
974/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100975#define DEFAULT_SYMBOL_WHITE_LIST \
976 "*driver", \
977 "*_template", /* scsi uses *_template a lot */ \
978 "*_timer", /* arm uses ops structures named _timer a lot */ \
979 "*_sht", /* scsi also used *_sht to some extent */ \
980 "*_ops", \
981 "*_probe", \
982 "*_probe_one", \
983 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984
Mathias Krause7a3ee752014-08-27 20:28:53 +0930985static const char *const head_sections[] = { ".head.text*", NULL };
986static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930988static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100989
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100990enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100991 TEXT_TO_ANY_INIT,
992 DATA_TO_ANY_INIT,
993 TEXT_TO_ANY_EXIT,
994 DATA_TO_ANY_EXIT,
995 XXXINIT_TO_SOME_INIT,
996 XXXEXIT_TO_SOME_EXIT,
997 ANY_INIT_TO_ANY_EXIT,
998 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100999 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301000 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001001};
1002
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301003/**
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301004 * Describe how to match sections on different criteria:
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301005 *
1006 * @fromsec: Array of sections to be matched.
1007 *
1008 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1009 * this array is forbidden (black-list). Can be empty.
1010 *
1011 * @good_tosec: Relocations applied to a section in @fromsec must be
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301012 * targeting sections in this array (white-list). Can be empty.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301013 *
1014 * @mismatch: Type of mismatch.
1015 *
1016 * @symbol_white_list: Do not match a relocation to a symbol in this list
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301017 * even if it is targeting a section in @bad_to_sec.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301018 *
1019 * @handler: Specific handler to call when a match is found. If NULL,
1020 * default_mismatch_handler() will be called.
1021 *
1022 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001023struct sectioncheck {
1024 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301025 const char *bad_tosec[20];
1026 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001027 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001028 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301029 void (*handler)(const char *modname, struct elf_info *elf,
1030 const struct sectioncheck* const mismatch,
1031 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1032
Sam Ravnborg10668222008-01-13 22:21:31 +01001033};
1034
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301035static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1036 const struct sectioncheck* const mismatch,
1037 Elf_Rela *r, Elf_Sym *sym,
1038 const char *fromsec);
1039
Mathias Krause7a3ee752014-08-27 20:28:53 +09301040static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001041/* Do not reference init/exit code/data from
1042 * normal code and data
1043 */
1044{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001045 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301046 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001047 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001048 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001049},
1050{
1051 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301052 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001053 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001054 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001055},
1056{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001057 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301058 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001059 .mismatch = DATA_TO_ANY_INIT,
1060 .symbol_white_list = {
1061 "*_template", "*_timer", "*_sht", "*_ops",
1062 "*_probe", "*_probe_one", "*_console", NULL
1063 },
1064},
1065{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001066 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301067 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001068 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001069 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001070},
1071{
1072 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001076},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001077/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001078{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001079 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001081 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001082 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001083},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001084/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001085{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001086 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001090},
1091/* Do not use exit code/data from init code */
1092{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001093 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001097},
1098/* Do not use init code/data from exit code */
1099{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001104},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001105{
1106 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001108 .mismatch = ANY_INIT_TO_ANY_EXIT,
1109 .symbol_white_list = { NULL },
1110},
Sam Ravnborg10668222008-01-13 22:21:31 +01001111/* Do not export init/exit functions or data */
1112{
Masahiro Yamadabcf20872022-06-11 03:32:30 +09001113 .fromsec = { "___ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001115 .mismatch = EXPORT_TO_INIT_EXIT,
1116 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301117},
1118{
1119 .fromsec = { "__ex_table", NULL },
1120 /* If you're adding any new black-listed sections in here, consider
1121 * adding a special 'printer' for them in scripts/check_extable.
1122 */
1123 .bad_tosec = { ".altinstr_replacement", NULL },
1124 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1125 .mismatch = EXTABLE_TO_NON_TEXT,
1126 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001127}
1128};
1129
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001130static const struct sectioncheck *section_mismatch(
1131 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001132{
1133 int i;
1134 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1135 const struct sectioncheck *check = &sectioncheck[0];
1136
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301137 /*
1138 * The target section could be the SHT_NUL section when we're
1139 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001140 * doesn't make much sense and causes build failures on parisc
1141 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301142 */
1143 if (*tosec == '\0')
1144 return NULL;
1145
Sam Ravnborg10668222008-01-13 22:21:31 +01001146 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301147 if (match(fromsec, check->fromsec)) {
1148 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1149 return check;
1150 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1151 return check;
1152 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001153 check++;
1154 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001155 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001156}
1157
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001158/**
1159 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001160 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001161 * Pattern 1:
1162 * If a module parameter is declared __initdata and permissions=0
1163 * then this is legal despite the warning generated.
1164 * We cannot see value of permissions here, so just ignore
1165 * this pattern.
1166 * The pattern is identified by:
1167 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001168 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001169 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001170 *
Rusty Russell6a841522010-08-11 23:04:16 -06001171 * Pattern 1a:
1172 * module_param_call() ops can refer to __init set function if permissions=0
1173 * The pattern is identified by:
1174 * tosec = .init.text
1175 * fromsec = .data*
1176 * atsym = __param_ops_*
1177 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001179 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001180 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001181 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001182 * tosec = init or exit section
1183 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001184 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1185 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001186 *
1187 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001188 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001189 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001190 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001191 * Some symbols belong to init section but still it is ok to reference
1192 * these from non-init sections as these symbols don't have any memory
1193 * allocated for them and symbol address and value are same. So even
1194 * if init section is freed, its ok to reference those symbols.
1195 * For ex. symbols marking the init section boundaries.
1196 * This pattern is identified by
1197 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001198 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301199 * Pattern 5:
1200 * GCC may optimize static inlines when fed constant arg(s) resulting
1201 * in functions like cpumask_empty() -- generating an associated symbol
1202 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1203 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1204 * meaningless section warning. May need to add isra symbols too...
1205 * This pattern is identified by
1206 * tosec = init section
1207 * fromsec = text section
1208 * refsymname = *.constprop.*
1209 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001210 * Pattern 6:
1211 * Hide section mismatch warnings for ELF local symbols. The goal
1212 * is to eliminate false positive modpost warnings caused by
1213 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1214 * Autogenerated symbol names bypass modpost's "Pattern 2"
1215 * whitelisting, which relies on pattern-matching against symbol
1216 * names to work. (One situation where gcc can autogenerate ELF
1217 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001218 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001219static int secref_whitelist(const struct sectioncheck *mismatch,
1220 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001221 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001222{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001223 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001224 if (match(tosec, init_data_sections) &&
1225 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001226 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001227 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001228
Rusty Russell6a841522010-08-11 23:04:16 -06001229 /* Check for pattern 1a */
1230 if (strcmp(tosec, ".init.text") == 0 &&
1231 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001232 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001233 return 0;
1234
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001235 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001236 if (match(tosec, init_exit_sections) &&
1237 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001238 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001239 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001241 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001242 if (match(fromsec, head_sections) &&
1243 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001244 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001245
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001246 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001247 if (match(tosym, linker_symbols))
1248 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001249
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301250 /* Check for pattern 5 */
1251 if (match(fromsec, text_sections) &&
1252 match(tosec, init_sections) &&
1253 match(fromsym, optim_symbols))
1254 return 0;
1255
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001256 /* Check for pattern 6 */
1257 if (strstarts(fromsym, ".L"))
1258 return 0;
1259
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001260 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001261}
1262
Sami Tolvanen5818c682018-10-23 15:15:35 -07001263static inline int is_arm_mapping_symbol(const char *str)
1264{
Masahiro Yamada4adc7d72022-05-24 01:46:22 +09001265 return str[0] == '$' &&
1266 (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x')
Sami Tolvanen5818c682018-10-23 15:15:35 -07001267 && (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
Masahiro Yamadacd7806e2023-06-01 21:09:55 +09001307 /*
1308 * Strive to find a better symbol name, but the resulting name may not
1309 * match the symbol referenced in the original code.
1310 */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001311 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001312 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001313 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001315 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1316 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001317 if (!is_valid_name(elf, sym))
1318 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001319 if (sym->st_value == addr)
1320 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001321 /* Find a symbol nearby - addr are maybe negative */
1322 d = sym->st_value - addr;
1323 if (d < 0)
1324 d = addr - sym->st_value;
1325 if (d < distance) {
1326 distance = d;
1327 near = sym;
1328 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001330 /* We need a close match */
1331 if (distance < 20)
1332 return near;
1333 else
1334 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001335}
1336
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001337/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001338 * Find symbols before or equal addr and after addr - in the section sec.
1339 * If we find two symbols with equal offset prefer one with a valid name.
1340 * The ELF format may have a better way to detect what type of symbol
1341 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001342 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001343static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1344 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001345{
1346 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001347 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001348 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001349
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001350 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1351 const char *symsec;
1352
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001353 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001355 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 if (strcmp(symsec, sec) != 0)
1357 continue;
David Brownellda68d612007-02-20 13:58:16 -08001358 if (!is_valid_name(elf, sym))
1359 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001360 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001361 if ((addr - sym->st_value) < distance) {
1362 distance = addr - sym->st_value;
1363 near = sym;
1364 } else if ((addr - sym->st_value) == distance) {
1365 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001366 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001367 }
1368 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001369 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370}
1371
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001372/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001373 * Convert a section name to the function/data attribute
1374 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001375 * .memexitconst => __memconst
1376 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001377 *
1378 * The memory of returned value has been allocated on a heap. The user of this
1379 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001380*/
1381static char *sec2annotation(const char *s)
1382{
1383 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001384 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001385 char *r = p;
1386
1387 *p++ = '_';
1388 *p++ = '_';
1389 if (*s == '.')
1390 s++;
1391 while (*s && *s != '.')
1392 *p++ = *s++;
1393 *p = '\0';
1394 if (*s == '.')
1395 s++;
1396 if (strstr(s, "rodata") != NULL)
1397 strcat(p, "const ");
1398 else if (strstr(s, "data") != NULL)
1399 strcat(p, "data ");
1400 else
1401 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001402 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001403 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001404 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 }
1406}
1407
1408static int is_function(Elf_Sym *sym)
1409{
1410 if (sym)
1411 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1412 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001413 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001414}
1415
Randy Dunlap00759c02011-03-15 14:13:47 -07001416static void print_section_list(const char * const list[20])
1417{
1418 const char *const *s = list;
1419
1420 while (*s) {
1421 fprintf(stderr, "%s", *s);
1422 s++;
1423 if (*s)
1424 fprintf(stderr, ", ");
1425 }
1426 fprintf(stderr, "\n");
1427}
1428
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301429static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1430{
1431 switch (is_func) {
1432 case 0: *name = "variable"; *name_p = ""; break;
1433 case 1: *name = "function"; *name_p = "()"; break;
1434 default: *name = "(unknown reference)"; *name_p = ""; break;
1435 }
1436}
1437
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001438/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001439 * Print a warning about a section mismatch.
1440 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001441 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001442 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001443static void report_sec_mismatch(const char *modname,
1444 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001445 const char *fromsec,
1446 unsigned long long fromaddr,
1447 const char *fromsym,
1448 int from_is_func,
1449 const char *tosec, const char *tosym,
1450 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001451{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452 const char *from, *from_p;
1453 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001454 char *prl_from;
1455 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001456
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001457 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001458
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301459 get_pretty_name(from_is_func, &from, &from_p);
1460 get_pretty_name(to_is_func, &to, &to_p);
1461
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001462 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1463 "to the %s %s:%s%s\n",
1464 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1465 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001466
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001467 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001468 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 prl_from = sec2annotation(fromsec);
1470 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001471 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001472 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001473 "the %s %s%s%s.\n"
1474 "This is often because %s lacks a %s\n"
1475 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001476 prl_from, fromsym,
1477 to, prl_to, tosym, to_p,
1478 fromsym, prl_to, tosym);
1479 free(prl_from);
1480 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001481 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001482 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001483 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001484 fprintf(stderr,
1485 "The variable %s references\n"
1486 "the %s %s%s%s\n"
1487 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001488 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001490 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001491 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001494 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001495 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001496 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001497 fprintf(stderr,
1498 "The function %s() references a %s in an exit section.\n"
1499 "Often the %s %s%s has valid usage outside the exit section\n"
1500 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 fromsym, to, to, tosym, to_p, prl_to, tosym);
1502 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001504 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 fprintf(stderr,
1507 "The variable %s references\n"
1508 "the %s %s%s%s\n"
1509 "If the reference is valid then annotate the\n"
1510 "variable with __exit* (see linux/init.h) or "
1511 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001513 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 break;
1516 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001517 case XXXINIT_TO_SOME_INIT:
1518 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001519 prl_from = sec2annotation(fromsec);
1520 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001521 fprintf(stderr,
1522 "The %s %s%s%s references\n"
1523 "a %s %s%s%s.\n"
1524 "If %s is only used by %s then\n"
1525 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 from, prl_from, fromsym, from_p,
1527 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001528 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 free(prl_from);
1530 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001532 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001533 prl_from = sec2annotation(fromsec);
1534 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001535 fprintf(stderr,
1536 "The %s %s%s%s references\n"
1537 "a %s %s%s%s.\n"
1538 "This is often seen when error handling "
1539 "in the init function\n"
1540 "uses functionality in the exit path.\n"
1541 "The fix is often to remove the %sannotation of\n"
1542 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 from, prl_from, fromsym, from_p,
1544 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001545 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 free(prl_from);
1547 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001549 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001550 prl_from = sec2annotation(fromsec);
1551 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001552 fprintf(stderr,
1553 "The %s %s%s%s references\n"
1554 "a %s %s%s%s.\n"
1555 "This is often seen when error handling "
1556 "in the exit function\n"
1557 "uses functionality in the init path.\n"
1558 "The fix is often to remove the %sannotation of\n"
1559 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 from, prl_from, fromsym, from_p,
1561 to, prl_to, tosym, to_p,
1562 prl_to, tosym, to_p);
1563 free(prl_from);
1564 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001565 break;
1566 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001567 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001568 fprintf(stderr,
1569 "The symbol %s is exported and annotated %s\n"
1570 "Fix this by removing the %sannotation of %s "
1571 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001572 tosym, prl_to, prl_to, tosym);
1573 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001574 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301575 case EXTABLE_TO_NON_TEXT:
1576 fatal("There's a special handler for this mismatch type, "
1577 "we should never get here.");
1578 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001579 }
1580 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001581}
1582
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301583static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1584 const struct sectioncheck* const mismatch,
1585 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1586{
1587 const char *tosec;
1588 Elf_Sym *to;
1589 Elf_Sym *from;
1590 const char *tosym;
1591 const char *fromsym;
1592
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301593 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1594 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001596 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301597 return;
1598
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301599 tosec = sec_name(elf, get_secindex(elf, sym));
1600 to = find_elf_symbol(elf, r->r_addend, sym);
1601 tosym = sym_name(elf, to);
1602
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301603 /* check whitelist - we may ignore it */
1604 if (secref_whitelist(mismatch,
1605 fromsec, fromsym, tosec, tosym)) {
1606 report_sec_mismatch(modname, mismatch,
1607 fromsec, r->r_offset, fromsym,
1608 is_function(from), tosec, tosym,
1609 is_function(to));
1610 }
1611}
1612
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301613static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1614{
Dan Carpenter5e0424c2023-06-08 11:23:40 +03001615 if (section_index >= elf->num_sections)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301616 fatal("section_index is outside elf->num_sections!\n");
1617
1618 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1619}
1620
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301621static void report_extable_warnings(const char* modname, struct elf_info* elf,
1622 const struct sectioncheck* const mismatch,
1623 Elf_Rela* r, Elf_Sym* sym,
1624 const char* fromsec, const char* tosec)
1625{
1626 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1627 const char* fromsym_name = sym_name(elf, fromsym);
1628 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1629 const char* tosym_name = sym_name(elf, tosym);
1630 const char* from_pretty_name;
1631 const char* from_pretty_name_p;
1632 const char* to_pretty_name;
1633 const char* to_pretty_name_p;
1634
1635 get_pretty_name(is_function(fromsym),
1636 &from_pretty_name, &from_pretty_name_p);
1637 get_pretty_name(is_function(tosym),
1638 &to_pretty_name, &to_pretty_name_p);
1639
1640 warn("%s(%s+0x%lx): Section mismatch in reference"
1641 " from the %s %s%s to the %s %s:%s%s\n",
1642 modname, fromsec, (long)r->r_offset, from_pretty_name,
1643 fromsym_name, from_pretty_name_p,
1644 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1645
1646 if (!match(tosec, mismatch->bad_tosec) &&
1647 is_executable_section(elf, get_secindex(elf, sym)))
1648 fprintf(stderr,
1649 "The relocation at %s+0x%lx references\n"
1650 "section \"%s\" which is not in the list of\n"
1651 "authorized sections. If you're adding a new section\n"
1652 "and/or if this reference is valid, add \"%s\" to the\n"
1653 "list of authorized sections to jump to on fault.\n"
1654 "This can be achieved by adding \"%s\" to \n"
1655 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1656 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1657}
1658
1659static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1660 const struct sectioncheck* const mismatch,
1661 Elf_Rela* r, Elf_Sym* sym,
1662 const char *fromsec)
1663{
1664 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1665
1666 sec_mismatch_count++;
1667
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001668 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301669
1670 if (match(tosec, mismatch->bad_tosec))
1671 fatal("The relocation at %s+0x%lx references\n"
1672 "section \"%s\" which is black-listed.\n"
1673 "Something is seriously wrong and should be fixed.\n"
1674 "You might get more information about where this is\n"
1675 "coming from by using scripts/check_extable.sh %s\n",
1676 fromsec, (long)r->r_offset, tosec, modname);
Masahiro Yamadab030d232023-05-15 00:27:19 +09001677 else if (!is_executable_section(elf, get_secindex(elf, sym)))
1678 error("%s+0x%lx references non-executable section '%s'\n",
1679 fromsec, (long)r->r_offset, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301680}
1681
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001682static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001683 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001684{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001685 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301686 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001687
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001688 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301689 if (mismatch->handler)
1690 mismatch->handler(modname, elf, mismatch,
1691 r, sym, fromsec);
1692 else
1693 default_mismatch_handler(modname, elf, mismatch,
1694 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001695 }
1696}
1697
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001698static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001699 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001700{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001701 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001702}
1703
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001704static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001705{
1706 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001707 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001708
1709 switch (r_typ) {
1710 case R_386_32:
1711 r->r_addend = TO_NATIVE(*location);
1712 break;
1713 case R_386_PC32:
1714 r->r_addend = TO_NATIVE(*location) + 4;
1715 /* For CONFIG_RELOCATABLE=y */
1716 if (elf->hdr->e_type == ET_EXEC)
1717 r->r_addend += r->r_offset;
1718 break;
1719 }
1720 return 0;
1721}
1722
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001723#ifndef R_ARM_CALL
1724#define R_ARM_CALL 28
1725#endif
1726#ifndef R_ARM_JUMP24
1727#define R_ARM_JUMP24 29
1728#endif
1729
David A. Longc9698e52014-02-14 22:41:18 +01001730#ifndef R_ARM_THM_CALL
1731#define R_ARM_THM_CALL 10
1732#endif
1733#ifndef R_ARM_THM_JUMP24
1734#define R_ARM_THM_JUMP24 30
1735#endif
1736#ifndef R_ARM_THM_JUMP19
1737#define R_ARM_THM_JUMP19 51
1738#endif
1739
Masahiro Yamada6bfdced2023-06-01 21:09:56 +09001740static int32_t sign_extend32(int32_t value, int index)
1741{
1742 uint8_t shift = 31 - index;
1743
1744 return (int32_t)(value << shift) >> shift;
1745}
1746
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001747static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001748{
1749 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Masahiro Yamadacd7806e2023-06-01 21:09:55 +09001750 Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info);
1751 void *loc = reloc_location(elf, sechdr, r);
1752 uint32_t inst;
Masahiro Yamada6bfdced2023-06-01 21:09:56 +09001753 int32_t offset;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001754
1755 switch (r_typ) {
1756 case R_ARM_ABS32:
Masahiro Yamadacd7806e2023-06-01 21:09:55 +09001757 inst = TO_NATIVE(*(uint32_t *)loc);
1758 r->r_addend = inst + sym->st_value;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001759 break;
1760 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001761 case R_ARM_CALL:
1762 case R_ARM_JUMP24:
Masahiro Yamada6bfdced2023-06-01 21:09:56 +09001763 inst = TO_NATIVE(*(uint32_t *)loc);
1764 offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
1765 r->r_addend = offset + sym->st_value + 8;
1766 break;
David A. Longc9698e52014-02-14 22:41:18 +01001767 case R_ARM_THM_CALL:
1768 case R_ARM_THM_JUMP24:
1769 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001770 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001771 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001772 sechdr->sh_offset +
1773 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001774 break;
1775 default:
1776 return 1;
1777 }
1778 return 0;
1779}
1780
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001781static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001782{
1783 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001784 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001785 unsigned int inst;
1786
1787 if (r_typ == R_MIPS_HI16)
1788 return 1; /* skip this */
1789 inst = TO_NATIVE(*location);
1790 switch (r_typ) {
1791 case R_MIPS_LO16:
1792 r->r_addend = inst & 0xffff;
1793 break;
1794 case R_MIPS_26:
1795 r->r_addend = (inst & 0x03ffffff) << 2;
1796 break;
1797 case R_MIPS_32:
1798 r->r_addend = inst;
1799 break;
1800 }
1801 return 0;
1802}
1803
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001804static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001805 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001806{
1807 Elf_Sym *sym;
1808 Elf_Rela *rela;
1809 Elf_Rela r;
1810 unsigned int r_sym;
1811 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001812
Sam Ravnborgff13f922008-01-23 19:54:27 +01001813 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001814 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1815
Sam Ravnborgff13f922008-01-23 19:54:27 +01001816 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001817 fromsec += strlen(".rela");
1818 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001819 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001820 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001821
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001822 for (rela = start; rela < stop; rela++) {
1823 r.r_offset = TO_NATIVE(rela->r_offset);
1824#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001825 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001826 unsigned int r_typ;
1827 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1828 r_sym = TO_NATIVE(r_sym);
1829 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1830 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1831 } else {
1832 r.r_info = TO_NATIVE(rela->r_info);
1833 r_sym = ELF_R_SYM(r.r_info);
1834 }
1835#else
1836 r.r_info = TO_NATIVE(rela->r_info);
1837 r_sym = ELF_R_SYM(r.r_info);
1838#endif
1839 r.r_addend = TO_NATIVE(rela->r_addend);
1840 sym = elf->symtab_start + r_sym;
1841 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001842 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001843 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001844 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001845 }
1846}
1847
1848static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001849 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001850{
1851 Elf_Sym *sym;
1852 Elf_Rel *rel;
1853 Elf_Rela r;
1854 unsigned int r_sym;
1855 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001856
Sam Ravnborgff13f922008-01-23 19:54:27 +01001857 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1859
Sam Ravnborgff13f922008-01-23 19:54:27 +01001860 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861 fromsec += strlen(".rel");
1862 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001863 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864 return;
1865
1866 for (rel = start; rel < stop; rel++) {
1867 r.r_offset = TO_NATIVE(rel->r_offset);
1868#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001869 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001870 unsigned int r_typ;
1871 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1872 r_sym = TO_NATIVE(r_sym);
1873 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1874 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1875 } else {
1876 r.r_info = TO_NATIVE(rel->r_info);
1877 r_sym = ELF_R_SYM(r.r_info);
1878 }
1879#else
1880 r.r_info = TO_NATIVE(rel->r_info);
1881 r_sym = ELF_R_SYM(r.r_info);
1882#endif
1883 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001884 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001885 case EM_386:
1886 if (addend_386_rel(elf, sechdr, &r))
1887 continue;
1888 break;
1889 case EM_ARM:
1890 if (addend_arm_rel(elf, sechdr, &r))
1891 continue;
1892 break;
1893 case EM_MIPS:
1894 if (addend_mips_rel(elf, sechdr, &r))
1895 continue;
1896 break;
1897 }
1898 sym = elf->symtab_start + r_sym;
1899 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001900 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001902 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903 }
1904}
1905
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001906/**
1907 * A module includes a number of sections that are discarded
1908 * either when loaded or when used as built-in.
1909 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001910 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001911 * Likewise for modules used built-in the sections marked __exit
1912 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001913 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001914 * The check_sec_ref() function traverses all relocation records
1915 * to find all references to a section that reference a section that will
1916 * be discarded and warns about it.
1917 **/
1918static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001919 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001920{
1921 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001922 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001923
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001924 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001925 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001926 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001927 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001928 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001929 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001930 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001931 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001932 }
1933}
1934
Andi Kleen7d02b492014-02-08 09:01:12 +01001935static char *remove_dot(char *s)
1936{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301937 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001938
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301939 if (n && s[n]) {
1940 size_t m = strspn(s + n + 1, "0123456789");
Alexander Lobakina53131a2022-05-24 17:27:18 +02001941 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001942 s[n] = 0;
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001943
1944 /* strip trailing .lto */
1945 if (strends(s, ".lto"))
1946 s[strlen(s) - 4] = '\0';
Andi Kleen7d02b492014-02-08 09:01:12 +01001947 }
1948 return s;
1949}
1950
Masahiro Yamada8b185742018-05-09 18:50:40 +09001951static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 const char *symname;
1954 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001955 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001956 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct module *mod;
1958 struct elf_info info = { };
1959 Elf_Sym *sym;
1960
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001961 if (!parse_elf(&info, modname))
1962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001964 {
1965 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001967 /* strip trailing .o */
1968 tmp = NOFAIL(strdup(modname));
1969 tmp[strlen(tmp) - 2] = '\0';
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001970 /* strip trailing .lto */
1971 if (strends(tmp, ".lto"))
1972 tmp[strlen(tmp) - 4] = '\0';
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001973 mod = new_module(tmp);
1974 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001977 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001978 license = get_modinfo(&info, "license");
1979 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09001980 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001981 while (license) {
1982 if (license_is_gpl_compatible(license))
1983 mod->gpl_compatible = 1;
1984 else {
1985 mod->gpl_compatible = 0;
1986 break;
1987 }
1988 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001989 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001990
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001991 namespace = get_modinfo(&info, "import_ns");
1992 while (namespace) {
1993 add_namespace(&mod->imported_namespaces, namespace);
1994 namespace = get_next_modinfo(&info, "import_ns",
1995 namespace);
1996 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001997 }
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002000 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002002 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 handle_moddevtable(mod, &info, sym, symname);
2004 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002005
Matthias Maennich69923202019-10-18 10:31:42 +01002006 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2007 symname = remove_dot(info.strtab + sym->st_name);
2008
Masahiro Yamada17436942019-11-15 02:42:24 +09002009 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002010 if (strstarts(symname, "__kstrtabns_"))
2011 sym_update_namespace(symname + strlen("__kstrtabns_"),
2012 namespace_from_kstrtabns(&info,
2013 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002014
2015 if (strstarts(symname, "__crc_"))
2016 handle_modversion(mod, &info, sym,
2017 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002018 }
2019
Denis Efremov15bfc232019-08-01 09:06:57 +03002020 // check for static EXPORT_SYMBOL_* functions && global vars
2021 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2022 unsigned char bind = ELF_ST_BIND(sym->st_info);
2023
2024 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2025 struct symbol *s =
2026 find_symbol(remove_dot(info.strtab +
2027 sym->st_name));
2028
2029 if (s)
2030 s->is_static = 0;
2031 }
2032 }
2033
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002034 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002036 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002037 version = get_modinfo(&info, "version");
2038 if (version || all_versions)
Masahiro Yamadae54dd932021-08-28 18:50:59 +09002039 get_src_version(mod->name, mod->srcversion,
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002040 sizeof(mod->srcversion) - 1);
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 parse_elf_finish(&info);
2044
Rusty Russell8c8ef422009-03-31 13:05:34 -06002045 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 * never passed as an argument to an exported function, so
2047 * the automatic versioning doesn't pick it up, but it's really
2048 * important anyhow */
2049 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002050 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Rusty Russell712f9b42013-04-04 17:37:38 +10302053static void read_symbols_from_files(const char *filename)
2054{
2055 FILE *in = stdin;
2056 char fname[PATH_MAX];
2057
2058 if (strcmp(filename, "-") != 0) {
2059 in = fopen(filename, "r");
2060 if (!in)
2061 fatal("Can't open filenames file %s: %m", filename);
2062 }
2063
2064 while (fgets(fname, PATH_MAX, in) != NULL) {
2065 if (strends(fname, "\n"))
2066 fname[strlen(fname)-1] = '\0';
2067 read_symbols(fname);
2068 }
2069
2070 if (in != stdin)
2071 fclose(in);
2072}
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#define SZ 500
2075
2076/* We first write the generated file into memory using the
2077 * following helper, then compare to the file on disk and
2078 * only update the later if anything changed */
2079
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002080void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2081 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 char tmp[SZ];
2084 int len;
2085 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 va_start(ap, fmt);
2088 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002089 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 va_end(ap);
2091}
2092
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002093void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002096 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002097 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 strncpy(buf->p + buf->pos, s, len);
2100 buf->pos += len;
2101}
2102
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002103static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2104{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002105 switch (exp) {
2106 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002107 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002108 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002109 break;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002110 case export_plain:
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002111 case export_unknown:
2112 /* ignore */
2113 break;
2114 }
2115}
2116
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002117static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002118{
2119 struct symbol *s, *exp;
2120
2121 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002122 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002123 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002124 if (!exp || exp->module == mod) {
Masahiro Yamada4475dff2021-03-26 03:54:11 +09002125 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
Jessica Yu93c95e52020-03-06 17:02:05 +01002126 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2127 "\"%s\" [%s.ko] undefined!\n",
2128 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002129 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002130 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002131 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002132 if (basename)
2133 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002134 else
2135 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002136
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002137 if (exp->namespace &&
2138 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002139 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2140 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2141 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002142 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002143 }
2144
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002145 if (!mod->gpl_compatible)
2146 check_for_gpl_usage(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002147 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002148}
2149
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002150static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002151{
2152 const char *mod_name;
2153
2154 mod_name = strrchr(mod->name, '/');
2155 if (mod_name == NULL)
2156 mod_name = mod->name;
2157 else
2158 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002159 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002160 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002161}
2162
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002163/**
2164 * Header for the generated file
2165 **/
2166static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002169 /*
2170 * Include build-salt.h after module.h in order to
2171 * inherit the definitions.
2172 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002173 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002174 buf_printf(b, "#include <linux/build-salt.h>\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07002175 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 buf_printf(b, "#include <linux/vermagic.h>\n");
2177 buf_printf(b, "#include <linux/compiler.h>\n");
2178 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002179 buf_printf(b, "BUILD_SALT;\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07002180 buf_printf(b, "BUILD_LTO_INFO;\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002181 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002183 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302185 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002186 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002187 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002189 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (mod->has_cleanup)
2191 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002192 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002194 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 buf_printf(b, "};\n");
2196}
2197
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002198static void add_intree_flag(struct buffer *b, int is_intree)
2199{
2200 if (is_intree)
2201 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2202}
2203
Will McVicker4b9c11a2020-11-19 13:46:37 -08002204/**
2205 * add_scmversion() - Adds the MODULE_INFO macro for the scmversion.
2206 * @b: Buffer to append to.
2207 *
2208 * This function fills in the module attribute `scmversion` for the kernel
2209 * module. This is useful for determining a given module's SCM version on
2210 * device via /sys/modules/<module>/scmversion and/or using the modinfo tool.
2211 */
2212static void add_scmversion(struct buffer *b)
2213{
2214 if (module_scmversion[0] != '\0')
2215 buf_printf(b, "\nMODULE_INFO(scmversion, \"%s\");\n", module_scmversion);
2216}
2217
Andi Kleencaf75012018-01-25 15:50:28 -08002218/* Cannot check for assembler */
2219static void add_retpoline(struct buffer *b)
2220{
WANG Chaoe4f35892018-12-11 00:37:25 +08002221 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002222 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2223 buf_printf(b, "#endif\n");
2224}
2225
Trevor Keith5c725132009-09-22 16:43:38 -07002226static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002227{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002228 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002229 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2230}
2231
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002232/**
2233 * Record CRCs for unresolved symbols
2234 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002235static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 struct symbol *s, *exp;
2238
2239 for (s = mod->unres; s; s = s->next) {
2240 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002241 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 s->module = exp->module;
2244 s->crc_valid = exp->crc_valid;
2245 s->crc = exp->crc;
2246 }
2247
2248 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 buf_printf(b, "\n");
2252 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002253 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002256 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002259 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 s->name, mod->name);
2261 continue;
2262 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302263 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002264 error("too long symbol \"%s\" [%s.ko]\n",
2265 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302266 break;
2267 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002268 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302269 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271
2272 buf_printf(b, "};\n");
2273}
2274
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002275static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int first = 1;
2279
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002280 /* Clear ->seen flag of modules that own symbols needed by this. */
2281 for (s = mod->unres; s; s = s->next)
2282 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002283 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002286 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002288 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (!s->module)
2290 continue;
2291
2292 if (s->module->seen)
2293 continue;
2294
2295 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002296 p = strrchr(s->module->name, '/');
2297 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002298 p++;
2299 else
2300 p = s->module->name;
2301 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 first = 0;
2303 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002304 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002307static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 if (mod->srcversion[0]) {
2310 buf_printf(b, "\n");
2311 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2312 mod->srcversion);
2313 }
2314}
2315
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002316static void write_buf(struct buffer *b, const char *fname)
2317{
2318 FILE *file;
2319
2320 file = fopen(fname, "w");
2321 if (!file) {
2322 perror(fname);
2323 exit(1);
2324 }
2325 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2326 perror(fname);
2327 exit(1);
2328 }
2329 if (fclose(file) != 0) {
2330 perror(fname);
2331 exit(1);
2332 }
2333}
2334
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002335static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 char *tmp;
2338 FILE *file;
2339 struct stat st;
2340
2341 file = fopen(fname, "r");
2342 if (!file)
2343 goto write;
2344
2345 if (fstat(fileno(file), &st) < 0)
2346 goto close_write;
2347
2348 if (st.st_size != b->pos)
2349 goto close_write;
2350
2351 tmp = NOFAIL(malloc(b->pos));
2352 if (fread(tmp, 1, b->pos, file) != b->pos)
2353 goto free_write;
2354
2355 if (memcmp(tmp, b->p, b->pos) != 0)
2356 goto free_write;
2357
2358 free(tmp);
2359 fclose(file);
2360 return;
2361
2362 free_write:
2363 free(tmp);
2364 close_write:
2365 fclose(file);
2366 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002367 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Ram Paibd5cbce2006-06-08 22:12:53 -07002370/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002371 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002372 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002373static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002375 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002377 buf = read_text_file(fname);
2378 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 /* No symbol versions, silently ignore */
2380 return;
2381
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002382 pos = buf;
2383
2384 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002385 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 unsigned int crc;
2387 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002388 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 if (!(symname = strchr(line, '\t')))
2391 goto fail;
2392 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002393 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto fail;
2395 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002396 if (!(export = strchr(modname, '\t')))
2397 goto fail;
2398 *export++ = '\0';
2399 if (!(namespace = strchr(export, '\t')))
2400 goto fail;
2401 *namespace++ = '\0';
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 crc = strtoul(line, &d, 16);
2404 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2405 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002406 mod = find_module(modname);
2407 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002408 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002409 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002411 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002412 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002413 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002414 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002416 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return;
2418fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002419 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 fatal("parse error in symbol dump file\n");
2421}
2422
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002423static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 struct buffer buf = { };
2426 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002427 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 int n;
2429
2430 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2431 symbol = symbolhash[n];
2432 while (symbol) {
Masahiro Yamada69bc8d32021-03-26 03:54:09 +09002433 if (!symbol->module->from_dump) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002434 namespace = symbol->namespace;
2435 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2436 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002437 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002438 export_str(symbol->export),
2439 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 symbol = symbol->next;
2442 }
2443 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002444 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002445 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002448static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002449{
2450 struct module *mod;
2451 struct namespace_list *ns;
2452 struct buffer ns_deps_buf = {};
2453
2454 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002455
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002456 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002457 continue;
2458
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002459 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002460
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002461 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2462 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002463
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002464 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002465 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002466
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002467 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002468 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002469}
2470
Masahiro Yamada79247992020-06-01 14:57:07 +09002471struct dump_list {
2472 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002473 const char *file;
2474};
2475
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002476int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct module *mod;
2479 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002480 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302481 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002483 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002484 struct dump_list *dump_read_start = NULL;
2485 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Will McVicker4b9c11a2020-11-19 13:46:37 -08002487 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:v:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002488 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002489 case 'e':
2490 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002491 break;
2492 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002493 *dump_read_iter =
2494 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2495 (*dump_read_iter)->file = optarg;
2496 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002497 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002498 case 'm':
2499 modversions = 1;
2500 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302501 case 'n':
2502 ignore_missing_files = 1;
2503 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002504 case 'o':
2505 dump_write = optarg;
2506 break;
2507 case 'a':
2508 all_versions = 1;
2509 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302510 case 'T':
2511 files_source = optarg;
2512 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002513 case 'w':
2514 warn_unresolved = 1;
2515 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302516 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002517 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302518 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002519 case 'N':
2520 allow_missing_ns_imports = 1;
2521 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002522 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002523 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002524 break;
Will McVicker4b9c11a2020-11-19 13:46:37 -08002525 case 'v':
2526 strncpy(module_scmversion, optarg, sizeof(module_scmversion) - 1);
2527 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002528 default:
2529 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 }
2532
Masahiro Yamada79247992020-06-01 14:57:07 +09002533 while (dump_read_start) {
2534 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002535
Masahiro Yamada79247992020-06-01 14:57:07 +09002536 read_dump(dump_read_start->file);
2537 tmp = dump_read_start->next;
2538 free(dump_read_start);
2539 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002542 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Rusty Russell712f9b42013-04-04 17:37:38 +10302545 if (files_source)
2546 read_symbols_from_files(files_source);
2547
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002548 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302549 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002550
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002551 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002552 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 buf.pos = 0;
2555
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002556 check_modname_len(mod);
2557 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002560 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002561 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002562 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002563 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002564 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 add_moddevtable(&buf, mod);
2566 add_srcversion(&buf, mod);
Will McVicker4b9c11a2020-11-19 13:46:37 -08002567 add_scmversion(&buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 sprintf(fname, "%s.mod.c", mod->name);
2570 write_if_changed(&buf, fname);
2571 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002572
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002573 if (missing_namespace_deps)
2574 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (dump_write)
2577 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002578 if (sec_mismatch_count && !sec_mismatch_warn_only)
2579 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002580 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002581 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002582 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002583
Masahiro Yamada47346e92019-09-24 21:07:40 +09002584 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002585 if (s->is_static)
Greg Kroah-Hartman09059c62021-02-02 11:31:33 +01002586 error("\"%s\" [%s] is a static %s\n",
2587 s->name, s->module->name,
2588 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002589 }
2590 }
2591
Masahiro Yamada4475dff2021-03-26 03:54:11 +09002592 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2593 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2594 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2595
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002596 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002598 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}