| 1 | |
| 2 | *
|
| 3 | * Copyright 2003 Kai Germaschewski
|
| 4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation
|
| 5 | * Copyright 2006-2008 Sam Ravnborg
|
| 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 |
|
| 14 | #define _GNU_SOURCE
|
| 15 | #include <elf.h>
|
| 16 | #include <fnmatch.h>
|
| 17 | #include <stdio.h>
|
| 18 | #include <ctype.h>
|
| 19 | #include <string.h>
|
| 20 | #include <limits.h>
|
| 21 | #include <stdbool.h>
|
| 22 | #include <errno.h>
|
| 23 |
|
| 24 | #include <hash.h>
|
| 25 | #include <hashtable.h>
|
| 26 | #include <list.h>
|
| 27 | #include <xalloc.h>
|
| 28 | #include "modpost.h"
|
| 29 | #include "../../include/linux/license.h"
|
| 30 |
|
| 31 | #define MODULE_NS_PREFIX "module:"
|
| 32 |
|
| 33 | static bool module_enabled;
|
| 34 |
|
| 35 | static bool modversions;
|
| 36 |
|
| 37 | static bool all_versions;
|
| 38 |
|
| 39 | static bool basic_modversions;
|
| 40 |
|
| 41 | static bool extended_modversions;
|
| 42 |
|
| 43 | static bool external_module;
|
| 44 |
|
| 45 | static bool warn_unresolved;
|
| 46 |
|
| 47 | static int sec_mismatch_count;
|
| 48 | static bool sec_mismatch_warn_only = true;
|
| 49 |
|
| 50 | static bool trim_unused_exports;
|
| 51 |
|
| 52 |
|
| 53 | static bool ignore_missing_files;
|
| 54 |
|
| 55 | static bool allow_missing_ns_imports;
|
| 56 |
|
| 57 | static bool error_occurred;
|
| 58 |
|
| 59 | static bool extra_warn __attribute__((unused));
|
| 60 |
|
| 61 | bool target_is_big_endian;
|
| 62 | bool host_is_big_endian;
|
| 63 |
|
| 64 | |
| 65 | * Cut off the warnings when there are too many. This typically occurs when
|
| 66 | * vmlinux is missing. ('make modules' without building vmlinux.)
|
| 67 |
|
| 68 | #define MAX_UNRESOLVED_REPORTS 10
|
| 69 | static unsigned int nr_unresolved;
|
| 70 |
|
| 71 | |
| 72 | * here we use Elf_Addr instead of long for covering cross-compile
|
| 73 |
|
| 74 |
|
| 75 | #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
|
| 76 |
|
| 77 | void modpost_log(bool is_error, struct module *mod, const char *fmt, ...)
|
| 78 | {
|
| 79 | va_list arglist;
|
| 80 |
|
| 81 | if (is_error) {
|
| 82 | fprintf(stderr, "ERROR: ");
|
| 83 | error_occurred = true;
|
| 84 | } else {
|
| 85 | fprintf(stderr, "WARNING: ");
|
| 86 | }
|
| 87 |
|
| 88 | fprintf(stderr, "modpost: ");
|
| 89 |
|
| 90 | if (mod)
|
| 91 | fprintf(stderr, "%s%s: ", mod->name, mod->is_vmlinux ? "" : ".ko");
|
| 92 |
|
| 93 | va_start(arglist, fmt);
|
| 94 | vfprintf(stderr, fmt, arglist);
|
| 95 | va_end(arglist);
|
| 96 | }
|
| 97 |
|
| 98 | #define mod_warn(mod, fmt, args...) modpost_log(false, mod, fmt, ##args)
|
| 99 | #define mod_error(mod, fmt, args...) modpost_log(true, mod, fmt, ##args)
|
| 100 |
|
| 101 | static inline bool strends(const char *str, const char *postfix)
|
| 102 | {
|
| 103 | if (strlen(str) < strlen(postfix))
|
| 104 | return false;
|
| 105 |
|
| 106 | return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | * get_basename - return the last part of a pathname.
|
| 111 | *
|
| 112 | * @path: path to extract the filename from.
|
| 113 |
|
| 114 | const char *get_basename(const char *path)
|
| 115 | {
|
| 116 | const char *tail = strrchr(path, '/');
|
| 117 |
|
| 118 | return tail ? tail + 1 : path;
|
| 119 | }
|
| 120 |
|
| 121 | char *read_text_file(const char *filename)
|
| 122 | {
|
| 123 | struct stat st;
|
| 124 | size_t nbytes;
|
| 125 | int fd;
|
| 126 | char *buf;
|
| 127 |
|
| 128 | fd = open(filename, O_RDONLY);
|
| 129 | if (fd < 0) {
|
| 130 | perror(filename);
|
| 131 | exit(1);
|
| 132 | }
|
| 133 |
|
| 134 | if (fstat(fd, &st) < 0) {
|
| 135 | perror(filename);
|
| 136 | exit(1);
|
| 137 | }
|
| 138 |
|
| 139 | buf = xmalloc(st.st_size + 1);
|
| 140 |
|
| 141 | nbytes = st.st_size;
|
| 142 |
|
| 143 | while (nbytes) {
|
| 144 | ssize_t bytes_read;
|
| 145 |
|
| 146 | bytes_read = read(fd, buf, nbytes);
|
| 147 | if (bytes_read < 0) {
|
| 148 | perror(filename);
|
| 149 | exit(1);
|
| 150 | }
|
| 151 |
|
| 152 | nbytes -= bytes_read;
|
| 153 | }
|
| 154 | buf[st.st_size] = '\0';
|
| 155 |
|
| 156 | close(fd);
|
| 157 |
|
| 158 | return buf;
|
| 159 | }
|
| 160 |
|
| 161 | char *get_line(char **stringp)
|
| 162 | {
|
| 163 | char *orig = *stringp, *next;
|
| 164 |
|
| 165 |
|
| 166 | if (!orig || *orig == '\0')
|
| 167 | return NULL;
|
| 168 |
|
| 169 |
|
| 170 | next = strchr(orig, '\n');
|
| 171 | if (next)
|
| 172 | *next++ = '\0';
|
| 173 |
|
| 174 | *stringp = next;
|
| 175 |
|
| 176 | return orig;
|
| 177 | }
|
| 178 |
|
| 179 |
|
| 180 | LIST_HEAD(modules);
|
| 181 |
|
| 182 | static struct module *find_module(const char *filename, const char *modname)
|
| 183 | {
|
| 184 | struct module *mod;
|
| 185 |
|
| 186 | list_for_each_entry(mod, &modules, list) {
|
| 187 | if (!strcmp(mod->dump_file, filename) &&
|
| 188 | !strcmp(mod->name, modname))
|
| 189 | return mod;
|
| 190 | }
|
| 191 | return NULL;
|
| 192 | }
|
| 193 |
|
| 194 | static struct module *new_module(const char *name, size_t namelen)
|
| 195 | {
|
| 196 | struct module *mod;
|
| 197 |
|
| 198 | mod = xmalloc(sizeof(*mod) + namelen + 1);
|
| 199 | memset(mod, 0, sizeof(*mod));
|
| 200 |
|
| 201 | INIT_LIST_HEAD(&mod->exported_symbols);
|
| 202 | INIT_LIST_HEAD(&mod->unresolved_symbols);
|
| 203 | INIT_LIST_HEAD(&mod->missing_namespaces);
|
| 204 | INIT_LIST_HEAD(&mod->imported_namespaces);
|
| 205 | INIT_LIST_HEAD(&mod->aliases);
|
| 206 |
|
| 207 | memcpy(mod->name, name, namelen);
|
| 208 | mod->name[namelen] = '\0';
|
| 209 | mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
|
| 210 |
|
| 211 | |
| 212 | * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
|
| 213 | * is missing, do not check the use for EXPORT_SYMBOL_GPL() because
|
| 214 | * modpost will exit with an error anyway.
|
| 215 |
|
| 216 | mod->is_gpl_compatible = true;
|
| 217 |
|
| 218 | list_add_tail(&mod->list, &modules);
|
| 219 |
|
| 220 | return mod;
|
| 221 | }
|
| 222 |
|
| 223 | struct symbol {
|
| 224 | struct hlist_node hnode;
|
| 225 | struct list_head list;
|
| 226 | struct module *module;
|
| 227 | char *namespace;
|
| 228 | unsigned int crc;
|
| 229 | bool crc_valid;
|
| 230 | bool weak;
|
| 231 | bool is_func;
|
| 232 | bool is_gpl_only;
|
| 233 | bool used;
|
| 234 | char name[];
|
| 235 | };
|
| 236 |
|
| 237 | static HASHTABLE_DEFINE(symbol_hashtable, 1U << 10);
|
| 238 |
|
| 239 | |
| 240 | * Allocate a new symbols for use in the hash of exported symbols or
|
| 241 | * the list of unresolved symbols per module
|
| 242 | *
|
| 243 | static struct symbol *alloc_symbol(const char *name)
|
| 244 | {
|
| 245 | struct symbol *s = xmalloc(sizeof(*s) + strlen(name) + 1);
|
| 246 |
|
| 247 | memset(s, 0, sizeof(*s));
|
| 248 | strcpy(s->name, name);
|
| 249 |
|
| 250 | return s;
|
| 251 | }
|
| 252 |
|
| 253 | static uint8_t get_symbol_flags(const struct symbol *sym)
|
| 254 | {
|
| 255 | return sym->is_gpl_only ? KSYM_FLAG_GPL_ONLY : 0;
|
| 256 | }
|
| 257 |
|
| 258 |
|
| 259 | static void hash_add_symbol(struct symbol *sym)
|
| 260 | {
|
| 261 | hash_add(symbol_hashtable, &sym->hnode, hash_str(sym->name));
|
| 262 | }
|
| 263 |
|
| 264 | static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
|
| 265 | {
|
| 266 | struct symbol *sym;
|
| 267 |
|
| 268 | sym = alloc_symbol(name);
|
| 269 | sym->weak = weak;
|
| 270 |
|
| 271 | list_add_tail(&sym->list, &mod->unresolved_symbols);
|
| 272 | }
|
| 273 |
|
| 274 | static struct symbol *sym_find_with_module(const char *name, struct module *mod)
|
| 275 | {
|
| 276 | struct symbol *s;
|
| 277 |
|
| 278 |
|
| 279 | if (name[0] == '.')
|
| 280 | name++;
|
| 281 |
|
| 282 | hash_for_each_possible(symbol_hashtable, s, hnode, hash_str(name)) {
|
| 283 | if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
|
| 284 | return s;
|
| 285 | }
|
| 286 | return NULL;
|
| 287 | }
|
| 288 |
|
| 289 | static struct symbol *find_symbol(const char *name)
|
| 290 | {
|
| 291 | return sym_find_with_module(name, NULL);
|
| 292 | }
|
| 293 |
|
| 294 | struct namespace_list {
|
| 295 | struct list_head list;
|
| 296 | char namespace[];
|
| 297 | };
|
| 298 |
|
| 299 | static bool contains_namespace(struct list_head *head, const char *namespace)
|
| 300 | {
|
| 301 | struct namespace_list *list;
|
| 302 |
|
| 303 | |
| 304 | * The default namespace is null string "", which is always implicitly
|
| 305 | * contained.
|
| 306 |
|
| 307 | if (!namespace[0])
|
| 308 | return true;
|
| 309 |
|
| 310 | list_for_each_entry(list, head, list) {
|
| 311 | if (!strcmp(list->namespace, namespace))
|
| 312 | return true;
|
| 313 | }
|
| 314 |
|
| 315 | return false;
|
| 316 | }
|
| 317 |
|
| 318 | static void add_namespace(struct list_head *head, const char *namespace)
|
| 319 | {
|
| 320 | struct namespace_list *ns_entry;
|
| 321 |
|
| 322 | if (!contains_namespace(head, namespace)) {
|
| 323 | ns_entry = xmalloc(sizeof(*ns_entry) + strlen(namespace) + 1);
|
| 324 | strcpy(ns_entry->namespace, namespace);
|
| 325 | list_add_tail(&ns_entry->list, head);
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | static void *sym_get_data_by_offset(const struct elf_info *info,
|
| 330 | unsigned int secindex, unsigned long offset)
|
| 331 | {
|
| 332 | Elf_Shdr *sechdr = &info->sechdrs[secindex];
|
| 333 |
|
| 334 | return (void *)info->hdr + sechdr->sh_offset + offset;
|
| 335 | }
|
| 336 |
|
| 337 | void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
|
| 338 | {
|
| 339 | return sym_get_data_by_offset(info, get_secindex(info, sym),
|
| 340 | sym->st_value);
|
| 341 | }
|
| 342 |
|
| 343 | static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
|
| 344 | {
|
| 345 | return sym_get_data_by_offset(info, info->secindex_strings,
|
| 346 | sechdr->sh_name);
|
| 347 | }
|
| 348 |
|
| 349 | static const char *sec_name(const struct elf_info *info, unsigned int secindex)
|
| 350 | {
|
| 351 | |
| 352 | * If sym->st_shndx is a special section index, there is no
|
| 353 | * corresponding section header.
|
| 354 | * Return "" if the index is out of range of info->sechdrs[] array.
|
| 355 |
|
| 356 | if (secindex >= info->num_sections)
|
| 357 | return "";
|
| 358 |
|
| 359 | return sech_name(info, &info->sechdrs[secindex]);
|
| 360 | }
|
| 361 |
|
| 362 | static struct symbol *sym_add_exported(const char *name, struct module *mod,
|
| 363 | bool gpl_only, const char *namespace)
|
| 364 | {
|
| 365 | struct symbol *s = find_symbol(name);
|
| 366 |
|
| 367 | if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
|
| 368 | mod_error(mod, "symbol '%s' exported twice. Previous export was in %s%s\n",
|
| 369 | name, s->module->name, s->module->is_vmlinux ? "" : ".ko");
|
| 370 | }
|
| 371 |
|
| 372 | s = alloc_symbol(name);
|
| 373 | s->module = mod;
|
| 374 | s->is_gpl_only = gpl_only;
|
| 375 | s->namespace = xstrdup(namespace);
|
| 376 | list_add_tail(&s->list, &mod->exported_symbols);
|
| 377 | hash_add_symbol(s);
|
| 378 |
|
| 379 | return s;
|
| 380 | }
|
| 381 |
|
| 382 | static void sym_set_crc(struct symbol *sym, unsigned int crc)
|
| 383 | {
|
| 384 | sym->crc = crc;
|
| 385 | sym->crc_valid = true;
|
| 386 | }
|
| 387 |
|
| 388 | static void *grab_file(const char *filename, size_t *size)
|
| 389 | {
|
| 390 | struct stat st;
|
| 391 | void *map = MAP_FAILED;
|
| 392 | int fd;
|
| 393 |
|
| 394 | fd = open(filename, O_RDONLY);
|
| 395 | if (fd < 0)
|
| 396 | return NULL;
|
| 397 | if (fstat(fd, &st))
|
| 398 | goto failed;
|
| 399 |
|
| 400 | *size = st.st_size;
|
| 401 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
| 402 |
|
| 403 | failed:
|
| 404 | close(fd);
|
| 405 | if (map == MAP_FAILED)
|
| 406 | return NULL;
|
| 407 | return map;
|
| 408 | }
|
| 409 |
|
| 410 | static void release_file(void *file, size_t size)
|
| 411 | {
|
| 412 | munmap(file, size);
|
| 413 | }
|
| 414 |
|
| 415 | static int parse_elf(struct elf_info *info, const char *filename)
|
| 416 | {
|
| 417 | unsigned int i;
|
| 418 | Elf_Ehdr *hdr;
|
| 419 | Elf_Shdr *sechdrs;
|
| 420 | Elf_Sym *sym;
|
| 421 | const char *secstrings;
|
| 422 | unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
|
| 423 |
|
| 424 | hdr = grab_file(filename, &info->size);
|
| 425 | if (!hdr) {
|
| 426 | if (ignore_missing_files) {
|
| 427 | fprintf(stderr, "%s: %s (ignored)\n", filename,
|
| 428 | strerror(errno));
|
| 429 | return 0;
|
| 430 | }
|
| 431 | perror(filename);
|
| 432 | exit(1);
|
| 433 | }
|
| 434 | info->hdr = hdr;
|
| 435 | if (info->size < sizeof(*hdr)) {
|
| 436 |
|
| 437 | return 0;
|
| 438 | }
|
| 439 |
|
| 440 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
|
| 441 | (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
|
| 442 | (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
|
| 443 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
|
| 444 |
|
| 445 | return 0;
|
| 446 | }
|
| 447 |
|
| 448 | switch (hdr->e_ident[EI_DATA]) {
|
| 449 | case ELFDATA2LSB:
|
| 450 | target_is_big_endian = false;
|
| 451 | break;
|
| 452 | case ELFDATA2MSB:
|
| 453 | target_is_big_endian = true;
|
| 454 | break;
|
| 455 | default:
|
| 456 | fatal("target endian is unknown\n");
|
| 457 | }
|
| 458 |
|
| 459 |
|
| 460 | hdr->e_type = TO_NATIVE(hdr->e_type);
|
| 461 | hdr->e_machine = TO_NATIVE(hdr->e_machine);
|
| 462 | hdr->e_version = TO_NATIVE(hdr->e_version);
|
| 463 | hdr->e_entry = TO_NATIVE(hdr->e_entry);
|
| 464 | hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
|
| 465 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
|
| 466 | hdr->e_flags = TO_NATIVE(hdr->e_flags);
|
| 467 | hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
|
| 468 | hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
|
| 469 | hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
|
| 470 | hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
|
| 471 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
|
| 472 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
|
| 473 | sechdrs = (void *)hdr + hdr->e_shoff;
|
| 474 | info->sechdrs = sechdrs;
|
| 475 |
|
| 476 |
|
| 477 | if (hdr->e_type != ET_REL)
|
| 478 | fatal("%s: not relocatable object.", filename);
|
| 479 |
|
| 480 |
|
| 481 | if (hdr->e_shoff > info->size)
|
| 482 | fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
|
| 483 | (unsigned long)hdr->e_shoff, filename, info->size);
|
| 484 |
|
| 485 | if (hdr->e_shnum == SHN_UNDEF) {
|
| 486 | |
| 487 | * There are more than 64k sections,
|
| 488 | * read count from .sh_size.
|
| 489 |
|
| 490 | info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
|
| 491 | }
|
| 492 | else {
|
| 493 | info->num_sections = hdr->e_shnum;
|
| 494 | }
|
| 495 | if (hdr->e_shstrndx == SHN_XINDEX) {
|
| 496 | info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
|
| 497 | }
|
| 498 | else {
|
| 499 | info->secindex_strings = hdr->e_shstrndx;
|
| 500 | }
|
| 501 |
|
| 502 |
|
| 503 | for (i = 0; i < info->num_sections; i++) {
|
| 504 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
|
| 505 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
|
| 506 | sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
|
| 507 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
|
| 508 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
|
| 509 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
|
| 510 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
|
| 511 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
|
| 512 | sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
|
| 513 | sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
|
| 514 | }
|
| 515 |
|
| 516 | secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
|
| 517 | for (i = 1; i < info->num_sections; i++) {
|
| 518 | const char *secname;
|
| 519 | int nobits = sechdrs[i].sh_type == SHT_NOBITS;
|
| 520 |
|
| 521 | if (!nobits && sechdrs[i].sh_offset > info->size)
|
| 522 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
|
| 523 | filename, (unsigned long)sechdrs[i].sh_offset,
|
| 524 | sizeof(*hdr));
|
| 525 |
|
| 526 | secname = secstrings + sechdrs[i].sh_name;
|
| 527 | if (strcmp(secname, ".modinfo") == 0) {
|
| 528 | if (nobits)
|
| 529 | fatal("%s has NOBITS .modinfo\n", filename);
|
| 530 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
|
| 531 | info->modinfo_len = sechdrs[i].sh_size;
|
| 532 | } else if (!strcmp(secname, ".export_symbol")) {
|
| 533 | info->export_symbol_secndx = i;
|
| 534 | } else if (!strcmp(secname, ".no_trim_symbol")) {
|
| 535 | info->no_trim_symbol = (void *)hdr + sechdrs[i].sh_offset;
|
| 536 | info->no_trim_symbol_len = sechdrs[i].sh_size;
|
| 537 | }
|
| 538 |
|
| 539 | if (sechdrs[i].sh_type == SHT_SYMTAB) {
|
| 540 | unsigned int sh_link_idx;
|
| 541 | symtab_idx = i;
|
| 542 | info->symtab_start = (void *)hdr +
|
| 543 | sechdrs[i].sh_offset;
|
| 544 | info->symtab_stop = (void *)hdr +
|
| 545 | sechdrs[i].sh_offset + sechdrs[i].sh_size;
|
| 546 | sh_link_idx = sechdrs[i].sh_link;
|
| 547 | info->strtab = (void *)hdr +
|
| 548 | sechdrs[sh_link_idx].sh_offset;
|
| 549 | }
|
| 550 |
|
| 551 |
|
| 552 | if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
|
| 553 | symtab_shndx_idx = i;
|
| 554 | info->symtab_shndx_start = (void *)hdr +
|
| 555 | sechdrs[i].sh_offset;
|
| 556 | info->symtab_shndx_stop = (void *)hdr +
|
| 557 | sechdrs[i].sh_offset + sechdrs[i].sh_size;
|
| 558 | }
|
| 559 | }
|
| 560 | if (!info->symtab_start)
|
| 561 | fatal("%s has no symtab?\n", filename);
|
| 562 |
|
| 563 |
|
| 564 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
|
| 565 | sym->st_shndx = TO_NATIVE(sym->st_shndx);
|
| 566 | sym->st_name = TO_NATIVE(sym->st_name);
|
| 567 | sym->st_value = TO_NATIVE(sym->st_value);
|
| 568 | sym->st_size = TO_NATIVE(sym->st_size);
|
| 569 | }
|
| 570 |
|
| 571 | if (symtab_shndx_idx != ~0U) {
|
| 572 | Elf32_Word *p;
|
| 573 | if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
|
| 574 | fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
|
| 575 | filename, sechdrs[symtab_shndx_idx].sh_link,
|
| 576 | symtab_idx);
|
| 577 |
|
| 578 | for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
|
| 579 | p++)
|
| 580 | *p = TO_NATIVE(*p);
|
| 581 | }
|
| 582 |
|
| 583 | symsearch_init(info);
|
| 584 |
|
| 585 | return 1;
|
| 586 | }
|
| 587 |
|
| 588 | static void parse_elf_finish(struct elf_info *info)
|
| 589 | {
|
| 590 | symsearch_finish(info);
|
| 591 | release_file(info->hdr, info->size);
|
| 592 | }
|
| 593 |
|
| 594 | static int ignore_undef_symbol(struct elf_info *info, const char *symname)
|
| 595 | {
|
| 596 |
|
| 597 | if (strcmp(symname, "__this_module") == 0)
|
| 598 | return 1;
|
| 599 |
|
| 600 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
|
| 601 | return 1;
|
| 602 | if (info->hdr->e_machine == EM_PPC)
|
| 603 |
|
| 604 | if (strstarts(symname, "_restgpr_") ||
|
| 605 | strstarts(symname, "_savegpr_") ||
|
| 606 | strstarts(symname, "_rest32gpr_") ||
|
| 607 | strstarts(symname, "_save32gpr_") ||
|
| 608 | strstarts(symname, "_restvr_") ||
|
| 609 | strstarts(symname, "_savevr_"))
|
| 610 | return 1;
|
| 611 | if (info->hdr->e_machine == EM_PPC64)
|
| 612 |
|
| 613 | if (strstarts(symname, "_restgpr0_") ||
|
| 614 | strstarts(symname, "_savegpr0_") ||
|
| 615 | strstarts(symname, "_restgpr1_") ||
|
| 616 | strstarts(symname, "_savegpr1_") ||
|
| 617 | strstarts(symname, "_restfpr_") ||
|
| 618 | strstarts(symname, "_savefpr_") ||
|
| 619 | strstarts(symname, "_restvr_") ||
|
| 620 | strstarts(symname, "_savevr_") ||
|
| 621 | strcmp(symname, ".TOC.") == 0)
|
| 622 | return 1;
|
| 623 |
|
| 624 |
|
| 625 | if (strstarts(symname, "__start_") || strstarts(symname, "__stop_"))
|
| 626 | return 1;
|
| 627 |
|
| 628 |
|
| 629 | return 0;
|
| 630 | }
|
| 631 |
|
| 632 | static void handle_symbol(struct module *mod, struct elf_info *info,
|
| 633 | const Elf_Sym *sym, const char *symname)
|
| 634 | {
|
| 635 | switch (sym->st_shndx) {
|
| 636 | case SHN_COMMON:
|
| 637 | if (strstarts(symname, "__gnu_lto_")) {
|
| 638 |
|
| 639 | } else
|
| 640 | mod_warn(mod, "'%s' is COMMON symbol\n", symname);
|
| 641 | break;
|
| 642 | case SHN_UNDEF:
|
| 643 |
|
| 644 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
|
| 645 | ELF_ST_BIND(sym->st_info) != STB_WEAK)
|
| 646 | break;
|
| 647 | if (ignore_undef_symbol(info, symname))
|
| 648 | break;
|
| 649 | if (info->hdr->e_machine == EM_SPARC ||
|
| 650 | info->hdr->e_machine == EM_SPARCV9) {
|
| 651 |
|
| 652 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
|
| 653 | break;
|
| 654 | if (symname[0] == '.') {
|
| 655 | char *munged = xstrdup(symname);
|
| 656 | munged[0] = '_';
|
| 657 | munged[1] = toupper(munged[1]);
|
| 658 | symname = munged;
|
| 659 | }
|
| 660 | }
|
| 661 |
|
| 662 | sym_add_unresolved(symname, mod,
|
| 663 | ELF_ST_BIND(sym->st_info) == STB_WEAK);
|
| 664 | break;
|
| 665 | default:
|
| 666 | if (strcmp(symname, "init_module") == 0)
|
| 667 | mod->has_init = true;
|
| 668 | if (strcmp(symname, "cleanup_module") == 0)
|
| 669 | mod->has_cleanup = true;
|
| 670 | break;
|
| 671 | }
|
| 672 | }
|
| 673 |
|
| 674 | |
| 675 | * Parse tag=value strings from .modinfo section
|
| 676 | *
|
| 677 | static char *next_string(char *string, unsigned long *secsize)
|
| 678 | {
|
| 679 |
|
| 680 | while (string[0]) {
|
| 681 | string++;
|
| 682 | if ((*secsize)-- <= 1)
|
| 683 | return NULL;
|
| 684 | }
|
| 685 |
|
| 686 |
|
| 687 | while (!string[0]) {
|
| 688 | string++;
|
| 689 | if ((*secsize)-- <= 1)
|
| 690 | return NULL;
|
| 691 | }
|
| 692 | return string;
|
| 693 | }
|
| 694 |
|
| 695 | static char *get_next_modinfo(struct elf_info *info, const char *tag,
|
| 696 | char *prev)
|
| 697 | {
|
| 698 | char *p;
|
| 699 | unsigned int taglen = strlen(tag);
|
| 700 | char *modinfo = info->modinfo;
|
| 701 | unsigned long size = info->modinfo_len;
|
| 702 |
|
| 703 | if (prev) {
|
| 704 | size -= prev - modinfo;
|
| 705 | modinfo = next_string(prev, &size);
|
| 706 | }
|
| 707 |
|
| 708 | for (p = modinfo; p; p = next_string(p, &size)) {
|
| 709 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
|
| 710 | return p + taglen + 1;
|
| 711 | }
|
| 712 | return NULL;
|
| 713 | }
|
| 714 |
|
| 715 | static char *get_modinfo(struct elf_info *info, const char *tag)
|
| 716 |
|
| 717 | {
|
| 718 | return get_next_modinfo(info, tag, NULL);
|
| 719 | }
|
| 720 |
|
| 721 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
|
| 722 | {
|
| 723 | return sym ? elf->strtab + sym->st_name : "";
|
| 724 | }
|
| 725 |
|
| 726 | |
| 727 | * Check whether the 'string' argument matches one of the 'patterns',
|
| 728 | * an array of shell wildcard patterns (glob).
|
| 729 | *
|
| 730 | * Return true is there is a match.
|
| 731 |
|
| 732 | static bool match(const char *string, const char *const patterns[])
|
| 733 | {
|
| 734 | const char *pattern;
|
| 735 |
|
| 736 | while ((pattern = *patterns++)) {
|
| 737 | if (!fnmatch(pattern, string, 0))
|
| 738 | return true;
|
| 739 | }
|
| 740 |
|
| 741 | return false;
|
| 742 | }
|
| 743 |
|
| 744 |
|
| 745 | #define PATTERNS(...) \
|
| 746 | ({ \
|
| 747 | static const char *const patterns[] = {__VA_ARGS__, NULL}; \
|
| 748 | patterns; \
|
| 749 | })
|
| 750 |
|
| 751 |
|
| 752 | static const char *const section_white_list[] =
|
| 753 | {
|
| 754 | ".comment*",
|
| 755 | ".debug*",
|
| 756 | ".zdebug*",
|
| 757 | ".GCC.command.line",
|
| 758 | ".mdebug*",
|
| 759 | ".pdr",
|
| 760 | ".stab*",
|
| 761 | ".note*",
|
| 762 | ".got*",
|
| 763 | ".toc*",
|
| 764 | ".xt.prop",
|
| 765 | ".xt.lit",
|
| 766 | ".arcextmap*",
|
| 767 | ".gnu.linkonce.arcext*",
|
| 768 | ".cmem*",
|
| 769 | ".fmt_slot*",
|
| 770 | ".gnu.lto*",
|
| 771 | ".discard.*",
|
| 772 | ".llvm.call-graph-profile",
|
| 773 | "__llvm_covfun",
|
| 774 | "__llvm_covmap",
|
| 775 | ".klp.symid",
|
| 776 | NULL
|
| 777 | };
|
| 778 |
|
| 779 | |
| 780 | * This is used to find sections missing the SHF_ALLOC flag.
|
| 781 | * The cause of this is often a section specified in assembler
|
| 782 | * without "ax" / "aw".
|
| 783 |
|
| 784 | static void check_section(struct module *mod, struct elf_info *elf,
|
| 785 | Elf_Shdr *sechdr)
|
| 786 | {
|
| 787 | const char *sec = sech_name(elf, sechdr);
|
| 788 |
|
| 789 | if (sechdr->sh_type == SHT_PROGBITS &&
|
| 790 | !(sechdr->sh_flags & SHF_ALLOC) &&
|
| 791 | !match(sec, section_white_list)) {
|
| 792 | mod_warn(mod, "unexpected non-allocatable section '%s'.\n"
|
| 793 | "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
|
| 794 | "Note that for example <linux/init.h> contains\n"
|
| 795 | "section definitions for use in .S files.\n\n",
|
| 796 | sec);
|
| 797 | }
|
| 798 | }
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 | #define ALL_INIT_DATA_SECTIONS \
|
| 803 | ".init.setup", ".init.rodata", ".init.data"
|
| 804 |
|
| 805 | #define ALL_PCI_INIT_SECTIONS \
|
| 806 | ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
|
| 807 | ".pci_fixup_enable", ".pci_fixup_resume", \
|
| 808 | ".pci_fixup_resume_early", ".pci_fixup_suspend"
|
| 809 |
|
| 810 | #define ALL_INIT_SECTIONS ".init.*"
|
| 811 | #define ALL_EXIT_SECTIONS ".exit.*"
|
| 812 |
|
| 813 | #define DATA_SECTIONS ".data", ".data.rel"
|
| 814 | #define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
|
| 815 | ".kprobes.text", ".cpuidle.text", ".noinstr.text", \
|
| 816 | ".ltext", ".ltext.*"
|
| 817 | #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
|
| 818 | ".fixup", ".entry.text", ".exception.text", \
|
| 819 | ".coldtext", ".softirqentry.text", ".irqentry.text"
|
| 820 |
|
| 821 | #define ALL_TEXT_SECTIONS ".init.text", ".exit.text", \
|
| 822 | TEXT_SECTIONS, OTHER_TEXT_SECTIONS
|
| 823 |
|
| 824 | enum mismatch {
|
| 825 | TEXTDATA_TO_ANY_INIT_EXIT,
|
| 826 | XXXINIT_TO_SOME_INIT,
|
| 827 | ANY_INIT_TO_ANY_EXIT,
|
| 828 | ANY_EXIT_TO_ANY_INIT,
|
| 829 | EXTABLE_TO_NON_TEXT,
|
| 830 | };
|
| 831 |
|
| 832 | |
| 833 | * Describe how to match sections on different criteria:
|
| 834 | *
|
| 835 | * @fromsec: Array of sections to be matched.
|
| 836 | *
|
| 837 | * @bad_tosec: Relocations applied to a section in @fromsec to a section in
|
| 838 | * this array is forbidden (black-list). Can be empty.
|
| 839 | *
|
| 840 | * @good_tosec: Relocations applied to a section in @fromsec must be
|
| 841 | * targeting sections in this array (white-list). Can be empty.
|
| 842 | *
|
| 843 | * @mismatch: Type of mismatch.
|
| 844 |
|
| 845 | struct sectioncheck {
|
| 846 | const char *fromsec[20];
|
| 847 | const char *bad_tosec[20];
|
| 848 | const char *good_tosec[20];
|
| 849 | enum mismatch mismatch;
|
| 850 | };
|
| 851 |
|
| 852 | static const struct sectioncheck sectioncheck[] = {
|
| 853 | |
| 854 | * normal code and data
|
| 855 |
|
| 856 | {
|
| 857 | .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
|
| 858 | .bad_tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
|
| 859 | .mismatch = TEXTDATA_TO_ANY_INIT_EXIT,
|
| 860 | },
|
| 861 |
|
| 862 | {
|
| 863 | .fromsec = { ALL_INIT_SECTIONS, NULL },
|
| 864 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
|
| 865 | .mismatch = ANY_INIT_TO_ANY_EXIT,
|
| 866 | },
|
| 867 |
|
| 868 | {
|
| 869 | .fromsec = { ALL_EXIT_SECTIONS, NULL },
|
| 870 | .bad_tosec = { ALL_INIT_SECTIONS, NULL },
|
| 871 | .mismatch = ANY_EXIT_TO_ANY_INIT,
|
| 872 | },
|
| 873 | {
|
| 874 | .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
|
| 875 | .bad_tosec = { ALL_INIT_SECTIONS, NULL },
|
| 876 | .mismatch = ANY_INIT_TO_ANY_EXIT,
|
| 877 | },
|
| 878 | {
|
| 879 | .fromsec = { "__ex_table", NULL },
|
| 880 | |
| 881 | * adding a special 'printer' for them in scripts/check_extable.
|
| 882 |
|
| 883 | .bad_tosec = { ".altinstr_replacement", NULL },
|
| 884 | .good_tosec = {ALL_TEXT_SECTIONS , NULL},
|
| 885 | .mismatch = EXTABLE_TO_NON_TEXT,
|
| 886 | }
|
| 887 | };
|
| 888 |
|
| 889 | static const struct sectioncheck *section_mismatch(
|
| 890 | const char *fromsec, const char *tosec)
|
| 891 | {
|
| 892 | int i;
|
| 893 |
|
| 894 | |
| 895 | * The target section could be the SHT_NUL section when we're
|
| 896 | * handling relocations to un-resolved symbols, trying to match it
|
| 897 | * doesn't make much sense and causes build failures on parisc
|
| 898 | * architectures.
|
| 899 |
|
| 900 | if (*tosec == '\0')
|
| 901 | return NULL;
|
| 902 |
|
| 903 | for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
|
| 904 | const struct sectioncheck *check = §ioncheck[i];
|
| 905 |
|
| 906 | if (match(fromsec, check->fromsec)) {
|
| 907 | if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
|
| 908 | return check;
|
| 909 | if (check->good_tosec[0] && !match(tosec, check->good_tosec))
|
| 910 | return check;
|
| 911 | }
|
| 912 | }
|
| 913 | return NULL;
|
| 914 | }
|
| 915 |
|
| 916 | |
| 917 | * Whitelist to allow certain references to pass with no warning.
|
| 918 | *
|
| 919 | * Pattern 1:
|
| 920 | * If a module parameter is declared __initdata and permissions=0
|
| 921 | * then this is legal despite the warning generated.
|
| 922 | * We cannot see value of permissions here, so just ignore
|
| 923 | * this pattern.
|
| 924 | * The pattern is identified by:
|
| 925 | * tosec = .init.data
|
| 926 | * fromsec = .data*
|
| 927 | * atsym =__param*
|
| 928 | *
|
| 929 | * Pattern 1a:
|
| 930 | * module_param_call() ops can refer to __init set function if permissions=0
|
| 931 | * The pattern is identified by:
|
| 932 | * tosec = .init.text
|
| 933 | * fromsec = .data*
|
| 934 | * atsym = __param_ops_*
|
| 935 | *
|
| 936 | * Pattern 3:
|
| 937 | * Whitelist all references from .head.text to any init section
|
| 938 | *
|
| 939 | * Pattern 4:
|
| 940 | * Some symbols belong to init section but still it is ok to reference
|
| 941 | * these from non-init sections as these symbols don't have any memory
|
| 942 | * allocated for them and symbol address and value are same. So even
|
| 943 | * if init section is freed, its ok to reference those symbols.
|
| 944 | * For ex. symbols marking the init section boundaries.
|
| 945 | * This pattern is identified by
|
| 946 | * refsymname = __init_begin, _sinittext, _einittext
|
| 947 | *
|
| 948 | * Pattern 5:
|
| 949 | * GCC may optimize static inlines when fed constant arg(s) resulting
|
| 950 | * in functions like cpumask_empty() -- generating an associated symbol
|
| 951 | * cpumask_empty.constprop.3 that appears in the audit. If the const that
|
| 952 | * is passed in comes from __init, like say nmi_ipi_mask, we get a
|
| 953 | * meaningless section warning. May need to add isra symbols too...
|
| 954 | * This pattern is identified by
|
| 955 | * tosec = init section
|
| 956 | * fromsec = text section
|
| 957 | * refsymname = *.constprop.*
|
| 958 | *
|
| 959 | *
|
| 960 | static int secref_whitelist(const char *fromsec, const char *fromsym,
|
| 961 | const char *tosec, const char *tosym)
|
| 962 | {
|
| 963 |
|
| 964 | if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) &&
|
| 965 | match(fromsec, PATTERNS(DATA_SECTIONS)) &&
|
| 966 | strstarts(fromsym, "__param"))
|
| 967 | return 0;
|
| 968 |
|
| 969 |
|
| 970 | if (strcmp(tosec, ".init.text") == 0 &&
|
| 971 | match(fromsec, PATTERNS(DATA_SECTIONS)) &&
|
| 972 | strstarts(fromsym, "__param_ops_"))
|
| 973 | return 0;
|
| 974 |
|
| 975 |
|
| 976 | if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
|
| 977 | match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
|
| 978 | match(fromsym, PATTERNS("*_ops", "*_ops.llvm.*", "*_console")))
|
| 979 | return 0;
|
| 980 |
|
| 981 |
|
| 982 | if (strstarts(fromsec, ".head.text") &&
|
| 983 | match(tosec, PATTERNS(ALL_INIT_SECTIONS)))
|
| 984 | return 0;
|
| 985 |
|
| 986 |
|
| 987 | if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext")))
|
| 988 | return 0;
|
| 989 |
|
| 990 |
|
| 991 | if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
|
| 992 | match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
|
| 993 | match(fromsym, PATTERNS("*.constprop.*")))
|
| 994 | return 0;
|
| 995 |
|
| 996 | return 1;
|
| 997 | }
|
| 998 |
|
| 999 | static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr,
|
| 1000 | unsigned int secndx)
|
| 1001 | {
|
| 1002 | return symsearch_find_nearest(elf, addr, secndx, false, ~0);
|
| 1003 | }
|
| 1004 |
|
| 1005 | static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
|
| 1006 | {
|
| 1007 | Elf_Sym *new_sym;
|
| 1008 |
|
| 1009 |
|
| 1010 | if (is_valid_name(elf, sym))
|
| 1011 | return sym;
|
| 1012 |
|
| 1013 | |
| 1014 | * Strive to find a better symbol name, but the resulting name may not
|
| 1015 | * match the symbol referenced in the original code.
|
| 1016 |
|
| 1017 | new_sym = symsearch_find_nearest(elf, addr, get_secindex(elf, sym),
|
| 1018 | true, 20);
|
| 1019 | return new_sym ? new_sym : sym;
|
| 1020 | }
|
| 1021 |
|
| 1022 | static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
|
| 1023 | {
|
| 1024 | if (secndx >= elf->num_sections)
|
| 1025 | return false;
|
| 1026 |
|
| 1027 | return (elf->sechdrs[secndx].sh_flags & SHF_EXECINSTR) != 0;
|
| 1028 | }
|
| 1029 |
|
| 1030 | static void default_mismatch_handler(struct module *mod, struct elf_info *elf,
|
| 1031 | const struct sectioncheck* const mismatch,
|
| 1032 | Elf_Sym *tsym,
|
| 1033 | unsigned int fsecndx, const char *fromsec, Elf_Addr faddr,
|
| 1034 | const char *tosec, Elf_Addr taddr)
|
| 1035 | {
|
| 1036 | Elf_Sym *from;
|
| 1037 | const char *tosym;
|
| 1038 | const char *fromsym;
|
| 1039 | char taddr_str[16];
|
| 1040 |
|
| 1041 | from = find_fromsym(elf, faddr, fsecndx);
|
| 1042 | fromsym = sym_name(elf, from);
|
| 1043 |
|
| 1044 | tsym = find_tosym(elf, taddr, tsym);
|
| 1045 | tosym = sym_name(elf, tsym);
|
| 1046 |
|
| 1047 |
|
| 1048 | if (!secref_whitelist(fromsec, fromsym, tosec, tosym))
|
| 1049 | return;
|
| 1050 |
|
| 1051 | sec_mismatch_count++;
|
| 1052 |
|
| 1053 | if (!tosym[0])
|
| 1054 | snprintf(taddr_str, sizeof(taddr_str), "0x%x", (unsigned int)taddr);
|
| 1055 |
|
| 1056 | |
| 1057 | * The format for the reference source: <symbol_name>+<offset> or <address>
|
| 1058 | * The format for the reference destination: <symbol_name> or <address>
|
| 1059 |
|
| 1060 | mod_warn(mod, "section mismatch in reference: %s%s0x%x (section: %s) -> %s (section: %s)\n",
|
| 1061 | fromsym, fromsym[0] ? "+" : "",
|
| 1062 | (unsigned int)(faddr - (fromsym[0] ? from->st_value : 0)),
|
| 1063 | fromsec, tosym[0] ? tosym : taddr_str, tosec);
|
| 1064 |
|
| 1065 | if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
|
| 1066 | if (match(tosec, mismatch->bad_tosec))
|
| 1067 | fatal("The relocation at %s+0x%lx references\n"
|
| 1068 | "section \"%s\" which is black-listed.\n"
|
| 1069 | "Something is seriously wrong and should be fixed.\n"
|
| 1070 | "You might get more information about where this is\n"
|
| 1071 | "coming from by using scripts/check_extable.sh %s\n",
|
| 1072 | fromsec, (long)faddr, tosec, mod->name);
|
| 1073 | else if (is_executable_section(elf, get_secindex(elf, tsym)))
|
| 1074 | warn("The relocation at %s+0x%lx references\n"
|
| 1075 | "section \"%s\" which is not in the list of\n"
|
| 1076 | "authorized sections. If you're adding a new section\n"
|
| 1077 | "and/or if this reference is valid, add \"%s\" to the\n"
|
| 1078 | "list of authorized sections to jump to on fault.\n"
|
| 1079 | "This can be achieved by adding \"%s\" to\n"
|
| 1080 | "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
|
| 1081 | fromsec, (long)faddr, tosec, tosec, tosec);
|
| 1082 | else
|
| 1083 | error("%s+0x%lx references non-executable section '%s'\n",
|
| 1084 | fromsec, (long)faddr, tosec);
|
| 1085 | }
|
| 1086 | }
|
| 1087 |
|
| 1088 | static void check_export_symbol(struct module *mod, struct elf_info *elf,
|
| 1089 | Elf_Addr faddr, const char *secname,
|
| 1090 | Elf_Sym *sym)
|
| 1091 | {
|
| 1092 | static const char *prefix = "__export_symbol_";
|
| 1093 | const char *label_name, *name, *data;
|
| 1094 | Elf_Sym *label;
|
| 1095 | struct symbol *s;
|
| 1096 | bool is_gpl;
|
| 1097 |
|
| 1098 | label = find_fromsym(elf, faddr, elf->export_symbol_secndx);
|
| 1099 | label_name = sym_name(elf, label);
|
| 1100 |
|
| 1101 | if (!strstarts(label_name, prefix)) {
|
| 1102 | mod_error(mod, ".export_symbol section contains strange symbol '%s'\n",
|
| 1103 | label_name);
|
| 1104 | return;
|
| 1105 | }
|
| 1106 |
|
| 1107 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
|
| 1108 | ELF_ST_BIND(sym->st_info) != STB_WEAK) {
|
| 1109 | mod_error(mod, "local symbol '%s' was exported\n",
|
| 1110 | label_name + strlen(prefix));
|
| 1111 | return;
|
| 1112 | }
|
| 1113 |
|
| 1114 | name = sym_name(elf, sym);
|
| 1115 | if (strcmp(label_name + strlen(prefix), name)) {
|
| 1116 | mod_error(mod, ".export_symbol section references '%s', but it does not seem to be an export symbol\n",
|
| 1117 | name);
|
| 1118 | return;
|
| 1119 | }
|
| 1120 |
|
| 1121 | data = sym_get_data(elf, label);
|
| 1122 | if (!strcmp(data, "GPL")) {
|
| 1123 | is_gpl = true;
|
| 1124 | } else if (!strcmp(data, "")) {
|
| 1125 | is_gpl = false;
|
| 1126 | } else {
|
| 1127 | mod_error(mod, "unknown license '%s' was specified for '%s'\n",
|
| 1128 | data, name);
|
| 1129 | return;
|
| 1130 | }
|
| 1131 |
|
| 1132 | data += strlen(data) + 1;
|
| 1133 | s = sym_add_exported(name, mod, is_gpl, data);
|
| 1134 |
|
| 1135 | |
| 1136 | * We need to be aware whether we are exporting a function or
|
| 1137 | * a data on some architectures.
|
| 1138 |
|
| 1139 | s->is_func = (ELF_ST_TYPE(sym->st_info) == STT_FUNC);
|
| 1140 |
|
| 1141 | |
| 1142 | * For parisc64, symbols prefixed $$ from the library have the symbol type
|
| 1143 | * STT_LOPROC. They should be handled as functions too.
|
| 1144 |
|
| 1145 | if (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64 &&
|
| 1146 | elf->hdr->e_machine == EM_PARISC &&
|
| 1147 | ELF_ST_TYPE(sym->st_info) == STT_LOPROC)
|
| 1148 | s->is_func = true;
|
| 1149 |
|
| 1150 | if (match(secname, PATTERNS(ALL_INIT_SECTIONS)))
|
| 1151 | mod_warn(mod, "EXPORT_SYMBOL used for init symbol '%s'. Remove __init or EXPORT_SYMBOL.\n",
|
| 1152 | name);
|
| 1153 | else if (match(secname, PATTERNS(ALL_EXIT_SECTIONS)))
|
| 1154 | mod_warn(mod, "EXPORT_SYMBOL used for exit symbol '%s'. Remove __exit or EXPORT_SYMBOL.\n",
|
| 1155 | name);
|
| 1156 | }
|
| 1157 |
|
| 1158 | static void check_section_mismatch(struct module *mod, struct elf_info *elf,
|
| 1159 | Elf_Sym *sym,
|
| 1160 | unsigned int fsecndx, const char *fromsec,
|
| 1161 | Elf_Addr faddr, Elf_Addr taddr)
|
| 1162 | {
|
| 1163 | const char *tosec = sec_name(elf, get_secindex(elf, sym));
|
| 1164 | const struct sectioncheck *mismatch;
|
| 1165 |
|
| 1166 | if (module_enabled && elf->export_symbol_secndx == fsecndx) {
|
| 1167 | check_export_symbol(mod, elf, faddr, tosec, sym);
|
| 1168 | return;
|
| 1169 | }
|
| 1170 |
|
| 1171 | mismatch = section_mismatch(fromsec, tosec);
|
| 1172 | if (!mismatch)
|
| 1173 | return;
|
| 1174 |
|
| 1175 | default_mismatch_handler(mod, elf, mismatch, sym,
|
| 1176 | fsecndx, fromsec, faddr,
|
| 1177 | tosec, taddr);
|
| 1178 | }
|
| 1179 |
|
| 1180 | static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
|
| 1181 | {
|
| 1182 | switch (r_type) {
|
| 1183 | case R_386_32:
|
| 1184 | return get_unaligned_native(location);
|
| 1185 | case R_386_PC32:
|
| 1186 | return get_unaligned_native(location) + 4;
|
| 1187 | }
|
| 1188 |
|
| 1189 | return (Elf_Addr)(-1);
|
| 1190 | }
|
| 1191 |
|
| 1192 | static int32_t sign_extend32(int32_t value, int index)
|
| 1193 | {
|
| 1194 | uint8_t shift = 31 - index;
|
| 1195 |
|
| 1196 | return (int32_t)(value << shift) >> shift;
|
| 1197 | }
|
| 1198 |
|
| 1199 | static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
|
| 1200 | {
|
| 1201 | uint32_t inst, upper, lower, sign, j1, j2;
|
| 1202 | int32_t offset;
|
| 1203 |
|
| 1204 | switch (r_type) {
|
| 1205 | case R_ARM_ABS32:
|
| 1206 | case R_ARM_REL32:
|
| 1207 | inst = get_unaligned_native((uint32_t *)loc);
|
| 1208 | return inst + sym->st_value;
|
| 1209 | case R_ARM_MOVW_ABS_NC:
|
| 1210 | case R_ARM_MOVT_ABS:
|
| 1211 | inst = get_unaligned_native((uint32_t *)loc);
|
| 1212 | offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff),
|
| 1213 | 15);
|
| 1214 | return offset + sym->st_value;
|
| 1215 | case R_ARM_PC24:
|
| 1216 | case R_ARM_CALL:
|
| 1217 | case R_ARM_JUMP24:
|
| 1218 | inst = get_unaligned_native((uint32_t *)loc);
|
| 1219 | offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
|
| 1220 | return offset + sym->st_value + 8;
|
| 1221 | case R_ARM_THM_MOVW_ABS_NC:
|
| 1222 | case R_ARM_THM_MOVT_ABS:
|
| 1223 | upper = get_unaligned_native((uint16_t *)loc);
|
| 1224 | lower = get_unaligned_native((uint16_t *)loc + 1);
|
| 1225 | offset = sign_extend32(((upper & 0x000f) << 12) |
|
| 1226 | ((upper & 0x0400) << 1) |
|
| 1227 | ((lower & 0x7000) >> 4) |
|
| 1228 | (lower & 0x00ff),
|
| 1229 | 15);
|
| 1230 | return offset + sym->st_value;
|
| 1231 | case R_ARM_THM_JUMP19:
|
| 1232 | |
| 1233 | * Encoding T3:
|
| 1234 | * S = upper[10]
|
| 1235 | * imm6 = upper[5:0]
|
| 1236 | * J1 = lower[13]
|
| 1237 | * J2 = lower[11]
|
| 1238 | * imm11 = lower[10:0]
|
| 1239 | * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
|
| 1240 |
|
| 1241 | upper = get_unaligned_native((uint16_t *)loc);
|
| 1242 | lower = get_unaligned_native((uint16_t *)loc + 1);
|
| 1243 |
|
| 1244 | sign = (upper >> 10) & 1;
|
| 1245 | j1 = (lower >> 13) & 1;
|
| 1246 | j2 = (lower >> 11) & 1;
|
| 1247 | offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) |
|
| 1248 | ((upper & 0x03f) << 12) |
|
| 1249 | ((lower & 0x07ff) << 1),
|
| 1250 | 20);
|
| 1251 | return offset + sym->st_value + 4;
|
| 1252 | case R_ARM_THM_PC22:
|
| 1253 | case R_ARM_THM_JUMP24:
|
| 1254 | |
| 1255 | * Encoding T4:
|
| 1256 | * S = upper[10]
|
| 1257 | * imm10 = upper[9:0]
|
| 1258 | * J1 = lower[13]
|
| 1259 | * J2 = lower[11]
|
| 1260 | * imm11 = lower[10:0]
|
| 1261 | * I1 = NOT(J1 XOR S)
|
| 1262 | * I2 = NOT(J2 XOR S)
|
| 1263 | * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
|
| 1264 |
|
| 1265 | upper = get_unaligned_native((uint16_t *)loc);
|
| 1266 | lower = get_unaligned_native((uint16_t *)loc + 1);
|
| 1267 |
|
| 1268 | sign = (upper >> 10) & 1;
|
| 1269 | j1 = (lower >> 13) & 1;
|
| 1270 | j2 = (lower >> 11) & 1;
|
| 1271 | offset = sign_extend32((sign << 24) |
|
| 1272 | ((~(j1 ^ sign) & 1) << 23) |
|
| 1273 | ((~(j2 ^ sign) & 1) << 22) |
|
| 1274 | ((upper & 0x03ff) << 12) |
|
| 1275 | ((lower & 0x07ff) << 1),
|
| 1276 | 24);
|
| 1277 | return offset + sym->st_value + 4;
|
| 1278 | }
|
| 1279 |
|
| 1280 | return (Elf_Addr)(-1);
|
| 1281 | }
|
| 1282 |
|
| 1283 | static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
|
| 1284 | {
|
| 1285 | uint32_t inst;
|
| 1286 |
|
| 1287 | inst = get_unaligned_native(location);
|
| 1288 | switch (r_type) {
|
| 1289 | case R_MIPS_LO16:
|
| 1290 | return inst & 0xffff;
|
| 1291 | case R_MIPS_26:
|
| 1292 | return (inst & 0x03ffffff) << 2;
|
| 1293 | case R_MIPS_32:
|
| 1294 | return inst;
|
| 1295 | }
|
| 1296 | return (Elf_Addr)(-1);
|
| 1297 | }
|
| 1298 |
|
| 1299 | #ifndef EM_RISCV
|
| 1300 | #define EM_RISCV 243
|
| 1301 | #endif
|
| 1302 |
|
| 1303 | #ifndef R_RISCV_SUB32
|
| 1304 | #define R_RISCV_SUB32 39
|
| 1305 | #endif
|
| 1306 |
|
| 1307 | #ifndef EM_LOONGARCH
|
| 1308 | #define EM_LOONGARCH 258
|
| 1309 | #endif
|
| 1310 |
|
| 1311 | #ifndef R_LARCH_SUB32
|
| 1312 | #define R_LARCH_SUB32 55
|
| 1313 | #endif
|
| 1314 |
|
| 1315 | #ifndef R_LARCH_RELAX
|
| 1316 | #define R_LARCH_RELAX 100
|
| 1317 | #endif
|
| 1318 |
|
| 1319 | #ifndef R_LARCH_ALIGN
|
| 1320 | #define R_LARCH_ALIGN 102
|
| 1321 | #endif
|
| 1322 |
|
| 1323 | static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
|
| 1324 | unsigned int *r_type, unsigned int *r_sym)
|
| 1325 | {
|
| 1326 | typedef struct {
|
| 1327 | Elf64_Word r_sym;
|
| 1328 | unsigned char r_ssym;
|
| 1329 | unsigned char r_type3;
|
| 1330 | unsigned char r_type2;
|
| 1331 | unsigned char r_type;
|
| 1332 | } Elf64_Mips_R_Info;
|
| 1333 |
|
| 1334 | bool is_64bit = (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64);
|
| 1335 |
|
| 1336 | if (elf->hdr->e_machine == EM_MIPS && is_64bit) {
|
| 1337 | Elf64_Mips_R_Info *mips64_r_info = (void *)&r_info;
|
| 1338 |
|
| 1339 | *r_type = mips64_r_info->r_type;
|
| 1340 | *r_sym = TO_NATIVE(mips64_r_info->r_sym);
|
| 1341 | return;
|
| 1342 | }
|
| 1343 |
|
| 1344 | if (is_64bit)
|
| 1345 | r_info = TO_NATIVE((Elf64_Xword)r_info);
|
| 1346 | else
|
| 1347 | r_info = TO_NATIVE((Elf32_Word)r_info);
|
| 1348 |
|
| 1349 | *r_type = ELF_R_TYPE(r_info);
|
| 1350 | *r_sym = ELF_R_SYM(r_info);
|
| 1351 | }
|
| 1352 |
|
| 1353 | static void section_rela(struct module *mod, struct elf_info *elf,
|
| 1354 | unsigned int fsecndx, const char *fromsec,
|
| 1355 | const Elf_Rela *start, const Elf_Rela *stop)
|
| 1356 | {
|
| 1357 | const Elf_Rela *rela;
|
| 1358 |
|
| 1359 | for (rela = start; rela < stop; rela++) {
|
| 1360 | Elf_Sym *tsym;
|
| 1361 | Elf_Addr taddr, r_offset;
|
| 1362 | unsigned int r_type, r_sym;
|
| 1363 |
|
| 1364 | r_offset = TO_NATIVE(rela->r_offset);
|
| 1365 | get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
|
| 1366 |
|
| 1367 | tsym = elf->symtab_start + r_sym;
|
| 1368 | taddr = tsym->st_value + TO_NATIVE(rela->r_addend);
|
| 1369 |
|
| 1370 | switch (elf->hdr->e_machine) {
|
| 1371 | case EM_RISCV:
|
| 1372 | if (!strcmp("__ex_table", fromsec) &&
|
| 1373 | r_type == R_RISCV_SUB32)
|
| 1374 | continue;
|
| 1375 | break;
|
| 1376 | case EM_LOONGARCH:
|
| 1377 | switch (r_type) {
|
| 1378 | case R_LARCH_SUB32:
|
| 1379 | if (!strcmp("__ex_table", fromsec))
|
| 1380 | continue;
|
| 1381 | break;
|
| 1382 | case R_LARCH_RELAX:
|
| 1383 | case R_LARCH_ALIGN:
|
| 1384 |
|
| 1385 | continue;
|
| 1386 | }
|
| 1387 | break;
|
| 1388 | }
|
| 1389 |
|
| 1390 | check_section_mismatch(mod, elf, tsym,
|
| 1391 | fsecndx, fromsec, r_offset, taddr);
|
| 1392 | }
|
| 1393 | }
|
| 1394 |
|
| 1395 | static void section_rel(struct module *mod, struct elf_info *elf,
|
| 1396 | unsigned int fsecndx, const char *fromsec,
|
| 1397 | const Elf_Rel *start, const Elf_Rel *stop)
|
| 1398 | {
|
| 1399 | const Elf_Rel *rel;
|
| 1400 |
|
| 1401 | for (rel = start; rel < stop; rel++) {
|
| 1402 | Elf_Sym *tsym;
|
| 1403 | Elf_Addr taddr, r_offset;
|
| 1404 | unsigned int r_type, r_sym;
|
| 1405 | void *loc;
|
| 1406 |
|
| 1407 | r_offset = TO_NATIVE(rel->r_offset);
|
| 1408 | get_rel_type_and_sym(elf, rel->r_info, &r_type, &r_sym);
|
| 1409 |
|
| 1410 | loc = sym_get_data_by_offset(elf, fsecndx, r_offset);
|
| 1411 | tsym = elf->symtab_start + r_sym;
|
| 1412 |
|
| 1413 | switch (elf->hdr->e_machine) {
|
| 1414 | case EM_386:
|
| 1415 | taddr = addend_386_rel(loc, r_type);
|
| 1416 | break;
|
| 1417 | case EM_ARM:
|
| 1418 | taddr = addend_arm_rel(loc, tsym, r_type);
|
| 1419 | break;
|
| 1420 | case EM_MIPS:
|
| 1421 | taddr = addend_mips_rel(loc, r_type);
|
| 1422 | break;
|
| 1423 | default:
|
| 1424 | fatal("Please add code to calculate addend for this architecture\n");
|
| 1425 | }
|
| 1426 |
|
| 1427 | check_section_mismatch(mod, elf, tsym,
|
| 1428 | fsecndx, fromsec, r_offset, taddr);
|
| 1429 | }
|
| 1430 | }
|
| 1431 |
|
| 1432 | |
| 1433 | * A module includes a number of sections that are discarded
|
| 1434 | * either when loaded or when used as built-in.
|
| 1435 | * For loaded modules all functions marked __init and all data
|
| 1436 | * marked __initdata will be discarded when the module has been initialized.
|
| 1437 | * Likewise for modules used built-in the sections marked __exit
|
| 1438 | * are discarded because __exit marked function are supposed to be called
|
| 1439 | * only when a module is unloaded which never happens for built-in modules.
|
| 1440 | * The check_sec_ref() function traverses all relocation records
|
| 1441 | * to find all references to a section that reference a section that will
|
| 1442 | * be discarded and warns about it.
|
| 1443 | *
|
| 1444 | static void check_sec_ref(struct module *mod, struct elf_info *elf)
|
| 1445 | {
|
| 1446 | int i;
|
| 1447 |
|
| 1448 |
|
| 1449 | for (i = 0; i < elf->num_sections; i++) {
|
| 1450 | Elf_Shdr *sechdr = &elf->sechdrs[i];
|
| 1451 |
|
| 1452 | check_section(mod, elf, sechdr);
|
| 1453 |
|
| 1454 | if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
|
| 1455 |
|
| 1456 | unsigned int secndx = sechdr->sh_info;
|
| 1457 | const char *secname = sec_name(elf, secndx);
|
| 1458 | const void *start, *stop;
|
| 1459 |
|
| 1460 |
|
| 1461 | if (match(secname, section_white_list))
|
| 1462 | continue;
|
| 1463 |
|
| 1464 | start = sym_get_data_by_offset(elf, i, 0);
|
| 1465 | stop = start + sechdr->sh_size;
|
| 1466 |
|
| 1467 | if (sechdr->sh_type == SHT_RELA)
|
| 1468 | section_rela(mod, elf, secndx, secname,
|
| 1469 | start, stop);
|
| 1470 | else
|
| 1471 | section_rel(mod, elf, secndx, secname,
|
| 1472 | start, stop);
|
| 1473 | }
|
| 1474 | }
|
| 1475 | }
|
| 1476 |
|
| 1477 | static char *remove_dot(char *s)
|
| 1478 | {
|
| 1479 | size_t n = strcspn(s, ".");
|
| 1480 |
|
| 1481 | if (n && s[n]) {
|
| 1482 | size_t m = strspn(s + n + 1, "0123456789");
|
| 1483 | if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
|
| 1484 | s[n] = 0;
|
| 1485 | }
|
| 1486 | return s;
|
| 1487 | }
|
| 1488 |
|
| 1489 | |
| 1490 | * The CRCs are recorded in .*.cmd files in the form of:
|
| 1491 | * #SYMVER <name> <crc>
|
| 1492 |
|
| 1493 | static void extract_crcs_for_object(const char *object, struct module *mod)
|
| 1494 | {
|
| 1495 | char cmd_file[PATH_MAX];
|
| 1496 | char *buf, *p;
|
| 1497 | const char *base;
|
| 1498 | int dirlen, baselen_without_suffix, ret;
|
| 1499 |
|
| 1500 | base = get_basename(object);
|
| 1501 | dirlen = base - object;
|
| 1502 |
|
| 1503 | baselen_without_suffix = strlen(object) - dirlen - strlen(".o");
|
| 1504 |
|
| 1505 | |
| 1506 | * When CONFIG_LTO_CLANG_THIN_DIST=y, the ELF is *.thinlto-native.o
|
| 1507 | * but the symbol CRCs are recorded in *.o.cmd file.
|
| 1508 |
|
| 1509 | if (strends(object, ".thinlto-native.o"))
|
| 1510 | baselen_without_suffix -= strlen(".thinlto-native");
|
| 1511 |
|
| 1512 | ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%.*s.o.cmd",
|
| 1513 | dirlen, object, baselen_without_suffix, base);
|
| 1514 | if (ret >= sizeof(cmd_file)) {
|
| 1515 | error("%s: too long path was truncated\n", cmd_file);
|
| 1516 | return;
|
| 1517 | }
|
| 1518 |
|
| 1519 | buf = read_text_file(cmd_file);
|
| 1520 | p = buf;
|
| 1521 |
|
| 1522 | while ((p = strstr(p, "\n#SYMVER "))) {
|
| 1523 | char *name;
|
| 1524 | size_t namelen;
|
| 1525 | unsigned int crc;
|
| 1526 | struct symbol *sym;
|
| 1527 |
|
| 1528 | name = p + strlen("\n#SYMVER ");
|
| 1529 |
|
| 1530 | p = strchr(name, ' ');
|
| 1531 | if (!p)
|
| 1532 | break;
|
| 1533 |
|
| 1534 | namelen = p - name;
|
| 1535 | p++;
|
| 1536 |
|
| 1537 | if (!isdigit(*p))
|
| 1538 | continue;
|
| 1539 |
|
| 1540 | crc = strtoul(p, &p, 0);
|
| 1541 | if (*p != '\n')
|
| 1542 | continue;
|
| 1543 |
|
| 1544 | name[namelen] = '\0';
|
| 1545 |
|
| 1546 | |
| 1547 | * sym_find_with_module() may return NULL here.
|
| 1548 | * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
|
| 1549 | * Since commit e1327a127703, genksyms calculates CRCs of all
|
| 1550 | * symbols, including trimmed ones. Ignore orphan CRCs.
|
| 1551 |
|
| 1552 | sym = sym_find_with_module(name, mod);
|
| 1553 | if (sym)
|
| 1554 | sym_set_crc(sym, crc);
|
| 1555 | }
|
| 1556 |
|
| 1557 | free(buf);
|
| 1558 | }
|
| 1559 |
|
| 1560 | |
| 1561 | * The symbol versions (CRC) are recorded in the .*.cmd files.
|
| 1562 | * Parse them to retrieve CRCs for the current module.
|
| 1563 |
|
| 1564 | static void mod_set_crcs(struct module *mod)
|
| 1565 | {
|
| 1566 | char objlist[PATH_MAX];
|
| 1567 | char *buf, *p, *obj;
|
| 1568 | int ret;
|
| 1569 |
|
| 1570 | if (mod->is_vmlinux) {
|
| 1571 | strcpy(objlist, ".vmlinux.objs");
|
| 1572 | } else {
|
| 1573 |
|
| 1574 | ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
|
| 1575 | if (ret >= sizeof(objlist)) {
|
| 1576 | error("%s: too long path was truncated\n", objlist);
|
| 1577 | return;
|
| 1578 | }
|
| 1579 | }
|
| 1580 |
|
| 1581 | buf = read_text_file(objlist);
|
| 1582 | p = buf;
|
| 1583 |
|
| 1584 | while ((obj = strsep(&p, "\n")) && obj[0])
|
| 1585 | extract_crcs_for_object(obj, mod);
|
| 1586 |
|
| 1587 | free(buf);
|
| 1588 | }
|
| 1589 |
|
| 1590 | static void read_symbols(const char *modname)
|
| 1591 | {
|
| 1592 | const char *symname;
|
| 1593 | char *version;
|
| 1594 | char *license;
|
| 1595 | char *namespace;
|
| 1596 | struct module *mod;
|
| 1597 | struct elf_info info = { };
|
| 1598 | Elf_Sym *sym;
|
| 1599 |
|
| 1600 | if (!strends(modname, ".o")) {
|
| 1601 | error("%s: filename must be suffixed with .o\n", modname);
|
| 1602 | return;
|
| 1603 | }
|
| 1604 |
|
| 1605 | if (!parse_elf(&info, modname))
|
| 1606 | return;
|
| 1607 |
|
| 1608 |
|
| 1609 | mod = new_module(modname, strlen(modname) - strlen(".o"));
|
| 1610 |
|
| 1611 |
|
| 1612 | if (info.no_trim_symbol_len) {
|
| 1613 | mod->no_trim_symbol = xmalloc(info.no_trim_symbol_len);
|
| 1614 | memcpy(mod->no_trim_symbol, info.no_trim_symbol,
|
| 1615 | info.no_trim_symbol_len);
|
| 1616 | mod->no_trim_symbol_len = info.no_trim_symbol_len;
|
| 1617 | }
|
| 1618 |
|
| 1619 | if (!mod->is_vmlinux) {
|
| 1620 | license = get_modinfo(&info, "license");
|
| 1621 | if (!license)
|
| 1622 | mod_error(mod, "missing MODULE_LICENSE()\n");
|
| 1623 | while (license) {
|
| 1624 | if (!license_is_gpl_compatible(license)) {
|
| 1625 | mod->is_gpl_compatible = false;
|
| 1626 | break;
|
| 1627 | }
|
| 1628 | license = get_next_modinfo(&info, "license", license);
|
| 1629 | }
|
| 1630 |
|
| 1631 | for (namespace = get_modinfo(&info, "import_ns");
|
| 1632 | namespace;
|
| 1633 | namespace = get_next_modinfo(&info, "import_ns", namespace)) {
|
| 1634 | if (strstarts(namespace, MODULE_NS_PREFIX))
|
| 1635 | mod_error(mod, "explicitly importing namespace '%s' is not allowed.\n",
|
| 1636 | namespace);
|
| 1637 |
|
| 1638 | add_namespace(&mod->imported_namespaces, namespace);
|
| 1639 | }
|
| 1640 |
|
| 1641 | if (!get_modinfo(&info, "description"))
|
| 1642 | mod_warn(mod, "missing MODULE_DESCRIPTION()\n");
|
| 1643 | }
|
| 1644 |
|
| 1645 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
|
| 1646 | symname = remove_dot(info.strtab + sym->st_name);
|
| 1647 |
|
| 1648 | handle_symbol(mod, &info, sym, symname);
|
| 1649 | handle_moddevtable(mod, &info, sym, symname);
|
| 1650 | }
|
| 1651 |
|
| 1652 | check_sec_ref(mod, &info);
|
| 1653 |
|
| 1654 | if (!mod->is_vmlinux) {
|
| 1655 | version = get_modinfo(&info, "version");
|
| 1656 | if (version || all_versions)
|
| 1657 | get_src_version(mod->name, mod->srcversion,
|
| 1658 | sizeof(mod->srcversion) - 1);
|
| 1659 | }
|
| 1660 |
|
| 1661 | parse_elf_finish(&info);
|
| 1662 |
|
| 1663 | if (modversions) {
|
| 1664 | |
| 1665 | * Our trick to get versioning for module struct etc. - it's
|
| 1666 | * never passed as an argument to an exported function, so
|
| 1667 | * the automatic versioning doesn't pick it up, but it's really
|
| 1668 | * important anyhow.
|
| 1669 |
|
| 1670 | sym_add_unresolved("module_layout", mod, false);
|
| 1671 |
|
| 1672 | mod_set_crcs(mod);
|
| 1673 | }
|
| 1674 | }
|
| 1675 |
|
| 1676 | static void read_symbols_from_files(const char *filename)
|
| 1677 | {
|
| 1678 | FILE *in = stdin;
|
| 1679 | char fname[PATH_MAX];
|
| 1680 |
|
| 1681 | in = fopen(filename, "r");
|
| 1682 | if (!in)
|
| 1683 | fatal("Can't open filenames file %s: %m", filename);
|
| 1684 |
|
| 1685 | while (fgets(fname, PATH_MAX, in) != NULL) {
|
| 1686 | if (strends(fname, "\n"))
|
| 1687 | fname[strlen(fname)-1] = '\0';
|
| 1688 | read_symbols(fname);
|
| 1689 | }
|
| 1690 |
|
| 1691 | fclose(in);
|
| 1692 | }
|
| 1693 |
|
| 1694 | #define SZ 500
|
| 1695 |
|
| 1696 | |
| 1697 | * following helper, then compare to the file on disk and
|
| 1698 | * only update the later if anything changed
|
| 1699 |
|
| 1700 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
|
| 1701 | const char *fmt, ...)
|
| 1702 | {
|
| 1703 | char tmp[SZ];
|
| 1704 | int len;
|
| 1705 | va_list ap;
|
| 1706 |
|
| 1707 | va_start(ap, fmt);
|
| 1708 | len = vsnprintf(tmp, SZ, fmt, ap);
|
| 1709 | va_end(ap);
|
| 1710 |
|
| 1711 | if (len < 0) {
|
| 1712 | perror("vsnprintf failed");
|
| 1713 | exit(1);
|
| 1714 | }
|
| 1715 | if (len >= SZ)
|
| 1716 | fatal("buf_printf output truncated for string %s: %d bytes needed, %d available\n",
|
| 1717 | tmp, len + 1, SZ);
|
| 1718 |
|
| 1719 | buf_write(buf, tmp, len);
|
| 1720 | }
|
| 1721 |
|
| 1722 | void buf_write(struct buffer *buf, const char *s, int len)
|
| 1723 | {
|
| 1724 | if (buf->size - buf->pos < len) {
|
| 1725 | buf->size += len + SZ;
|
| 1726 | buf->p = xrealloc(buf->p, buf->size);
|
| 1727 | }
|
| 1728 | strncpy(buf->p + buf->pos, s, len);
|
| 1729 | buf->pos += len;
|
| 1730 | }
|
| 1731 |
|
| 1732 | |
| 1733 | * verify_module_namespace() - does @modname have access to this symbol's @namespace
|
| 1734 | * @namespace: export symbol namespace
|
| 1735 | * @modname: module name
|
| 1736 | *
|
| 1737 | * If @namespace is prefixed with "module:" to indicate it is a module namespace
|
| 1738 | * then test if @modname matches any of the comma separated patterns.
|
| 1739 | *
|
| 1740 | * The patterns only support tail-glob.
|
| 1741 |
|
| 1742 | static bool verify_module_namespace(const char *namespace, const char *modname)
|
| 1743 | {
|
| 1744 | size_t len, modlen = strlen(modname);
|
| 1745 | const char *prefix = "module:";
|
| 1746 | const char *sep;
|
| 1747 | bool glob;
|
| 1748 |
|
| 1749 | if (!strstarts(namespace, prefix))
|
| 1750 | return false;
|
| 1751 |
|
| 1752 | for (namespace += strlen(prefix); *namespace; namespace = sep) {
|
| 1753 | sep = strchrnul(namespace, ',');
|
| 1754 | len = sep - namespace;
|
| 1755 |
|
| 1756 | glob = false;
|
| 1757 | if (sep[-1] == '*') {
|
| 1758 | len--;
|
| 1759 | glob = true;
|
| 1760 | }
|
| 1761 |
|
| 1762 | if (*sep)
|
| 1763 | sep++;
|
| 1764 |
|
| 1765 | if (strncmp(namespace, modname, len) == 0 && (glob || len == modlen))
|
| 1766 | return true;
|
| 1767 | }
|
| 1768 |
|
| 1769 | return false;
|
| 1770 | }
|
| 1771 |
|
| 1772 | static void check_exports(struct module *mod)
|
| 1773 | {
|
| 1774 | struct symbol *s, *exp;
|
| 1775 |
|
| 1776 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 1777 | const char *basename;
|
| 1778 | exp = find_symbol(s->name);
|
| 1779 | if (!exp) {
|
| 1780 | if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
|
| 1781 | modpost_log(!warn_unresolved, mod,
|
| 1782 | "symbol '%s' undefined!\n",
|
| 1783 | s->name);
|
| 1784 | continue;
|
| 1785 | }
|
| 1786 | if (exp->module == mod) {
|
| 1787 | mod_error(mod, "symbol '%s' was exported without definition\n", s->name);
|
| 1788 | continue;
|
| 1789 | }
|
| 1790 |
|
| 1791 | exp->used = true;
|
| 1792 | s->module = exp->module;
|
| 1793 | s->crc_valid = exp->crc_valid;
|
| 1794 | s->crc = exp->crc;
|
| 1795 |
|
| 1796 | basename = get_basename(mod->name);
|
| 1797 |
|
| 1798 | if (!verify_module_namespace(exp->namespace, basename) &&
|
| 1799 | !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
|
| 1800 | modpost_log(!allow_missing_ns_imports, mod,
|
| 1801 | "module uses symbol '%s' from namespace '%s', but does not import it.\n",
|
| 1802 | exp->name, exp->namespace);
|
| 1803 | add_namespace(&mod->missing_namespaces, exp->namespace);
|
| 1804 | }
|
| 1805 |
|
| 1806 | if (!mod->is_gpl_compatible && exp->is_gpl_only)
|
| 1807 | mod_error(mod, "GPL-incompatible module uses GPL-only symbol '%s'\n",
|
| 1808 | exp->name);
|
| 1809 | }
|
| 1810 | }
|
| 1811 |
|
| 1812 | static void handle_white_list_exports(const char *white_list)
|
| 1813 | {
|
| 1814 | char *buf, *p, *name;
|
| 1815 |
|
| 1816 | buf = read_text_file(white_list);
|
| 1817 | p = buf;
|
| 1818 |
|
| 1819 | while ((name = strsep(&p, "\n"))) {
|
| 1820 | struct symbol *sym = find_symbol(name);
|
| 1821 |
|
| 1822 | if (sym)
|
| 1823 | sym->used = true;
|
| 1824 | }
|
| 1825 |
|
| 1826 | free(buf);
|
| 1827 | }
|
| 1828 |
|
| 1829 | |
| 1830 | * Keep symbols recorded in the .no_trim_symbol section. This is necessary to
|
| 1831 | * prevent CONFIG_TRIM_UNUSED_KSYMS from dropping EXPORT_SYMBOL because
|
| 1832 | * symbol_get() relies on the symbol being present in the ksymtab for lookups.
|
| 1833 |
|
| 1834 | static void keep_no_trim_symbols(struct module *mod)
|
| 1835 | {
|
| 1836 | unsigned long size = mod->no_trim_symbol_len;
|
| 1837 |
|
| 1838 | for (char *s = mod->no_trim_symbol; s; s = next_string(s , &size)) {
|
| 1839 | struct symbol *sym;
|
| 1840 |
|
| 1841 | |
| 1842 | * If find_symbol() returns NULL, this symbol is not provided
|
| 1843 | * by any module, and symbol_get() will fail.
|
| 1844 |
|
| 1845 | sym = find_symbol(s);
|
| 1846 | if (sym)
|
| 1847 | sym->used = true;
|
| 1848 | }
|
| 1849 | }
|
| 1850 |
|
| 1851 | static void check_modname_len(struct module *mod)
|
| 1852 | {
|
| 1853 | const char *mod_name;
|
| 1854 |
|
| 1855 | mod_name = get_basename(mod->name);
|
| 1856 |
|
| 1857 | if (strlen(mod_name) >= MODULE_NAME_LEN)
|
| 1858 | mod_error(mod, "module name is too long\n");
|
| 1859 | }
|
| 1860 |
|
| 1861 | |
| 1862 | * Header for the generated file
|
| 1863 | *
|
| 1864 | static void add_header(struct buffer *b, struct module *mod)
|
| 1865 | {
|
| 1866 | buf_printf(b, "#include <linux/module.h>\n");
|
| 1867 | buf_printf(b, "#include <linux/export-internal.h>\n");
|
| 1868 | buf_printf(b, "#include <linux/compiler.h>\n");
|
| 1869 | buf_printf(b, "\n");
|
| 1870 | buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
|
| 1871 | buf_printf(b, "\n");
|
| 1872 | buf_printf(b, "__visible struct module __this_module\n");
|
| 1873 | buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
|
| 1874 | buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
|
| 1875 | if (mod->has_init)
|
| 1876 | buf_printf(b, "\t.init = init_module,\n");
|
| 1877 | if (mod->has_cleanup)
|
| 1878 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
|
| 1879 | "\t.exit = cleanup_module,\n"
|
| 1880 | "#endif\n");
|
| 1881 | buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
|
| 1882 | buf_printf(b, "};\n");
|
| 1883 |
|
| 1884 | if (!external_module)
|
| 1885 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
|
| 1886 |
|
| 1887 | if (strstarts(mod->name, "drivers/staging"))
|
| 1888 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
|
| 1889 |
|
| 1890 | if (strstarts(mod->name, "tools/testing"))
|
| 1891 | buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
|
| 1892 | }
|
| 1893 |
|
| 1894 | static void add_exported_symbols(struct buffer *buf, struct module *mod)
|
| 1895 | {
|
| 1896 | struct symbol *sym;
|
| 1897 |
|
| 1898 |
|
| 1899 | buf_printf(buf, "\n");
|
| 1900 | list_for_each_entry(sym, &mod->exported_symbols, list) {
|
| 1901 | if (trim_unused_exports && !sym->used)
|
| 1902 | continue;
|
| 1903 |
|
| 1904 | buf_printf(buf, "KSYMTAB_%s(%s, \"%s\");\n",
|
| 1905 | sym->is_func ? "FUNC" : "DATA", sym->name,
|
| 1906 | sym->namespace);
|
| 1907 |
|
| 1908 | buf_printf(buf, "SYMBOL_FLAGS(%s, 0x%02x);\n",
|
| 1909 | sym->name, get_symbol_flags(sym));
|
| 1910 | }
|
| 1911 |
|
| 1912 | if (!modversions)
|
| 1913 | return;
|
| 1914 |
|
| 1915 |
|
| 1916 | buf_printf(buf, "\n");
|
| 1917 | list_for_each_entry(sym, &mod->exported_symbols, list) {
|
| 1918 | if (trim_unused_exports && !sym->used)
|
| 1919 | continue;
|
| 1920 |
|
| 1921 | if (!sym->crc_valid)
|
| 1922 | mod_warn(mod, "EXPORT symbol '%s' version generation failed, symbol will not be versioned.\n"
|
| 1923 | "Is '%s' prototyped in <asm/asm-prototypes.h>?\n",
|
| 1924 | sym->name, sym->name);
|
| 1925 |
|
| 1926 | buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x);\n",
|
| 1927 | sym->name, sym->crc);
|
| 1928 | }
|
| 1929 | }
|
| 1930 |
|
| 1931 | |
| 1932 | * Record CRCs for unresolved symbols, supporting long names
|
| 1933 |
|
| 1934 | static void add_extended_versions(struct buffer *b, struct module *mod)
|
| 1935 | {
|
| 1936 | struct symbol *s;
|
| 1937 |
|
| 1938 | if (!extended_modversions)
|
| 1939 | return;
|
| 1940 |
|
| 1941 | buf_printf(b, "\n");
|
| 1942 | buf_printf(b, "static const u32 ____version_ext_crcs[]\n");
|
| 1943 | buf_printf(b, "__used __section(\"__version_ext_crcs\") = {\n");
|
| 1944 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 1945 | if (!s->module)
|
| 1946 | continue;
|
| 1947 | if (!s->crc_valid) {
|
| 1948 | mod_warn(mod, "symbol '%s' has no CRC!\n", s->name);
|
| 1949 | continue;
|
| 1950 | }
|
| 1951 | buf_printf(b, "\t0x%08x,\n", s->crc);
|
| 1952 | }
|
| 1953 | buf_printf(b, "};\n");
|
| 1954 |
|
| 1955 | buf_printf(b, "static const char ____version_ext_names[]\n");
|
| 1956 | buf_printf(b, "__used __section(\"__version_ext_names\") =\n");
|
| 1957 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 1958 | if (!s->module)
|
| 1959 | continue;
|
| 1960 | if (!s->crc_valid)
|
| 1961 | |
| 1962 | * We already warned on this when producing the crc
|
| 1963 | * table.
|
| 1964 | * We need to skip its name too, as the indexes in
|
| 1965 | * both tables need to align.
|
| 1966 |
|
| 1967 | continue;
|
| 1968 | buf_printf(b, "\t\"%s\\0\"\n", s->name);
|
| 1969 | }
|
| 1970 | buf_printf(b, ";\n");
|
| 1971 | }
|
| 1972 |
|
| 1973 | |
| 1974 | * Record CRCs for unresolved symbols
|
| 1975 | *
|
| 1976 | static void add_versions(struct buffer *b, struct module *mod)
|
| 1977 | {
|
| 1978 | struct symbol *s;
|
| 1979 |
|
| 1980 | if (!basic_modversions)
|
| 1981 | return;
|
| 1982 |
|
| 1983 | buf_printf(b, "\n");
|
| 1984 | buf_printf(b, "static const struct modversion_info ____versions[]\n");
|
| 1985 | buf_printf(b, "__used __section(\"__versions\") = {\n");
|
| 1986 |
|
| 1987 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 1988 | if (!s->module)
|
| 1989 | continue;
|
| 1990 | if (!s->crc_valid) {
|
| 1991 | mod_warn(mod, "symbol '%s' has no CRC!\n", s->name);
|
| 1992 | continue;
|
| 1993 | }
|
| 1994 | if (strlen(s->name) >= MODULE_NAME_LEN) {
|
| 1995 | if (extended_modversions) {
|
| 1996 |
|
| 1997 | continue;
|
| 1998 | } else {
|
| 1999 | mod_error(mod, "too long symbol '%s'\n", s->name);
|
| 2000 | break;
|
| 2001 | }
|
| 2002 | }
|
| 2003 | buf_printf(b, "\t{ 0x%08x, \"%s\" },\n",
|
| 2004 | s->crc, s->name);
|
| 2005 | }
|
| 2006 |
|
| 2007 | buf_printf(b, "};\n");
|
| 2008 | }
|
| 2009 |
|
| 2010 | static void add_depends(struct buffer *b, struct module *mod)
|
| 2011 | {
|
| 2012 | struct symbol *s;
|
| 2013 | int first = 1;
|
| 2014 |
|
| 2015 |
|
| 2016 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 2017 | if (s->module)
|
| 2018 | s->module->seen = s->module->is_vmlinux;
|
| 2019 | }
|
| 2020 |
|
| 2021 | buf_printf(b, "\n");
|
| 2022 | buf_printf(b, "MODULE_INFO(depends, \"");
|
| 2023 | list_for_each_entry(s, &mod->unresolved_symbols, list) {
|
| 2024 | const char *p;
|
| 2025 | if (!s->module)
|
| 2026 | continue;
|
| 2027 |
|
| 2028 | if (s->module->seen)
|
| 2029 | continue;
|
| 2030 |
|
| 2031 | s->module->seen = true;
|
| 2032 | p = get_basename(s->module->name);
|
| 2033 | buf_printf(b, "%s%s", first ? "" : ",", p);
|
| 2034 | first = 0;
|
| 2035 | }
|
| 2036 | buf_printf(b, "\");\n");
|
| 2037 | }
|
| 2038 |
|
| 2039 | static void add_srcversion(struct buffer *b, struct module *mod)
|
| 2040 | {
|
| 2041 | if (mod->srcversion[0]) {
|
| 2042 | buf_printf(b, "\n");
|
| 2043 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
|
| 2044 | mod->srcversion);
|
| 2045 | }
|
| 2046 | }
|
| 2047 |
|
| 2048 | static void write_buf(struct buffer *b, const char *fname)
|
| 2049 | {
|
| 2050 | FILE *file;
|
| 2051 |
|
| 2052 | if (error_occurred)
|
| 2053 | return;
|
| 2054 |
|
| 2055 | file = fopen(fname, "w");
|
| 2056 | if (!file) {
|
| 2057 | perror(fname);
|
| 2058 | exit(1);
|
| 2059 | }
|
| 2060 | if (fwrite(b->p, 1, b->pos, file) != b->pos) {
|
| 2061 | perror(fname);
|
| 2062 | exit(1);
|
| 2063 | }
|
| 2064 | if (fclose(file) != 0) {
|
| 2065 | perror(fname);
|
| 2066 | exit(1);
|
| 2067 | }
|
| 2068 | }
|
| 2069 |
|
| 2070 | static void write_if_changed(struct buffer *b, const char *fname)
|
| 2071 | {
|
| 2072 | char *tmp;
|
| 2073 | FILE *file;
|
| 2074 | struct stat st;
|
| 2075 |
|
| 2076 | file = fopen(fname, "r");
|
| 2077 | if (!file)
|
| 2078 | goto write;
|
| 2079 |
|
| 2080 | if (fstat(fileno(file), &st) < 0)
|
| 2081 | goto close_write;
|
| 2082 |
|
| 2083 | if (st.st_size != b->pos)
|
| 2084 | goto close_write;
|
| 2085 |
|
| 2086 | tmp = xmalloc(b->pos);
|
| 2087 | if (fread(tmp, 1, b->pos, file) != b->pos)
|
| 2088 | goto free_write;
|
| 2089 |
|
| 2090 | if (memcmp(tmp, b->p, b->pos) != 0)
|
| 2091 | goto free_write;
|
| 2092 |
|
| 2093 | free(tmp);
|
| 2094 | fclose(file);
|
| 2095 | return;
|
| 2096 |
|
| 2097 | free_write:
|
| 2098 | free(tmp);
|
| 2099 | close_write:
|
| 2100 | fclose(file);
|
| 2101 | write:
|
| 2102 | write_buf(b, fname);
|
| 2103 | }
|
| 2104 |
|
| 2105 | static void write_vmlinux_export_c_file(struct module *mod)
|
| 2106 | {
|
| 2107 | struct buffer buf = { };
|
| 2108 | struct module_alias *alias, *next;
|
| 2109 |
|
| 2110 | buf_printf(&buf,
|
| 2111 | "#include <linux/export-internal.h>\n");
|
| 2112 |
|
| 2113 | add_exported_symbols(&buf, mod);
|
| 2114 |
|
| 2115 | buf_printf(&buf,
|
| 2116 | "#include <linux/module.h>\n"
|
| 2117 | "#undef __MODULE_INFO_PREFIX\n"
|
| 2118 | "#define __MODULE_INFO_PREFIX\n");
|
| 2119 |
|
| 2120 | list_for_each_entry_safe(alias, next, &mod->aliases, node) {
|
| 2121 | buf_printf(&buf, "MODULE_INFO(%s.alias, \"%s\");\n",
|
| 2122 | alias->builtin_modname, alias->str);
|
| 2123 | list_del(&alias->node);
|
| 2124 | free(alias->builtin_modname);
|
| 2125 | free(alias);
|
| 2126 | }
|
| 2127 |
|
| 2128 | write_if_changed(&buf, ".vmlinux.export.c");
|
| 2129 | free(buf.p);
|
| 2130 | }
|
| 2131 |
|
| 2132 |
|
| 2133 | static void write_mod_c_file(struct module *mod)
|
| 2134 | {
|
| 2135 | struct buffer buf = { };
|
| 2136 | struct module_alias *alias, *next;
|
| 2137 | char fname[PATH_MAX];
|
| 2138 | int ret;
|
| 2139 |
|
| 2140 | add_header(&buf, mod);
|
| 2141 | add_exported_symbols(&buf, mod);
|
| 2142 | add_versions(&buf, mod);
|
| 2143 | add_extended_versions(&buf, mod);
|
| 2144 | add_depends(&buf, mod);
|
| 2145 |
|
| 2146 | buf_printf(&buf, "\n");
|
| 2147 | list_for_each_entry_safe(alias, next, &mod->aliases, node) {
|
| 2148 | buf_printf(&buf, "MODULE_ALIAS(\"%s\");\n", alias->str);
|
| 2149 | list_del(&alias->node);
|
| 2150 | free(alias);
|
| 2151 | }
|
| 2152 |
|
| 2153 | add_srcversion(&buf, mod);
|
| 2154 |
|
| 2155 | ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
|
| 2156 | if (ret >= sizeof(fname)) {
|
| 2157 | error("%s: too long path was truncated\n", fname);
|
| 2158 | goto free;
|
| 2159 | }
|
| 2160 |
|
| 2161 | write_if_changed(&buf, fname);
|
| 2162 |
|
| 2163 | free:
|
| 2164 | free(buf.p);
|
| 2165 | }
|
| 2166 |
|
| 2167 | |
| 2168 | * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
|
| 2169 | *
|
| 2170 | static void read_dump(const char *fname)
|
| 2171 | {
|
| 2172 | char *buf, *pos, *line;
|
| 2173 |
|
| 2174 | buf = read_text_file(fname);
|
| 2175 | if (!buf)
|
| 2176 |
|
| 2177 | return;
|
| 2178 |
|
| 2179 | pos = buf;
|
| 2180 |
|
| 2181 | while ((line = get_line(&pos))) {
|
| 2182 | char *symname, *namespace, *modname, *d, *export;
|
| 2183 | unsigned int crc;
|
| 2184 | struct module *mod;
|
| 2185 | struct symbol *s;
|
| 2186 | bool gpl_only;
|
| 2187 |
|
| 2188 | if (!(symname = strchr(line, '\t')))
|
| 2189 | goto fail;
|
| 2190 | *symname++ = '\0';
|
| 2191 | if (!(modname = strchr(symname, '\t')))
|
| 2192 | goto fail;
|
| 2193 | *modname++ = '\0';
|
| 2194 | if (!(export = strchr(modname, '\t')))
|
| 2195 | goto fail;
|
| 2196 | *export++ = '\0';
|
| 2197 | if (!(namespace = strchr(export, '\t')))
|
| 2198 | goto fail;
|
| 2199 | *namespace++ = '\0';
|
| 2200 |
|
| 2201 | crc = strtoul(line, &d, 16);
|
| 2202 | if (*symname == '\0' || *modname == '\0' || *d != '\0')
|
| 2203 | goto fail;
|
| 2204 |
|
| 2205 | if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
|
| 2206 | gpl_only = true;
|
| 2207 | } else if (!strcmp(export, "EXPORT_SYMBOL")) {
|
| 2208 | gpl_only = false;
|
| 2209 | } else {
|
| 2210 | error("%s: unknown license %s. skip", symname, export);
|
| 2211 | continue;
|
| 2212 | }
|
| 2213 |
|
| 2214 | mod = find_module(fname, modname);
|
| 2215 | if (!mod) {
|
| 2216 | mod = new_module(modname, strlen(modname));
|
| 2217 | mod->dump_file = fname;
|
| 2218 | }
|
| 2219 | s = sym_add_exported(symname, mod, gpl_only, namespace);
|
| 2220 | sym_set_crc(s, crc);
|
| 2221 | }
|
| 2222 | free(buf);
|
| 2223 | return;
|
| 2224 | fail:
|
| 2225 | free(buf);
|
| 2226 | fatal("parse error in symbol dump file\n");
|
| 2227 | }
|
| 2228 |
|
| 2229 | static void write_dump(const char *fname)
|
| 2230 | {
|
| 2231 | struct buffer buf = { };
|
| 2232 | struct module *mod;
|
| 2233 | struct symbol *sym;
|
| 2234 |
|
| 2235 | list_for_each_entry(mod, &modules, list) {
|
| 2236 | if (mod->dump_file)
|
| 2237 | continue;
|
| 2238 | list_for_each_entry(sym, &mod->exported_symbols, list) {
|
| 2239 | if (trim_unused_exports && !sym->used)
|
| 2240 | continue;
|
| 2241 |
|
| 2242 | buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
|
| 2243 | sym->crc, sym->name, mod->name,
|
| 2244 | sym->is_gpl_only ? "_GPL" : "",
|
| 2245 | sym->namespace);
|
| 2246 | }
|
| 2247 | }
|
| 2248 | write_buf(&buf, fname);
|
| 2249 | free(buf.p);
|
| 2250 | }
|
| 2251 |
|
| 2252 | static void write_namespace_deps_files(const char *fname)
|
| 2253 | {
|
| 2254 | struct module *mod;
|
| 2255 | struct namespace_list *ns;
|
| 2256 | struct buffer ns_deps_buf = {};
|
| 2257 |
|
| 2258 | list_for_each_entry(mod, &modules, list) {
|
| 2259 |
|
| 2260 | if (mod->dump_file || list_empty(&mod->missing_namespaces))
|
| 2261 | continue;
|
| 2262 |
|
| 2263 | buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
|
| 2264 |
|
| 2265 | list_for_each_entry(ns, &mod->missing_namespaces, list)
|
| 2266 | buf_printf(&ns_deps_buf, " %s", ns->namespace);
|
| 2267 |
|
| 2268 | buf_printf(&ns_deps_buf, "\n");
|
| 2269 | }
|
| 2270 |
|
| 2271 | write_if_changed(&ns_deps_buf, fname);
|
| 2272 | free(ns_deps_buf.p);
|
| 2273 | }
|
| 2274 |
|
| 2275 | struct dump_list {
|
| 2276 | struct list_head list;
|
| 2277 | const char *file;
|
| 2278 | };
|
| 2279 |
|
| 2280 | static void check_host_endian(void)
|
| 2281 | {
|
| 2282 | static const union {
|
| 2283 | short s;
|
| 2284 | char c[2];
|
| 2285 | } endian_test = { .c = {0x01, 0x02} };
|
| 2286 |
|
| 2287 | switch (endian_test.s) {
|
| 2288 | case 0x0102:
|
| 2289 | host_is_big_endian = true;
|
| 2290 | break;
|
| 2291 | case 0x0201:
|
| 2292 | host_is_big_endian = false;
|
| 2293 | break;
|
| 2294 | default:
|
| 2295 | fatal("Unknown host endian\n");
|
| 2296 | }
|
| 2297 | }
|
| 2298 |
|
| 2299 | int main(int argc, char **argv)
|
| 2300 | {
|
| 2301 | struct module *mod;
|
| 2302 | char *missing_namespace_deps = NULL;
|
| 2303 | char *unused_exports_white_list = NULL;
|
| 2304 | char *dump_write = NULL, *files_source = NULL;
|
| 2305 | int opt;
|
| 2306 | LIST_HEAD(dump_lists);
|
| 2307 | struct dump_list *dl, *dl2;
|
| 2308 |
|
| 2309 | while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:xb")) != -1) {
|
| 2310 | switch (opt) {
|
| 2311 | case 'e':
|
| 2312 | external_module = true;
|
| 2313 | break;
|
| 2314 | case 'i':
|
| 2315 | dl = xmalloc(sizeof(*dl));
|
| 2316 | dl->file = optarg;
|
| 2317 | list_add_tail(&dl->list, &dump_lists);
|
| 2318 | break;
|
| 2319 | case 'M':
|
| 2320 | module_enabled = true;
|
| 2321 | break;
|
| 2322 | case 'm':
|
| 2323 | modversions = true;
|
| 2324 | break;
|
| 2325 | case 'n':
|
| 2326 | ignore_missing_files = true;
|
| 2327 | break;
|
| 2328 | case 'o':
|
| 2329 | dump_write = optarg;
|
| 2330 | break;
|
| 2331 | case 'a':
|
| 2332 | all_versions = true;
|
| 2333 | break;
|
| 2334 | case 'T':
|
| 2335 | files_source = optarg;
|
| 2336 | break;
|
| 2337 | case 't':
|
| 2338 | trim_unused_exports = true;
|
| 2339 | break;
|
| 2340 | case 'u':
|
| 2341 | unused_exports_white_list = optarg;
|
| 2342 | break;
|
| 2343 | case 'W':
|
| 2344 | extra_warn = true;
|
| 2345 | break;
|
| 2346 | case 'w':
|
| 2347 | warn_unresolved = true;
|
| 2348 | break;
|
| 2349 | case 'E':
|
| 2350 | sec_mismatch_warn_only = false;
|
| 2351 | break;
|
| 2352 | case 'N':
|
| 2353 | allow_missing_ns_imports = true;
|
| 2354 | break;
|
| 2355 | case 'd':
|
| 2356 | missing_namespace_deps = optarg;
|
| 2357 | break;
|
| 2358 | case 'b':
|
| 2359 | basic_modversions = true;
|
| 2360 | break;
|
| 2361 | case 'x':
|
| 2362 | extended_modversions = true;
|
| 2363 | break;
|
| 2364 | default:
|
| 2365 | exit(1);
|
| 2366 | }
|
| 2367 | }
|
| 2368 |
|
| 2369 | check_host_endian();
|
| 2370 |
|
| 2371 | list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
|
| 2372 | read_dump(dl->file);
|
| 2373 | list_del(&dl->list);
|
| 2374 | free(dl);
|
| 2375 | }
|
| 2376 |
|
| 2377 | while (optind < argc)
|
| 2378 | read_symbols(argv[optind++]);
|
| 2379 |
|
| 2380 | if (files_source)
|
| 2381 | read_symbols_from_files(files_source);
|
| 2382 |
|
| 2383 | list_for_each_entry(mod, &modules, list) {
|
| 2384 | keep_no_trim_symbols(mod);
|
| 2385 |
|
| 2386 | if (mod->dump_file || mod->is_vmlinux)
|
| 2387 | continue;
|
| 2388 |
|
| 2389 | check_modname_len(mod);
|
| 2390 | check_exports(mod);
|
| 2391 | }
|
| 2392 |
|
| 2393 | if (unused_exports_white_list)
|
| 2394 | handle_white_list_exports(unused_exports_white_list);
|
| 2395 |
|
| 2396 | list_for_each_entry(mod, &modules, list) {
|
| 2397 | if (mod->dump_file)
|
| 2398 | continue;
|
| 2399 |
|
| 2400 | if (mod->is_vmlinux)
|
| 2401 | write_vmlinux_export_c_file(mod);
|
| 2402 | else
|
| 2403 | write_mod_c_file(mod);
|
| 2404 | }
|
| 2405 |
|
| 2406 | if (missing_namespace_deps)
|
| 2407 | write_namespace_deps_files(missing_namespace_deps);
|
| 2408 |
|
| 2409 | if (dump_write)
|
| 2410 | write_dump(dump_write);
|
| 2411 | if (sec_mismatch_count && !sec_mismatch_warn_only)
|
| 2412 | error("Section mismatches detected.\n"
|
| 2413 | "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
|
| 2414 |
|
| 2415 | if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
|
| 2416 | warn("suppressed %u unresolved symbol warnings because there were too many)\n",
|
| 2417 | nr_unresolved - MAX_UNRESOLVED_REPORTS);
|
| 2418 |
|
| 2419 | return error_occurred ? 1 : 0;
|
| 2420 | }
|