]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Postprocess module symbol versions |
2 | * | |
3 | * Copyright 2003 Kai Germaschewski | |
4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation | |
df578e7d | 5 | * Copyright 2006-2008 Sam Ravnborg |
1da177e4 LT |
6 | * 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 | ||
b2e3e658 | 14 | #define _GNU_SOURCE |
5370d4ac | 15 | #include <elf.h> |
b2e3e658 | 16 | #include <stdio.h> |
1da177e4 | 17 | #include <ctype.h> |
5003bab8 | 18 | #include <string.h> |
712f9b46 | 19 | #include <limits.h> |
e54dd93a | 20 | #include <stdbool.h> |
eed380f3 | 21 | #include <errno.h> |
1da177e4 | 22 | #include "modpost.h" |
b817f6fe | 23 | #include "../../include/linux/license.h" |
9e1b9b80 | 24 | |
1da177e4 | 25 | /* Are we using CONFIG_MODVERSIONS? */ |
58e01fca | 26 | static bool modversions; |
1da177e4 | 27 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
58e01fca | 28 | static bool all_versions; |
040fcc81 | 29 | /* If we are modposting external module set to 1 */ |
58e01fca | 30 | static bool external_module; |
c53ddacd | 31 | /* Only warn about unresolved symbols */ |
58e01fca | 32 | static bool warn_unresolved; |
2a66c312 | 33 | |
b5f1a52a | 34 | static int sec_mismatch_count; |
58e01fca | 35 | static bool sec_mismatch_warn_only = true; |
eed380f3 | 36 | /* ignore missing files */ |
58e01fca | 37 | static bool ignore_missing_files; |
54b77847 | 38 | /* If set to 1, only warn (instead of error) about missing ns imports */ |
58e01fca | 39 | static bool allow_missing_ns_imports; |
588ccd73 | 40 | |
0fd3fbad MY |
41 | static bool error_occurred; |
42 | ||
4475dff5 MY |
43 | /* |
44 | * Cut off the warnings when there are too many. This typically occurs when | |
45 | * vmlinux is missing. ('make modules' without building vmlinux.) | |
46 | */ | |
47 | #define MAX_UNRESOLVED_REPORTS 10 | |
48 | static unsigned int nr_unresolved; | |
49 | ||
4fd3e4ef WG |
50 | /* In kernel, this size is defined in linux/module.h; |
51 | * here we use Elf_Addr instead of long for covering cross-compile | |
52 | */ | |
53 | ||
54 | #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) | |
55 | ||
93c95e52 JY |
56 | void __attribute__((format(printf, 2, 3))) |
57 | modpost_log(enum loglevel loglevel, const char *fmt, ...) | |
1da177e4 LT |
58 | { |
59 | va_list arglist; | |
60 | ||
93c95e52 JY |
61 | switch (loglevel) { |
62 | case LOG_WARN: | |
63 | fprintf(stderr, "WARNING: "); | |
64 | break; | |
65 | case LOG_ERROR: | |
66 | fprintf(stderr, "ERROR: "); | |
67 | break; | |
68 | case LOG_FATAL: | |
69 | fprintf(stderr, "FATAL: "); | |
70 | break; | |
71 | default: /* invalid loglevel, ignore */ | |
72 | break; | |
73 | } | |
1da177e4 | 74 | |
93c95e52 | 75 | fprintf(stderr, "modpost: "); |
1da177e4 LT |
76 | |
77 | va_start(arglist, fmt); | |
78 | vfprintf(stderr, fmt, arglist); | |
79 | va_end(arglist); | |
2a116659 | 80 | |
93c95e52 JY |
81 | if (loglevel == LOG_FATAL) |
82 | exit(1); | |
0fd3fbad MY |
83 | if (loglevel == LOG_ERROR) |
84 | error_occurred = true; | |
2a116659 MW |
85 | } |
86 | ||
e54dd93a MY |
87 | static inline bool strends(const char *str, const char *postfix) |
88 | { | |
89 | if (strlen(str) < strlen(postfix)) | |
90 | return false; | |
91 | ||
92 | return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; | |
93 | } | |
94 | ||
1da177e4 | 95 | void *do_nofail(void *ptr, const char *expr) |
040fcc81 | 96 | { |
df578e7d | 97 | if (!ptr) |
93c95e52 | 98 | fatal("Memory allocation failure: %s.\n", expr); |
040fcc81 | 99 | |
1da177e4 LT |
100 | return ptr; |
101 | } | |
102 | ||
ac5100f5 MY |
103 | char *read_text_file(const char *filename) |
104 | { | |
105 | struct stat st; | |
106 | size_t nbytes; | |
107 | int fd; | |
108 | char *buf; | |
109 | ||
110 | fd = open(filename, O_RDONLY); | |
111 | if (fd < 0) { | |
112 | perror(filename); | |
113 | exit(1); | |
114 | } | |
040fcc81 | 115 | |
ac5100f5 MY |
116 | if (fstat(fd, &st) < 0) { |
117 | perror(filename); | |
118 | exit(1); | |
119 | } | |
120 | ||
121 | buf = NOFAIL(malloc(st.st_size + 1)); | |
122 | ||
123 | nbytes = st.st_size; | |
124 | ||
125 | while (nbytes) { | |
126 | ssize_t bytes_read; | |
127 | ||
128 | bytes_read = read(fd, buf, nbytes); | |
129 | if (bytes_read < 0) { | |
130 | perror(filename); | |
131 | exit(1); | |
132 | } | |
133 | ||
134 | nbytes -= bytes_read; | |
135 | } | |
136 | buf[st.st_size] = '\0'; | |
137 | ||
138 | close(fd); | |
139 | ||
140 | return buf; | |
040fcc81 SR |
141 | } |
142 | ||
ac5100f5 | 143 | char *get_line(char **stringp) |
1da177e4 | 144 | { |
736bb118 NS |
145 | char *orig = *stringp, *next; |
146 | ||
ac5100f5 | 147 | /* do not return the unwanted extra line at EOF */ |
736bb118 | 148 | if (!orig || *orig == '\0') |
ac5100f5 | 149 | return NULL; |
df578e7d | 150 | |
6020db50 | 151 | /* don't use strsep here, it is not available everywhere */ |
736bb118 NS |
152 | next = strchr(orig, '\n'); |
153 | if (next) | |
154 | *next++ = '\0'; | |
155 | ||
156 | *stringp = next; | |
157 | ||
158 | return orig; | |
1da177e4 LT |
159 | } |
160 | ||
161 | /* A list of all modules we processed */ | |
325eba05 | 162 | LIST_HEAD(modules); |
1da177e4 | 163 | |
8b185743 | 164 | static struct module *find_module(const char *modname) |
1da177e4 LT |
165 | { |
166 | struct module *mod; | |
167 | ||
325eba05 | 168 | list_for_each_entry(mod, &modules, list) { |
1da177e4 | 169 | if (strcmp(mod->name, modname) == 0) |
325eba05 MY |
170 | return mod; |
171 | } | |
172 | return NULL; | |
1da177e4 LT |
173 | } |
174 | ||
d4ef1c30 | 175 | static struct module *new_module(const char *modname) |
1da177e4 LT |
176 | { |
177 | struct module *mod; | |
62070fa4 | 178 | |
a82f794c | 179 | mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1)); |
1da177e4 | 180 | memset(mod, 0, sizeof(*mod)); |
1da177e4 | 181 | |
f841536e | 182 | INIT_LIST_HEAD(&mod->exported_symbols); |
8a69152b | 183 | INIT_LIST_HEAD(&mod->unresolved_symbols); |
ab489d60 MY |
184 | INIT_LIST_HEAD(&mod->missing_namespaces); |
185 | INIT_LIST_HEAD(&mod->imported_namespaces); | |
8a69152b | 186 | |
a82f794c | 187 | strcpy(mod->name, modname); |
4de7b629 | 188 | mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0); |
5066743e MY |
189 | |
190 | /* | |
191 | * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE() | |
192 | * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue | |
193 | * modpost will exit wiht error anyway. | |
194 | */ | |
195 | mod->is_gpl_compatible = true; | |
196 | ||
325eba05 | 197 | list_add_tail(&mod->list, &modules); |
1da177e4 LT |
198 | |
199 | return mod; | |
200 | } | |
201 | ||
202 | /* A hash of all exported symbols, | |
203 | * struct symbol is also used for lists of unresolved symbols */ | |
204 | ||
205 | #define SYMBOL_HASH_SIZE 1024 | |
206 | ||
207 | struct symbol { | |
208 | struct symbol *next; | |
f841536e | 209 | struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */ |
1da177e4 | 210 | struct module *module; |
389eb3f5 | 211 | char *namespace; |
58e01fca MY |
212 | unsigned int crc; |
213 | bool crc_valid; | |
214 | bool weak; | |
215 | bool is_static; /* true if symbol is not global */ | |
2a66c312 | 216 | bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */ |
859c8175 | 217 | char name[]; |
1da177e4 LT |
218 | }; |
219 | ||
220 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; | |
221 | ||
f3945833 | 222 | /* This is based on the hash algorithm from gdbm, via tdb */ |
1da177e4 LT |
223 | static inline unsigned int tdb_hash(const char *name) |
224 | { | |
225 | unsigned value; /* Used to compute the hash value. */ | |
226 | unsigned i; /* Used to cycle through random values. */ | |
227 | ||
228 | /* Set the initial value from the key size. */ | |
df578e7d | 229 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
1da177e4 LT |
230 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
231 | ||
232 | return (1103515243 * value + 12345); | |
233 | } | |
234 | ||
5c3ead8c SR |
235 | /** |
236 | * Allocate a new symbols for use in the hash of exported symbols or | |
237 | * the list of unresolved symbols per module | |
238 | **/ | |
f18379a3 | 239 | static struct symbol *alloc_symbol(const char *name) |
1da177e4 LT |
240 | { |
241 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); | |
242 | ||
243 | memset(s, 0, sizeof(*s)); | |
244 | strcpy(s->name, name); | |
58e01fca | 245 | s->is_static = true; |
1da177e4 LT |
246 | return s; |
247 | } | |
248 | ||
249 | /* For the hash of exported symbols */ | |
f18379a3 | 250 | static void hash_add_symbol(struct symbol *sym) |
1da177e4 LT |
251 | { |
252 | unsigned int hash; | |
1da177e4 | 253 | |
f18379a3 MY |
254 | hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE; |
255 | sym->next = symbolhash[hash]; | |
256 | symbolhash[hash] = sym; | |
1da177e4 LT |
257 | } |
258 | ||
e882e89b MY |
259 | static void sym_add_unresolved(const char *name, struct module *mod, bool weak) |
260 | { | |
8a69152b MY |
261 | struct symbol *sym; |
262 | ||
f18379a3 | 263 | sym = alloc_symbol(name); |
8a69152b MY |
264 | sym->weak = weak; |
265 | ||
266 | list_add_tail(&sym->list, &mod->unresolved_symbols); | |
e882e89b MY |
267 | } |
268 | ||
69c4cc99 | 269 | static struct symbol *sym_find_with_module(const char *name, struct module *mod) |
1da177e4 LT |
270 | { |
271 | struct symbol *s; | |
272 | ||
273 | /* For our purposes, .foo matches foo. PPC64 needs this. */ | |
274 | if (name[0] == '.') | |
275 | name++; | |
276 | ||
df578e7d | 277 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) { |
69c4cc99 | 278 | if (strcmp(s->name, name) == 0 && (!mod || s->module == mod)) |
1da177e4 LT |
279 | return s; |
280 | } | |
281 | return NULL; | |
282 | } | |
283 | ||
69c4cc99 MY |
284 | static struct symbol *find_symbol(const char *name) |
285 | { | |
286 | return sym_find_with_module(name, NULL); | |
287 | } | |
288 | ||
70ddb48d | 289 | struct namespace_list { |
ab489d60 | 290 | struct list_head list; |
70ddb48d MY |
291 | char namespace[]; |
292 | }; | |
293 | ||
ab489d60 | 294 | static bool contains_namespace(struct list_head *head, const char *namespace) |
cb9b55d2 | 295 | { |
ab489d60 MY |
296 | struct namespace_list *list; |
297 | ||
298 | list_for_each_entry(list, head, list) { | |
76b54cf0 | 299 | if (!strcmp(list->namespace, namespace)) |
cb9b55d2 | 300 | return true; |
ab489d60 | 301 | } |
cb9b55d2 MM |
302 | |
303 | return false; | |
304 | } | |
305 | ||
ab489d60 | 306 | static void add_namespace(struct list_head *head, const char *namespace) |
cb9b55d2 MM |
307 | { |
308 | struct namespace_list *ns_entry; | |
309 | ||
ab489d60 MY |
310 | if (!contains_namespace(head, namespace)) { |
311 | ns_entry = NOFAIL(malloc(sizeof(*ns_entry) + | |
cb9b55d2 MM |
312 | strlen(namespace) + 1)); |
313 | strcpy(ns_entry->namespace, namespace); | |
ab489d60 | 314 | list_add_tail(&ns_entry->list, head); |
cb9b55d2 MM |
315 | } |
316 | } | |
317 | ||
d2e4d05c MY |
318 | static void *sym_get_data_by_offset(const struct elf_info *info, |
319 | unsigned int secindex, unsigned long offset) | |
6124c04c | 320 | { |
4b8a5cfb | 321 | Elf_Shdr *sechdr = &info->sechdrs[secindex]; |
6124c04c | 322 | |
afa0459d MY |
323 | if (info->hdr->e_type != ET_REL) |
324 | offset -= sechdr->sh_addr; | |
325 | ||
326 | return (void *)info->hdr + sechdr->sh_offset + offset; | |
6124c04c | 327 | } |
62a26356 | 328 | |
afa0459d MY |
329 | static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym) |
330 | { | |
d2e4d05c MY |
331 | return sym_get_data_by_offset(info, get_secindex(info, sym), |
332 | sym->st_value); | |
333 | } | |
afa0459d | 334 | |
565587d8 MY |
335 | static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr) |
336 | { | |
337 | return sym_get_data_by_offset(info, info->secindex_strings, | |
338 | sechdr->sh_name); | |
339 | } | |
afa0459d | 340 | |
565587d8 MY |
341 | static const char *sec_name(const struct elf_info *info, int secindex) |
342 | { | |
343 | return sech_name(info, &info->sechdrs[secindex]); | |
afa0459d MY |
344 | } |
345 | ||
62a26356 AIB |
346 | #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) |
347 | ||
a2b11184 MM |
348 | static void sym_update_namespace(const char *symname, const char *namespace) |
349 | { | |
350 | struct symbol *s = find_symbol(symname); | |
351 | ||
352 | /* | |
353 | * That symbol should have been created earlier and thus this is | |
354 | * actually an assertion. | |
355 | */ | |
356 | if (!s) { | |
bc72d723 MY |
357 | error("Could not update namespace(%s) for symbol %s\n", |
358 | namespace, symname); | |
a2b11184 MM |
359 | return; |
360 | } | |
361 | ||
362 | free(s->namespace); | |
79f646e8 | 363 | s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL; |
a2b11184 MM |
364 | } |
365 | ||
9ae5bd18 | 366 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
2a66c312 | 367 | bool gpl_only) |
1da177e4 LT |
368 | { |
369 | struct symbol *s = find_symbol(name); | |
370 | ||
e76cc48d | 371 | if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) { |
b8422711 MY |
372 | error("%s: '%s' exported twice. Previous export was in %s%s\n", |
373 | mod->name, name, s->module->name, | |
374 | s->module->is_vmlinux ? "" : ".ko"); | |
1da177e4 | 375 | } |
7ef9ab3b | 376 | |
f18379a3 | 377 | s = alloc_symbol(name); |
7ef9ab3b | 378 | s->module = mod; |
2a66c312 | 379 | s->is_gpl_only = gpl_only; |
e76cc48d | 380 | list_add_tail(&s->list, &mod->exported_symbols); |
f18379a3 | 381 | hash_add_symbol(s); |
e76cc48d | 382 | |
040fcc81 SR |
383 | return s; |
384 | } | |
385 | ||
f292d875 | 386 | static void sym_set_crc(struct symbol *sym, unsigned int crc) |
040fcc81 | 387 | { |
f292d875 MY |
388 | sym->crc = crc; |
389 | sym->crc_valid = true; | |
1da177e4 LT |
390 | } |
391 | ||
3b09efc4 | 392 | static void *grab_file(const char *filename, size_t *size) |
1da177e4 LT |
393 | { |
394 | struct stat st; | |
eb3d5cc6 | 395 | void *map = MAP_FAILED; |
1da177e4 LT |
396 | int fd; |
397 | ||
398 | fd = open(filename, O_RDONLY); | |
eb3d5cc6 | 399 | if (fd < 0) |
1da177e4 | 400 | return NULL; |
eb3d5cc6 JJ |
401 | if (fstat(fd, &st)) |
402 | goto failed; | |
1da177e4 LT |
403 | |
404 | *size = st.st_size; | |
405 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | |
1da177e4 | 406 | |
eb3d5cc6 JJ |
407 | failed: |
408 | close(fd); | |
1da177e4 LT |
409 | if (map == MAP_FAILED) |
410 | return NULL; | |
411 | return map; | |
412 | } | |
413 | ||
3b09efc4 | 414 | static void release_file(void *file, size_t size) |
1da177e4 LT |
415 | { |
416 | munmap(file, size); | |
417 | } | |
418 | ||
85bd2fdd | 419 | static int parse_elf(struct elf_info *info, const char *filename) |
1da177e4 LT |
420 | { |
421 | unsigned int i; | |
85bd2fdd | 422 | Elf_Ehdr *hdr; |
1da177e4 LT |
423 | Elf_Shdr *sechdrs; |
424 | Elf_Sym *sym; | |
1ce53adf DV |
425 | const char *secstrings; |
426 | unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U; | |
1da177e4 LT |
427 | |
428 | hdr = grab_file(filename, &info->size); | |
429 | if (!hdr) { | |
eed380f3 GR |
430 | if (ignore_missing_files) { |
431 | fprintf(stderr, "%s: %s (ignored)\n", filename, | |
432 | strerror(errno)); | |
433 | return 0; | |
434 | } | |
1da177e4 | 435 | perror(filename); |
6803dc0e | 436 | exit(1); |
1da177e4 LT |
437 | } |
438 | info->hdr = hdr; | |
85bd2fdd SR |
439 | if (info->size < sizeof(*hdr)) { |
440 | /* file too small, assume this is an empty .o file */ | |
441 | return 0; | |
442 | } | |
443 | /* Is this a valid ELF file? */ | |
444 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || | |
445 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || | |
446 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || | |
447 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { | |
448 | /* Not an ELF file - silently ignore it */ | |
449 | return 0; | |
450 | } | |
1da177e4 | 451 | /* Fix endianness in ELF header */ |
7d875a02 AK |
452 | hdr->e_type = TO_NATIVE(hdr->e_type); |
453 | hdr->e_machine = TO_NATIVE(hdr->e_machine); | |
454 | hdr->e_version = TO_NATIVE(hdr->e_version); | |
455 | hdr->e_entry = TO_NATIVE(hdr->e_entry); | |
456 | hdr->e_phoff = TO_NATIVE(hdr->e_phoff); | |
457 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); | |
458 | hdr->e_flags = TO_NATIVE(hdr->e_flags); | |
459 | hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize); | |
460 | hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize); | |
461 | hdr->e_phnum = TO_NATIVE(hdr->e_phnum); | |
462 | hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize); | |
463 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); | |
464 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); | |
1da177e4 LT |
465 | sechdrs = (void *)hdr + hdr->e_shoff; |
466 | info->sechdrs = sechdrs; | |
467 | ||
a83710e5 PS |
468 | /* Check if file offset is correct */ |
469 | if (hdr->e_shoff > info->size) { | |
3b09efc4 MY |
470 | fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n", |
471 | (unsigned long)hdr->e_shoff, filename, info->size); | |
a83710e5 PS |
472 | return 0; |
473 | } | |
474 | ||
6845756b | 475 | if (hdr->e_shnum == SHN_UNDEF) { |
1ce53adf DV |
476 | /* |
477 | * There are more than 64k sections, | |
478 | * read count from .sh_size. | |
1ce53adf DV |
479 | */ |
480 | info->num_sections = TO_NATIVE(sechdrs[0].sh_size); | |
481 | } | |
482 | else { | |
483 | info->num_sections = hdr->e_shnum; | |
484 | } | |
485 | if (hdr->e_shstrndx == SHN_XINDEX) { | |
6845756b | 486 | info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link); |
1ce53adf DV |
487 | } |
488 | else { | |
489 | info->secindex_strings = hdr->e_shstrndx; | |
490 | } | |
491 | ||
1da177e4 | 492 | /* Fix endianness in section headers */ |
1ce53adf | 493 | for (i = 0; i < info->num_sections; i++) { |
7d875a02 AK |
494 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
495 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); | |
496 | sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags); | |
497 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); | |
498 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); | |
499 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); | |
500 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); | |
501 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); | |
502 | sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign); | |
503 | sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize); | |
1da177e4 LT |
504 | } |
505 | /* Find symbol table. */ | |
1ce53adf DV |
506 | secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset; |
507 | for (i = 1; i < info->num_sections; i++) { | |
bd5cbced | 508 | const char *secname; |
56fc82c5 | 509 | int nobits = sechdrs[i].sh_type == SHT_NOBITS; |
1da177e4 | 510 | |
56fc82c5 | 511 | if (!nobits && sechdrs[i].sh_offset > info->size) { |
df578e7d SR |
512 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
513 | "sizeof(*hrd)=%zu\n", filename, | |
514 | (unsigned long)sechdrs[i].sh_offset, | |
515 | sizeof(*hdr)); | |
85bd2fdd SR |
516 | return 0; |
517 | } | |
bd5cbced RP |
518 | secname = secstrings + sechdrs[i].sh_name; |
519 | if (strcmp(secname, ".modinfo") == 0) { | |
56fc82c5 TH |
520 | if (nobits) |
521 | fatal("%s has NOBITS .modinfo\n", filename); | |
1da177e4 LT |
522 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
523 | info->modinfo_len = sechdrs[i].sh_size; | |
7ce3e410 | 524 | } |
bd5cbced | 525 | |
1ce53adf DV |
526 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
527 | unsigned int sh_link_idx; | |
528 | symtab_idx = i; | |
529 | info->symtab_start = (void *)hdr + | |
530 | sechdrs[i].sh_offset; | |
531 | info->symtab_stop = (void *)hdr + | |
532 | sechdrs[i].sh_offset + sechdrs[i].sh_size; | |
6845756b | 533 | sh_link_idx = sechdrs[i].sh_link; |
1ce53adf DV |
534 | info->strtab = (void *)hdr + |
535 | sechdrs[sh_link_idx].sh_offset; | |
536 | } | |
1da177e4 | 537 | |
1ce53adf DV |
538 | /* 32bit section no. table? ("more than 64k sections") */ |
539 | if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) { | |
540 | symtab_shndx_idx = i; | |
541 | info->symtab_shndx_start = (void *)hdr + | |
542 | sechdrs[i].sh_offset; | |
543 | info->symtab_shndx_stop = (void *)hdr + | |
544 | sechdrs[i].sh_offset + sechdrs[i].sh_size; | |
545 | } | |
1da177e4 | 546 | } |
df578e7d | 547 | if (!info->symtab_start) |
cb80514d | 548 | fatal("%s has no symtab?\n", filename); |
df578e7d | 549 | |
1da177e4 LT |
550 | /* Fix endianness in symbols */ |
551 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { | |
552 | sym->st_shndx = TO_NATIVE(sym->st_shndx); | |
553 | sym->st_name = TO_NATIVE(sym->st_name); | |
554 | sym->st_value = TO_NATIVE(sym->st_value); | |
555 | sym->st_size = TO_NATIVE(sym->st_size); | |
556 | } | |
1ce53adf DV |
557 | |
558 | if (symtab_shndx_idx != ~0U) { | |
559 | Elf32_Word *p; | |
6845756b | 560 | if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link) |
1ce53adf | 561 | fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n", |
6845756b | 562 | filename, sechdrs[symtab_shndx_idx].sh_link, |
1ce53adf DV |
563 | symtab_idx); |
564 | /* Fix endianness */ | |
565 | for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop; | |
566 | p++) | |
567 | *p = TO_NATIVE(*p); | |
568 | } | |
569 | ||
85bd2fdd | 570 | return 1; |
1da177e4 LT |
571 | } |
572 | ||
5c3ead8c | 573 | static void parse_elf_finish(struct elf_info *info) |
1da177e4 LT |
574 | { |
575 | release_file(info->hdr, info->size); | |
576 | } | |
577 | ||
4d7365d6 SR |
578 | static int ignore_undef_symbol(struct elf_info *info, const char *symname) |
579 | { | |
580 | /* ignore __this_module, it will be resolved shortly */ | |
b2c5cdcf | 581 | if (strcmp(symname, "__this_module") == 0) |
4d7365d6 SR |
582 | return 1; |
583 | /* ignore global offset table */ | |
584 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) | |
585 | return 1; | |
586 | if (info->hdr->e_machine == EM_PPC) | |
587 | /* Special register function linked on all modules during final link of .ko */ | |
d62c4765 MY |
588 | if (strstarts(symname, "_restgpr_") || |
589 | strstarts(symname, "_savegpr_") || | |
590 | strstarts(symname, "_rest32gpr_") || | |
591 | strstarts(symname, "_save32gpr_") || | |
592 | strstarts(symname, "_restvr_") || | |
593 | strstarts(symname, "_savevr_")) | |
4d7365d6 | 594 | return 1; |
7fca5dc8 SR |
595 | if (info->hdr->e_machine == EM_PPC64) |
596 | /* Special register function linked on all modules during final link of .ko */ | |
d62c4765 MY |
597 | if (strstarts(symname, "_restgpr0_") || |
598 | strstarts(symname, "_savegpr0_") || | |
599 | strstarts(symname, "_restvr_") || | |
600 | strstarts(symname, "_savevr_") || | |
c153693d | 601 | strcmp(symname, ".TOC.") == 0) |
7fca5dc8 | 602 | return 1; |
1d2ad084 VG |
603 | |
604 | if (info->hdr->e_machine == EM_S390) | |
605 | /* Expoline thunks are linked on all kernel modules during final link of .ko */ | |
606 | if (strstarts(symname, "__s390_indirect_jump_r")) | |
607 | return 1; | |
4d7365d6 SR |
608 | /* Do not ignore this symbol */ |
609 | return 0; | |
610 | } | |
611 | ||
9bd2a099 MY |
612 | static void handle_symbol(struct module *mod, struct elf_info *info, |
613 | const Elf_Sym *sym, const char *symname) | |
1da177e4 | 614 | { |
1da177e4 LT |
615 | switch (sym->st_shndx) { |
616 | case SHN_COMMON: | |
d62c4765 | 617 | if (strstarts(symname, "__gnu_lto_")) { |
ef178f92 AK |
618 | /* Should warn here, but modpost runs before the linker */ |
619 | } else | |
620 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); | |
1da177e4 | 621 | break; |
1da177e4 LT |
622 | case SHN_UNDEF: |
623 | /* undefined symbol */ | |
624 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && | |
625 | ELF_ST_BIND(sym->st_info) != STB_WEAK) | |
626 | break; | |
4d7365d6 | 627 | if (ignore_undef_symbol(info, symname)) |
1da177e4 | 628 | break; |
1da177e4 LT |
629 | if (info->hdr->e_machine == EM_SPARC || |
630 | info->hdr->e_machine == EM_SPARCV9) { | |
631 | /* Ignore register directives. */ | |
8d529014 | 632 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
1da177e4 | 633 | break; |
62070fa4 | 634 | if (symname[0] == '.') { |
1f3aa900 | 635 | char *munged = NOFAIL(strdup(symname)); |
62070fa4 SR |
636 | munged[0] = '_'; |
637 | munged[1] = toupper(munged[1]); | |
638 | symname = munged; | |
639 | } | |
1da177e4 | 640 | } |
62070fa4 | 641 | |
e882e89b MY |
642 | sym_add_unresolved(symname, mod, |
643 | ELF_ST_BIND(sym->st_info) == STB_WEAK); | |
1da177e4 LT |
644 | break; |
645 | default: | |
646 | /* All exported symbols */ | |
d62c4765 | 647 | if (strstarts(symname, "__ksymtab_")) { |
2a66c312 | 648 | const char *name, *secname; |
535b3e05 | 649 | |
cb9b55d2 | 650 | name = symname + strlen("__ksymtab_"); |
2a66c312 MY |
651 | secname = sec_name(info, get_secindex(info, sym)); |
652 | ||
653 | if (strstarts(secname, "___ksymtab_gpl+")) | |
654 | sym_add_exported(name, mod, true); | |
655 | else if (strstarts(secname, "___ksymtab+")) | |
656 | sym_add_exported(name, mod, false); | |
1da177e4 | 657 | } |
b2c5cdcf | 658 | if (strcmp(symname, "init_module") == 0) |
58e01fca | 659 | mod->has_init = true; |
b2c5cdcf | 660 | if (strcmp(symname, "cleanup_module") == 0) |
58e01fca | 661 | mod->has_cleanup = true; |
1da177e4 LT |
662 | break; |
663 | } | |
664 | } | |
665 | ||
5c3ead8c SR |
666 | /** |
667 | * Parse tag=value strings from .modinfo section | |
668 | **/ | |
1da177e4 LT |
669 | static 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 | ||
bca2ccee MY |
687 | static char *get_next_modinfo(struct elf_info *info, const char *tag, |
688 | char *prev) | |
1da177e4 LT |
689 | { |
690 | char *p; | |
691 | unsigned int taglen = strlen(tag); | |
bca2ccee MY |
692 | char *modinfo = info->modinfo; |
693 | unsigned long size = info->modinfo_len; | |
1da177e4 | 694 | |
bca2ccee MY |
695 | if (prev) { |
696 | size -= prev - modinfo; | |
697 | modinfo = next_string(prev, &size); | |
b817f6fe SR |
698 | } |
699 | ||
1da177e4 LT |
700 | 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 | ||
bca2ccee | 707 | static char *get_modinfo(struct elf_info *info, const char *tag) |
b817f6fe SR |
708 | |
709 | { | |
bca2ccee | 710 | return get_next_modinfo(info, tag, NULL); |
b817f6fe SR |
711 | } |
712 | ||
4c8fbca5 SR |
713 | /** |
714 | * Test if string s ends in string sub | |
715 | * return 0 if match | |
716 | **/ | |
717 | static int strrcmp(const char *s, const char *sub) | |
718 | { | |
df578e7d | 719 | int slen, sublen; |
62070fa4 | 720 | |
4c8fbca5 SR |
721 | if (!s || !sub) |
722 | return 1; | |
62070fa4 | 723 | |
4c8fbca5 | 724 | slen = strlen(s); |
df578e7d | 725 | sublen = strlen(sub); |
62070fa4 | 726 | |
4c8fbca5 SR |
727 | if ((slen == 0) || (sublen == 0)) |
728 | return 1; | |
729 | ||
df578e7d SR |
730 | if (sublen > slen) |
731 | return 1; | |
4c8fbca5 | 732 | |
df578e7d | 733 | return memcmp(s + slen - sublen, sub, sublen); |
4c8fbca5 SR |
734 | } |
735 | ||
ff13f926 SR |
736 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
737 | { | |
58fb0d4f SR |
738 | if (sym) |
739 | return elf->strtab + sym->st_name; | |
740 | else | |
f666751a | 741 | return "(unknown)"; |
ff13f926 SR |
742 | } |
743 | ||
10668220 SR |
744 | /* The pattern is an array of simple patterns. |
745 | * "foo" will match an exact string equal to "foo" | |
6c5bd235 | 746 | * "*foo" will match a string that ends with "foo" |
10668220 | 747 | * "foo*" will match a string that begins with "foo" |
09c20c03 | 748 | * "*foo*" will match a string that contains "foo" |
10668220 | 749 | */ |
5c725138 | 750 | static int match(const char *sym, const char * const pat[]) |
10668220 SR |
751 | { |
752 | const char *p; | |
753 | while (*pat) { | |
4d94f910 MR |
754 | const char *endp; |
755 | ||
10668220 | 756 | p = *pat++; |
4d94f910 | 757 | endp = p + strlen(p) - 1; |
10668220 | 758 | |
09c20c03 PG |
759 | /* "*foo*" */ |
760 | if (*p == '*' && *endp == '*') { | |
6f02bdfc DE |
761 | char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2)); |
762 | char *here = strstr(sym, bare); | |
09c20c03 | 763 | |
09c20c03 PG |
764 | free(bare); |
765 | if (here != NULL) | |
766 | return 1; | |
767 | } | |
6c5bd235 | 768 | /* "*foo" */ |
09c20c03 | 769 | else if (*p == '*') { |
6c5bd235 SR |
770 | if (strrcmp(sym, p + 1) == 0) |
771 | return 1; | |
772 | } | |
10668220 | 773 | /* "foo*" */ |
6c5bd235 | 774 | else if (*endp == '*') { |
10668220 SR |
775 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
776 | return 1; | |
777 | } | |
10668220 SR |
778 | /* no wildcards */ |
779 | else { | |
780 | if (strcmp(p, sym) == 0) | |
781 | return 1; | |
782 | } | |
783 | } | |
784 | /* no match */ | |
785 | return 0; | |
786 | } | |
787 | ||
10668220 | 788 | /* sections that we do not want to do full section mismatch check on */ |
7a3ee753 | 789 | static const char *const section_white_list[] = |
4391ed6a SR |
790 | { |
791 | ".comment*", | |
792 | ".debug*", | |
4d10c223 | 793 | ".cranges", /* sh64 */ |
1121584f | 794 | ".zdebug*", /* Compressed debug sections. */ |
739d875d | 795 | ".GCC.command.line", /* record-gcc-switches */ |
4391ed6a SR |
796 | ".mdebug*", /* alpha, score, mips etc. */ |
797 | ".pdr", /* alpha, score, mips etc. */ | |
798 | ".stab*", | |
799 | ".note*", | |
800 | ".got*", | |
801 | ".toc*", | |
af42e970 MF |
802 | ".xt.prop", /* xtensa */ |
803 | ".xt.lit", /* xtensa */ | |
f2e207f3 VG |
804 | ".arcextmap*", /* arc */ |
805 | ".gnu.linkonce.arcext*", /* arc : modules */ | |
d1189c63 NC |
806 | ".cmem*", /* EZchip */ |
807 | ".fmt_slot*", /* EZchip */ | |
ef178f92 | 808 | ".gnu.lto*", |
e390f9a9 | 809 | ".discard.*", |
4391ed6a SR |
810 | NULL |
811 | }; | |
10668220 | 812 | |
e241a630 | 813 | /* |
b614a697 | 814 | * This is used to find sections missing the SHF_ALLOC flag. |
e241a630 | 815 | * The cause of this is often a section specified in assembler |
b614a697 | 816 | * without "ax" / "aw". |
e241a630 | 817 | */ |
b614a697 | 818 | static void check_section(const char *modname, struct elf_info *elf, |
bb66fc67 | 819 | Elf_Shdr *sechdr) |
e241a630 | 820 | { |
b614a697 AK |
821 | const char *sec = sech_name(elf, sechdr); |
822 | ||
823 | if (sechdr->sh_type == SHT_PROGBITS && | |
824 | !(sechdr->sh_flags & SHF_ALLOC) && | |
825 | !match(sec, section_white_list)) { | |
826 | warn("%s (%s): unexpected non-allocatable section.\n" | |
827 | "Did you forget to use \"ax\"/\"aw\" in a .S file?\n" | |
828 | "Note that for example <linux/init.h> contains\n" | |
829 | "section definitions for use in .S files.\n\n", | |
830 | modname, sec); | |
e241a630 | 831 | } |
e241a630 SR |
832 | } |
833 | ||
834 | ||
835 | ||
eb8f6890 | 836 | #define ALL_INIT_DATA_SECTIONS \ |
a0d8f803 RV |
837 | ".init.setup", ".init.rodata", ".meminit.rodata", \ |
838 | ".init.data", ".meminit.data" | |
eb8f6890 | 839 | #define ALL_EXIT_DATA_SECTIONS \ |
a0d8f803 | 840 | ".exit.data", ".memexit.data" |
10668220 | 841 | |
eb8f6890 | 842 | #define ALL_INIT_TEXT_SECTIONS \ |
a0d8f803 | 843 | ".init.text", ".meminit.text" |
eb8f6890 | 844 | #define ALL_EXIT_TEXT_SECTIONS \ |
a0d8f803 | 845 | ".exit.text", ".memexit.text" |
10668220 | 846 | |
bb15d8db | 847 | #define ALL_PCI_INIT_SECTIONS \ |
a0d8f803 RV |
848 | ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \ |
849 | ".pci_fixup_enable", ".pci_fixup_resume", \ | |
850 | ".pci_fixup_resume_early", ".pci_fixup_suspend" | |
bb15d8db | 851 | |
e24f6628 PG |
852 | #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS |
853 | #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS | |
4a31a229 UKK |
854 | |
855 | #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS | |
856 | #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS | |
10668220 | 857 | |
a0d8f803 | 858 | #define DATA_SECTIONS ".data", ".data.rel" |
157d1972 | 859 | #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \ |
65538966 | 860 | ".kprobes.text", ".cpuidle.text", ".noinstr.text" |
52dc0595 | 861 | #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ |
673c2c34 | 862 | ".fixup", ".entry.text", ".exception.text", ".text.*", \ |
1e688dd2 | 863 | ".coldtext", ".softirqentry.text" |
10668220 | 864 | |
fd6c3a8d | 865 | #define INIT_SECTIONS ".init.*" |
fd6c3a8d | 866 | #define MEM_INIT_SECTIONS ".meminit.*" |
eb8f6890 | 867 | |
fd6c3a8d | 868 | #define EXIT_SECTIONS ".exit.*" |
fd6c3a8d | 869 | #define MEM_EXIT_SECTIONS ".memexit.*" |
eb8f6890 | 870 | |
52dc0595 QC |
871 | #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \ |
872 | TEXT_SECTIONS, OTHER_TEXT_SECTIONS | |
873 | ||
6c5bd235 | 874 | /* init data sections */ |
7a3ee753 MK |
875 | static const char *const init_data_sections[] = |
876 | { ALL_INIT_DATA_SECTIONS, NULL }; | |
6c5bd235 SR |
877 | |
878 | /* all init sections */ | |
7a3ee753 | 879 | static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
6c5bd235 SR |
880 | |
881 | /* All init and exit sections (code + data) */ | |
7a3ee753 | 882 | static const char *const init_exit_sections[] = |
eb8f6890 | 883 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
6c5bd235 | 884 | |
4a3893d0 PG |
885 | /* all text sections */ |
886 | static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL }; | |
887 | ||
6c5bd235 | 888 | /* data section */ |
7a3ee753 | 889 | static const char *const data_sections[] = { DATA_SECTIONS, NULL }; |
6c5bd235 | 890 | |
6c5bd235 SR |
891 | |
892 | /* symbols in .data that may refer to init/exit sections */ | |
af92a82d UKK |
893 | #define DEFAULT_SYMBOL_WHITE_LIST \ |
894 | "*driver", \ | |
895 | "*_template", /* scsi uses *_template a lot */ \ | |
896 | "*_timer", /* arm uses ops structures named _timer a lot */ \ | |
897 | "*_sht", /* scsi also used *_sht to some extent */ \ | |
898 | "*_ops", \ | |
899 | "*_probe", \ | |
900 | "*_probe_one", \ | |
901 | "*_console" | |
6c5bd235 | 902 | |
7a3ee753 MK |
903 | static const char *const head_sections[] = { ".head.text*", NULL }; |
904 | static const char *const linker_symbols[] = | |
6c5bd235 | 905 | { "__init_begin", "_sinittext", "_einittext", NULL }; |
4a3893d0 | 906 | static const char *const optim_symbols[] = { "*.constprop.*", NULL }; |
6c5bd235 | 907 | |
588ccd73 | 908 | enum mismatch { |
bbd3f4fb UKK |
909 | TEXT_TO_ANY_INIT, |
910 | DATA_TO_ANY_INIT, | |
911 | TEXT_TO_ANY_EXIT, | |
912 | DATA_TO_ANY_EXIT, | |
913 | XXXINIT_TO_SOME_INIT, | |
914 | XXXEXIT_TO_SOME_EXIT, | |
915 | ANY_INIT_TO_ANY_EXIT, | |
916 | ANY_EXIT_TO_ANY_INIT, | |
588ccd73 | 917 | EXPORT_TO_INIT_EXIT, |
52dc0595 | 918 | EXTABLE_TO_NON_TEXT, |
588ccd73 SR |
919 | }; |
920 | ||
e5d8f59a | 921 | /** |
f3945833 | 922 | * Describe how to match sections on different criteria: |
e5d8f59a QC |
923 | * |
924 | * @fromsec: Array of sections to be matched. | |
925 | * | |
926 | * @bad_tosec: Relocations applied to a section in @fromsec to a section in | |
927 | * this array is forbidden (black-list). Can be empty. | |
928 | * | |
929 | * @good_tosec: Relocations applied to a section in @fromsec must be | |
f3945833 | 930 | * targeting sections in this array (white-list). Can be empty. |
e5d8f59a QC |
931 | * |
932 | * @mismatch: Type of mismatch. | |
933 | * | |
934 | * @symbol_white_list: Do not match a relocation to a symbol in this list | |
f3945833 | 935 | * even if it is targeting a section in @bad_to_sec. |
e5d8f59a QC |
936 | * |
937 | * @handler: Specific handler to call when a match is found. If NULL, | |
938 | * default_mismatch_handler() will be called. | |
939 | * | |
940 | */ | |
10668220 SR |
941 | struct sectioncheck { |
942 | const char *fromsec[20]; | |
050e57fd QC |
943 | const char *bad_tosec[20]; |
944 | const char *good_tosec[20]; | |
588ccd73 | 945 | enum mismatch mismatch; |
af92a82d | 946 | const char *symbol_white_list[20]; |
644e8f14 QC |
947 | void (*handler)(const char *modname, struct elf_info *elf, |
948 | const struct sectioncheck* const mismatch, | |
949 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec); | |
950 | ||
10668220 SR |
951 | }; |
952 | ||
52dc0595 QC |
953 | static void extable_mismatch_handler(const char *modname, struct elf_info *elf, |
954 | const struct sectioncheck* const mismatch, | |
955 | Elf_Rela *r, Elf_Sym *sym, | |
956 | const char *fromsec); | |
957 | ||
7a3ee753 | 958 | static const struct sectioncheck sectioncheck[] = { |
10668220 SR |
959 | /* Do not reference init/exit code/data from |
960 | * normal code and data | |
961 | */ | |
962 | { | |
588ccd73 | 963 | .fromsec = { TEXT_SECTIONS, NULL }, |
050e57fd | 964 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
bbd3f4fb | 965 | .mismatch = TEXT_TO_ANY_INIT, |
af92a82d | 966 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 SR |
967 | }, |
968 | { | |
969 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 970 | .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL }, |
bbd3f4fb | 971 | .mismatch = DATA_TO_ANY_INIT, |
af92a82d | 972 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 | 973 | }, |
0db25245 UKK |
974 | { |
975 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 976 | .bad_tosec = { INIT_SECTIONS, NULL }, |
0db25245 UKK |
977 | .mismatch = DATA_TO_ANY_INIT, |
978 | .symbol_white_list = { | |
979 | "*_template", "*_timer", "*_sht", "*_ops", | |
980 | "*_probe", "*_probe_one", "*_console", NULL | |
981 | }, | |
982 | }, | |
588ccd73 SR |
983 | { |
984 | .fromsec = { TEXT_SECTIONS, NULL }, | |
050e57fd | 985 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 986 | .mismatch = TEXT_TO_ANY_EXIT, |
af92a82d | 987 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 SR |
988 | }, |
989 | { | |
990 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 991 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 992 | .mismatch = DATA_TO_ANY_EXIT, |
af92a82d | 993 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
eb8f6890 | 994 | }, |
e24f6628 | 995 | /* Do not reference init code/data from meminit code/data */ |
eb8f6890 | 996 | { |
4a31a229 | 997 | .fromsec = { ALL_XXXINIT_SECTIONS, NULL }, |
050e57fd | 998 | .bad_tosec = { INIT_SECTIONS, NULL }, |
bbd3f4fb | 999 | .mismatch = XXXINIT_TO_SOME_INIT, |
af92a82d | 1000 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
eb8f6890 | 1001 | }, |
e24f6628 | 1002 | /* Do not reference exit code/data from memexit code/data */ |
eb8f6890 | 1003 | { |
4a31a229 | 1004 | .fromsec = { ALL_XXXEXIT_SECTIONS, NULL }, |
050e57fd | 1005 | .bad_tosec = { EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1006 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
af92a82d | 1007 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 SR |
1008 | }, |
1009 | /* Do not use exit code/data from init code */ | |
1010 | { | |
eb8f6890 | 1011 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
050e57fd | 1012 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1013 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
af92a82d | 1014 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 SR |
1015 | }, |
1016 | /* Do not use init code/data from exit code */ | |
1017 | { | |
eb8f6890 | 1018 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
050e57fd | 1019 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
bbd3f4fb | 1020 | .mismatch = ANY_EXIT_TO_ANY_INIT, |
af92a82d | 1021 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 | 1022 | }, |
bb15d8db SAS |
1023 | { |
1024 | .fromsec = { ALL_PCI_INIT_SECTIONS, NULL }, | |
050e57fd | 1025 | .bad_tosec = { INIT_SECTIONS, NULL }, |
bb15d8db SAS |
1026 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
1027 | .symbol_white_list = { NULL }, | |
1028 | }, | |
10668220 SR |
1029 | /* Do not export init/exit functions or data */ |
1030 | { | |
1031 | .fromsec = { "__ksymtab*", NULL }, | |
050e57fd | 1032 | .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, |
af92a82d UKK |
1033 | .mismatch = EXPORT_TO_INIT_EXIT, |
1034 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, | |
52dc0595 QC |
1035 | }, |
1036 | { | |
1037 | .fromsec = { "__ex_table", NULL }, | |
1038 | /* If you're adding any new black-listed sections in here, consider | |
1039 | * adding a special 'printer' for them in scripts/check_extable. | |
1040 | */ | |
1041 | .bad_tosec = { ".altinstr_replacement", NULL }, | |
1042 | .good_tosec = {ALL_TEXT_SECTIONS , NULL}, | |
1043 | .mismatch = EXTABLE_TO_NON_TEXT, | |
1044 | .handler = extable_mismatch_handler, | |
10668220 SR |
1045 | } |
1046 | }; | |
1047 | ||
0d2a636e UKK |
1048 | static const struct sectioncheck *section_mismatch( |
1049 | const char *fromsec, const char *tosec) | |
10668220 SR |
1050 | { |
1051 | int i; | |
1052 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); | |
1053 | const struct sectioncheck *check = §ioncheck[0]; | |
1054 | ||
c5c3439a QC |
1055 | /* |
1056 | * The target section could be the SHT_NUL section when we're | |
1057 | * handling relocations to un-resolved symbols, trying to match it | |
739d875d DH |
1058 | * doesn't make much sense and causes build failures on parisc |
1059 | * architectures. | |
c5c3439a QC |
1060 | */ |
1061 | if (*tosec == '\0') | |
1062 | return NULL; | |
1063 | ||
10668220 | 1064 | for (i = 0; i < elems; i++) { |
050e57fd QC |
1065 | if (match(fromsec, check->fromsec)) { |
1066 | if (check->bad_tosec[0] && match(tosec, check->bad_tosec)) | |
1067 | return check; | |
1068 | if (check->good_tosec[0] && !match(tosec, check->good_tosec)) | |
1069 | return check; | |
1070 | } | |
10668220 SR |
1071 | check++; |
1072 | } | |
0d2a636e | 1073 | return NULL; |
10668220 SR |
1074 | } |
1075 | ||
4c8fbca5 SR |
1076 | /** |
1077 | * Whitelist to allow certain references to pass with no warning. | |
0e0d314e | 1078 | * |
4c8fbca5 SR |
1079 | * Pattern 1: |
1080 | * If a module parameter is declared __initdata and permissions=0 | |
1081 | * then this is legal despite the warning generated. | |
1082 | * We cannot see value of permissions here, so just ignore | |
1083 | * this pattern. | |
1084 | * The pattern is identified by: | |
1085 | * tosec = .init.data | |
9209aed0 | 1086 | * fromsec = .data* |
4c8fbca5 | 1087 | * atsym =__param* |
62070fa4 | 1088 | * |
6a841528 RR |
1089 | * Pattern 1a: |
1090 | * module_param_call() ops can refer to __init set function if permissions=0 | |
1091 | * The pattern is identified by: | |
1092 | * tosec = .init.text | |
1093 | * fromsec = .data* | |
1094 | * atsym = __param_ops_* | |
1095 | * | |
4c8fbca5 | 1096 | * Pattern 2: |
72ee59b5 | 1097 | * Many drivers utilise a *driver container with references to |
4c8fbca5 | 1098 | * add, remove, probe functions etc. |
4c8fbca5 | 1099 | * the pattern is identified by: |
83cda2bb SR |
1100 | * tosec = init or exit section |
1101 | * fromsec = data section | |
df578e7d SR |
1102 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
1103 | * *probe_one, *_console, *_timer | |
ee6a8545 VG |
1104 | * |
1105 | * Pattern 3: | |
c993971f | 1106 | * Whitelist all references from .head.text to any init section |
9bf8cb9b | 1107 | * |
1d8af559 | 1108 | * Pattern 4: |
ee6a8545 VG |
1109 | * Some symbols belong to init section but still it is ok to reference |
1110 | * these from non-init sections as these symbols don't have any memory | |
1111 | * allocated for them and symbol address and value are same. So even | |
1112 | * if init section is freed, its ok to reference those symbols. | |
1113 | * For ex. symbols marking the init section boundaries. | |
1114 | * This pattern is identified by | |
1115 | * refsymname = __init_begin, _sinittext, _einittext | |
9bf8cb9b | 1116 | * |
4a3893d0 PG |
1117 | * Pattern 5: |
1118 | * GCC may optimize static inlines when fed constant arg(s) resulting | |
1119 | * in functions like cpumask_empty() -- generating an associated symbol | |
1120 | * cpumask_empty.constprop.3 that appears in the audit. If the const that | |
1121 | * is passed in comes from __init, like say nmi_ipi_mask, we get a | |
1122 | * meaningless section warning. May need to add isra symbols too... | |
1123 | * This pattern is identified by | |
1124 | * tosec = init section | |
1125 | * fromsec = text section | |
1126 | * refsymname = *.constprop.* | |
1127 | * | |
a4d26f1a PW |
1128 | * Pattern 6: |
1129 | * Hide section mismatch warnings for ELF local symbols. The goal | |
1130 | * is to eliminate false positive modpost warnings caused by | |
1131 | * compiler-generated ELF local symbol names such as ".LANCHOR1". | |
1132 | * Autogenerated symbol names bypass modpost's "Pattern 2" | |
1133 | * whitelisting, which relies on pattern-matching against symbol | |
1134 | * names to work. (One situation where gcc can autogenerate ELF | |
1135 | * local symbols is when "-fsection-anchors" is used.) | |
4c8fbca5 | 1136 | **/ |
af92a82d UKK |
1137 | static int secref_whitelist(const struct sectioncheck *mismatch, |
1138 | const char *fromsec, const char *fromsym, | |
58fb0d4f | 1139 | const char *tosec, const char *tosym) |
4c8fbca5 | 1140 | { |
4c8fbca5 | 1141 | /* Check for pattern 1 */ |
6c5bd235 SR |
1142 | if (match(tosec, init_data_sections) && |
1143 | match(fromsec, data_sections) && | |
d62c4765 | 1144 | strstarts(fromsym, "__param")) |
58fb0d4f | 1145 | return 0; |
4c8fbca5 | 1146 | |
6a841528 RR |
1147 | /* Check for pattern 1a */ |
1148 | if (strcmp(tosec, ".init.text") == 0 && | |
1149 | match(fromsec, data_sections) && | |
d62c4765 | 1150 | strstarts(fromsym, "__param_ops_")) |
6a841528 RR |
1151 | return 0; |
1152 | ||
4c8fbca5 | 1153 | /* Check for pattern 2 */ |
6c5bd235 SR |
1154 | if (match(tosec, init_exit_sections) && |
1155 | match(fromsec, data_sections) && | |
af92a82d | 1156 | match(fromsym, mismatch->symbol_white_list)) |
58fb0d4f | 1157 | return 0; |
4c8fbca5 | 1158 | |
9bf8cb9b | 1159 | /* Check for pattern 3 */ |
6c5bd235 SR |
1160 | if (match(fromsec, head_sections) && |
1161 | match(tosec, init_sections)) | |
58fb0d4f | 1162 | return 0; |
9bf8cb9b | 1163 | |
1d8af559 | 1164 | /* Check for pattern 4 */ |
58fb0d4f SR |
1165 | if (match(tosym, linker_symbols)) |
1166 | return 0; | |
9bf8cb9b | 1167 | |
4a3893d0 PG |
1168 | /* Check for pattern 5 */ |
1169 | if (match(fromsec, text_sections) && | |
1170 | match(tosec, init_sections) && | |
1171 | match(fromsym, optim_symbols)) | |
1172 | return 0; | |
1173 | ||
a4d26f1a PW |
1174 | /* Check for pattern 6 */ |
1175 | if (strstarts(fromsym, ".L")) | |
1176 | return 0; | |
1177 | ||
58fb0d4f | 1178 | return 1; |
4c8fbca5 SR |
1179 | } |
1180 | ||
5818c683 ST |
1181 | static inline int is_arm_mapping_symbol(const char *str) |
1182 | { | |
1183 | return str[0] == '$' && strchr("axtd", str[1]) | |
1184 | && (str[2] == '\0' || str[2] == '.'); | |
1185 | } | |
1186 | ||
1187 | /* | |
1188 | * If there's no name there, ignore it; likewise, ignore it if it's | |
1189 | * one of the magic symbols emitted used by current ARM tools. | |
1190 | * | |
1191 | * Otherwise if find_symbols_between() returns those symbols, they'll | |
1192 | * fail the whitelist tests and cause lots of false alarms ... fixable | |
1193 | * only by merging __exit and __init sections into __text, bloating | |
1194 | * the kernel (which is especially evil on embedded platforms). | |
1195 | */ | |
1196 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) | |
1197 | { | |
1198 | const char *name = elf->strtab + sym->st_name; | |
1199 | ||
1200 | if (!name || !strlen(name)) | |
1201 | return 0; | |
1202 | return !is_arm_mapping_symbol(name); | |
1203 | } | |
1204 | ||
93684d3b SR |
1205 | /** |
1206 | * Find symbol based on relocation record info. | |
1207 | * In some cases the symbol supplied is a valid symbol so | |
1208 | * return refsym. If st_name != 0 we assume this is a valid symbol. | |
1209 | * In other cases the symbol needs to be looked up in the symbol table | |
1210 | * based on section and address. | |
1211 | * **/ | |
9ad21c3f | 1212 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, |
93684d3b SR |
1213 | Elf_Sym *relsym) |
1214 | { | |
1215 | Elf_Sym *sym; | |
9ad21c3f SR |
1216 | Elf_Sym *near = NULL; |
1217 | Elf64_Sword distance = 20; | |
1218 | Elf64_Sword d; | |
1ce53adf | 1219 | unsigned int relsym_secindex; |
93684d3b SR |
1220 | |
1221 | if (relsym->st_name != 0) | |
1222 | return relsym; | |
1ce53adf DV |
1223 | |
1224 | relsym_secindex = get_secindex(elf, relsym); | |
93684d3b | 1225 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
1ce53adf | 1226 | if (get_secindex(elf, sym) != relsym_secindex) |
93684d3b | 1227 | continue; |
ae4ac123 AN |
1228 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
1229 | continue; | |
5818c683 ST |
1230 | if (!is_valid_name(elf, sym)) |
1231 | continue; | |
93684d3b SR |
1232 | if (sym->st_value == addr) |
1233 | return sym; | |
9ad21c3f SR |
1234 | /* Find a symbol nearby - addr are maybe negative */ |
1235 | d = sym->st_value - addr; | |
1236 | if (d < 0) | |
1237 | d = addr - sym->st_value; | |
1238 | if (d < distance) { | |
1239 | distance = d; | |
1240 | near = sym; | |
1241 | } | |
93684d3b | 1242 | } |
9ad21c3f SR |
1243 | /* We need a close match */ |
1244 | if (distance < 20) | |
1245 | return near; | |
1246 | else | |
1247 | return NULL; | |
93684d3b SR |
1248 | } |
1249 | ||
b39927cf | 1250 | /* |
43c74d17 SR |
1251 | * Find symbols before or equal addr and after addr - in the section sec. |
1252 | * If we find two symbols with equal offset prefer one with a valid name. | |
1253 | * The ELF format may have a better way to detect what type of symbol | |
1254 | * it is, but this works for now. | |
b39927cf | 1255 | **/ |
157c23c8 SR |
1256 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
1257 | const char *sec) | |
b39927cf SR |
1258 | { |
1259 | Elf_Sym *sym; | |
157c23c8 | 1260 | Elf_Sym *near = NULL; |
157c23c8 | 1261 | Elf_Addr distance = ~0; |
62070fa4 | 1262 | |
b39927cf SR |
1263 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
1264 | const char *symsec; | |
1265 | ||
1ce53adf | 1266 | if (is_shndx_special(sym->st_shndx)) |
b39927cf | 1267 | continue; |
1ce53adf | 1268 | symsec = sec_name(elf, get_secindex(elf, sym)); |
b39927cf SR |
1269 | if (strcmp(symsec, sec) != 0) |
1270 | continue; | |
da68d61f DB |
1271 | if (!is_valid_name(elf, sym)) |
1272 | continue; | |
b39927cf | 1273 | if (sym->st_value <= addr) { |
157c23c8 SR |
1274 | if ((addr - sym->st_value) < distance) { |
1275 | distance = addr - sym->st_value; | |
1276 | near = sym; | |
1277 | } else if ((addr - sym->st_value) == distance) { | |
1278 | near = sym; | |
43c74d17 | 1279 | } |
b39927cf SR |
1280 | } |
1281 | } | |
157c23c8 | 1282 | return near; |
b39927cf SR |
1283 | } |
1284 | ||
588ccd73 SR |
1285 | /* |
1286 | * Convert a section name to the function/data attribute | |
1287 | * .init.text => __init | |
588ccd73 SR |
1288 | * .memexitconst => __memconst |
1289 | * etc. | |
cbcf14a9 AS |
1290 | * |
1291 | * The memory of returned value has been allocated on a heap. The user of this | |
1292 | * method should free it after usage. | |
588ccd73 SR |
1293 | */ |
1294 | static char *sec2annotation(const char *s) | |
1295 | { | |
1296 | if (match(s, init_exit_sections)) { | |
1f3aa900 | 1297 | char *p = NOFAIL(malloc(20)); |
588ccd73 SR |
1298 | char *r = p; |
1299 | ||
1300 | *p++ = '_'; | |
1301 | *p++ = '_'; | |
1302 | if (*s == '.') | |
1303 | s++; | |
1304 | while (*s && *s != '.') | |
1305 | *p++ = *s++; | |
1306 | *p = '\0'; | |
1307 | if (*s == '.') | |
1308 | s++; | |
1309 | if (strstr(s, "rodata") != NULL) | |
1310 | strcat(p, "const "); | |
1311 | else if (strstr(s, "data") != NULL) | |
1312 | strcat(p, "data "); | |
1313 | else | |
1314 | strcat(p, " "); | |
cbcf14a9 | 1315 | return r; |
588ccd73 | 1316 | } else { |
1f3aa900 | 1317 | return NOFAIL(strdup("")); |
588ccd73 SR |
1318 | } |
1319 | } | |
1320 | ||
1321 | static int is_function(Elf_Sym *sym) | |
1322 | { | |
1323 | if (sym) | |
1324 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; | |
1325 | else | |
f666751a | 1326 | return -1; |
588ccd73 SR |
1327 | } |
1328 | ||
00759c0e RD |
1329 | static void print_section_list(const char * const list[20]) |
1330 | { | |
1331 | const char *const *s = list; | |
1332 | ||
1333 | while (*s) { | |
1334 | fprintf(stderr, "%s", *s); | |
1335 | s++; | |
1336 | if (*s) | |
1337 | fprintf(stderr, ", "); | |
1338 | } | |
1339 | fprintf(stderr, "\n"); | |
1340 | } | |
1341 | ||
356ad538 QC |
1342 | static inline void get_pretty_name(int is_func, const char** name, const char** name_p) |
1343 | { | |
1344 | switch (is_func) { | |
1345 | case 0: *name = "variable"; *name_p = ""; break; | |
1346 | case 1: *name = "function"; *name_p = "()"; break; | |
1347 | default: *name = "(unknown reference)"; *name_p = ""; break; | |
1348 | } | |
1349 | } | |
1350 | ||
58fb0d4f | 1351 | /* |
b39927cf SR |
1352 | * Print a warning about a section mismatch. |
1353 | * Try to find symbols near it so user can find it. | |
4c8fbca5 | 1354 | * Check whitelist before warning - it may be a false positive. |
58fb0d4f | 1355 | */ |
0d2a636e UKK |
1356 | static void report_sec_mismatch(const char *modname, |
1357 | const struct sectioncheck *mismatch, | |
bb66fc67 MY |
1358 | const char *fromsec, |
1359 | unsigned long long fromaddr, | |
1360 | const char *fromsym, | |
1361 | int from_is_func, | |
1362 | const char *tosec, const char *tosym, | |
1363 | int to_is_func) | |
588ccd73 SR |
1364 | { |
1365 | const char *from, *from_p; | |
1366 | const char *to, *to_p; | |
37ed19d5 AF |
1367 | char *prl_from; |
1368 | char *prl_to; | |
f666751a | 1369 | |
e5f95c8b | 1370 | sec_mismatch_count++; |
e5f95c8b | 1371 | |
356ad538 QC |
1372 | get_pretty_name(from_is_func, &from, &from_p); |
1373 | get_pretty_name(to_is_func, &to, &to_p); | |
1374 | ||
7c0ac495 GU |
1375 | warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s " |
1376 | "to the %s %s:%s%s\n", | |
1377 | modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec, | |
1378 | tosym, to_p); | |
588ccd73 | 1379 | |
0d2a636e | 1380 | switch (mismatch->mismatch) { |
bbd3f4fb | 1381 | case TEXT_TO_ANY_INIT: |
37ed19d5 AF |
1382 | prl_from = sec2annotation(fromsec); |
1383 | prl_to = sec2annotation(tosec); | |
588ccd73 | 1384 | fprintf(stderr, |
f666751a | 1385 | "The function %s%s() references\n" |
588ccd73 SR |
1386 | "the %s %s%s%s.\n" |
1387 | "This is often because %s lacks a %s\n" | |
1388 | "annotation or the annotation of %s is wrong.\n", | |
37ed19d5 AF |
1389 | prl_from, fromsym, |
1390 | to, prl_to, tosym, to_p, | |
1391 | fromsym, prl_to, tosym); | |
1392 | free(prl_from); | |
1393 | free(prl_to); | |
588ccd73 | 1394 | break; |
bbd3f4fb | 1395 | case DATA_TO_ANY_INIT: { |
37ed19d5 | 1396 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1397 | fprintf(stderr, |
1398 | "The variable %s references\n" | |
1399 | "the %s %s%s%s\n" | |
1400 | "If the reference is valid then annotate the\n" | |
8b8b76c0 | 1401 | "variable with __init* or __refdata (see linux/init.h) " |
588ccd73 | 1402 | "or name the variable:\n", |
37ed19d5 | 1403 | fromsym, to, prl_to, tosym, to_p); |
00759c0e | 1404 | print_section_list(mismatch->symbol_white_list); |
37ed19d5 | 1405 | free(prl_to); |
588ccd73 | 1406 | break; |
58fb0d4f | 1407 | } |
bbd3f4fb | 1408 | case TEXT_TO_ANY_EXIT: |
37ed19d5 | 1409 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1410 | fprintf(stderr, |
1411 | "The function %s() references a %s in an exit section.\n" | |
1412 | "Often the %s %s%s has valid usage outside the exit section\n" | |
1413 | "and the fix is to remove the %sannotation of %s.\n", | |
37ed19d5 AF |
1414 | fromsym, to, to, tosym, to_p, prl_to, tosym); |
1415 | free(prl_to); | |
588ccd73 | 1416 | break; |
bbd3f4fb | 1417 | case DATA_TO_ANY_EXIT: { |
37ed19d5 | 1418 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1419 | fprintf(stderr, |
1420 | "The variable %s references\n" | |
1421 | "the %s %s%s%s\n" | |
1422 | "If the reference is valid then annotate the\n" | |
1423 | "variable with __exit* (see linux/init.h) or " | |
1424 | "name the variable:\n", | |
37ed19d5 | 1425 | fromsym, to, prl_to, tosym, to_p); |
00759c0e | 1426 | print_section_list(mismatch->symbol_white_list); |
37ed19d5 | 1427 | free(prl_to); |
588ccd73 SR |
1428 | break; |
1429 | } | |
bbd3f4fb UKK |
1430 | case XXXINIT_TO_SOME_INIT: |
1431 | case XXXEXIT_TO_SOME_EXIT: | |
37ed19d5 AF |
1432 | prl_from = sec2annotation(fromsec); |
1433 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1434 | fprintf(stderr, |
1435 | "The %s %s%s%s references\n" | |
1436 | "a %s %s%s%s.\n" | |
1437 | "If %s is only used by %s then\n" | |
1438 | "annotate %s with a matching annotation.\n", | |
37ed19d5 AF |
1439 | from, prl_from, fromsym, from_p, |
1440 | to, prl_to, tosym, to_p, | |
b1d2675a | 1441 | tosym, fromsym, tosym); |
37ed19d5 AF |
1442 | free(prl_from); |
1443 | free(prl_to); | |
588ccd73 | 1444 | break; |
bbd3f4fb | 1445 | case ANY_INIT_TO_ANY_EXIT: |
37ed19d5 AF |
1446 | prl_from = sec2annotation(fromsec); |
1447 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1448 | fprintf(stderr, |
1449 | "The %s %s%s%s references\n" | |
1450 | "a %s %s%s%s.\n" | |
1451 | "This is often seen when error handling " | |
1452 | "in the init function\n" | |
1453 | "uses functionality in the exit path.\n" | |
1454 | "The fix is often to remove the %sannotation of\n" | |
1455 | "%s%s so it may be used outside an exit section.\n", | |
37ed19d5 AF |
1456 | from, prl_from, fromsym, from_p, |
1457 | to, prl_to, tosym, to_p, | |
5003bab8 | 1458 | prl_to, tosym, to_p); |
37ed19d5 AF |
1459 | free(prl_from); |
1460 | free(prl_to); | |
588ccd73 | 1461 | break; |
bbd3f4fb | 1462 | case ANY_EXIT_TO_ANY_INIT: |
37ed19d5 AF |
1463 | prl_from = sec2annotation(fromsec); |
1464 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1465 | fprintf(stderr, |
1466 | "The %s %s%s%s references\n" | |
1467 | "a %s %s%s%s.\n" | |
1468 | "This is often seen when error handling " | |
1469 | "in the exit function\n" | |
1470 | "uses functionality in the init path.\n" | |
1471 | "The fix is often to remove the %sannotation of\n" | |
1472 | "%s%s so it may be used outside an init section.\n", | |
37ed19d5 AF |
1473 | from, prl_from, fromsym, from_p, |
1474 | to, prl_to, tosym, to_p, | |
1475 | prl_to, tosym, to_p); | |
1476 | free(prl_from); | |
1477 | free(prl_to); | |
588ccd73 SR |
1478 | break; |
1479 | case EXPORT_TO_INIT_EXIT: | |
37ed19d5 | 1480 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1481 | fprintf(stderr, |
1482 | "The symbol %s is exported and annotated %s\n" | |
1483 | "Fix this by removing the %sannotation of %s " | |
1484 | "or drop the export.\n", | |
37ed19d5 AF |
1485 | tosym, prl_to, prl_to, tosym); |
1486 | free(prl_to); | |
588ccd73 | 1487 | break; |
52dc0595 QC |
1488 | case EXTABLE_TO_NON_TEXT: |
1489 | fatal("There's a special handler for this mismatch type, " | |
1490 | "we should never get here."); | |
1491 | break; | |
588ccd73 SR |
1492 | } |
1493 | fprintf(stderr, "\n"); | |
58fb0d4f SR |
1494 | } |
1495 | ||
644e8f14 QC |
1496 | static void default_mismatch_handler(const char *modname, struct elf_info *elf, |
1497 | const struct sectioncheck* const mismatch, | |
1498 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) | |
58fb0d4f SR |
1499 | { |
1500 | const char *tosec; | |
644e8f14 QC |
1501 | Elf_Sym *to; |
1502 | Elf_Sym *from; | |
1503 | const char *tosym; | |
1504 | const char *fromsym; | |
58fb0d4f | 1505 | |
644e8f14 QC |
1506 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
1507 | fromsym = sym_name(elf, from); | |
644e8f14 | 1508 | |
d62c4765 | 1509 | if (strstarts(fromsym, "reference___initcall")) |
644e8f14 QC |
1510 | return; |
1511 | ||
c7a65e06 QC |
1512 | tosec = sec_name(elf, get_secindex(elf, sym)); |
1513 | to = find_elf_symbol(elf, r->r_addend, sym); | |
1514 | tosym = sym_name(elf, to); | |
1515 | ||
644e8f14 QC |
1516 | /* check whitelist - we may ignore it */ |
1517 | if (secref_whitelist(mismatch, | |
1518 | fromsec, fromsym, tosec, tosym)) { | |
1519 | report_sec_mismatch(modname, mismatch, | |
1520 | fromsec, r->r_offset, fromsym, | |
1521 | is_function(from), tosec, tosym, | |
1522 | is_function(to)); | |
1523 | } | |
1524 | } | |
1525 | ||
52dc0595 QC |
1526 | static int is_executable_section(struct elf_info* elf, unsigned int section_index) |
1527 | { | |
1528 | if (section_index > elf->num_sections) | |
1529 | fatal("section_index is outside elf->num_sections!\n"); | |
1530 | ||
1531 | return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR); | |
1532 | } | |
1533 | ||
1534 | /* | |
1535 | * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size() | |
1536 | * to know the sizeof(struct exception_table_entry) for the target architecture. | |
1537 | */ | |
1538 | static unsigned int extable_entry_size = 0; | |
e84048aa | 1539 | static void find_extable_entry_size(const char* const sec, const Elf_Rela* r) |
52dc0595 QC |
1540 | { |
1541 | /* | |
1542 | * If we're currently checking the second relocation within __ex_table, | |
1543 | * that relocation offset tells us the offsetof(struct | |
1544 | * exception_table_entry, fixup) which is equal to sizeof(struct | |
1545 | * exception_table_entry) divided by two. We use that to our advantage | |
1546 | * since there's no portable way to get that size as every architecture | |
1547 | * seems to go with different sized types. Not pretty but better than | |
1548 | * hard-coding the size for every architecture.. | |
1549 | */ | |
e84048aa | 1550 | if (!extable_entry_size) |
52dc0595 QC |
1551 | extable_entry_size = r->r_offset * 2; |
1552 | } | |
e84048aa | 1553 | |
52dc0595 QC |
1554 | static inline bool is_extable_fault_address(Elf_Rela *r) |
1555 | { | |
d3df4de7 QC |
1556 | /* |
1557 | * extable_entry_size is only discovered after we've handled the | |
1558 | * _second_ relocation in __ex_table, so only abort when we're not | |
1559 | * handling the first reloc and extable_entry_size is zero. | |
1560 | */ | |
1561 | if (r->r_offset && extable_entry_size == 0) | |
52dc0595 QC |
1562 | fatal("extable_entry size hasn't been discovered!\n"); |
1563 | ||
1564 | return ((r->r_offset == 0) || | |
1565 | (r->r_offset % extable_entry_size == 0)); | |
1566 | } | |
1567 | ||
e84048aa QC |
1568 | #define is_second_extable_reloc(Start, Cur, Sec) \ |
1569 | (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0)) | |
1570 | ||
52dc0595 QC |
1571 | static void report_extable_warnings(const char* modname, struct elf_info* elf, |
1572 | const struct sectioncheck* const mismatch, | |
1573 | Elf_Rela* r, Elf_Sym* sym, | |
1574 | const char* fromsec, const char* tosec) | |
1575 | { | |
1576 | Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec); | |
1577 | const char* fromsym_name = sym_name(elf, fromsym); | |
1578 | Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym); | |
1579 | const char* tosym_name = sym_name(elf, tosym); | |
1580 | const char* from_pretty_name; | |
1581 | const char* from_pretty_name_p; | |
1582 | const char* to_pretty_name; | |
1583 | const char* to_pretty_name_p; | |
1584 | ||
1585 | get_pretty_name(is_function(fromsym), | |
1586 | &from_pretty_name, &from_pretty_name_p); | |
1587 | get_pretty_name(is_function(tosym), | |
1588 | &to_pretty_name, &to_pretty_name_p); | |
1589 | ||
1590 | warn("%s(%s+0x%lx): Section mismatch in reference" | |
1591 | " from the %s %s%s to the %s %s:%s%s\n", | |
1592 | modname, fromsec, (long)r->r_offset, from_pretty_name, | |
1593 | fromsym_name, from_pretty_name_p, | |
1594 | to_pretty_name, tosec, tosym_name, to_pretty_name_p); | |
1595 | ||
1596 | if (!match(tosec, mismatch->bad_tosec) && | |
1597 | is_executable_section(elf, get_secindex(elf, sym))) | |
1598 | fprintf(stderr, | |
1599 | "The relocation at %s+0x%lx references\n" | |
1600 | "section \"%s\" which is not in the list of\n" | |
1601 | "authorized sections. If you're adding a new section\n" | |
1602 | "and/or if this reference is valid, add \"%s\" to the\n" | |
1603 | "list of authorized sections to jump to on fault.\n" | |
1604 | "This can be achieved by adding \"%s\" to \n" | |
1605 | "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n", | |
1606 | fromsec, (long)r->r_offset, tosec, tosec, tosec); | |
1607 | } | |
1608 | ||
1609 | static void extable_mismatch_handler(const char* modname, struct elf_info *elf, | |
1610 | const struct sectioncheck* const mismatch, | |
1611 | Elf_Rela* r, Elf_Sym* sym, | |
1612 | const char *fromsec) | |
1613 | { | |
1614 | const char* tosec = sec_name(elf, get_secindex(elf, sym)); | |
1615 | ||
1616 | sec_mismatch_count++; | |
1617 | ||
46c7dd56 | 1618 | report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec); |
52dc0595 QC |
1619 | |
1620 | if (match(tosec, mismatch->bad_tosec)) | |
1621 | fatal("The relocation at %s+0x%lx references\n" | |
1622 | "section \"%s\" which is black-listed.\n" | |
1623 | "Something is seriously wrong and should be fixed.\n" | |
1624 | "You might get more information about where this is\n" | |
1625 | "coming from by using scripts/check_extable.sh %s\n", | |
1626 | fromsec, (long)r->r_offset, tosec, modname); | |
1627 | else if (!is_executable_section(elf, get_secindex(elf, sym))) { | |
1628 | if (is_extable_fault_address(r)) | |
1629 | fatal("The relocation at %s+0x%lx references\n" | |
1630 | "section \"%s\" which is not executable, IOW\n" | |
1631 | "it is not possible for the kernel to fault\n" | |
1632 | "at that address. Something is seriously wrong\n" | |
1633 | "and should be fixed.\n", | |
1634 | fromsec, (long)r->r_offset, tosec); | |
1635 | else | |
1636 | fatal("The relocation at %s+0x%lx references\n" | |
1637 | "section \"%s\" which is not executable, IOW\n" | |
1638 | "the kernel will fault if it ever tries to\n" | |
1639 | "jump to it. Something is seriously wrong\n" | |
1640 | "and should be fixed.\n", | |
1641 | fromsec, (long)r->r_offset, tosec); | |
1642 | } | |
1643 | } | |
1644 | ||
644e8f14 QC |
1645 | static void check_section_mismatch(const char *modname, struct elf_info *elf, |
1646 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) | |
1647 | { | |
0cad61d7 | 1648 | const char *tosec = sec_name(elf, get_secindex(elf, sym)); |
644e8f14 QC |
1649 | const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec); |
1650 | ||
0d2a636e | 1651 | if (mismatch) { |
644e8f14 QC |
1652 | if (mismatch->handler) |
1653 | mismatch->handler(modname, elf, mismatch, | |
1654 | r, sym, fromsec); | |
1655 | else | |
1656 | default_mismatch_handler(modname, elf, mismatch, | |
1657 | r, sym, fromsec); | |
b39927cf SR |
1658 | } |
1659 | } | |
1660 | ||
ae4ac123 | 1661 | static unsigned int *reloc_location(struct elf_info *elf, |
5b24c071 | 1662 | Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 | 1663 | { |
d2e4d05c | 1664 | return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset); |
ae4ac123 AN |
1665 | } |
1666 | ||
5b24c071 | 1667 | static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1668 | { |
1669 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1670 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1671 | |
1672 | switch (r_typ) { | |
1673 | case R_386_32: | |
1674 | r->r_addend = TO_NATIVE(*location); | |
1675 | break; | |
1676 | case R_386_PC32: | |
1677 | r->r_addend = TO_NATIVE(*location) + 4; | |
1678 | /* For CONFIG_RELOCATABLE=y */ | |
1679 | if (elf->hdr->e_type == ET_EXEC) | |
1680 | r->r_addend += r->r_offset; | |
1681 | break; | |
1682 | } | |
1683 | return 0; | |
1684 | } | |
1685 | ||
6e2e340b TL |
1686 | #ifndef R_ARM_CALL |
1687 | #define R_ARM_CALL 28 | |
1688 | #endif | |
1689 | #ifndef R_ARM_JUMP24 | |
1690 | #define R_ARM_JUMP24 29 | |
1691 | #endif | |
1692 | ||
c9698e5c DL |
1693 | #ifndef R_ARM_THM_CALL |
1694 | #define R_ARM_THM_CALL 10 | |
1695 | #endif | |
1696 | #ifndef R_ARM_THM_JUMP24 | |
1697 | #define R_ARM_THM_JUMP24 30 | |
1698 | #endif | |
1699 | #ifndef R_ARM_THM_JUMP19 | |
1700 | #define R_ARM_THM_JUMP19 51 | |
1701 | #endif | |
1702 | ||
5b24c071 | 1703 | static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
56a974fa SR |
1704 | { |
1705 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
1706 | ||
1707 | switch (r_typ) { | |
1708 | case R_ARM_ABS32: | |
1709 | /* From ARM ABI: (S + A) | T */ | |
df578e7d | 1710 | r->r_addend = (int)(long) |
bb66fc67 | 1711 | (elf->symtab_start + ELF_R_SYM(r->r_info)); |
56a974fa SR |
1712 | break; |
1713 | case R_ARM_PC24: | |
6e2e340b TL |
1714 | case R_ARM_CALL: |
1715 | case R_ARM_JUMP24: | |
c9698e5c DL |
1716 | case R_ARM_THM_CALL: |
1717 | case R_ARM_THM_JUMP24: | |
1718 | case R_ARM_THM_JUMP19: | |
56a974fa | 1719 | /* From ARM ABI: ((S + A) | T) - P */ |
df578e7d | 1720 | r->r_addend = (int)(long)(elf->hdr + |
bb66fc67 MY |
1721 | sechdr->sh_offset + |
1722 | (r->r_offset - sechdr->sh_addr)); | |
56a974fa SR |
1723 | break; |
1724 | default: | |
1725 | return 1; | |
1726 | } | |
1727 | return 0; | |
1728 | } | |
1729 | ||
5b24c071 | 1730 | static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1731 | { |
1732 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1733 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1734 | unsigned int inst; |
1735 | ||
1736 | if (r_typ == R_MIPS_HI16) | |
1737 | return 1; /* skip this */ | |
1738 | inst = TO_NATIVE(*location); | |
1739 | switch (r_typ) { | |
1740 | case R_MIPS_LO16: | |
1741 | r->r_addend = inst & 0xffff; | |
1742 | break; | |
1743 | case R_MIPS_26: | |
1744 | r->r_addend = (inst & 0x03ffffff) << 2; | |
1745 | break; | |
1746 | case R_MIPS_32: | |
1747 | r->r_addend = inst; | |
1748 | break; | |
1749 | } | |
1750 | return 0; | |
1751 | } | |
1752 | ||
bb1f85d6 JZ |
1753 | #ifndef EM_RISCV |
1754 | #define EM_RISCV 243 | |
1755 | #endif | |
1756 | ||
1757 | #ifndef R_RISCV_SUB32 | |
1758 | #define R_RISCV_SUB32 39 | |
1759 | #endif | |
1760 | ||
5b24c071 | 1761 | static void section_rela(const char *modname, struct elf_info *elf, |
bb66fc67 | 1762 | Elf_Shdr *sechdr) |
5b24c071 SR |
1763 | { |
1764 | Elf_Sym *sym; | |
1765 | Elf_Rela *rela; | |
1766 | Elf_Rela r; | |
1767 | unsigned int r_sym; | |
1768 | const char *fromsec; | |
5b24c071 | 1769 | |
ff13f926 | 1770 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1771 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
1772 | ||
ff13f926 | 1773 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1774 | fromsec += strlen(".rela"); |
1775 | /* if from section (name) is know good then skip it */ | |
b614a697 | 1776 | if (match(fromsec, section_white_list)) |
5b24c071 | 1777 | return; |
e241a630 | 1778 | |
5b24c071 SR |
1779 | for (rela = start; rela < stop; rela++) { |
1780 | r.r_offset = TO_NATIVE(rela->r_offset); | |
1781 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1782 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1783 | unsigned int r_typ; |
1784 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); | |
1785 | r_sym = TO_NATIVE(r_sym); | |
1786 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); | |
1787 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1788 | } else { | |
1789 | r.r_info = TO_NATIVE(rela->r_info); | |
1790 | r_sym = ELF_R_SYM(r.r_info); | |
1791 | } | |
1792 | #else | |
1793 | r.r_info = TO_NATIVE(rela->r_info); | |
1794 | r_sym = ELF_R_SYM(r.r_info); | |
1795 | #endif | |
1796 | r.r_addend = TO_NATIVE(rela->r_addend); | |
bb1f85d6 JZ |
1797 | switch (elf->hdr->e_machine) { |
1798 | case EM_RISCV: | |
1799 | if (!strcmp("__ex_table", fromsec) && | |
1800 | ELF_R_TYPE(r.r_info) == R_RISCV_SUB32) | |
1801 | continue; | |
1802 | break; | |
1803 | } | |
5b24c071 SR |
1804 | sym = elf->symtab_start + r_sym; |
1805 | /* Skip special sections */ | |
1ce53adf | 1806 | if (is_shndx_special(sym->st_shndx)) |
5b24c071 | 1807 | continue; |
e84048aa QC |
1808 | if (is_second_extable_reloc(start, rela, fromsec)) |
1809 | find_extable_entry_size(fromsec, &r); | |
58fb0d4f | 1810 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1811 | } |
1812 | } | |
1813 | ||
1814 | static void section_rel(const char *modname, struct elf_info *elf, | |
bb66fc67 | 1815 | Elf_Shdr *sechdr) |
5b24c071 SR |
1816 | { |
1817 | Elf_Sym *sym; | |
1818 | Elf_Rel *rel; | |
1819 | Elf_Rela r; | |
1820 | unsigned int r_sym; | |
1821 | const char *fromsec; | |
5b24c071 | 1822 | |
ff13f926 | 1823 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1824 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
1825 | ||
ff13f926 | 1826 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1827 | fromsec += strlen(".rel"); |
1828 | /* if from section (name) is know good then skip it */ | |
b614a697 | 1829 | if (match(fromsec, section_white_list)) |
5b24c071 SR |
1830 | return; |
1831 | ||
1832 | for (rel = start; rel < stop; rel++) { | |
1833 | r.r_offset = TO_NATIVE(rel->r_offset); | |
1834 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1835 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1836 | unsigned int r_typ; |
1837 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); | |
1838 | r_sym = TO_NATIVE(r_sym); | |
1839 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); | |
1840 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1841 | } else { | |
1842 | r.r_info = TO_NATIVE(rel->r_info); | |
1843 | r_sym = ELF_R_SYM(r.r_info); | |
1844 | } | |
1845 | #else | |
1846 | r.r_info = TO_NATIVE(rel->r_info); | |
1847 | r_sym = ELF_R_SYM(r.r_info); | |
1848 | #endif | |
1849 | r.r_addend = 0; | |
ff13f926 | 1850 | switch (elf->hdr->e_machine) { |
5b24c071 SR |
1851 | case EM_386: |
1852 | if (addend_386_rel(elf, sechdr, &r)) | |
1853 | continue; | |
1854 | break; | |
1855 | case EM_ARM: | |
1856 | if (addend_arm_rel(elf, sechdr, &r)) | |
1857 | continue; | |
1858 | break; | |
1859 | case EM_MIPS: | |
1860 | if (addend_mips_rel(elf, sechdr, &r)) | |
1861 | continue; | |
1862 | break; | |
1863 | } | |
1864 | sym = elf->symtab_start + r_sym; | |
1865 | /* Skip special sections */ | |
1ce53adf | 1866 | if (is_shndx_special(sym->st_shndx)) |
5b24c071 | 1867 | continue; |
e84048aa QC |
1868 | if (is_second_extable_reloc(start, rel, fromsec)) |
1869 | find_extable_entry_size(fromsec, &r); | |
58fb0d4f | 1870 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1871 | } |
1872 | } | |
1873 | ||
b39927cf SR |
1874 | /** |
1875 | * A module includes a number of sections that are discarded | |
1876 | * either when loaded or when used as built-in. | |
1877 | * For loaded modules all functions marked __init and all data | |
b595076a | 1878 | * marked __initdata will be discarded when the module has been initialized. |
b39927cf SR |
1879 | * Likewise for modules used built-in the sections marked __exit |
1880 | * are discarded because __exit marked function are supposed to be called | |
32be1d22 | 1881 | * only when a module is unloaded which never happens for built-in modules. |
b39927cf SR |
1882 | * The check_sec_ref() function traverses all relocation records |
1883 | * to find all references to a section that reference a section that will | |
1884 | * be discarded and warns about it. | |
1885 | **/ | |
1886 | static void check_sec_ref(struct module *mod, const char *modname, | |
bb66fc67 | 1887 | struct elf_info *elf) |
b39927cf SR |
1888 | { |
1889 | int i; | |
b39927cf | 1890 | Elf_Shdr *sechdrs = elf->sechdrs; |
62070fa4 | 1891 | |
b39927cf | 1892 | /* Walk through all sections */ |
1ce53adf | 1893 | for (i = 0; i < elf->num_sections; i++) { |
b614a697 | 1894 | check_section(modname, elf, &elf->sechdrs[i]); |
b39927cf | 1895 | /* We want to process only relocation sections and not .init */ |
5b24c071 | 1896 | if (sechdrs[i].sh_type == SHT_RELA) |
10668220 | 1897 | section_rela(modname, elf, &elf->sechdrs[i]); |
5b24c071 | 1898 | else if (sechdrs[i].sh_type == SHT_REL) |
10668220 | 1899 | section_rel(modname, elf, &elf->sechdrs[i]); |
b39927cf SR |
1900 | } |
1901 | } | |
1902 | ||
7d02b490 AK |
1903 | static char *remove_dot(char *s) |
1904 | { | |
fcd38ed0 | 1905 | size_t n = strcspn(s, "."); |
7d02b490 | 1906 | |
fcd38ed0 MN |
1907 | if (n && s[n]) { |
1908 | size_t m = strspn(s + n + 1, "0123456789"); | |
1909 | if (m && (s[n + m] == '.' || s[n + m] == 0)) | |
7d02b490 | 1910 | s[n] = 0; |
7ac204b5 | 1911 | |
d31ed5d7 PZ |
1912 | /* strip trailing .prelink */ |
1913 | if (strends(s, ".prelink")) | |
1914 | s[strlen(s) - 8] = '\0'; | |
7d02b490 AK |
1915 | } |
1916 | return s; | |
1917 | } | |
1918 | ||
f292d875 MY |
1919 | /* |
1920 | * The CRCs are recorded in .*.cmd files in the form of: | |
1921 | * #SYMVER <name> <crc> | |
1922 | */ | |
1923 | static void extract_crcs_for_object(const char *object, struct module *mod) | |
1924 | { | |
1925 | char cmd_file[PATH_MAX]; | |
1926 | char *buf, *p; | |
1927 | const char *base; | |
1928 | int dirlen, ret; | |
1929 | ||
1930 | base = strrchr(object, '/'); | |
1931 | if (base) { | |
1932 | base++; | |
1933 | dirlen = base - object; | |
1934 | } else { | |
1935 | dirlen = 0; | |
1936 | base = object; | |
1937 | } | |
1938 | ||
1939 | ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd", | |
1940 | dirlen, object, base); | |
1941 | if (ret >= sizeof(cmd_file)) { | |
1942 | error("%s: too long path was truncated\n", cmd_file); | |
1943 | return; | |
1944 | } | |
1945 | ||
1946 | buf = read_text_file(cmd_file); | |
1947 | p = buf; | |
1948 | ||
1949 | while ((p = strstr(p, "\n#SYMVER "))) { | |
1950 | char *name; | |
1951 | size_t namelen; | |
1952 | unsigned int crc; | |
1953 | struct symbol *sym; | |
1954 | ||
1955 | name = p + strlen("\n#SYMVER "); | |
1956 | ||
1957 | p = strchr(name, ' '); | |
1958 | if (!p) | |
1959 | break; | |
1960 | ||
1961 | namelen = p - name; | |
1962 | p++; | |
1963 | ||
1964 | if (!isdigit(*p)) | |
1965 | continue; /* skip this line */ | |
1966 | ||
1967 | crc = strtol(p, &p, 0); | |
1968 | if (*p != '\n') | |
1969 | continue; /* skip this line */ | |
1970 | ||
1971 | name[namelen] = '\0'; | |
1972 | ||
1973 | /* | |
1974 | * sym_find_with_module() may return NULL here. | |
1975 | * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y. | |
1976 | * Since commit e1327a127703, genksyms calculates CRCs of all | |
1977 | * symbols, including trimmed ones. Ignore orphan CRCs. | |
1978 | */ | |
1979 | sym = sym_find_with_module(name, mod); | |
1980 | if (sym) | |
1981 | sym_set_crc(sym, crc); | |
1982 | } | |
1983 | ||
1984 | free(buf); | |
1985 | } | |
1986 | ||
1987 | /* | |
1988 | * The symbol versions (CRC) are recorded in the .*.cmd files. | |
1989 | * Parse them to retrieve CRCs for the current module. | |
1990 | */ | |
1991 | static void mod_set_crcs(struct module *mod) | |
1992 | { | |
1993 | char objlist[PATH_MAX]; | |
1994 | char *buf, *p, *obj; | |
1995 | int ret; | |
1996 | ||
1997 | if (mod->is_vmlinux) { | |
1998 | strcpy(objlist, ".vmlinux.objs"); | |
1999 | } else { | |
2000 | /* objects for a module are listed in the *.mod file. */ | |
2001 | ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name); | |
2002 | if (ret >= sizeof(objlist)) { | |
2003 | error("%s: too long path was truncated\n", objlist); | |
2004 | return; | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | buf = read_text_file(objlist); | |
2009 | p = buf; | |
2010 | ||
2011 | while ((obj = strsep(&p, "\n")) && obj[0]) | |
2012 | extract_crcs_for_object(obj, mod); | |
2013 | ||
2014 | free(buf); | |
2015 | } | |
2016 | ||
8b185743 | 2017 | static void read_symbols(const char *modname) |
1da177e4 LT |
2018 | { |
2019 | const char *symname; | |
2020 | char *version; | |
b817f6fe | 2021 | char *license; |
cb9b55d2 | 2022 | char *namespace; |
1da177e4 LT |
2023 | struct module *mod; |
2024 | struct elf_info info = { }; | |
2025 | Elf_Sym *sym; | |
2026 | ||
85bd2fdd SR |
2027 | if (!parse_elf(&info, modname)) |
2028 | return; | |
1da177e4 | 2029 | |
a82f794c MY |
2030 | { |
2031 | char *tmp; | |
2032 | ||
2033 | /* strip trailing .o */ | |
2034 | tmp = NOFAIL(strdup(modname)); | |
2035 | tmp[strlen(tmp) - 2] = '\0'; | |
d31ed5d7 PZ |
2036 | /* strip trailing .prelink */ |
2037 | if (strends(tmp, ".prelink")) | |
2038 | tmp[strlen(tmp) - 8] = '\0'; | |
a82f794c MY |
2039 | mod = new_module(tmp); |
2040 | free(tmp); | |
2041 | } | |
1da177e4 | 2042 | |
5a438af9 | 2043 | if (!mod->is_vmlinux) { |
4ddea2f8 MY |
2044 | license = get_modinfo(&info, "license"); |
2045 | if (!license) | |
1d6cd392 | 2046 | error("missing MODULE_LICENSE() in %s\n", modname); |
4ddea2f8 | 2047 | while (license) { |
5066743e MY |
2048 | if (!license_is_gpl_compatible(license)) { |
2049 | mod->is_gpl_compatible = false; | |
4ddea2f8 MY |
2050 | break; |
2051 | } | |
2052 | license = get_next_modinfo(&info, "license", license); | |
b817f6fe | 2053 | } |
b817f6fe | 2054 | |
4ddea2f8 MY |
2055 | namespace = get_modinfo(&info, "import_ns"); |
2056 | while (namespace) { | |
2057 | add_namespace(&mod->imported_namespaces, namespace); | |
2058 | namespace = get_next_modinfo(&info, "import_ns", | |
2059 | namespace); | |
2060 | } | |
cb9b55d2 MM |
2061 | } |
2062 | ||
1da177e4 | 2063 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
7d02b490 | 2064 | symname = remove_dot(info.strtab + sym->st_name); |
1da177e4 | 2065 | |
9bd2a099 | 2066 | handle_symbol(mod, &info, sym, symname); |
1da177e4 LT |
2067 | handle_moddevtable(mod, &info, sym, symname); |
2068 | } | |
15bfc234 | 2069 | |
69923208 MM |
2070 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
2071 | symname = remove_dot(info.strtab + sym->st_name); | |
2072 | ||
1743694e | 2073 | /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */ |
69923208 MM |
2074 | if (strstarts(symname, "__kstrtabns_")) |
2075 | sym_update_namespace(symname + strlen("__kstrtabns_"), | |
79f646e8 | 2076 | sym_get_data(&info, sym)); |
69923208 MM |
2077 | } |
2078 | ||
15bfc234 DE |
2079 | // check for static EXPORT_SYMBOL_* functions && global vars |
2080 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { | |
2081 | unsigned char bind = ELF_ST_BIND(sym->st_info); | |
2082 | ||
2083 | if (bind == STB_GLOBAL || bind == STB_WEAK) { | |
2084 | struct symbol *s = | |
2085 | find_symbol(remove_dot(info.strtab + | |
2086 | sym->st_name)); | |
2087 | ||
2088 | if (s) | |
58e01fca | 2089 | s->is_static = false; |
15bfc234 DE |
2090 | } |
2091 | } | |
2092 | ||
467b82d7 | 2093 | check_sec_ref(mod, modname, &info); |
1da177e4 | 2094 | |
5a438af9 | 2095 | if (!mod->is_vmlinux) { |
4ddea2f8 MY |
2096 | version = get_modinfo(&info, "version"); |
2097 | if (version || all_versions) | |
e54dd93a | 2098 | get_src_version(mod->name, mod->srcversion, |
4ddea2f8 MY |
2099 | sizeof(mod->srcversion) - 1); |
2100 | } | |
1da177e4 LT |
2101 | |
2102 | parse_elf_finish(&info); | |
2103 | ||
f292d875 MY |
2104 | if (modversions) { |
2105 | /* | |
2106 | * Our trick to get versioning for module struct etc. - it's | |
2107 | * never passed as an argument to an exported function, so | |
2108 | * the automatic versioning doesn't pick it up, but it's really | |
2109 | * important anyhow. | |
2110 | */ | |
e882e89b | 2111 | sym_add_unresolved("module_layout", mod, false); |
f292d875 MY |
2112 | |
2113 | mod_set_crcs(mod); | |
2114 | } | |
1da177e4 LT |
2115 | } |
2116 | ||
712f9b46 RR |
2117 | static void read_symbols_from_files(const char *filename) |
2118 | { | |
2119 | FILE *in = stdin; | |
2120 | char fname[PATH_MAX]; | |
2121 | ||
2122 | if (strcmp(filename, "-") != 0) { | |
2123 | in = fopen(filename, "r"); | |
2124 | if (!in) | |
2125 | fatal("Can't open filenames file %s: %m", filename); | |
2126 | } | |
2127 | ||
2128 | while (fgets(fname, PATH_MAX, in) != NULL) { | |
2129 | if (strends(fname, "\n")) | |
2130 | fname[strlen(fname)-1] = '\0'; | |
2131 | read_symbols(fname); | |
2132 | } | |
2133 | ||
2134 | if (in != stdin) | |
2135 | fclose(in); | |
2136 | } | |
2137 | ||
1da177e4 LT |
2138 | #define SZ 500 |
2139 | ||
2140 | /* We first write the generated file into memory using the | |
2141 | * following helper, then compare to the file on disk and | |
2142 | * only update the later if anything changed */ | |
2143 | ||
5c3ead8c SR |
2144 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
2145 | const char *fmt, ...) | |
1da177e4 LT |
2146 | { |
2147 | char tmp[SZ]; | |
2148 | int len; | |
2149 | va_list ap; | |
62070fa4 | 2150 | |
1da177e4 LT |
2151 | va_start(ap, fmt); |
2152 | len = vsnprintf(tmp, SZ, fmt, ap); | |
7670f023 | 2153 | buf_write(buf, tmp, len); |
1da177e4 LT |
2154 | va_end(ap); |
2155 | } | |
2156 | ||
5c3ead8c | 2157 | void buf_write(struct buffer *buf, const char *s, int len) |
1da177e4 LT |
2158 | { |
2159 | if (buf->size - buf->pos < len) { | |
7670f023 | 2160 | buf->size += len + SZ; |
1f3aa900 | 2161 | buf->p = NOFAIL(realloc(buf->p, buf->size)); |
1da177e4 LT |
2162 | } |
2163 | strncpy(buf->p + buf->pos, s, len); | |
2164 | buf->pos += len; | |
2165 | } | |
2166 | ||
0fd3fbad | 2167 | static void check_exports(struct module *mod) |
b817f6fe SR |
2168 | { |
2169 | struct symbol *s, *exp; | |
2170 | ||
8a69152b | 2171 | list_for_each_entry(s, &mod->unresolved_symbols, list) { |
6449bd62 | 2172 | const char *basename; |
b817f6fe | 2173 | exp = find_symbol(s->name); |
23beb44a | 2174 | if (!exp) { |
4475dff5 | 2175 | if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS) |
93c95e52 JY |
2176 | modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR, |
2177 | "\"%s\" [%s.ko] undefined!\n", | |
2178 | s->name, mod->name); | |
b817f6fe | 2179 | continue; |
3b415288 | 2180 | } |
23beb44a MY |
2181 | if (exp->module == mod) { |
2182 | error("\"%s\" [%s.ko] was exported without definition\n", | |
2183 | s->name, mod->name); | |
2184 | continue; | |
2185 | } | |
4cae77ac MY |
2186 | |
2187 | s->module = exp->module; | |
2188 | s->crc_valid = exp->crc_valid; | |
2189 | s->crc = exp->crc; | |
2190 | ||
6449bd62 | 2191 | basename = strrchr(mod->name, '/'); |
b817f6fe SR |
2192 | if (basename) |
2193 | basename++; | |
c96fca21 SR |
2194 | else |
2195 | basename = mod->name; | |
cb9b55d2 | 2196 | |
bbc55bde | 2197 | if (exp->namespace && |
ab489d60 | 2198 | !contains_namespace(&mod->imported_namespaces, exp->namespace)) { |
54b77847 JY |
2199 | modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR, |
2200 | "module %s uses symbol %s from namespace %s, but does not import it.\n", | |
2201 | basename, exp->name, exp->namespace); | |
bbc55bde | 2202 | add_namespace(&mod->missing_namespaces, exp->namespace); |
cb9b55d2 MM |
2203 | } |
2204 | ||
2a66c312 MY |
2205 | if (!mod->is_gpl_compatible && exp->is_gpl_only) |
2206 | error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n", | |
2207 | basename, exp->name); | |
df578e7d | 2208 | } |
b817f6fe SR |
2209 | } |
2210 | ||
0fd3fbad | 2211 | static void check_modname_len(struct module *mod) |
4fd3e4ef WG |
2212 | { |
2213 | const char *mod_name; | |
2214 | ||
2215 | mod_name = strrchr(mod->name, '/'); | |
2216 | if (mod_name == NULL) | |
2217 | mod_name = mod->name; | |
2218 | else | |
2219 | mod_name++; | |
0fd3fbad | 2220 | if (strlen(mod_name) >= MODULE_NAME_LEN) |
bc72d723 | 2221 | error("module name is too long [%s.ko]\n", mod->name); |
4fd3e4ef WG |
2222 | } |
2223 | ||
5c3ead8c SR |
2224 | /** |
2225 | * Header for the generated file | |
2226 | **/ | |
2227 | static void add_header(struct buffer *b, struct module *mod) | |
1da177e4 LT |
2228 | { |
2229 | buf_printf(b, "#include <linux/module.h>\n"); | |
f58dd03b VF |
2230 | /* |
2231 | * Include build-salt.h after module.h in order to | |
2232 | * inherit the definitions. | |
2233 | */ | |
51161bfc | 2234 | buf_printf(b, "#define INCLUDE_VERMAGIC\n"); |
f58dd03b | 2235 | buf_printf(b, "#include <linux/build-salt.h>\n"); |
1fdd7433 | 2236 | buf_printf(b, "#include <linux/elfnote-lto.h>\n"); |
7b453719 | 2237 | buf_printf(b, "#include <linux/export-internal.h>\n"); |
1da177e4 LT |
2238 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
2239 | buf_printf(b, "#include <linux/compiler.h>\n"); | |
2240 | buf_printf(b, "\n"); | |
9afb719e | 2241 | buf_printf(b, "BUILD_SALT;\n"); |
1fdd7433 | 2242 | buf_printf(b, "BUILD_LTO_INFO;\n"); |
9afb719e | 2243 | buf_printf(b, "\n"); |
1da177e4 | 2244 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
3e2e857f | 2245 | buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); |
1da177e4 | 2246 | buf_printf(b, "\n"); |
e0f244c6 | 2247 | buf_printf(b, "__visible struct module __this_module\n"); |
33def849 | 2248 | buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n"); |
3c7ec94d | 2249 | buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); |
1da177e4 | 2250 | if (mod->has_init) |
3c7ec94d | 2251 | buf_printf(b, "\t.init = init_module,\n"); |
1da177e4 LT |
2252 | if (mod->has_cleanup) |
2253 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" | |
3c7ec94d | 2254 | "\t.exit = cleanup_module,\n" |
1da177e4 | 2255 | "#endif\n"); |
3c7ec94d | 2256 | buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n"); |
1da177e4 | 2257 | buf_printf(b, "};\n"); |
1da177e4 | 2258 | |
7fedac96 | 2259 | if (!external_module) |
2449b8ba | 2260 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); |
2449b8ba | 2261 | |
7fedac96 MY |
2262 | buf_printf(b, |
2263 | "\n" | |
2264 | "#ifdef CONFIG_RETPOLINE\n" | |
2265 | "MODULE_INFO(retpoline, \"Y\");\n" | |
2266 | "#endif\n"); | |
caf7501a | 2267 | |
7fedac96 | 2268 | if (strstarts(mod->name, "drivers/staging")) |
a9860bf0 GKH |
2269 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); |
2270 | } | |
2271 | ||
7b453719 | 2272 | static void add_exported_symbols(struct buffer *buf, struct module *mod) |
f292d875 MY |
2273 | { |
2274 | struct symbol *sym; | |
2275 | ||
2276 | if (!modversions) | |
2277 | return; | |
2278 | ||
7b453719 MY |
2279 | /* record CRCs for exported symbols */ |
2280 | buf_printf(buf, "\n"); | |
f292d875 MY |
2281 | list_for_each_entry(sym, &mod->exported_symbols, list) { |
2282 | if (!sym->crc_valid) { | |
2283 | warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n" | |
2284 | "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n", | |
2285 | sym->name, mod->name, mod->is_vmlinux ? "" : ".ko", | |
2286 | sym->name); | |
7b453719 | 2287 | continue; |
f292d875 | 2288 | } |
7b453719 MY |
2289 | |
2290 | buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n", | |
2291 | sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : ""); | |
f292d875 MY |
2292 | } |
2293 | } | |
2294 | ||
5c3ead8c SR |
2295 | /** |
2296 | * Record CRCs for unresolved symbols | |
2297 | **/ | |
0fd3fbad | 2298 | static void add_versions(struct buffer *b, struct module *mod) |
1da177e4 | 2299 | { |
4cae77ac | 2300 | struct symbol *s; |
1da177e4 LT |
2301 | |
2302 | if (!modversions) | |
0fd3fbad | 2303 | return; |
1da177e4 LT |
2304 | |
2305 | buf_printf(b, "\n"); | |
2306 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); | |
33def849 | 2307 | buf_printf(b, "__used __section(\"__versions\") = {\n"); |
1da177e4 | 2308 | |
8a69152b | 2309 | list_for_each_entry(s, &mod->unresolved_symbols, list) { |
df578e7d | 2310 | if (!s->module) |
1da177e4 | 2311 | continue; |
1da177e4 | 2312 | if (!s->crc_valid) { |
cb80514d | 2313 | warn("\"%s\" [%s.ko] has no CRC!\n", |
1da177e4 LT |
2314 | s->name, mod->name); |
2315 | continue; | |
2316 | } | |
5cfb203a | 2317 | if (strlen(s->name) >= MODULE_NAME_LEN) { |
bc72d723 MY |
2318 | error("too long symbol \"%s\" [%s.ko]\n", |
2319 | s->name, mod->name); | |
5cfb203a TI |
2320 | break; |
2321 | } | |
b2c5cdcf | 2322 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", |
a4b6a77b | 2323 | s->crc, s->name); |
1da177e4 LT |
2324 | } |
2325 | ||
2326 | buf_printf(b, "};\n"); | |
2327 | } | |
2328 | ||
d2665ca8 | 2329 | static void add_depends(struct buffer *b, struct module *mod) |
1da177e4 LT |
2330 | { |
2331 | struct symbol *s; | |
1da177e4 LT |
2332 | int first = 1; |
2333 | ||
d2665ca8 | 2334 | /* Clear ->seen flag of modules that own symbols needed by this. */ |
8a69152b | 2335 | list_for_each_entry(s, &mod->unresolved_symbols, list) { |
d2665ca8 | 2336 | if (s->module) |
5a438af9 | 2337 | s->module->seen = s->module->is_vmlinux; |
8a69152b | 2338 | } |
1da177e4 LT |
2339 | |
2340 | buf_printf(b, "\n"); | |
6df7e1ec | 2341 | buf_printf(b, "MODULE_INFO(depends, \""); |
8a69152b | 2342 | list_for_each_entry(s, &mod->unresolved_symbols, list) { |
a61b2dfd | 2343 | const char *p; |
1da177e4 LT |
2344 | if (!s->module) |
2345 | continue; | |
2346 | ||
2347 | if (s->module->seen) | |
2348 | continue; | |
2349 | ||
58e01fca | 2350 | s->module->seen = true; |
df578e7d SR |
2351 | p = strrchr(s->module->name, '/'); |
2352 | if (p) | |
a61b2dfd SR |
2353 | p++; |
2354 | else | |
2355 | p = s->module->name; | |
2356 | buf_printf(b, "%s%s", first ? "" : ",", p); | |
1da177e4 LT |
2357 | first = 0; |
2358 | } | |
6df7e1ec | 2359 | buf_printf(b, "\");\n"); |
1da177e4 LT |
2360 | } |
2361 | ||
5c3ead8c | 2362 | static void add_srcversion(struct buffer *b, struct module *mod) |
1da177e4 LT |
2363 | { |
2364 | if (mod->srcversion[0]) { | |
2365 | buf_printf(b, "\n"); | |
2366 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", | |
2367 | mod->srcversion); | |
2368 | } | |
2369 | } | |
2370 | ||
436b2ac6 MY |
2371 | static void write_buf(struct buffer *b, const char *fname) |
2372 | { | |
2373 | FILE *file; | |
2374 | ||
c155a47d MY |
2375 | if (error_occurred) |
2376 | return; | |
2377 | ||
436b2ac6 MY |
2378 | file = fopen(fname, "w"); |
2379 | if (!file) { | |
2380 | perror(fname); | |
2381 | exit(1); | |
2382 | } | |
2383 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { | |
2384 | perror(fname); | |
2385 | exit(1); | |
2386 | } | |
2387 | if (fclose(file) != 0) { | |
2388 | perror(fname); | |
2389 | exit(1); | |
2390 | } | |
2391 | } | |
2392 | ||
5c3ead8c | 2393 | static void write_if_changed(struct buffer *b, const char *fname) |
1da177e4 LT |
2394 | { |
2395 | char *tmp; | |
2396 | FILE *file; | |
2397 | struct stat st; | |
2398 | ||
2399 | file = fopen(fname, "r"); | |
2400 | if (!file) | |
2401 | goto write; | |
2402 | ||
2403 | if (fstat(fileno(file), &st) < 0) | |
2404 | goto close_write; | |
2405 | ||
2406 | if (st.st_size != b->pos) | |
2407 | goto close_write; | |
2408 | ||
2409 | tmp = NOFAIL(malloc(b->pos)); | |
2410 | if (fread(tmp, 1, b->pos, file) != b->pos) | |
2411 | goto free_write; | |
2412 | ||
2413 | if (memcmp(tmp, b->p, b->pos) != 0) | |
2414 | goto free_write; | |
2415 | ||
2416 | free(tmp); | |
2417 | fclose(file); | |
2418 | return; | |
2419 | ||
2420 | free_write: | |
2421 | free(tmp); | |
2422 | close_write: | |
2423 | fclose(file); | |
2424 | write: | |
436b2ac6 | 2425 | write_buf(b, fname); |
1da177e4 LT |
2426 | } |
2427 | ||
7b453719 MY |
2428 | static void write_vmlinux_export_c_file(struct module *mod) |
2429 | { | |
2430 | struct buffer buf = { }; | |
2431 | ||
2432 | buf_printf(&buf, | |
2433 | "#include <linux/export-internal.h>\n"); | |
2434 | ||
2435 | add_exported_symbols(&buf, mod); | |
2436 | write_if_changed(&buf, ".vmlinux.export.c"); | |
2437 | free(buf.p); | |
2438 | } | |
2439 | ||
a44abaca MY |
2440 | /* do sanity checks, and generate *.mod.c file */ |
2441 | static void write_mod_c_file(struct module *mod) | |
2442 | { | |
2443 | struct buffer buf = { }; | |
2444 | char fname[PATH_MAX]; | |
2445 | int ret; | |
2446 | ||
2447 | check_modname_len(mod); | |
2448 | check_exports(mod); | |
2449 | ||
2450 | add_header(&buf, mod); | |
7b453719 | 2451 | add_exported_symbols(&buf, mod); |
a44abaca MY |
2452 | add_versions(&buf, mod); |
2453 | add_depends(&buf, mod); | |
2454 | add_moddevtable(&buf, mod); | |
2455 | add_srcversion(&buf, mod); | |
2456 | ||
2457 | ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name); | |
2458 | if (ret >= sizeof(fname)) { | |
2459 | error("%s: too long path was truncated\n", fname); | |
2460 | goto free; | |
2461 | } | |
2462 | ||
2463 | write_if_changed(&buf, fname); | |
2464 | ||
2465 | free: | |
2466 | free(buf.p); | |
2467 | } | |
2468 | ||
bd5cbced | 2469 | /* parse Module.symvers file. line format: |
5190044c | 2470 | * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace |
bd5cbced | 2471 | **/ |
52c3416d | 2472 | static void read_dump(const char *fname) |
1da177e4 | 2473 | { |
70f30cfe | 2474 | char *buf, *pos, *line; |
1da177e4 | 2475 | |
70f30cfe MY |
2476 | buf = read_text_file(fname); |
2477 | if (!buf) | |
1da177e4 LT |
2478 | /* No symbol versions, silently ignore */ |
2479 | return; | |
2480 | ||
70f30cfe MY |
2481 | pos = buf; |
2482 | ||
2483 | while ((line = get_line(&pos))) { | |
5190044c | 2484 | char *symname, *namespace, *modname, *d, *export; |
1da177e4 LT |
2485 | unsigned int crc; |
2486 | struct module *mod; | |
040fcc81 | 2487 | struct symbol *s; |
2a66c312 | 2488 | bool gpl_only; |
1da177e4 LT |
2489 | |
2490 | if (!(symname = strchr(line, '\t'))) | |
2491 | goto fail; | |
2492 | *symname++ = '\0'; | |
5190044c | 2493 | if (!(modname = strchr(symname, '\t'))) |
1da177e4 LT |
2494 | goto fail; |
2495 | *modname++ = '\0'; | |
5190044c JY |
2496 | if (!(export = strchr(modname, '\t'))) |
2497 | goto fail; | |
2498 | *export++ = '\0'; | |
2499 | if (!(namespace = strchr(export, '\t'))) | |
2500 | goto fail; | |
2501 | *namespace++ = '\0'; | |
2502 | ||
1da177e4 LT |
2503 | crc = strtoul(line, &d, 16); |
2504 | if (*symname == '\0' || *modname == '\0' || *d != '\0') | |
2505 | goto fail; | |
2a66c312 MY |
2506 | |
2507 | if (!strcmp(export, "EXPORT_SYMBOL_GPL")) { | |
2508 | gpl_only = true; | |
2509 | } else if (!strcmp(export, "EXPORT_SYMBOL")) { | |
2510 | gpl_only = false; | |
2511 | } else { | |
2512 | error("%s: unknown license %s. skip", symname, export); | |
2513 | continue; | |
2514 | } | |
2515 | ||
df578e7d SR |
2516 | mod = find_module(modname); |
2517 | if (!mod) { | |
0fa3a88c | 2518 | mod = new_module(modname); |
58e01fca | 2519 | mod->from_dump = true; |
1da177e4 | 2520 | } |
2a66c312 | 2521 | s = sym_add_exported(symname, mod, gpl_only); |
58e01fca | 2522 | s->is_static = false; |
f292d875 | 2523 | sym_set_crc(s, crc); |
9ae5bd18 | 2524 | sym_update_namespace(symname, namespace); |
1da177e4 | 2525 | } |
70f30cfe | 2526 | free(buf); |
1da177e4 LT |
2527 | return; |
2528 | fail: | |
70f30cfe | 2529 | free(buf); |
1da177e4 LT |
2530 | fatal("parse error in symbol dump file\n"); |
2531 | } | |
2532 | ||
5c3ead8c | 2533 | static void write_dump(const char *fname) |
1da177e4 LT |
2534 | { |
2535 | struct buffer buf = { }; | |
f841536e MY |
2536 | struct module *mod; |
2537 | struct symbol *sym; | |
2538 | ||
2539 | list_for_each_entry(mod, &modules, list) { | |
2540 | if (mod->from_dump) | |
2541 | continue; | |
2542 | list_for_each_entry(sym, &mod->exported_symbols, list) { | |
2a66c312 | 2543 | buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n", |
f841536e | 2544 | sym->crc, sym->name, mod->name, |
2a66c312 | 2545 | sym->is_gpl_only ? "_GPL" : "", |
f841536e | 2546 | sym->namespace ?: ""); |
1da177e4 LT |
2547 | } |
2548 | } | |
436b2ac6 | 2549 | write_buf(&buf, fname); |
c7d47f26 | 2550 | free(buf.p); |
1da177e4 LT |
2551 | } |
2552 | ||
bbc55bde | 2553 | static void write_namespace_deps_files(const char *fname) |
1d082773 MM |
2554 | { |
2555 | struct module *mod; | |
2556 | struct namespace_list *ns; | |
2557 | struct buffer ns_deps_buf = {}; | |
2558 | ||
325eba05 | 2559 | list_for_each_entry(mod, &modules, list) { |
1d082773 | 2560 | |
ab489d60 | 2561 | if (mod->from_dump || list_empty(&mod->missing_namespaces)) |
1d082773 MM |
2562 | continue; |
2563 | ||
bbc55bde | 2564 | buf_printf(&ns_deps_buf, "%s.ko:", mod->name); |
1d082773 | 2565 | |
ab489d60 | 2566 | list_for_each_entry(ns, &mod->missing_namespaces, list) |
bbc55bde | 2567 | buf_printf(&ns_deps_buf, " %s", ns->namespace); |
1d082773 | 2568 | |
bbc55bde | 2569 | buf_printf(&ns_deps_buf, "\n"); |
1d082773 | 2570 | } |
0241ea8c | 2571 | |
bbc55bde | 2572 | write_if_changed(&ns_deps_buf, fname); |
0241ea8c | 2573 | free(ns_deps_buf.p); |
1d082773 MM |
2574 | } |
2575 | ||
7924799e | 2576 | struct dump_list { |
44840548 | 2577 | struct list_head list; |
2d04b5ae RH |
2578 | const char *file; |
2579 | }; | |
2580 | ||
5c3ead8c | 2581 | int main(int argc, char **argv) |
1da177e4 LT |
2582 | { |
2583 | struct module *mod; | |
bbc55bde | 2584 | char *missing_namespace_deps = NULL; |
712f9b46 | 2585 | char *dump_write = NULL, *files_source = NULL; |
1da177e4 | 2586 | int opt; |
15bfc234 | 2587 | int n; |
44840548 MY |
2588 | LIST_HEAD(dump_lists); |
2589 | struct dump_list *dl, *dl2; | |
1da177e4 | 2590 | |
467b82d7 | 2591 | while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) { |
df578e7d | 2592 | switch (opt) { |
2d04b5ae | 2593 | case 'e': |
58e01fca | 2594 | external_module = true; |
e3fb4df7 MY |
2595 | break; |
2596 | case 'i': | |
44840548 MY |
2597 | dl = NOFAIL(malloc(sizeof(*dl))); |
2598 | dl->file = optarg; | |
2599 | list_add_tail(&dl->list, &dump_lists); | |
2d04b5ae | 2600 | break; |
df578e7d | 2601 | case 'm': |
58e01fca | 2602 | modversions = true; |
df578e7d | 2603 | break; |
eed380f3 | 2604 | case 'n': |
58e01fca | 2605 | ignore_missing_files = true; |
eed380f3 | 2606 | break; |
df578e7d SR |
2607 | case 'o': |
2608 | dump_write = optarg; | |
2609 | break; | |
2610 | case 'a': | |
58e01fca | 2611 | all_versions = true; |
df578e7d | 2612 | break; |
712f9b46 RR |
2613 | case 'T': |
2614 | files_source = optarg; | |
2615 | break; | |
df578e7d | 2616 | case 'w': |
58e01fca | 2617 | warn_unresolved = true; |
df578e7d | 2618 | break; |
47490ec1 | 2619 | case 'E': |
c7299d98 | 2620 | sec_mismatch_warn_only = false; |
47490ec1 | 2621 | break; |
54b77847 | 2622 | case 'N': |
58e01fca | 2623 | allow_missing_ns_imports = true; |
54b77847 | 2624 | break; |
1d082773 | 2625 | case 'd': |
bbc55bde | 2626 | missing_namespace_deps = optarg; |
1d082773 | 2627 | break; |
df578e7d SR |
2628 | default: |
2629 | exit(1); | |
1da177e4 LT |
2630 | } |
2631 | } | |
2632 | ||
44840548 MY |
2633 | list_for_each_entry_safe(dl, dl2, &dump_lists, list) { |
2634 | read_dump(dl->file); | |
2635 | list_del(&dl->list); | |
2636 | free(dl); | |
2d04b5ae | 2637 | } |
1da177e4 | 2638 | |
df578e7d | 2639 | while (optind < argc) |
1da177e4 | 2640 | read_symbols(argv[optind++]); |
1da177e4 | 2641 | |
712f9b46 RR |
2642 | if (files_source) |
2643 | read_symbols_from_files(files_source); | |
2644 | ||
325eba05 | 2645 | list_for_each_entry(mod, &modules, list) { |
a44abaca | 2646 | if (mod->from_dump) |
15a28c7c | 2647 | continue; |
15a28c7c | 2648 | |
7b453719 MY |
2649 | if (mod->is_vmlinux) |
2650 | write_vmlinux_export_c_file(mod); | |
2651 | else | |
a44abaca | 2652 | write_mod_c_file(mod); |
1da177e4 | 2653 | } |
1d082773 | 2654 | |
bbc55bde MY |
2655 | if (missing_namespace_deps) |
2656 | write_namespace_deps_files(missing_namespace_deps); | |
1d082773 | 2657 | |
1da177e4 LT |
2658 | if (dump_write) |
2659 | write_dump(dump_write); | |
c7299d98 MY |
2660 | if (sec_mismatch_count && !sec_mismatch_warn_only) |
2661 | error("Section mismatches detected.\n" | |
46c7dd56 | 2662 | "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); |
15bfc234 | 2663 | for (n = 0; n < SYMBOL_HASH_SIZE; n++) { |
47346e96 MY |
2664 | struct symbol *s; |
2665 | ||
2666 | for (s = symbolhash[n]; s; s = s->next) { | |
15bfc234 | 2667 | if (s->is_static) |
2a66c312 MY |
2668 | error("\"%s\" [%s] is a static EXPORT_SYMBOL\n", |
2669 | s->name, s->module->name); | |
15bfc234 DE |
2670 | } |
2671 | } | |
2672 | ||
4475dff5 MY |
2673 | if (nr_unresolved > MAX_UNRESOLVED_REPORTS) |
2674 | warn("suppressed %u unresolved symbol warnings because there were too many)\n", | |
2675 | nr_unresolved - MAX_UNRESOLVED_REPORTS); | |
2676 | ||
0fd3fbad | 2677 | return error_occurred ? 1 : 0; |
1da177e4 | 2678 | } |