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