blob: 973b5e5ae2dddc955b01bab3fff585d58421dbea [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>
Masahiro Yamadaa89227d2022-05-30 18:01:39 +090016#include <fnmatch.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080017#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070019#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103020#include <limits.h>
Masahiro Yamadae54dd932021-08-28 18:50:59 +090021#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093022#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020024#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000025
Masahiro Yamada481461f2023-07-16 19:15:54 +090026static bool module_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Are we using CONFIG_MODVERSIONS? */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090028static bool modversions;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090030static bool all_versions;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090032static bool external_module;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090034static bool warn_unresolved;
Masahiro Yamada2a66c312022-05-09 04:06:19 +090035
Masahiro Yamadab5f1a522022-04-05 20:33:53 +090036static int sec_mismatch_count;
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090037static bool sec_mismatch_warn_only = true;
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +090038/* Trim EXPORT_SYMBOLs that are unused by in-tree modules */
39static bool trim_unused_exports;
40
Guenter Roeckeed380f2013-09-23 15:23:54 +093041/* ignore missing files */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090042static bool ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010043/* If set to 1, only warn (instead of error) about missing ns imports */
Masahiro Yamada58e01fc2022-05-01 17:40:07 +090044static bool allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010045
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090046static bool error_occurred;
47
Masahiro Yamada20ff3682023-06-06 18:41:59 +090048static bool extra_warn;
49
Masahiro Yamada4475dff2021-03-26 03:54:11 +090050/*
51 * Cut off the warnings when there are too many. This typically occurs when
52 * vmlinux is missing. ('make modules' without building vmlinux.)
53 */
54#define MAX_UNRESOLVED_REPORTS 10
55static unsigned int nr_unresolved;
56
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080057/* In kernel, this size is defined in linux/module.h;
58 * here we use Elf_Addr instead of long for covering cross-compile
59 */
60
61#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
62
Jessica Yu93c95e52020-03-06 17:02:05 +010063void __attribute__((format(printf, 2, 3)))
64modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 va_list arglist;
67
Jessica Yu93c95e52020-03-06 17:02:05 +010068 switch (loglevel) {
69 case LOG_WARN:
70 fprintf(stderr, "WARNING: ");
71 break;
72 case LOG_ERROR:
73 fprintf(stderr, "ERROR: ");
74 break;
75 case LOG_FATAL:
76 fprintf(stderr, "FATAL: ");
77 break;
78 default: /* invalid loglevel, ignore */
79 break;
80 }
81
82 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 va_start(arglist, fmt);
85 vfprintf(stderr, fmt, arglist);
86 va_end(arglist);
87
Jessica Yu93c95e52020-03-06 17:02:05 +010088 if (loglevel == LOG_FATAL)
89 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090090 if (loglevel == LOG_ERROR)
91 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060092}
93
Masahiro Yamadae54dd932021-08-28 18:50:59 +090094static inline bool strends(const char *str, const char *postfix)
95{
96 if (strlen(str) < strlen(postfix))
97 return false;
98
99 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void *do_nofail(void *ptr, const char *expr)
103{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100104 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100105 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return ptr;
108}
109
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900110char *read_text_file(const char *filename)
111{
112 struct stat st;
113 size_t nbytes;
114 int fd;
115 char *buf;
116
117 fd = open(filename, O_RDONLY);
118 if (fd < 0) {
119 perror(filename);
120 exit(1);
121 }
122
123 if (fstat(fd, &st) < 0) {
124 perror(filename);
125 exit(1);
126 }
127
128 buf = NOFAIL(malloc(st.st_size + 1));
129
130 nbytes = st.st_size;
131
132 while (nbytes) {
133 ssize_t bytes_read;
134
135 bytes_read = read(fd, buf, nbytes);
136 if (bytes_read < 0) {
137 perror(filename);
138 exit(1);
139 }
140
141 nbytes -= bytes_read;
142 }
143 buf[st.st_size] = '\0';
144
145 close(fd);
146
147 return buf;
148}
149
150char *get_line(char **stringp)
151{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200152 char *orig = *stringp, *next;
153
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900154 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200155 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900156 return NULL;
157
Wolfram Sang6020db52020-07-26 23:44:19 +0200158 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200159 next = strchr(orig, '\n');
160 if (next)
161 *next++ = '\0';
162
163 *stringp = next;
164
165 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* A list of all modules we processed */
Masahiro Yamada325eba052022-05-01 17:40:10 +0900169LIST_HEAD(modules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Masahiro Yamada8b185742018-05-09 18:50:40 +0900171static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct module *mod;
174
Masahiro Yamada325eba052022-05-01 17:40:10 +0900175 list_for_each_entry(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (strcmp(mod->name, modname) == 0)
Masahiro Yamada325eba052022-05-01 17:40:10 +0900177 return mod;
178 }
179 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Masahiro Yamada8c9ce892022-05-30 18:01:38 +0900182static struct module *new_module(const char *name, size_t namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100185
Masahiro Yamada8c9ce892022-05-30 18:01:38 +0900186 mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Masahiro Yamadaf8415362022-05-01 17:40:15 +0900189 INIT_LIST_HEAD(&mod->exported_symbols);
Masahiro Yamada8a691522022-05-01 17:40:12 +0900190 INIT_LIST_HEAD(&mod->unresolved_symbols);
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900191 INIT_LIST_HEAD(&mod->missing_namespaces);
192 INIT_LIST_HEAD(&mod->imported_namespaces);
Masahiro Yamada8a691522022-05-01 17:40:12 +0900193
Masahiro Yamada8c9ce892022-05-30 18:01:38 +0900194 memcpy(mod->name, name, namelen);
195 mod->name[namelen] = '\0';
196 mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
Masahiro Yamada50667432022-05-01 17:40:08 +0900197
198 /*
199 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
200 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
201 * modpost will exit wiht error anyway.
202 */
203 mod->is_gpl_compatible = true;
204
Masahiro Yamada325eba052022-05-01 17:40:10 +0900205 list_add_tail(&mod->list, &modules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 return mod;
208}
209
210/* A hash of all exported symbols,
211 * struct symbol is also used for lists of unresolved symbols */
212
213#define SYMBOL_HASH_SIZE 1024
214
215struct symbol {
216 struct symbol *next;
Masahiro Yamadaf8415362022-05-01 17:40:15 +0900217 struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct module *module;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900219 char *namespace;
Masahiro Yamada58e01fc2022-05-01 17:40:07 +0900220 unsigned int crc;
221 bool crc_valid;
222 bool weak;
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +0900223 bool is_func;
Masahiro Yamada2a66c312022-05-09 04:06:19 +0900224 bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +0900225 bool used; /* there exists a user of this symbol */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500226 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
230
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +0530231/* This is based on the hash algorithm from gdbm, via tdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static inline unsigned int tdb_hash(const char *name)
233{
234 unsigned value; /* Used to compute the hash value. */
235 unsigned i; /* Used to cycle through random values. */
236
237 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100238 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
240
241 return (1103515243 * value + 12345);
242}
243
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100244/**
245 * Allocate a new symbols for use in the hash of exported symbols or
246 * the list of unresolved symbols per module
247 **/
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900248static struct symbol *alloc_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
251
252 memset(s, 0, sizeof(*s));
253 strcpy(s->name, name);
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return s;
256}
257
258/* For the hash of exported symbols */
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900259static void hash_add_symbol(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900263 hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
264 sym->next = symbolhash[hash];
265 symbolhash[hash] = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Masahiro Yamadae882e892022-05-01 17:40:11 +0900268static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
269{
Masahiro Yamada8a691522022-05-01 17:40:12 +0900270 struct symbol *sym;
271
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900272 sym = alloc_symbol(name);
Masahiro Yamada8a691522022-05-01 17:40:12 +0900273 sym->weak = weak;
274
275 list_add_tail(&sym->list, &mod->unresolved_symbols);
Masahiro Yamadae882e892022-05-01 17:40:11 +0900276}
277
Masahiro Yamada69c4cc992022-05-12 01:45:04 +0900278static struct symbol *sym_find_with_module(const char *name, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct symbol *s;
281
282 /* For our purposes, .foo matches foo. PPC64 needs this. */
283 if (name[0] == '.')
284 name++;
285
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100286 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Masahiro Yamada69c4cc992022-05-12 01:45:04 +0900287 if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return s;
289 }
290 return NULL;
291}
292
Masahiro Yamada69c4cc992022-05-12 01:45:04 +0900293static struct symbol *find_symbol(const char *name)
294{
295 return sym_find_with_module(name, NULL);
296}
297
Masahiro Yamada70ddb482022-04-25 04:07:56 +0900298struct namespace_list {
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900299 struct list_head list;
Masahiro Yamada70ddb482022-04-25 04:07:56 +0900300 char namespace[];
301};
302
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900303static bool contains_namespace(struct list_head *head, const char *namespace)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100304{
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900305 struct namespace_list *list;
306
Masahiro Yamada700c48b2023-06-12 00:50:56 +0900307 /*
308 * The default namespace is null string "", which is always implicitly
309 * contained.
310 */
311 if (!namespace[0])
312 return true;
313
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900314 list_for_each_entry(list, head, list) {
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900315 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100316 return true;
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900317 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100318
319 return false;
320}
321
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900322static void add_namespace(struct list_head *head, const char *namespace)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100323{
324 struct namespace_list *ns_entry;
325
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900326 if (!contains_namespace(head, namespace)) {
327 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100328 strlen(namespace) + 1));
329 strcpy(ns_entry->namespace, namespace);
Masahiro Yamadaab489d62022-05-01 17:40:14 +0900330 list_add_tail(&ns_entry->list, head);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100331 }
332}
333
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900334static void *sym_get_data_by_offset(const struct elf_info *info,
335 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700336{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800337 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900338
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900339 return (void *)info->hdr + sechdr->sh_offset + offset;
340}
341
Masahiro Yamadaabe864b2022-07-20 01:53:00 +0900342void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900343{
344 return sym_get_data_by_offset(info, get_secindex(info, sym),
345 sym->st_value);
346}
347
Masahiro Yamada565587d2020-05-25 14:47:05 +0900348static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
349{
350 return sym_get_data_by_offset(info, info->secindex_strings,
351 sechdr->sh_name);
352}
353
Masahiro Yamada125ed242022-07-31 02:36:34 +0900354static const char *sec_name(const struct elf_info *info, unsigned int secindex)
Masahiro Yamada565587d2020-05-25 14:47:05 +0900355{
Masahiro Yamada125ed242022-07-31 02:36:34 +0900356 /*
357 * If sym->st_shndx is a special section index, there is no
358 * corresponding section header.
359 * Return "" if the index is out of range of info->sechdrs[] array.
360 */
361 if (secindex >= info->num_sections)
362 return "";
363
Masahiro Yamada565587d2020-05-25 14:47:05 +0900364 return sech_name(info, &info->sechdrs[secindex]);
365}
366
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200367#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
368
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100369static struct symbol *sym_add_exported(const char *name, struct module *mod,
Masahiro Yamada6e7611c2023-06-12 00:50:55 +0900370 bool gpl_only, const char *namespace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct symbol *s = find_symbol(name);
373
Masahiro Yamadae76cc482022-05-01 17:40:18 +0900374 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
Masahiro Yamadab84227112022-05-01 17:40:17 +0900375 error("%s: '%s' exported twice. Previous export was in %s%s\n",
376 mod->name, name, s->module->name,
377 s->module->is_vmlinux ? "" : ".ko");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900379
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900380 s = alloc_symbol(name);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900381 s->module = mod;
Masahiro Yamada2a66c312022-05-09 04:06:19 +0900382 s->is_gpl_only = gpl_only;
Masahiro Yamada700c48b2023-06-12 00:50:56 +0900383 s->namespace = NOFAIL(strdup(namespace));
Masahiro Yamadae76cc482022-05-01 17:40:18 +0900384 list_add_tail(&s->list, &mod->exported_symbols);
Masahiro Yamadaf18379a2022-05-01 17:40:19 +0900385 hash_add_symbol(s);
Masahiro Yamadae76cc482022-05-01 17:40:18 +0900386
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100387 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Masahiro Yamadaf292d872022-05-13 20:39:21 +0900390static void sym_set_crc(struct symbol *sym, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100391{
Masahiro Yamadaf292d872022-05-13 20:39:21 +0900392 sym->crc = crc;
393 sym->crc_valid = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900396static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930399 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int fd;
401
402 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930403 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930405 if (fstat(fd, &st))
406 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 *size = st.st_size;
409 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930411failed:
412 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (map == MAP_FAILED)
414 return NULL;
415 return map;
416}
417
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900418static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 munmap(file, size);
421}
422
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100423static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100426 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 Elf_Shdr *sechdrs;
428 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200429 const char *secstrings;
430 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 hdr = grab_file(filename, &info->size);
433 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930434 if (ignore_missing_files) {
435 fprintf(stderr, "%s: %s (ignored)\n", filename,
436 strerror(errno));
437 return 0;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200440 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100443 if (info->size < sizeof(*hdr)) {
444 /* file too small, assume this is an empty .o file */
445 return 0;
446 }
447 /* Is this a valid ELF file? */
448 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
449 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
450 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
451 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
452 /* Not an ELF file - silently ignore it */
453 return 0;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200456 hdr->e_type = TO_NATIVE(hdr->e_type);
457 hdr->e_machine = TO_NATIVE(hdr->e_machine);
458 hdr->e_version = TO_NATIVE(hdr->e_version);
459 hdr->e_entry = TO_NATIVE(hdr->e_entry);
460 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
461 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
462 hdr->e_flags = TO_NATIVE(hdr->e_flags);
463 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
464 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
465 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
466 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
467 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
468 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sechdrs = (void *)hdr + hdr->e_shoff;
470 info->sechdrs = sechdrs;
471
Masahiro Yamada5764f662022-07-20 01:52:59 +0900472 /* modpost only works for relocatable objects */
473 if (hdr->e_type != ET_REL)
474 fatal("%s: not relocatable object.", filename);
475
Petr Stetiara83710e2007-08-27 12:15:07 +0200476 /* Check if file offset is correct */
477 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900478 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
479 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200480 return 0;
481 }
482
Anders Kaseorg6845756b2011-05-19 16:55:27 -0600483 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200484 /*
485 * There are more than 64k sections,
486 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200487 */
488 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
489 }
490 else {
491 info->num_sections = hdr->e_shnum;
492 }
493 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg6845756b2011-05-19 16:55:27 -0600494 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200495 }
496 else {
497 info->secindex_strings = hdr->e_shstrndx;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200501 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200502 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
503 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
504 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
505 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
506 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
507 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
508 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
509 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
510 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
511 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200514 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
515 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700516 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900517 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Tejun Heo56fc82c2009-02-06 00:48:02 +0900519 if (!nobits && sechdrs[i].sh_offset > info->size) {
Geert Uytterhoeven0d2573a2022-11-09 14:30:55 +0100520 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
521 filename, (unsigned long)sechdrs[i].sh_offset,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100522 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100523 return 0;
524 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700525 secname = secstrings + sechdrs[i].sh_name;
526 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900527 if (nobits)
528 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
530 info->modinfo_len = sechdrs[i].sh_size;
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +0900531 } else if (!strcmp(secname, ".export_symbol")) {
532 info->export_symbol_secndx = i;
Masahiro Yamada7ce3e412022-04-05 20:33:51 +0900533 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700534
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200535 if (sechdrs[i].sh_type == SHT_SYMTAB) {
536 unsigned int sh_link_idx;
537 symtab_idx = i;
538 info->symtab_start = (void *)hdr +
539 sechdrs[i].sh_offset;
540 info->symtab_stop = (void *)hdr +
541 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg6845756b2011-05-19 16:55:27 -0600542 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200543 info->strtab = (void *)hdr +
544 sechdrs[sh_link_idx].sh_offset;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200547 /* 32bit section no. table? ("more than 64k sections") */
548 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
549 symtab_shndx_idx = i;
550 info->symtab_shndx_start = (void *)hdr +
551 sechdrs[i].sh_offset;
552 info->symtab_shndx_stop = (void *)hdr +
553 sechdrs[i].sh_offset + sechdrs[i].sh_size;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100556 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100557 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Fix endianness in symbols */
560 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
561 sym->st_shndx = TO_NATIVE(sym->st_shndx);
562 sym->st_name = TO_NATIVE(sym->st_name);
563 sym->st_value = TO_NATIVE(sym->st_value);
564 sym->st_size = TO_NATIVE(sym->st_size);
565 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200566
567 if (symtab_shndx_idx != ~0U) {
568 Elf32_Word *p;
Anders Kaseorg6845756b2011-05-19 16:55:27 -0600569 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200570 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg6845756b2011-05-19 16:55:27 -0600571 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200572 symtab_idx);
573 /* Fix endianness */
574 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
575 p++)
576 *p = TO_NATIVE(*p);
577 }
578
Jack Brennen40745322023-09-26 08:40:44 -0400579 symsearch_init(info);
580
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100581 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100584static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jack Brennen40745322023-09-26 08:40:44 -0400586 symsearch_finish(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 release_file(info->hdr, info->size);
588}
589
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200590static int ignore_undef_symbol(struct elf_info *info, const char *symname)
591{
592 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900593 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200594 return 1;
595 /* ignore global offset table */
596 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
597 return 1;
598 if (info->hdr->e_machine == EM_PPC)
599 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900600 if (strstarts(symname, "_restgpr_") ||
601 strstarts(symname, "_savegpr_") ||
602 strstarts(symname, "_rest32gpr_") ||
603 strstarts(symname, "_save32gpr_") ||
604 strstarts(symname, "_restvr_") ||
605 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200606 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000607 if (info->hdr->e_machine == EM_PPC64)
608 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900609 if (strstarts(symname, "_restgpr0_") ||
610 strstarts(symname, "_savegpr0_") ||
611 strstarts(symname, "_restvr_") ||
612 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100613 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000614 return 1;
Vasily Gorbik1d2ad082022-03-06 20:56:07 +0100615
616 if (info->hdr->e_machine == EM_S390)
617 /* Expoline thunks are linked on all kernel modules during final link of .ko */
618 if (strstarts(symname, "__s390_indirect_jump_r"))
619 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200620 /* Do not ignore this symbol */
621 return 0;
622}
623
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900624static void handle_symbol(struct module *mod, struct elf_info *info,
625 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 switch (sym->st_shndx) {
628 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900629 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100630 /* Should warn here, but modpost runs before the linker */
631 } else
632 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 case SHN_UNDEF:
635 /* undefined symbol */
636 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
637 ELF_ST_BIND(sym->st_info) != STB_WEAK)
638 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200639 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (info->hdr->e_machine == EM_SPARC ||
642 info->hdr->e_machine == EM_SPARCV9) {
643 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700644 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100646 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700647 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100648 munged[0] = '_';
649 munged[1] = toupper(munged[1]);
650 symname = munged;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100653
Masahiro Yamadae882e892022-05-01 17:40:11 +0900654 sym_add_unresolved(symname, mod,
655 ELF_ST_BIND(sym->st_info) == STB_WEAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
657 default:
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900658 if (strcmp(symname, "init_module") == 0)
Masahiro Yamada58e01fc2022-05-01 17:40:07 +0900659 mod->has_init = true;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900660 if (strcmp(symname, "cleanup_module") == 0)
Masahiro Yamada58e01fc2022-05-01 17:40:07 +0900661 mod->has_cleanup = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 }
664}
665
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100666/**
667 * Parse tag=value strings from .modinfo section
668 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static char *next_string(char *string, unsigned long *secsize)
670{
671 /* Skip non-zero chars */
672 while (string[0]) {
673 string++;
674 if ((*secsize)-- <= 1)
675 return NULL;
676 }
677
678 /* Skip any zero padding. */
679 while (!string[0]) {
680 string++;
681 if ((*secsize)-- <= 1)
682 return NULL;
683 }
684 return string;
685}
686
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900687static char *get_next_modinfo(struct elf_info *info, const char *tag,
688 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 char *p;
691 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900692 char *modinfo = info->modinfo;
693 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900695 if (prev) {
696 size -= prev - modinfo;
697 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 for (p = modinfo; p; p = next_string(p, &size)) {
701 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
702 return p + taglen + 1;
703 }
704 return NULL;
705}
706
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900707static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200708
709{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900710 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200711}
712
Sam Ravnborgff13f922008-01-23 19:54:27 +0100713static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
714{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100715 if (sym)
716 return elf->strtab + sym->st_name;
717 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100718 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100719}
720
Masahiro Yamadaa89227d2022-05-30 18:01:39 +0900721/*
722 * Check whether the 'string' argument matches one of the 'patterns',
723 * an array of shell wildcard patterns (glob).
724 *
725 * Return true is there is a match.
Sam Ravnborg10668222008-01-13 22:21:31 +0100726 */
Masahiro Yamadaa89227d2022-05-30 18:01:39 +0900727static bool match(const char *string, const char *const patterns[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100728{
Masahiro Yamadaa89227d2022-05-30 18:01:39 +0900729 const char *pattern;
Mark Rutland4d94f912022-03-08 22:56:13 +0100730
Masahiro Yamadaa89227d2022-05-30 18:01:39 +0900731 while ((pattern = *patterns++)) {
732 if (!fnmatch(pattern, string, 0))
733 return true;
Sam Ravnborg10668222008-01-13 22:21:31 +0100734 }
Masahiro Yamadaa89227d2022-05-30 18:01:39 +0900735
736 return false;
Sam Ravnborg10668222008-01-13 22:21:31 +0100737}
738
Masahiro Yamada7452dd22022-08-01 18:39:00 +0900739/* useful to pass patterns to match() directly */
740#define PATTERNS(...) \
741 ({ \
742 static const char *const patterns[] = {__VA_ARGS__, NULL}; \
743 patterns; \
744 })
745
Sam Ravnborg10668222008-01-13 22:21:31 +0100746/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930747static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200748{
749 ".comment*",
750 ".debug*",
H.J. Lu11215842010-12-15 17:11:22 -0800751 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000752 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200753 ".mdebug*", /* alpha, score, mips etc. */
754 ".pdr", /* alpha, score, mips etc. */
755 ".stab*",
756 ".note*",
757 ".got*",
758 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400759 ".xt.prop", /* xtensa */
760 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030761 ".arcextmap*", /* arc */
762 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030763 ".cmem*", /* EZchip */
764 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100765 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600766 ".discard.*",
Denis Nikitin1ef061a2023-08-25 00:27:43 -0700767 ".llvm.call-graph-profile", /* call graph */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200768 NULL
769};
Sam Ravnborg10668222008-01-13 22:21:31 +0100770
Sam Ravnborge241a632008-01-28 20:13:13 +0100771/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400772 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100773 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400774 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100775 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400776static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900777 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100778{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400779 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100780
Anders Kaseorgb614a692009-04-23 16:49:33 -0400781 if (sechdr->sh_type == SHT_PROGBITS &&
782 !(sechdr->sh_flags & SHF_ALLOC) &&
783 !match(sec, section_white_list)) {
784 warn("%s (%s): unexpected non-allocatable section.\n"
785 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
786 "Note that for example <linux/init.h> contains\n"
787 "section definitions for use in .S files.\n\n",
788 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100789 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100790}
791
792
793
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100794#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930795 ".init.setup", ".init.rodata", ".meminit.rodata", \
796 ".init.data", ".meminit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100797
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200798#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930799 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
800 ".pci_fixup_enable", ".pci_fixup_resume", \
801 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200802
Masahiro Yamada473a45b2023-10-23 02:06:08 +0900803#define ALL_XXXINIT_SECTIONS ".meminit.*"
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100804
805#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
Masahiro Yamada48cd8df2023-10-23 02:06:09 +0900806#define ALL_EXIT_SECTIONS ".exit.*"
Sam Ravnborg10668222008-01-13 22:21:31 +0100807
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930808#define DATA_SECTIONS ".data", ".data.rel"
Nathan Chancellor19331e82022-12-13 11:35:29 -0700809#define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100810 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930811#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Nathan Chancellor19331e82022-12-13 11:35:29 -0700812 ".fixup", ".entry.text", ".exception.text", \
Christophe Leroy1e688dd22021-04-13 16:38:10 +0000813 ".coldtext", ".softirqentry.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100814
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000815#define INIT_SECTIONS ".init.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100816
Masahiro Yamada34fcf232023-10-23 02:06:13 +0900817#define ALL_TEXT_SECTIONS ".init.text", ".meminit.text", ".exit.text", \
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930818 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
819
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100820enum mismatch {
Masahiro Yamadab3d4f442023-10-23 02:06:12 +0900821 TEXTDATA_TO_ANY_INIT_EXIT,
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100822 XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100823 ANY_INIT_TO_ANY_EXIT,
824 ANY_EXIT_TO_ANY_INIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930825 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100826};
827
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930828/**
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +0530829 * Describe how to match sections on different criteria:
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930830 *
831 * @fromsec: Array of sections to be matched.
832 *
833 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
834 * this array is forbidden (black-list). Can be empty.
835 *
836 * @good_tosec: Relocations applied to a section in @fromsec must be
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +0530837 * targeting sections in this array (white-list). Can be empty.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930838 *
839 * @mismatch: Type of mismatch.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930840 */
Sam Ravnborg10668222008-01-13 22:21:31 +0100841struct sectioncheck {
842 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930843 const char *bad_tosec[20];
844 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100845 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100846};
847
Mathias Krause7a3ee752014-08-27 20:28:53 +0930848static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +0100849/* Do not reference init/exit code/data from
850 * normal code and data
851 */
852{
Masahiro Yamadaabc23972023-05-22 01:04:22 +0900853 .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
Masahiro Yamadab3d4f442023-10-23 02:06:12 +0900854 .bad_tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
855 .mismatch = TEXTDATA_TO_ANY_INIT_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100856},
Paul Gortmakere24f6622013-06-19 19:30:48 -0400857/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100858{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100859 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930860 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100861 .mismatch = XXXINIT_TO_SOME_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100862},
Sam Ravnborg10668222008-01-13 22:21:31 +0100863/* Do not use exit code/data from init code */
864{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100865 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930866 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100867 .mismatch = ANY_INIT_TO_ANY_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100868},
869/* Do not use init code/data from exit code */
870{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100871 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930872 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100873 .mismatch = ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100874},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200875{
876 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930877 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200878 .mismatch = ANY_INIT_TO_ANY_EXIT,
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200879},
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930880{
881 .fromsec = { "__ex_table", NULL },
882 /* If you're adding any new black-listed sections in here, consider
883 * adding a special 'printer' for them in scripts/check_extable.
884 */
885 .bad_tosec = { ".altinstr_replacement", NULL },
886 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
887 .mismatch = EXTABLE_TO_NON_TEXT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100888}
889};
890
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +0100891static const struct sectioncheck *section_mismatch(
892 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +0100893{
894 int i;
Sam Ravnborg10668222008-01-13 22:21:31 +0100895
Quentin Casasnovasc5c34392015-04-16 13:16:41 +0930896 /*
897 * The target section could be the SHT_NUL section when we're
898 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +0000899 * doesn't make much sense and causes build failures on parisc
900 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +0930901 */
902 if (*tosec == '\0')
903 return NULL;
904
Masahiro Yamadac5c468d2022-05-24 01:46:25 +0900905 for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
906 const struct sectioncheck *check = &sectioncheck[i];
907
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930908 if (match(fromsec, check->fromsec)) {
909 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
910 return check;
911 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
912 return check;
913 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100914 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +0100915 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +0100916}
917
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100918/**
919 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200920 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100921 * Pattern 1:
922 * If a module parameter is declared __initdata and permissions=0
923 * then this is legal despite the warning generated.
924 * We cannot see value of permissions here, so just ignore
925 * this pattern.
926 * The pattern is identified by:
927 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100928 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100929 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100930 *
Rusty Russell6a841522010-08-11 23:04:16 -0600931 * Pattern 1a:
932 * module_param_call() ops can refer to __init set function if permissions=0
933 * The pattern is identified by:
934 * tosec = .init.text
935 * fromsec = .data*
936 * atsym = __param_ops_*
937 *
Vivek Goyalee6a8542007-01-11 01:52:44 +0100938 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +0200939 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100940 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200941 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100942 * Some symbols belong to init section but still it is ok to reference
943 * these from non-init sections as these symbols don't have any memory
944 * allocated for them and symbol address and value are same. So even
945 * if init section is freed, its ok to reference those symbols.
946 * For ex. symbols marking the init section boundaries.
947 * This pattern is identified by
948 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100949 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930950 * Pattern 5:
951 * GCC may optimize static inlines when fed constant arg(s) resulting
952 * in functions like cpumask_empty() -- generating an associated symbol
953 * cpumask_empty.constprop.3 that appears in the audit. If the const that
954 * is passed in comes from __init, like say nmi_ipi_mask, we get a
955 * meaningless section warning. May need to add isra symbols too...
956 * This pattern is identified by
957 * tosec = init section
958 * fromsec = text section
959 * refsymname = *.constprop.*
960 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100961 **/
Masahiro Yamada05bb0702023-05-22 01:04:09 +0900962static int secref_whitelist(const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100963 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100964{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100965 /* Check for pattern 1 */
Masahiro Yamada1df380f2023-05-22 01:04:23 +0900966 if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) &&
967 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900968 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100969 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100970
Rusty Russell6a841522010-08-11 23:04:16 -0600971 /* Check for pattern 1a */
972 if (strcmp(tosec, ".init.text") == 0 &&
Masahiro Yamada1df380f2023-05-22 01:04:23 +0900973 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900974 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -0600975 return 0;
976
Masahiro Yamada672fb672022-08-01 18:39:02 +0900977 /* symbols in data sections that may refer to any init/exit sections */
978 if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
979 match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
Masahiro Yamadae1dc1bf2023-10-23 02:06:07 +0900980 match(fromsym, PATTERNS("*_ops", "*_probe", "*_console")))
Masahiro Yamada672fb672022-08-01 18:39:02 +0900981 return 0;
982
Uwe Kleine-Königf177cd02023-09-30 18:52:04 +0200983 /*
984 * symbols in data sections must not refer to .exit.*, but there are
985 * quite a few offenders, so hide these unless for W=1 builds until
986 * these are fixed.
987 */
988 if (!extra_warn &&
989 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
Masahiro Yamada48cd8df2023-10-23 02:06:09 +0900990 match(tosec, PATTERNS(ALL_EXIT_SECTIONS)) &&
Masahiro Yamada672fb672022-08-01 18:39:02 +0900991 match(fromsym, PATTERNS("*driver")))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100992 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100993
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100994 /* Check for pattern 3 */
Masahiro Yamada1df380f2023-05-22 01:04:23 +0900995 if (strstarts(fromsec, ".head.text") &&
996 match(tosec, PATTERNS(ALL_INIT_SECTIONS)))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100997 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100998
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200999 /* Check for pattern 4 */
Masahiro Yamada1df380f2023-05-22 01:04:23 +09001000 if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext")))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001001 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001002
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301003 /* Check for pattern 5 */
Masahiro Yamada1df380f2023-05-22 01:04:23 +09001004 if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
1005 match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
1006 match(fromsym, PATTERNS("*.constprop.*")))
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001007 return 0;
1008
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001009 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001010}
1011
Masahiro Yamadab1a96512023-06-01 21:09:58 +09001012static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr,
1013 unsigned int secndx)
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001014{
Jack Brennen40745322023-09-26 08:40:44 -04001015 return symsearch_find_nearest(elf, addr, secndx, false, ~0);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001016}
1017
Masahiro Yamadab1a96512023-06-01 21:09:58 +09001018static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301019{
Masahiro Yamadab1a96512023-06-01 21:09:58 +09001020 /* If the supplied symbol has a valid name, return it */
1021 if (is_valid_name(elf, sym))
1022 return sym;
1023
1024 /*
1025 * Strive to find a better symbol name, but the resulting name may not
1026 * match the symbol referenced in the original code.
1027 */
Jack Brennen40745322023-09-26 08:40:44 -04001028 return symsearch_find_nearest(elf, addr, get_secindex(elf, sym),
1029 true, 20);
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301030}
1031
Masahiro Yamadaf4c35482023-05-15 00:27:24 +09001032static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001033{
Dan Carpenter3a3f1e52023-06-08 11:23:40 +03001034 if (secndx >= elf->num_sections)
Masahiro Yamadaf4c35482023-05-15 00:27:24 +09001035 return false;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001036
Masahiro Yamadaf4c35482023-05-15 00:27:24 +09001037 return (elf->sechdrs[secndx].sh_flags & SHF_EXECINSTR) != 0;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001038}
1039
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301040static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1041 const struct sectioncheck* const mismatch,
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001042 Elf_Sym *tsym,
1043 unsigned int fsecndx, const char *fromsec, Elf_Addr faddr,
1044 const char *tosec, Elf_Addr taddr)
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301045{
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301046 Elf_Sym *from;
1047 const char *tosym;
1048 const char *fromsym;
1049
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001050 from = find_fromsym(elf, faddr, fsecndx);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301051 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301052
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001053 tsym = find_tosym(elf, taddr, tsym);
Masahiro Yamadaa23e7582023-05-22 01:04:11 +09001054 tosym = sym_name(elf, tsym);
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301055
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301056 /* check whitelist - we may ignore it */
Masahiro Yamada05bb0702023-05-22 01:04:09 +09001057 if (!secref_whitelist(fromsec, fromsym, tosec, tosym))
Masahiro Yamadafc5fa8622023-05-15 00:27:23 +09001058 return;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301059
1060 sec_mismatch_count++;
1061
Masahiro Yamadaf2346272023-06-12 00:50:59 +09001062 warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n",
1063 modname, fromsym, (unsigned int)(faddr - from->st_value), fromsec, tosym, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301064
Masahiro Yamada78dac1a2023-06-12 00:50:58 +09001065 if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
Masahiro Yamada856567d2023-05-15 00:27:25 +09001066 if (match(tosec, mismatch->bad_tosec))
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301067 fatal("The relocation at %s+0x%lx references\n"
Masahiro Yamada856567d2023-05-15 00:27:25 +09001068 "section \"%s\" which is black-listed.\n"
1069 "Something is seriously wrong and should be fixed.\n"
1070 "You might get more information about where this is\n"
1071 "coming from by using scripts/check_extable.sh %s\n",
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001072 fromsec, (long)faddr, tosec, modname);
Masahiro Yamadaa23e7582023-05-22 01:04:11 +09001073 else if (is_executable_section(elf, get_secindex(elf, tsym)))
Masahiro Yamada856567d2023-05-15 00:27:25 +09001074 warn("The relocation at %s+0x%lx references\n"
1075 "section \"%s\" which is not in the list of\n"
1076 "authorized sections. If you're adding a new section\n"
1077 "and/or if this reference is valid, add \"%s\" to the\n"
1078 "list of authorized sections to jump to on fault.\n"
1079 "This can be achieved by adding \"%s\" to\n"
1080 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001081 fromsec, (long)faddr, tosec, tosec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301082 else
Masahiro Yamada856567d2023-05-15 00:27:25 +09001083 error("%s+0x%lx references non-executable section '%s'\n",
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001084 fromsec, (long)faddr, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301085 }
1086}
1087
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001088static void check_export_symbol(struct module *mod, struct elf_info *elf,
1089 Elf_Addr faddr, const char *secname,
1090 Elf_Sym *sym)
1091{
1092 static const char *prefix = "__export_symbol_";
1093 const char *label_name, *name, *data;
1094 Elf_Sym *label;
1095 struct symbol *s;
1096 bool is_gpl;
1097
1098 label = find_fromsym(elf, faddr, elf->export_symbol_secndx);
1099 label_name = sym_name(elf, label);
1100
1101 if (!strstarts(label_name, prefix)) {
1102 error("%s: .export_symbol section contains strange symbol '%s'\n",
1103 mod->name, label_name);
1104 return;
1105 }
1106
Masahiro Yamada6d62b1c2023-06-12 00:50:54 +09001107 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
1108 ELF_ST_BIND(sym->st_info) != STB_WEAK) {
1109 error("%s: local symbol '%s' was exported\n", mod->name,
1110 label_name + strlen(prefix));
1111 return;
1112 }
1113
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001114 name = sym_name(elf, sym);
1115 if (strcmp(label_name + strlen(prefix), name)) {
1116 error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",
1117 mod->name, name);
1118 return;
1119 }
1120
1121 data = sym_get_data(elf, label); /* license */
1122 if (!strcmp(data, "GPL")) {
1123 is_gpl = true;
1124 } else if (!strcmp(data, "")) {
1125 is_gpl = false;
1126 } else {
1127 error("%s: unknown license '%s' was specified for '%s'\n",
1128 mod->name, data, name);
1129 return;
1130 }
1131
1132 data += strlen(data) + 1; /* namespace */
Masahiro Yamada6e7611c2023-06-12 00:50:55 +09001133 s = sym_add_exported(name, mod, is_gpl, data);
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001134
1135 /*
1136 * We need to be aware whether we are exporting a function or
1137 * a data on some architectures.
1138 */
1139 s->is_func = (ELF_ST_TYPE(sym->st_info) == STT_FUNC);
1140
Masahiro Yamada08700ec2023-09-06 03:46:57 +09001141 /*
1142 * For parisc64, symbols prefixed $$ from the library have the symbol type
1143 * STT_LOPROC. They should be handled as functions too.
1144 */
1145 if (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64 &&
1146 elf->hdr->e_machine == EM_PARISC &&
1147 ELF_ST_TYPE(sym->st_info) == STT_LOPROC)
1148 s->is_func = true;
1149
Masahiro Yamadaa3df1522023-10-23 02:06:10 +09001150 if (match(secname, PATTERNS(ALL_INIT_SECTIONS)))
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001151 warn("%s: %s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_SYMBOL.\n",
1152 mod->name, name);
Masahiro Yamada48cd8df2023-10-23 02:06:09 +09001153 else if (match(secname, PATTERNS(ALL_EXIT_SECTIONS)))
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001154 warn("%s: %s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_SYMBOL.\n",
1155 mod->name, name);
1156}
1157
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001158static void check_section_mismatch(struct module *mod, struct elf_info *elf,
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001159 Elf_Sym *sym,
1160 unsigned int fsecndx, const char *fromsec,
1161 Elf_Addr faddr, Elf_Addr taddr)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001162{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001163 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001164 const struct sectioncheck *mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001165
Masahiro Yamada481461f2023-07-16 19:15:54 +09001166 if (module_enabled && elf->export_symbol_secndx == fsecndx) {
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001167 check_export_symbol(mod, elf, faddr, tosec, sym);
1168 return;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001169 }
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001170
1171 mismatch = section_mismatch(fromsec, tosec);
Masahiro Yamada856567d2023-05-15 00:27:25 +09001172 if (!mismatch)
1173 return;
1174
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001175 default_mismatch_handler(mod->name, elf, mismatch, sym,
Masahiro Yamada04ed3b42023-05-22 01:04:12 +09001176 fsecndx, fromsec, faddr,
1177 tosec, taddr);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001178}
1179
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001180static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001181{
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001182 switch (r_type) {
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001183 case R_386_32:
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001184 return TO_NATIVE(*location);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001185 case R_386_PC32:
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001186 return TO_NATIVE(*location) + 4;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001187 }
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001188
1189 return (Elf_Addr)(-1);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001190}
1191
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001192#ifndef R_ARM_CALL
1193#define R_ARM_CALL 28
1194#endif
1195#ifndef R_ARM_JUMP24
1196#define R_ARM_JUMP24 29
1197#endif
1198
David A. Longc9698e52014-02-14 22:41:18 +01001199#ifndef R_ARM_THM_CALL
1200#define R_ARM_THM_CALL 10
1201#endif
1202#ifndef R_ARM_THM_JUMP24
1203#define R_ARM_THM_JUMP24 30
1204#endif
Masahiro Yamadaf5983da2023-06-28 01:32:05 +09001205
1206#ifndef R_ARM_MOVW_ABS_NC
1207#define R_ARM_MOVW_ABS_NC 43
1208#endif
1209
1210#ifndef R_ARM_MOVT_ABS
1211#define R_ARM_MOVT_ABS 44
1212#endif
1213
1214#ifndef R_ARM_THM_MOVW_ABS_NC
1215#define R_ARM_THM_MOVW_ABS_NC 47
1216#endif
1217
1218#ifndef R_ARM_THM_MOVT_ABS
1219#define R_ARM_THM_MOVT_ABS 48
1220#endif
1221
David A. Longc9698e52014-02-14 22:41:18 +01001222#ifndef R_ARM_THM_JUMP19
1223#define R_ARM_THM_JUMP19 51
1224#endif
1225
Masahiro Yamada56a24b82023-06-01 21:09:56 +09001226static int32_t sign_extend32(int32_t value, int index)
1227{
1228 uint8_t shift = 31 - index;
1229
1230 return (int32_t)(value << shift) >> shift;
1231}
1232
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001233static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001234{
Masahiro Yamada3310bae2023-06-01 21:10:00 +09001235 uint32_t inst, upper, lower, sign, j1, j2;
Masahiro Yamada56a24b82023-06-01 21:09:56 +09001236 int32_t offset;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001237
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001238 switch (r_type) {
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001239 case R_ARM_ABS32:
Masahiro Yamada2cb74942023-06-01 21:10:01 +09001240 case R_ARM_REL32:
Masahiro Yamadab7c63520f2023-06-01 21:09:55 +09001241 inst = TO_NATIVE(*(uint32_t *)loc);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001242 return inst + sym->st_value;
Masahiro Yamada12ca2c62023-06-01 21:09:57 +09001243 case R_ARM_MOVW_ABS_NC:
1244 case R_ARM_MOVT_ABS:
1245 inst = TO_NATIVE(*(uint32_t *)loc);
1246 offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff),
1247 15);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001248 return offset + sym->st_value;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001249 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001250 case R_ARM_CALL:
1251 case R_ARM_JUMP24:
Masahiro Yamada56a24b82023-06-01 21:09:56 +09001252 inst = TO_NATIVE(*(uint32_t *)loc);
1253 offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001254 return offset + sym->st_value + 8;
Masahiro Yamadacd1824f2023-06-01 21:09:59 +09001255 case R_ARM_THM_MOVW_ABS_NC:
1256 case R_ARM_THM_MOVT_ABS:
1257 upper = TO_NATIVE(*(uint16_t *)loc);
1258 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1259 offset = sign_extend32(((upper & 0x000f) << 12) |
1260 ((upper & 0x0400) << 1) |
1261 ((lower & 0x7000) >> 4) |
1262 (lower & 0x00ff),
1263 15);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001264 return offset + sym->st_value;
Masahiro Yamada3310bae2023-06-01 21:10:00 +09001265 case R_ARM_THM_JUMP19:
1266 /*
1267 * Encoding T3:
1268 * S = upper[10]
1269 * imm6 = upper[5:0]
1270 * J1 = lower[13]
1271 * J2 = lower[11]
1272 * imm11 = lower[10:0]
1273 * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
1274 */
1275 upper = TO_NATIVE(*(uint16_t *)loc);
1276 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1277
1278 sign = (upper >> 10) & 1;
1279 j1 = (lower >> 13) & 1;
1280 j2 = (lower >> 11) & 1;
1281 offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) |
1282 ((upper & 0x03f) << 12) |
1283 ((lower & 0x07ff) << 1),
1284 20);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001285 return offset + sym->st_value + 4;
David A. Longc9698e52014-02-14 22:41:18 +01001286 case R_ARM_THM_CALL:
1287 case R_ARM_THM_JUMP24:
Masahiro Yamada3310bae2023-06-01 21:10:00 +09001288 /*
1289 * Encoding T4:
1290 * S = upper[10]
1291 * imm10 = upper[9:0]
1292 * J1 = lower[13]
1293 * J2 = lower[11]
1294 * imm11 = lower[10:0]
1295 * I1 = NOT(J1 XOR S)
1296 * I2 = NOT(J2 XOR S)
1297 * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
1298 */
1299 upper = TO_NATIVE(*(uint16_t *)loc);
1300 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1301
1302 sign = (upper >> 10) & 1;
1303 j1 = (lower >> 13) & 1;
1304 j2 = (lower >> 11) & 1;
1305 offset = sign_extend32((sign << 24) |
1306 ((~(j1 ^ sign) & 1) << 23) |
1307 ((~(j2 ^ sign) & 1) << 22) |
1308 ((upper & 0x03ff) << 12) |
1309 ((lower & 0x07ff) << 1),
1310 24);
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001311 return offset + sym->st_value + 4;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001312 }
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001313
1314 return (Elf_Addr)(-1);
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001315}
1316
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001317static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001318{
Masahiro Yamadab31db652023-06-20 21:05:19 +09001319 uint32_t inst;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001320
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001321 inst = TO_NATIVE(*location);
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001322 switch (r_type) {
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001323 case R_MIPS_LO16:
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001324 return inst & 0xffff;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001325 case R_MIPS_26:
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001326 return (inst & 0x03ffffff) << 2;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001327 case R_MIPS_32:
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001328 return inst;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001329 }
Masahiro Yamadaa68914a2023-07-23 19:04:43 +09001330 return (Elf_Addr)(-1);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001331}
1332
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001333#ifndef EM_RISCV
1334#define EM_RISCV 243
1335#endif
1336
1337#ifndef R_RISCV_SUB32
1338#define R_RISCV_SUB32 39
1339#endif
1340
Youling Tang3d36f422022-12-10 22:39:48 +08001341#ifndef EM_LOONGARCH
1342#define EM_LOONGARCH 258
1343#endif
1344
1345#ifndef R_LARCH_SUB32
1346#define R_LARCH_SUB32 55
1347#endif
1348
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001349static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
1350 unsigned int *r_type, unsigned int *r_sym)
1351{
1352 typedef struct {
1353 Elf64_Word r_sym; /* Symbol index */
1354 unsigned char r_ssym; /* Special symbol for 2nd relocation */
1355 unsigned char r_type3; /* 3rd relocation type */
1356 unsigned char r_type2; /* 2nd relocation type */
1357 unsigned char r_type; /* 1st relocation type */
1358 } Elf64_Mips_R_Info;
1359
1360 bool is_64bit = (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64);
1361
1362 if (elf->hdr->e_machine == EM_MIPS && is_64bit) {
1363 Elf64_Mips_R_Info *mips64_r_info = (void *)&r_info;
1364
1365 *r_type = mips64_r_info->r_type;
1366 *r_sym = TO_NATIVE(mips64_r_info->r_sym);
1367 return;
1368 }
1369
Masahiro Yamadabd78c9d2023-10-08 02:04:46 +09001370 if (is_64bit)
1371 r_info = TO_NATIVE((Elf64_Xword)r_info);
1372 else
1373 r_info = TO_NATIVE((Elf32_Word)r_info);
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001374
1375 *r_type = ELF_R_TYPE(r_info);
1376 *r_sym = ELF_R_SYM(r_info);
1377}
1378
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001379static void section_rela(struct module *mod, struct elf_info *elf,
Masahiro Yamada77f9f572023-10-08 02:04:48 +09001380 unsigned int fsecndx, const char *fromsec,
1381 const Elf_Rela *start, const Elf_Rela *stop)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001382{
Masahiro Yamada77f9f572023-10-08 02:04:48 +09001383 const Elf_Rela *rela;
Sam Ravnborge241a632008-01-28 20:13:13 +01001384
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001385 for (rela = start; rela < stop; rela++) {
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001386 Elf_Addr taddr, r_offset;
1387 unsigned int r_type, r_sym;
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001388
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001389 r_offset = TO_NATIVE(rela->r_offset);
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001390 get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
1391
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001392 taddr = TO_NATIVE(rela->r_addend);
1393
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001394 switch (elf->hdr->e_machine) {
1395 case EM_RISCV:
1396 if (!strcmp("__ex_table", fromsec) &&
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001397 r_type == R_RISCV_SUB32)
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001398 continue;
1399 break;
Youling Tang3d36f422022-12-10 22:39:48 +08001400 case EM_LOONGARCH:
1401 if (!strcmp("__ex_table", fromsec) &&
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001402 r_type == R_LARCH_SUB32)
Youling Tang3d36f422022-12-10 22:39:48 +08001403 continue;
1404 break;
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001405 }
Masahiro Yamadaa9bb3e52023-05-22 01:04:13 +09001406
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001407 check_section_mismatch(mod, elf, elf->symtab_start + r_sym,
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001408 fsecndx, fromsec, r_offset, taddr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001409 }
1410}
1411
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001412static void section_rel(struct module *mod, struct elf_info *elf,
Masahiro Yamada77f9f572023-10-08 02:04:48 +09001413 unsigned int fsecndx, const char *fromsec,
1414 const Elf_Rel *start, const Elf_Rel *stop)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001415{
Masahiro Yamada77f9f572023-10-08 02:04:48 +09001416 const Elf_Rel *rel;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001417
1418 for (rel = start; rel < stop; rel++) {
Masahiro Yamada8aa00e22023-06-20 21:05:20 +09001419 Elf_Sym *tsym;
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001420 Elf_Addr taddr = 0, r_offset;
1421 unsigned int r_type, r_sym;
Masahiro Yamadab31db652023-06-20 21:05:19 +09001422 void *loc;
1423
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001424 r_offset = TO_NATIVE(rel->r_offset);
Masahiro Yamada4732acb2023-07-23 19:04:45 +09001425 get_rel_type_and_sym(elf, rel->r_info, &r_type, &r_sym);
Masahiro Yamadab31db652023-06-20 21:05:19 +09001426
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001427 loc = sym_get_data_by_offset(elf, fsecndx, r_offset);
Masahiro Yamada8e86ebe2023-06-20 21:05:21 +09001428 tsym = elf->symtab_start + r_sym;
Masahiro Yamadab31db652023-06-20 21:05:19 +09001429
Sam Ravnborgff13f922008-01-23 19:54:27 +01001430 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001431 case EM_386:
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001432 taddr = addend_386_rel(loc, r_type);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001433 break;
1434 case EM_ARM:
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001435 taddr = addend_arm_rel(loc, tsym, r_type);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001436 break;
1437 case EM_MIPS:
Masahiro Yamada71d965c2023-07-23 19:04:44 +09001438 taddr = addend_mips_rel(loc, r_type);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001439 break;
Masahiro Yamada64f14042023-05-12 01:24:22 +09001440 default:
1441 fatal("Please add code to calculate addend for this architecture\n");
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001442 }
Masahiro Yamadaa9bb3e52023-05-22 01:04:13 +09001443
Masahiro Yamada8aa00e22023-06-20 21:05:20 +09001444 check_section_mismatch(mod, elf, tsym,
Masahiro Yamada77f39e92023-07-23 19:04:46 +09001445 fsecndx, fromsec, r_offset, taddr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001446 }
1447}
1448
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001449/**
1450 * A module includes a number of sections that are discarded
1451 * either when loaded or when used as built-in.
1452 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001453 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001454 * Likewise for modules used built-in the sections marked __exit
1455 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001456 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001457 * The check_sec_ref() function traverses all relocation records
1458 * to find all references to a section that reference a section that will
1459 * be discarded and warns about it.
1460 **/
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001461static void check_sec_ref(struct module *mod, struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001462{
1463 int i;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001464
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001465 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001466 for (i = 0; i < elf->num_sections; i++) {
Masahiro Yamada29ae5c02023-10-08 02:04:47 +09001467 Elf_Shdr *sechdr = &elf->sechdrs[i];
1468
1469 check_section(mod->name, elf, sechdr);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001470 /* We want to process only relocation sections and not .init */
Masahiro Yamada77f9f572023-10-08 02:04:48 +09001471 if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
1472 /* section to which the relocation applies */
1473 unsigned int secndx = sechdr->sh_info;
1474 const char *secname = sec_name(elf, secndx);
1475 const void *start, *stop;
1476
1477 /* If the section is known good, skip it */
1478 if (match(secname, section_white_list))
1479 continue;
1480
1481 start = sym_get_data_by_offset(elf, i, 0);
1482 stop = start + sechdr->sh_size;
1483
1484 if (sechdr->sh_type == SHT_RELA)
1485 section_rela(mod, elf, secndx, secname,
1486 start, stop);
1487 else
1488 section_rel(mod, elf, secndx, secname,
1489 start, stop);
1490 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001491 }
1492}
1493
Andi Kleen7d02b492014-02-08 09:01:12 +01001494static char *remove_dot(char *s)
1495{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301496 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001497
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301498 if (n && s[n]) {
1499 size_t m = strspn(s + n + 1, "0123456789");
Alexander Lobakinb5beffa2022-05-24 17:27:18 +02001500 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001501 s[n] = 0;
1502 }
1503 return s;
1504}
1505
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001506/*
1507 * The CRCs are recorded in .*.cmd files in the form of:
1508 * #SYMVER <name> <crc>
1509 */
1510static void extract_crcs_for_object(const char *object, struct module *mod)
1511{
1512 char cmd_file[PATH_MAX];
1513 char *buf, *p;
1514 const char *base;
1515 int dirlen, ret;
1516
1517 base = strrchr(object, '/');
1518 if (base) {
1519 base++;
1520 dirlen = base - object;
1521 } else {
1522 dirlen = 0;
1523 base = object;
1524 }
1525
1526 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
1527 dirlen, object, base);
1528 if (ret >= sizeof(cmd_file)) {
1529 error("%s: too long path was truncated\n", cmd_file);
1530 return;
1531 }
1532
1533 buf = read_text_file(cmd_file);
1534 p = buf;
1535
1536 while ((p = strstr(p, "\n#SYMVER "))) {
1537 char *name;
1538 size_t namelen;
1539 unsigned int crc;
1540 struct symbol *sym;
1541
1542 name = p + strlen("\n#SYMVER ");
1543
1544 p = strchr(name, ' ');
1545 if (!p)
1546 break;
1547
1548 namelen = p - name;
1549 p++;
1550
1551 if (!isdigit(*p))
1552 continue; /* skip this line */
1553
Ben Hutchingsfb27e702023-03-22 19:11:45 +01001554 crc = strtoul(p, &p, 0);
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001555 if (*p != '\n')
1556 continue; /* skip this line */
1557
1558 name[namelen] = '\0';
1559
1560 /*
1561 * sym_find_with_module() may return NULL here.
1562 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
1563 * Since commit e1327a127703, genksyms calculates CRCs of all
1564 * symbols, including trimmed ones. Ignore orphan CRCs.
1565 */
1566 sym = sym_find_with_module(name, mod);
1567 if (sym)
1568 sym_set_crc(sym, crc);
1569 }
1570
1571 free(buf);
1572}
1573
1574/*
1575 * The symbol versions (CRC) are recorded in the .*.cmd files.
1576 * Parse them to retrieve CRCs for the current module.
1577 */
1578static void mod_set_crcs(struct module *mod)
1579{
1580 char objlist[PATH_MAX];
1581 char *buf, *p, *obj;
1582 int ret;
1583
1584 if (mod->is_vmlinux) {
1585 strcpy(objlist, ".vmlinux.objs");
1586 } else {
1587 /* objects for a module are listed in the *.mod file. */
1588 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
1589 if (ret >= sizeof(objlist)) {
1590 error("%s: too long path was truncated\n", objlist);
1591 return;
1592 }
1593 }
1594
1595 buf = read_text_file(objlist);
1596 p = buf;
1597
1598 while ((obj = strsep(&p, "\n")) && obj[0])
1599 extract_crcs_for_object(obj, mod);
1600
1601 free(buf);
1602}
1603
Masahiro Yamada8b185742018-05-09 18:50:40 +09001604static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 const char *symname;
1607 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001608 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001609 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct module *mod;
1611 struct elf_info info = { };
1612 Elf_Sym *sym;
1613
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001614 if (!parse_elf(&info, modname))
1615 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Masahiro Yamada8c9ce892022-05-30 18:01:38 +09001617 if (!strends(modname, ".o")) {
1618 error("%s: filename must be suffixed with .o\n", modname);
1619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621
Masahiro Yamada8c9ce892022-05-30 18:01:38 +09001622 /* strip trailing .o */
1623 mod = new_module(modname, strlen(modname) - strlen(".o"));
1624
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001625 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001626 license = get_modinfo(&info, "license");
1627 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09001628 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001629 while (license) {
Masahiro Yamada50667432022-05-01 17:40:08 +09001630 if (!license_is_gpl_compatible(license)) {
1631 mod->is_gpl_compatible = false;
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001632 break;
1633 }
1634 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001635 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001636
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001637 namespace = get_modinfo(&info, "import_ns");
1638 while (namespace) {
1639 add_namespace(&mod->imported_namespaces, namespace);
1640 namespace = get_next_modinfo(&info, "import_ns",
1641 namespace);
1642 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001643 }
1644
Vincenzo Palazzo1fffe7a2023-06-16 01:40:37 +02001645 if (extra_warn && !get_modinfo(&info, "description"))
1646 warn("missing MODULE_DESCRIPTION() in %s\n", modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01001648 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09001650 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 handle_moddevtable(mod, &info, sym, symname);
1652 }
Denis Efremov15bfc232019-08-01 09:06:57 +03001653
Masahiro Yamada94d6cb62023-06-12 00:50:51 +09001654 check_sec_ref(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001656 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001657 version = get_modinfo(&info, "version");
1658 if (version || all_versions)
Masahiro Yamadae54dd932021-08-28 18:50:59 +09001659 get_src_version(mod->name, mod->srcversion,
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001660 sizeof(mod->srcversion) - 1);
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 parse_elf_finish(&info);
1664
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001665 if (modversions) {
1666 /*
1667 * Our trick to get versioning for module struct etc. - it's
1668 * never passed as an argument to an exported function, so
1669 * the automatic versioning doesn't pick it up, but it's really
1670 * important anyhow.
1671 */
Masahiro Yamadae882e892022-05-01 17:40:11 +09001672 sym_add_unresolved("module_layout", mod, false);
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001673
1674 mod_set_crcs(mod);
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Rusty Russell712f9b42013-04-04 17:37:38 +10301678static void read_symbols_from_files(const char *filename)
1679{
1680 FILE *in = stdin;
1681 char fname[PATH_MAX];
1682
Masahiro Yamadaf65a4862022-12-11 22:04:07 +09001683 in = fopen(filename, "r");
1684 if (!in)
1685 fatal("Can't open filenames file %s: %m", filename);
Rusty Russell712f9b42013-04-04 17:37:38 +10301686
1687 while (fgets(fname, PATH_MAX, in) != NULL) {
1688 if (strends(fname, "\n"))
1689 fname[strlen(fname)-1] = '\0';
1690 read_symbols(fname);
1691 }
1692
Masahiro Yamadaf65a4862022-12-11 22:04:07 +09001693 fclose(in);
Rusty Russell712f9b42013-04-04 17:37:38 +10301694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696#define SZ 500
1697
1698/* We first write the generated file into memory using the
1699 * following helper, then compare to the file on disk and
1700 * only update the later if anything changed */
1701
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001702void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1703 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 char tmp[SZ];
1706 int len;
1707 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 va_start(ap, fmt);
1710 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08001711 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 va_end(ap);
1713}
1714
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001715void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08001718 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001719 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 strncpy(buf->p + buf->pos, s, len);
1722 buf->pos += len;
1723}
1724
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09001725static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001726{
1727 struct symbol *s, *exp;
1728
Masahiro Yamada8a691522022-05-01 17:40:12 +09001729 list_for_each_entry(s, &mod->unresolved_symbols, list) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001730 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001731 exp = find_symbol(s->name);
Masahiro Yamada23beb442022-04-25 04:07:48 +09001732 if (!exp) {
Masahiro Yamada4475dff2021-03-26 03:54:11 +09001733 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
Jessica Yu93c95e52020-03-06 17:02:05 +01001734 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
1735 "\"%s\" [%s.ko] undefined!\n",
1736 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001737 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09001738 }
Masahiro Yamada23beb442022-04-25 04:07:48 +09001739 if (exp->module == mod) {
1740 error("\"%s\" [%s.ko] was exported without definition\n",
1741 s->name, mod->name);
1742 continue;
1743 }
Masahiro Yamada4cae77a2022-04-25 04:07:49 +09001744
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09001745 exp->used = true;
Masahiro Yamada4cae77a2022-04-25 04:07:49 +09001746 s->module = exp->module;
1747 s->crc_valid = exp->crc_valid;
1748 s->crc = exp->crc;
1749
Andrew Morton6449bd62006-06-09 20:45:06 -07001750 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001751 if (basename)
1752 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001753 else
1754 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001755
Masahiro Yamada700c48b2023-06-12 00:50:56 +09001756 if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01001757 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
1758 "module %s uses symbol %s from namespace %s, but does not import it.\n",
1759 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09001760 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001761 }
1762
Masahiro Yamada2a66c312022-05-09 04:06:19 +09001763 if (!mod->is_gpl_compatible && exp->is_gpl_only)
1764 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
1765 basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001766 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001767}
1768
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09001769static void handle_white_list_exports(const char *white_list)
1770{
1771 char *buf, *p, *name;
1772
1773 buf = read_text_file(white_list);
1774 p = buf;
1775
1776 while ((name = strsep(&p, "\n"))) {
1777 struct symbol *sym = find_symbol(name);
1778
1779 if (sym)
1780 sym->used = true;
1781 }
1782
1783 free(buf);
1784}
1785
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09001786static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08001787{
1788 const char *mod_name;
1789
1790 mod_name = strrchr(mod->name, '/');
1791 if (mod_name == NULL)
1792 mod_name = mod->name;
1793 else
1794 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09001795 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09001796 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08001797}
1798
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001799/**
1800 * Header for the generated file
1801 **/
1802static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00001805 /*
1806 * Include build-salt.h after module.h in order to
1807 * inherit the definitions.
1808 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03001809 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00001810 buf_printf(b, "#include <linux/build-salt.h>\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07001811 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
Masahiro Yamada7b453712022-05-13 20:39:22 +09001812 buf_printf(b, "#include <linux/export-internal.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 buf_printf(b, "#include <linux/vermagic.h>\n");
1814 buf_printf(b, "#include <linux/compiler.h>\n");
1815 buf_printf(b, "\n");
Omar Sandovalb9f174c2023-06-13 14:14:56 -07001816 buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n");
1817 buf_printf(b, "#include <asm/orc_header.h>\n");
1818 buf_printf(b, "ORC_HEADER;\n");
1819 buf_printf(b, "#endif\n");
1820 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07001821 buf_printf(b, "BUILD_SALT;\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07001822 buf_printf(b, "BUILD_LTO_INFO;\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07001823 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07001825 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10301827 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07001828 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001829 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001831 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (mod->has_cleanup)
1833 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001834 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001836 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 buf_printf(b, "};\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Masahiro Yamada7fedac92022-05-05 16:22:32 +09001839 if (!external_module)
Ben Hutchings2449b8b2011-10-24 15:12:28 +02001840 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
Ben Hutchings2449b8b2011-10-24 15:12:28 +02001841
Masahiro Yamada7fedac92022-05-05 16:22:32 +09001842 buf_printf(b,
1843 "\n"
1844 "#ifdef CONFIG_RETPOLINE\n"
1845 "MODULE_INFO(retpoline, \"Y\");\n"
1846 "#endif\n");
Andi Kleencaf75012018-01-25 15:50:28 -08001847
Masahiro Yamada7fedac92022-05-05 16:22:32 +09001848 if (strstarts(mod->name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001849 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
David Gow74829dd2022-07-08 12:48:45 +08001850
1851 if (strstarts(mod->name, "tools/testing"))
1852 buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001853}
1854
Masahiro Yamada7b453712022-05-13 20:39:22 +09001855static void add_exported_symbols(struct buffer *buf, struct module *mod)
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001856{
1857 struct symbol *sym;
1858
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001859 /* generate struct for exported symbols */
1860 buf_printf(buf, "\n");
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09001861 list_for_each_entry(sym, &mod->exported_symbols, list) {
1862 if (trim_unused_exports && !sym->used)
1863 continue;
1864
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001865 buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n",
1866 sym->is_func ? "FUNC" : "DATA", sym->name,
Masahiro Yamada700c48b2023-06-12 00:50:56 +09001867 sym->is_gpl_only ? "_gpl" : "", sym->namespace);
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09001868 }
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +09001869
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001870 if (!modversions)
1871 return;
1872
Masahiro Yamada7b453712022-05-13 20:39:22 +09001873 /* record CRCs for exported symbols */
1874 buf_printf(buf, "\n");
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001875 list_for_each_entry(sym, &mod->exported_symbols, list) {
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09001876 if (trim_unused_exports && !sym->used)
1877 continue;
1878
Masahiro Yamada5b8a9a82022-08-09 23:11:17 +09001879 if (!sym->crc_valid)
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001880 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
1881 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
1882 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
1883 sym->name);
Masahiro Yamada7b453712022-05-13 20:39:22 +09001884
1885 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
1886 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
Masahiro Yamadaf292d872022-05-13 20:39:21 +09001887 }
1888}
1889
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001890/**
1891 * Record CRCs for unresolved symbols
1892 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09001893static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Masahiro Yamada4cae77a2022-04-25 04:07:49 +09001895 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09001898 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 buf_printf(b, "\n");
1901 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07001902 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Masahiro Yamada8a691522022-05-01 17:40:12 +09001904 list_for_each_entry(s, &mod->unresolved_symbols, list) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001905 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001908 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 s->name, mod->name);
1910 continue;
1911 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09301912 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09001913 error("too long symbol \"%s\" [%s.ko]\n",
1914 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09301915 break;
1916 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09001917 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10301918 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 buf_printf(b, "};\n");
1922}
1923
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09001924static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int first = 1;
1928
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09001929 /* Clear ->seen flag of modules that own symbols needed by this. */
Masahiro Yamada8a691522022-05-01 17:40:12 +09001930 list_for_each_entry(s, &mod->unresolved_symbols, list) {
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09001931 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001932 s->module->seen = s->module->is_vmlinux;
Masahiro Yamada8a691522022-05-01 17:40:12 +09001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09001936 buf_printf(b, "MODULE_INFO(depends, \"");
Masahiro Yamada8a691522022-05-01 17:40:12 +09001937 list_for_each_entry(s, &mod->unresolved_symbols, list) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001938 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!s->module)
1940 continue;
1941
1942 if (s->module->seen)
1943 continue;
1944
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09001945 s->module->seen = true;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001946 p = strrchr(s->module->name, '/');
1947 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001948 p++;
1949 else
1950 p = s->module->name;
1951 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 first = 0;
1953 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09001954 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001957static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 if (mod->srcversion[0]) {
1960 buf_printf(b, "\n");
1961 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1962 mod->srcversion);
1963 }
1964}
1965
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09001966static void write_buf(struct buffer *b, const char *fname)
1967{
1968 FILE *file;
1969
Masahiro Yamadac155a472022-04-25 04:07:46 +09001970 if (error_occurred)
1971 return;
1972
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09001973 file = fopen(fname, "w");
1974 if (!file) {
1975 perror(fname);
1976 exit(1);
1977 }
1978 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1979 perror(fname);
1980 exit(1);
1981 }
1982 if (fclose(file) != 0) {
1983 perror(fname);
1984 exit(1);
1985 }
1986}
1987
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001988static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 char *tmp;
1991 FILE *file;
1992 struct stat st;
1993
1994 file = fopen(fname, "r");
1995 if (!file)
1996 goto write;
1997
1998 if (fstat(fileno(file), &st) < 0)
1999 goto close_write;
2000
2001 if (st.st_size != b->pos)
2002 goto close_write;
2003
2004 tmp = NOFAIL(malloc(b->pos));
2005 if (fread(tmp, 1, b->pos, file) != b->pos)
2006 goto free_write;
2007
2008 if (memcmp(tmp, b->p, b->pos) != 0)
2009 goto free_write;
2010
2011 free(tmp);
2012 fclose(file);
2013 return;
2014
2015 free_write:
2016 free(tmp);
2017 close_write:
2018 fclose(file);
2019 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002020 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Masahiro Yamada7b453712022-05-13 20:39:22 +09002023static void write_vmlinux_export_c_file(struct module *mod)
2024{
2025 struct buffer buf = { };
2026
2027 buf_printf(&buf,
2028 "#include <linux/export-internal.h>\n");
2029
2030 add_exported_symbols(&buf, mod);
2031 write_if_changed(&buf, ".vmlinux.export.c");
2032 free(buf.p);
2033}
2034
Masahiro Yamadaa44abac2022-05-05 16:22:33 +09002035/* do sanity checks, and generate *.mod.c file */
2036static void write_mod_c_file(struct module *mod)
2037{
2038 struct buffer buf = { };
2039 char fname[PATH_MAX];
2040 int ret;
2041
Masahiro Yamadaa44abac2022-05-05 16:22:33 +09002042 add_header(&buf, mod);
Masahiro Yamada7b453712022-05-13 20:39:22 +09002043 add_exported_symbols(&buf, mod);
Masahiro Yamadaa44abac2022-05-05 16:22:33 +09002044 add_versions(&buf, mod);
2045 add_depends(&buf, mod);
2046 add_moddevtable(&buf, mod);
2047 add_srcversion(&buf, mod);
2048
2049 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2050 if (ret >= sizeof(fname)) {
2051 error("%s: too long path was truncated\n", fname);
2052 goto free;
2053 }
2054
2055 write_if_changed(&buf, fname);
2056
2057free:
2058 free(buf.p);
2059}
2060
Ram Paibd5cbce2006-06-08 22:12:53 -07002061/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002062 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002063 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002064static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002066 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002068 buf = read_text_file(fname);
2069 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* No symbol versions, silently ignore */
2071 return;
2072
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002073 pos = buf;
2074
2075 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002076 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 unsigned int crc;
2078 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002079 struct symbol *s;
Masahiro Yamada2a66c312022-05-09 04:06:19 +09002080 bool gpl_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (!(symname = strchr(line, '\t')))
2083 goto fail;
2084 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002085 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto fail;
2087 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002088 if (!(export = strchr(modname, '\t')))
2089 goto fail;
2090 *export++ = '\0';
2091 if (!(namespace = strchr(export, '\t')))
2092 goto fail;
2093 *namespace++ = '\0';
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 crc = strtoul(line, &d, 16);
2096 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2097 goto fail;
Masahiro Yamada2a66c312022-05-09 04:06:19 +09002098
2099 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2100 gpl_only = true;
2101 } else if (!strcmp(export, "EXPORT_SYMBOL")) {
2102 gpl_only = false;
2103 } else {
2104 error("%s: unknown license %s. skip", symname, export);
2105 continue;
2106 }
2107
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002108 mod = find_module(modname);
2109 if (!mod) {
Masahiro Yamada8c9ce892022-05-30 18:01:38 +09002110 mod = new_module(modname, strlen(modname));
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002111 mod->from_dump = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Masahiro Yamada6e7611c2023-06-12 00:50:55 +09002113 s = sym_add_exported(symname, mod, gpl_only, namespace);
Masahiro Yamadaf292d872022-05-13 20:39:21 +09002114 sym_set_crc(s, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002116 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return;
2118fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002119 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 fatal("parse error in symbol dump file\n");
2121}
2122
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002123static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 struct buffer buf = { };
Masahiro Yamadaf8415362022-05-01 17:40:15 +09002126 struct module *mod;
2127 struct symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Masahiro Yamadaf8415362022-05-01 17:40:15 +09002129 list_for_each_entry(mod, &modules, list) {
2130 if (mod->from_dump)
2131 continue;
2132 list_for_each_entry(sym, &mod->exported_symbols, list) {
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09002133 if (trim_unused_exports && !sym->used)
2134 continue;
2135
Masahiro Yamada2a66c312022-05-09 04:06:19 +09002136 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
Masahiro Yamadaf8415362022-05-01 17:40:15 +09002137 sym->crc, sym->name, mod->name,
Masahiro Yamada2a66c312022-05-09 04:06:19 +09002138 sym->is_gpl_only ? "_GPL" : "",
Masahiro Yamada700c48b2023-06-12 00:50:56 +09002139 sym->namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002142 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002143 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002146static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002147{
2148 struct module *mod;
2149 struct namespace_list *ns;
2150 struct buffer ns_deps_buf = {};
2151
Masahiro Yamada325eba052022-05-01 17:40:10 +09002152 list_for_each_entry(mod, &modules, list) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002153
Masahiro Yamadaab489d62022-05-01 17:40:14 +09002154 if (mod->from_dump || list_empty(&mod->missing_namespaces))
Matthias Maennich1d082772019-09-06 11:32:31 +01002155 continue;
2156
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002157 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002158
Masahiro Yamadaab489d62022-05-01 17:40:14 +09002159 list_for_each_entry(ns, &mod->missing_namespaces, list)
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002160 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002161
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002162 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002163 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002164
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002165 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002166 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002167}
2168
Masahiro Yamada79247992020-06-01 14:57:07 +09002169struct dump_list {
Masahiro Yamada44840542022-05-01 17:40:13 +09002170 struct list_head list;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002171 const char *file;
2172};
2173
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002174int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 struct module *mod;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002177 char *missing_namespace_deps = NULL;
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09002178 char *unused_exports_white_list = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302179 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int opt;
Masahiro Yamada44840542022-05-01 17:40:13 +09002181 LIST_HEAD(dump_lists);
2182 struct dump_list *dl, *dl2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Masahiro Yamada481461f2023-07-16 19:15:54 +09002184 while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002185 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002186 case 'e':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002187 external_module = true;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002188 break;
2189 case 'i':
Masahiro Yamada44840542022-05-01 17:40:13 +09002190 dl = NOFAIL(malloc(sizeof(*dl)));
2191 dl->file = optarg;
2192 list_add_tail(&dl->list, &dump_lists);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002193 break;
Masahiro Yamada481461f2023-07-16 19:15:54 +09002194 case 'M':
2195 module_enabled = true;
2196 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002197 case 'm':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002198 modversions = true;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002199 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302200 case 'n':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002201 ignore_missing_files = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302202 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002203 case 'o':
2204 dump_write = optarg;
2205 break;
2206 case 'a':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002207 all_versions = true;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002208 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302209 case 'T':
2210 files_source = optarg;
2211 break;
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09002212 case 't':
2213 trim_unused_exports = true;
2214 break;
2215 case 'u':
2216 unused_exports_white_list = optarg;
2217 break;
Masahiro Yamada20ff3682023-06-06 18:41:59 +09002218 case 'W':
2219 extra_warn = true;
2220 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002221 case 'w':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002222 warn_unresolved = true;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002223 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302224 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002225 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302226 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002227 case 'N':
Masahiro Yamada58e01fc2022-05-01 17:40:07 +09002228 allow_missing_ns_imports = true;
Jessica Yu54b77842020-03-06 17:02:06 +01002229 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002230 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002231 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002232 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002233 default:
2234 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
2237
Masahiro Yamada44840542022-05-01 17:40:13 +09002238 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2239 read_dump(dl->file);
2240 list_del(&dl->list);
2241 free(dl);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002244 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Rusty Russell712f9b42013-04-04 17:37:38 +10302247 if (files_source)
2248 read_symbols_from_files(files_source);
2249
Masahiro Yamada325eba052022-05-01 17:40:10 +09002250 list_for_each_entry(mod, &modules, list) {
Masahiro Yamada5e9e95cc92023-06-12 00:50:57 +09002251 if (mod->from_dump || mod->is_vmlinux)
2252 continue;
2253
2254 check_modname_len(mod);
2255 check_exports(mod);
2256 }
2257
2258 if (unused_exports_white_list)
2259 handle_white_list_exports(unused_exports_white_list);
2260
2261 list_for_each_entry(mod, &modules, list) {
Masahiro Yamadaa44abac2022-05-05 16:22:33 +09002262 if (mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002263 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Masahiro Yamada7b453712022-05-13 20:39:22 +09002265 if (mod->is_vmlinux)
2266 write_vmlinux_export_c_file(mod);
2267 else
Masahiro Yamadaa44abac2022-05-05 16:22:33 +09002268 write_mod_c_file(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002270
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002271 if (missing_namespace_deps)
2272 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (dump_write)
2275 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002276 if (sec_mismatch_count && !sec_mismatch_warn_only)
2277 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002278 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002279
Masahiro Yamada4475dff2021-03-26 03:54:11 +09002280 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2281 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2282 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2283
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002284 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}