Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Postprocess module symbol versions |
| 2 | * |
| 3 | * Copyright 2003 Kai Germaschewski |
| 4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation |
Sam Ravnborg | 382168f | 2006-02-26 20:11:17 +0100 | [diff] [blame] | 5 | * Copyright 2006 Sam Ravnborg |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <ctype.h> |
| 15 | #include "modpost.h" |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 16 | #include "../../include/linux/license.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* Are we using CONFIG_MODVERSIONS? */ |
| 19 | int modversions = 0; |
| 20 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
| 21 | int have_vmlinux = 0; |
| 22 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
| 23 | static int all_versions = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 24 | /* If we are modposting external module set to 1 */ |
| 25 | static int external_module = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 26 | /* How a symbol is exported */ |
| 27 | enum export {export_plain, export_gpl, export_gpl_future, export_unknown}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 29 | void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | va_list arglist; |
| 32 | |
| 33 | fprintf(stderr, "FATAL: "); |
| 34 | |
| 35 | va_start(arglist, fmt); |
| 36 | vfprintf(stderr, fmt, arglist); |
| 37 | va_end(arglist); |
| 38 | |
| 39 | exit(1); |
| 40 | } |
| 41 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 42 | void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | va_list arglist; |
| 45 | |
| 46 | fprintf(stderr, "WARNING: "); |
| 47 | |
| 48 | va_start(arglist, fmt); |
| 49 | vfprintf(stderr, fmt, arglist); |
| 50 | va_end(arglist); |
| 51 | } |
| 52 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 53 | static int is_vmlinux(const char *modname) |
| 54 | { |
| 55 | const char *myname; |
| 56 | |
| 57 | if ((myname = strrchr(modname, '/'))) |
| 58 | myname++; |
| 59 | else |
| 60 | myname = modname; |
| 61 | |
| 62 | return strcmp(myname, "vmlinux") == 0; |
| 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | void *do_nofail(void *ptr, const char *expr) |
| 66 | { |
| 67 | if (!ptr) { |
| 68 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
| 69 | } |
| 70 | return ptr; |
| 71 | } |
| 72 | |
| 73 | /* A list of all modules we processed */ |
| 74 | |
| 75 | static struct module *modules; |
| 76 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 77 | static struct module *find_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | struct module *mod; |
| 80 | |
| 81 | for (mod = modules; mod; mod = mod->next) |
| 82 | if (strcmp(mod->name, modname) == 0) |
| 83 | break; |
| 84 | return mod; |
| 85 | } |
| 86 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 87 | static struct module *new_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct module *mod; |
| 90 | char *p, *s; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 93 | memset(mod, 0, sizeof(*mod)); |
| 94 | p = NOFAIL(strdup(modname)); |
| 95 | |
| 96 | /* strip trailing .o */ |
| 97 | if ((s = strrchr(p, '.')) != NULL) |
| 98 | if (strcmp(s, ".o") == 0) |
| 99 | *s = '\0'; |
| 100 | |
| 101 | /* add to list */ |
| 102 | mod->name = p; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 103 | mod->gpl_compatible = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | mod->next = modules; |
| 105 | modules = mod; |
| 106 | |
| 107 | return mod; |
| 108 | } |
| 109 | |
| 110 | /* A hash of all exported symbols, |
| 111 | * struct symbol is also used for lists of unresolved symbols */ |
| 112 | |
| 113 | #define SYMBOL_HASH_SIZE 1024 |
| 114 | |
| 115 | struct symbol { |
| 116 | struct symbol *next; |
| 117 | struct module *module; |
| 118 | unsigned int crc; |
| 119 | int crc_valid; |
| 120 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 121 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 122 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 123 | * (only for external modules) **/ |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 124 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 125 | enum export export; /* Type of export */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | char name[0]; |
| 127 | }; |
| 128 | |
| 129 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 130 | |
| 131 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 132 | static inline unsigned int tdb_hash(const char *name) |
| 133 | { |
| 134 | unsigned value; /* Used to compute the hash value. */ |
| 135 | unsigned i; /* Used to cycle through random values. */ |
| 136 | |
| 137 | /* Set the initial value from the key size. */ |
| 138 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) |
| 139 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 140 | |
| 141 | return (1103515243 * value + 12345); |
| 142 | } |
| 143 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 144 | /** |
| 145 | * Allocate a new symbols for use in the hash of exported symbols or |
| 146 | * the list of unresolved symbols per module |
| 147 | **/ |
| 148 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 149 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 152 | |
| 153 | memset(s, 0, sizeof(*s)); |
| 154 | strcpy(s->name, name); |
| 155 | s->weak = weak; |
| 156 | s->next = next; |
| 157 | return s; |
| 158 | } |
| 159 | |
| 160 | /* For the hash of exported symbols */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 161 | static struct symbol *new_symbol(const char *name, struct module *module, |
| 162 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | unsigned int hash; |
| 165 | struct symbol *new; |
| 166 | |
| 167 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 168 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 169 | new->module = module; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 170 | new->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 171 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 174 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct symbol *s; |
| 177 | |
| 178 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 179 | if (name[0] == '.') |
| 180 | name++; |
| 181 | |
| 182 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { |
| 183 | if (strcmp(s->name, name) == 0) |
| 184 | return s; |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 189 | static struct { |
| 190 | const char *str; |
| 191 | enum export export; |
| 192 | } export_list[] = { |
| 193 | { .str = "EXPORT_SYMBOL", .export = export_plain }, |
| 194 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
| 195 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
| 196 | { .str = "(unknown)", .export = export_unknown }, |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | static const char *export_str(enum export ex) |
| 201 | { |
| 202 | return export_list[ex].str; |
| 203 | } |
| 204 | |
| 205 | static enum export export_no(const char * s) |
| 206 | { |
| 207 | int i; |
| 208 | for (i = 0; export_list[i].export != export_unknown; i++) { |
| 209 | if (strcmp(export_list[i].str, s) == 0) |
| 210 | return export_list[i].export; |
| 211 | } |
| 212 | return export_unknown; |
| 213 | } |
| 214 | |
| 215 | static enum export export_from_sec(struct elf_info *elf, Elf_Section sec) |
| 216 | { |
| 217 | if (sec == elf->export_sec) |
| 218 | return export_plain; |
| 219 | else if (sec == elf->export_gpl_sec) |
| 220 | return export_gpl; |
| 221 | else if (sec == elf->export_gpl_future_sec) |
| 222 | return export_gpl_future; |
| 223 | else |
| 224 | return export_unknown; |
| 225 | } |
| 226 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 227 | /** |
| 228 | * Add an exported symbol - it may have already been added without a |
| 229 | * CRC, in this case just update the CRC |
| 230 | **/ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 231 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
| 232 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | struct symbol *s = find_symbol(name); |
| 235 | |
| 236 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 237 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 238 | } else { |
| 239 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 240 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 241 | "was in %s%s\n", mod->name, name, |
| 242 | s->module->name, |
| 243 | is_vmlinux(s->module->name) ?"":".ko"); |
| 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 246 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 247 | s->vmlinux = is_vmlinux(mod->name); |
| 248 | s->kernel = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 249 | s->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 250 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 253 | static void sym_update_crc(const char *name, struct module *mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 254 | unsigned int crc, enum export export) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 255 | { |
| 256 | struct symbol *s = find_symbol(name); |
| 257 | |
| 258 | if (!s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 259 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 260 | s->crc = crc; |
| 261 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 264 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | struct stat st; |
| 267 | void *map; |
| 268 | int fd; |
| 269 | |
| 270 | fd = open(filename, O_RDONLY); |
| 271 | if (fd < 0 || fstat(fd, &st) != 0) |
| 272 | return NULL; |
| 273 | |
| 274 | *size = st.st_size; |
| 275 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 276 | close(fd); |
| 277 | |
| 278 | if (map == MAP_FAILED) |
| 279 | return NULL; |
| 280 | return map; |
| 281 | } |
| 282 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 283 | /** |
| 284 | * Return a copy of the next line in a mmap'ed file. |
| 285 | * spaces in the beginning of the line is trimmed away. |
| 286 | * Return a pointer to a static buffer. |
| 287 | **/ |
| 288 | char* get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | static char line[4096]; |
| 291 | int skip = 1; |
| 292 | size_t len = 0; |
| 293 | signed char *p = (signed char *)file + *pos; |
| 294 | char *s = line; |
| 295 | |
| 296 | for (; *pos < size ; (*pos)++) |
| 297 | { |
| 298 | if (skip && isspace(*p)) { |
| 299 | p++; |
| 300 | continue; |
| 301 | } |
| 302 | skip = 0; |
| 303 | if (*p != '\n' && (*pos < size)) { |
| 304 | len++; |
| 305 | *s++ = *p++; |
| 306 | if (len > 4095) |
| 307 | break; /* Too long, stop */ |
| 308 | } else { |
| 309 | /* End of string */ |
| 310 | *s = '\0'; |
| 311 | return line; |
| 312 | } |
| 313 | } |
| 314 | /* End of buffer */ |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 318 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
| 320 | munmap(file, size); |
| 321 | } |
| 322 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 323 | static void parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | unsigned int i; |
| 326 | Elf_Ehdr *hdr = info->hdr; |
| 327 | Elf_Shdr *sechdrs; |
| 328 | Elf_Sym *sym; |
| 329 | |
| 330 | hdr = grab_file(filename, &info->size); |
| 331 | if (!hdr) { |
| 332 | perror(filename); |
Sam Ravnborg | 6803dc0 | 2006-06-24 23:46:54 +0200 | [diff] [blame^] | 333 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | info->hdr = hdr; |
| 336 | if (info->size < sizeof(*hdr)) |
| 337 | goto truncated; |
| 338 | |
| 339 | /* Fix endianness in ELF header */ |
| 340 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 341 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
| 342 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 343 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
| 344 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 345 | info->sechdrs = sechdrs; |
| 346 | |
| 347 | /* Fix endianness in section headers */ |
| 348 | for (i = 0; i < hdr->e_shnum; i++) { |
| 349 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 350 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 351 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 352 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 353 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
| 354 | } |
| 355 | /* Find symbol table. */ |
| 356 | for (i = 1; i < hdr->e_shnum; i++) { |
| 357 | const char *secstrings |
| 358 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 359 | const char *secname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (sechdrs[i].sh_offset > info->size) |
| 362 | goto truncated; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 363 | secname = secstrings + sechdrs[i].sh_name; |
| 364 | if (strcmp(secname, ".modinfo") == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 366 | info->modinfo_len = sechdrs[i].sh_size; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 367 | } else if (strcmp(secname, "__ksymtab") == 0) |
| 368 | info->export_sec = i; |
| 369 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
| 370 | info->export_gpl_sec = i; |
| 371 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
| 372 | info->export_gpl_future_sec = i; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
| 375 | continue; |
| 376 | |
| 377 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 378 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | + sechdrs[i].sh_size; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 380 | info->strtab = (void *)hdr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | sechdrs[sechdrs[i].sh_link].sh_offset; |
| 382 | } |
| 383 | if (!info->symtab_start) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 384 | fatal("%s has no symtab?\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | /* Fix endianness in symbols */ |
| 387 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 388 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 389 | sym->st_name = TO_NATIVE(sym->st_name); |
| 390 | sym->st_value = TO_NATIVE(sym->st_value); |
| 391 | sym->st_size = TO_NATIVE(sym->st_size); |
| 392 | } |
| 393 | return; |
| 394 | |
| 395 | truncated: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 396 | fatal("%s is truncated.\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 399 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
| 401 | release_file(info->hdr, info->size); |
| 402 | } |
| 403 | |
Luke Yang | f7b05e6 | 2006-03-02 18:35:31 +0800 | [diff] [blame] | 404 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
| 405 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 407 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 408 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
| 410 | unsigned int crc; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 411 | enum export export = export_from_sec(info, sym->st_shndx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | switch (sym->st_shndx) { |
| 414 | case SHN_COMMON: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 415 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | break; |
| 417 | case SHN_ABS: |
| 418 | /* CRC'd symbol */ |
| 419 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
| 420 | crc = (unsigned int) sym->st_value; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 421 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
| 422 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | break; |
| 425 | case SHN_UNDEF: |
| 426 | /* undefined symbol */ |
| 427 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 428 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 429 | break; |
| 430 | /* ignore global offset table */ |
| 431 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 432 | break; |
| 433 | /* ignore __this_module, it will be resolved shortly */ |
| 434 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) |
| 435 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 436 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 437 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 438 | /* add compatibility with older glibc */ |
| 439 | #ifndef STT_SPARC_REGISTER |
| 440 | #define STT_SPARC_REGISTER STT_REGISTER |
| 441 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (info->hdr->e_machine == EM_SPARC || |
| 443 | info->hdr->e_machine == EM_SPARCV9) { |
| 444 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 445 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 447 | if (symname[0] == '.') { |
| 448 | char *munged = strdup(symname); |
| 449 | munged[0] = '_'; |
| 450 | munged[1] = toupper(munged[1]); |
| 451 | symname = munged; |
| 452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
| 457 | strlen(MODULE_SYMBOL_PREFIX)) == 0) |
| 458 | mod->unres = alloc_symbol(symname + |
| 459 | strlen(MODULE_SYMBOL_PREFIX), |
| 460 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 461 | mod->unres); |
| 462 | break; |
| 463 | default: |
| 464 | /* All exported symbols */ |
| 465 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 466 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
| 467 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) |
| 470 | mod->has_init = 1; |
| 471 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) |
| 472 | mod->has_cleanup = 1; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 477 | /** |
| 478 | * Parse tag=value strings from .modinfo section |
| 479 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | static char *next_string(char *string, unsigned long *secsize) |
| 481 | { |
| 482 | /* Skip non-zero chars */ |
| 483 | while (string[0]) { |
| 484 | string++; |
| 485 | if ((*secsize)-- <= 1) |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | /* Skip any zero padding. */ |
| 490 | while (!string[0]) { |
| 491 | string++; |
| 492 | if ((*secsize)-- <= 1) |
| 493 | return NULL; |
| 494 | } |
| 495 | return string; |
| 496 | } |
| 497 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 498 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
| 499 | const char *tag, char *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | char *p; |
| 502 | unsigned int taglen = strlen(tag); |
| 503 | unsigned long size = modinfo_len; |
| 504 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 505 | if (info) { |
| 506 | size -= info - (char *)modinfo; |
| 507 | modinfo = next_string(info, &size); |
| 508 | } |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 511 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 512 | return p + taglen + 1; |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 517 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 518 | const char *tag) |
| 519 | |
| 520 | { |
| 521 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); |
| 522 | } |
| 523 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 524 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 525 | * Test if string s ends in string sub |
| 526 | * return 0 if match |
| 527 | **/ |
| 528 | static int strrcmp(const char *s, const char *sub) |
| 529 | { |
| 530 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 531 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 532 | if (!s || !sub) |
| 533 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 534 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 535 | slen = strlen(s); |
| 536 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 537 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 538 | if ((slen == 0) || (sublen == 0)) |
| 539 | return 1; |
| 540 | |
| 541 | if (sublen > slen) |
| 542 | return 1; |
| 543 | |
| 544 | return memcmp(s + slen - sublen, sub, sublen); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Whitelist to allow certain references to pass with no warning. |
| 549 | * Pattern 1: |
| 550 | * If a module parameter is declared __initdata and permissions=0 |
| 551 | * then this is legal despite the warning generated. |
| 552 | * We cannot see value of permissions here, so just ignore |
| 553 | * this pattern. |
| 554 | * The pattern is identified by: |
| 555 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 556 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 557 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 558 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 559 | * Pattern 2: |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 560 | * Many drivers utilise a *driver container with references to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 561 | * add, remove, probe functions etc. |
| 562 | * These functions may often be marked __init and we do not want to |
| 563 | * warn here. |
| 564 | * the pattern is identified by: |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 565 | * tosec = .init.text | .exit.text | .init.data |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 566 | * fromsec = .data |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 567 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 568 | **/ |
| 569 | static int secref_whitelist(const char *tosec, const char *fromsec, |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 570 | const char *atsym) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 571 | { |
| 572 | int f1 = 1, f2 = 1; |
| 573 | const char **s; |
| 574 | const char *pat2sym[] = { |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 575 | "driver", |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 576 | "_template", /* scsi uses *_template a lot */ |
| 577 | "_sht", /* scsi also used *_sht to some extent */ |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 578 | "_ops", |
| 579 | "_probe", |
| 580 | "_probe_one", |
| 581 | NULL |
| 582 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 583 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 584 | /* Check for pattern 1 */ |
| 585 | if (strcmp(tosec, ".init.data") != 0) |
| 586 | f1 = 0; |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 587 | if (strncmp(fromsec, ".data", strlen(".data")) != 0) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 588 | f1 = 0; |
| 589 | if (strncmp(atsym, "__param", strlen("__param")) != 0) |
| 590 | f1 = 0; |
| 591 | |
| 592 | if (f1) |
| 593 | return f1; |
| 594 | |
| 595 | /* Check for pattern 2 */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 596 | if ((strcmp(tosec, ".init.text") != 0) && |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 597 | (strcmp(tosec, ".exit.text") != 0) && |
| 598 | (strcmp(tosec, ".init.data") != 0)) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 599 | f2 = 0; |
| 600 | if (strcmp(fromsec, ".data") != 0) |
| 601 | f2 = 0; |
| 602 | |
| 603 | for (s = pat2sym; *s; s++) |
| 604 | if (strrcmp(atsym, *s) == 0) |
| 605 | f1 = 1; |
| 606 | |
| 607 | return f1 && f2; |
| 608 | } |
| 609 | |
| 610 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 611 | * Find symbol based on relocation record info. |
| 612 | * In some cases the symbol supplied is a valid symbol so |
| 613 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 614 | * In other cases the symbol needs to be looked up in the symbol table |
| 615 | * based on section and address. |
| 616 | * **/ |
| 617 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, |
| 618 | Elf_Sym *relsym) |
| 619 | { |
| 620 | Elf_Sym *sym; |
| 621 | |
| 622 | if (relsym->st_name != 0) |
| 623 | return relsym; |
| 624 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 625 | if (sym->st_shndx != relsym->st_shndx) |
| 626 | continue; |
| 627 | if (sym->st_value == addr) |
| 628 | return sym; |
| 629 | } |
| 630 | return NULL; |
| 631 | } |
| 632 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 633 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 634 | * Find symbols before or equal addr and after addr - in the section sec. |
| 635 | * If we find two symbols with equal offset prefer one with a valid name. |
| 636 | * The ELF format may have a better way to detect what type of symbol |
| 637 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 638 | **/ |
| 639 | static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, |
| 640 | const char *sec, |
| 641 | Elf_Sym **before, Elf_Sym **after) |
| 642 | { |
| 643 | Elf_Sym *sym; |
| 644 | Elf_Ehdr *hdr = elf->hdr; |
| 645 | Elf_Addr beforediff = ~0; |
| 646 | Elf_Addr afterdiff = ~0; |
| 647 | const char *secstrings = (void *)hdr + |
| 648 | elf->sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 649 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 650 | *before = NULL; |
| 651 | *after = NULL; |
| 652 | |
| 653 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 654 | const char *symsec; |
| 655 | |
| 656 | if (sym->st_shndx >= SHN_LORESERVE) |
| 657 | continue; |
| 658 | symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; |
| 659 | if (strcmp(symsec, sec) != 0) |
| 660 | continue; |
| 661 | if (sym->st_value <= addr) { |
| 662 | if ((addr - sym->st_value) < beforediff) { |
| 663 | beforediff = addr - sym->st_value; |
| 664 | *before = sym; |
| 665 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 666 | else if ((addr - sym->st_value) == beforediff) { |
| 667 | /* equal offset, valid name? */ |
| 668 | const char *name = elf->strtab + sym->st_name; |
| 669 | if (name && strlen(name)) |
| 670 | *before = sym; |
| 671 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 672 | } |
| 673 | else |
| 674 | { |
| 675 | if ((sym->st_value - addr) < afterdiff) { |
| 676 | afterdiff = sym->st_value - addr; |
| 677 | *after = sym; |
| 678 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 679 | else if ((sym->st_value - addr) == afterdiff) { |
| 680 | /* equal offset, valid name? */ |
| 681 | const char *name = elf->strtab + sym->st_name; |
| 682 | if (name && strlen(name)) |
| 683 | *after = sym; |
| 684 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Print a warning about a section mismatch. |
| 691 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 692 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 693 | **/ |
| 694 | static void warn_sec_mismatch(const char *modname, const char *fromsec, |
| 695 | struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) |
| 696 | { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 697 | const char *refsymname = ""; |
| 698 | Elf_Sym *before, *after; |
| 699 | Elf_Sym *refsym; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 700 | Elf_Ehdr *hdr = elf->hdr; |
| 701 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 702 | const char *secstrings = (void *)hdr + |
| 703 | sechdrs[hdr->e_shstrndx].sh_offset; |
| 704 | const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 705 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 706 | find_symbols_between(elf, r.r_offset, fromsec, &before, &after); |
| 707 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 708 | refsym = find_elf_symbol(elf, r.r_addend, sym); |
| 709 | if (refsym && strlen(elf->strtab + refsym->st_name)) |
| 710 | refsymname = elf->strtab + refsym->st_name; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 711 | |
| 712 | /* check whitelist - we may ignore it */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 713 | if (before && |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 714 | secref_whitelist(secname, fromsec, elf->strtab + before->st_name)) |
| 715 | return; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 716 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 717 | if (before && after) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 718 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 719 | "between '%s' (at offset 0x%llx) and '%s'\n", |
| 720 | modname, secname, refsymname, fromsec, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 721 | elf->strtab + before->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 722 | (long long)r.r_offset, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 723 | elf->strtab + after->st_name); |
| 724 | } else if (before) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 725 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 726 | "after '%s' (at offset 0x%llx)\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 727 | modname, secname, refsymname, fromsec, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 728 | elf->strtab + before->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 729 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 730 | } else if (after) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 731 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 732 | "before '%s' (at offset -0x%llx)\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 733 | modname, secname, refsymname, fromsec, |
Eric Sesterhenn | eaaae38 | 2006-04-11 00:54:16 +0200 | [diff] [blame] | 734 | elf->strtab + after->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 735 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 736 | } else { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 737 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 738 | "(offset 0x%llx)\n", |
| 739 | modname, secname, fromsec, refsymname, |
| 740 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * A module includes a number of sections that are discarded |
| 746 | * either when loaded or when used as built-in. |
| 747 | * For loaded modules all functions marked __init and all data |
| 748 | * marked __initdata will be discarded when the module has been intialized. |
| 749 | * Likewise for modules used built-in the sections marked __exit |
| 750 | * are discarded because __exit marked function are supposed to be called |
| 751 | * only when a moduel is unloaded which never happes for built-in modules. |
| 752 | * The check_sec_ref() function traverses all relocation records |
| 753 | * to find all references to a section that reference a section that will |
| 754 | * be discarded and warns about it. |
| 755 | **/ |
| 756 | static void check_sec_ref(struct module *mod, const char *modname, |
| 757 | struct elf_info *elf, |
| 758 | int section(const char*), |
| 759 | int section_ref_ok(const char *)) |
| 760 | { |
| 761 | int i; |
| 762 | Elf_Sym *sym; |
| 763 | Elf_Ehdr *hdr = elf->hdr; |
| 764 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 765 | const char *secstrings = (void *)hdr + |
| 766 | sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 767 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 768 | /* Walk through all sections */ |
| 769 | for (i = 0; i < hdr->e_shnum; i++) { |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 770 | const char *name = secstrings + sechdrs[i].sh_name; |
| 771 | const char *secname; |
| 772 | Elf_Rela r; |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 773 | unsigned int r_sym; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 774 | /* We want to process only relocation sections and not .init */ |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 775 | if (sechdrs[i].sh_type == SHT_RELA) { |
| 776 | Elf_Rela *rela; |
| 777 | Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; |
| 778 | Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; |
| 779 | name += strlen(".rela"); |
| 780 | if (section_ref_ok(name)) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 781 | continue; |
| 782 | |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 783 | for (rela = start; rela < stop; rela++) { |
| 784 | r.r_offset = TO_NATIVE(rela->r_offset); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 785 | #if KERNEL_ELFCLASS == ELFCLASS64 |
| 786 | if (hdr->e_machine == EM_MIPS) { |
| 787 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); |
| 788 | r_sym = TO_NATIVE(r_sym); |
| 789 | } else { |
| 790 | r.r_info = TO_NATIVE(rela->r_info); |
| 791 | r_sym = ELF_R_SYM(r.r_info); |
| 792 | } |
| 793 | #else |
| 794 | r.r_info = TO_NATIVE(rela->r_info); |
| 795 | r_sym = ELF_R_SYM(r.r_info); |
| 796 | #endif |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 797 | r.r_addend = TO_NATIVE(rela->r_addend); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 798 | sym = elf->symtab_start + r_sym; |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 799 | /* Skip special sections */ |
| 800 | if (sym->st_shndx >= SHN_LORESERVE) |
| 801 | continue; |
| 802 | |
| 803 | secname = secstrings + |
| 804 | sechdrs[sym->st_shndx].sh_name; |
| 805 | if (section(secname)) |
| 806 | warn_sec_mismatch(modname, name, |
| 807 | elf, sym, r); |
| 808 | } |
| 809 | } else if (sechdrs[i].sh_type == SHT_REL) { |
| 810 | Elf_Rel *rel; |
| 811 | Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset; |
| 812 | Elf_Rel *stop = (void*)start + sechdrs[i].sh_size; |
| 813 | name += strlen(".rel"); |
| 814 | if (section_ref_ok(name)) |
| 815 | continue; |
| 816 | |
| 817 | for (rel = start; rel < stop; rel++) { |
| 818 | r.r_offset = TO_NATIVE(rel->r_offset); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 819 | #if KERNEL_ELFCLASS == ELFCLASS64 |
| 820 | if (hdr->e_machine == EM_MIPS) { |
| 821 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); |
| 822 | r_sym = TO_NATIVE(r_sym); |
| 823 | } else { |
| 824 | r.r_info = TO_NATIVE(rel->r_info); |
| 825 | r_sym = ELF_R_SYM(r.r_info); |
| 826 | } |
| 827 | #else |
| 828 | r.r_info = TO_NATIVE(rel->r_info); |
| 829 | r_sym = ELF_R_SYM(r.r_info); |
| 830 | #endif |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 831 | r.r_addend = 0; |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 832 | sym = elf->symtab_start + r_sym; |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 833 | /* Skip special sections */ |
| 834 | if (sym->st_shndx >= SHN_LORESERVE) |
| 835 | continue; |
| 836 | |
| 837 | secname = secstrings + |
| 838 | sechdrs[sym->st_shndx].sh_name; |
| 839 | if (section(secname)) |
| 840 | warn_sec_mismatch(modname, name, |
| 841 | elf, sym, r); |
| 842 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Functions used only during module init is marked __init and is stored in |
| 849 | * a .init.text section. Likewise data is marked __initdata and stored in |
| 850 | * a .init.data section. |
| 851 | * If this section is one of these sections return 1 |
| 852 | * See include/linux/init.h for the details |
| 853 | **/ |
| 854 | static int init_section(const char *name) |
| 855 | { |
| 856 | if (strcmp(name, ".init") == 0) |
| 857 | return 1; |
| 858 | if (strncmp(name, ".init.", strlen(".init.")) == 0) |
| 859 | return 1; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Identify sections from which references to a .init section is OK. |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 865 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 866 | * Unfortunately references to read only data that referenced .init |
| 867 | * sections had to be excluded. Almost all of these are false |
| 868 | * positives, they are created by gcc. The downside of excluding rodata |
| 869 | * is that there really are some user references from rodata to |
| 870 | * init code, e.g. drivers/video/vgacon.c: |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 871 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 872 | * const struct consw vga_con = { |
| 873 | * con_startup: vgacon_startup, |
| 874 | * |
| 875 | * where vgacon_startup is __init. If you want to wade through the false |
| 876 | * positives, take out the check for rodata. |
| 877 | **/ |
| 878 | static int init_section_ref_ok(const char *name) |
| 879 | { |
| 880 | const char **s; |
| 881 | /* Absolute section names */ |
| 882 | const char *namelist1[] = { |
| 883 | ".init", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 884 | ".opd", /* see comment [OPD] at exit_section_ref_ok() */ |
| 885 | ".toc1", /* used by ppc64 */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 886 | ".stab", |
| 887 | ".rodata", |
| 888 | ".text.lock", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 889 | "__bug_table", /* used by powerpc for BUG() */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 890 | ".pci_fixup_header", |
| 891 | ".pci_fixup_final", |
| 892 | ".pdr", |
| 893 | "__param", |
Al Viro | 468d949 | 2006-06-23 23:22:43 +0100 | [diff] [blame] | 894 | "__ex_table", |
| 895 | ".fixup", |
Randy Dunlap | 35899c5 | 2006-06-07 16:23:26 -0700 | [diff] [blame] | 896 | ".smp_locks", |
Sam Ravnborg | 909252d | 2006-06-08 20:37:30 +0200 | [diff] [blame] | 897 | ".plt", /* seen on ARCH=um build on x86_64. Harmless */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 898 | NULL |
| 899 | }; |
| 900 | /* Start of section names */ |
| 901 | const char *namelist2[] = { |
| 902 | ".init.", |
| 903 | ".altinstructions", |
| 904 | ".eh_frame", |
| 905 | ".debug", |
| 906 | NULL |
| 907 | }; |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 908 | /* part of section name */ |
| 909 | const char *namelist3 [] = { |
| 910 | ".unwind", /* sample: IA_64.unwind.init.text */ |
| 911 | NULL |
| 912 | }; |
| 913 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 914 | for (s = namelist1; *s; s++) |
| 915 | if (strcmp(*s, name) == 0) |
| 916 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 917 | for (s = namelist2; *s; s++) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 918 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 919 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 920 | for (s = namelist3; *s; s++) |
Sam Ravnborg | e835a39 | 2006-03-05 11:34:15 +0100 | [diff] [blame] | 921 | if (strstr(name, *s) != NULL) |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 922 | return 1; |
Al Viro | 468d949 | 2006-06-23 23:22:43 +0100 | [diff] [blame] | 923 | if (strrcmp(name, ".init") == 0) |
| 924 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Functions used only during module exit is marked __exit and is stored in |
| 930 | * a .exit.text section. Likewise data is marked __exitdata and stored in |
| 931 | * a .exit.data section. |
| 932 | * If this section is one of these sections return 1 |
| 933 | * See include/linux/init.h for the details |
| 934 | **/ |
| 935 | static int exit_section(const char *name) |
| 936 | { |
| 937 | if (strcmp(name, ".exit.text") == 0) |
| 938 | return 1; |
| 939 | if (strcmp(name, ".exit.data") == 0) |
| 940 | return 1; |
| 941 | return 0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 942 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Identify sections from which references to a .exit section is OK. |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 947 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 948 | * [OPD] Keith Ownes <kaos@sgi.com> commented: |
| 949 | * For our future {in}sanity, add a comment that this is the ppc .opd |
| 950 | * section, not the ia64 .opd section. |
| 951 | * ia64 .opd should not point to discarded sections. |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 952 | * [.rodata] like for .init.text we ignore .rodata references -same reason |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 953 | **/ |
| 954 | static int exit_section_ref_ok(const char *name) |
| 955 | { |
| 956 | const char **s; |
| 957 | /* Absolute section names */ |
| 958 | const char *namelist1[] = { |
| 959 | ".exit.text", |
| 960 | ".exit.data", |
| 961 | ".init.text", |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 962 | ".rodata", |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 963 | ".opd", /* See comment [OPD] */ |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 964 | ".toc1", /* used by ppc64 */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 965 | ".altinstructions", |
| 966 | ".pdr", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 967 | "__bug_table", /* used by powerpc for BUG() */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 968 | ".exitcall.exit", |
| 969 | ".eh_frame", |
| 970 | ".stab", |
Al Viro | 468d949 | 2006-06-23 23:22:43 +0100 | [diff] [blame] | 971 | "__ex_table", |
| 972 | ".fixup", |
Randy Dunlap | 35899c5 | 2006-06-07 16:23:26 -0700 | [diff] [blame] | 973 | ".smp_locks", |
Sam Ravnborg | 909252d | 2006-06-08 20:37:30 +0200 | [diff] [blame] | 974 | ".plt", /* seen on ARCH=um build on x86_64. Harmless */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 975 | NULL |
| 976 | }; |
| 977 | /* Start of section names */ |
| 978 | const char *namelist2[] = { |
| 979 | ".debug", |
| 980 | NULL |
| 981 | }; |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 982 | /* part of section name */ |
| 983 | const char *namelist3 [] = { |
| 984 | ".unwind", /* Sample: IA_64.unwind.exit.text */ |
| 985 | NULL |
| 986 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 987 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 988 | for (s = namelist1; *s; s++) |
| 989 | if (strcmp(*s, name) == 0) |
| 990 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 991 | for (s = namelist2; *s; s++) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 992 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 993 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 994 | for (s = namelist3; *s; s++) |
Sam Ravnborg | e835a39 | 2006-03-05 11:34:15 +0100 | [diff] [blame] | 995 | if (strstr(name, *s) != NULL) |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 996 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 997 | return 0; |
| 998 | } |
| 999 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1000 | static void read_symbols(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | { |
| 1002 | const char *symname; |
| 1003 | char *version; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1004 | char *license; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | struct module *mod; |
| 1006 | struct elf_info info = { }; |
| 1007 | Elf_Sym *sym; |
| 1008 | |
| 1009 | parse_elf(&info, modname); |
| 1010 | |
| 1011 | mod = new_module(modname); |
| 1012 | |
| 1013 | /* When there's no vmlinux, don't print warnings about |
| 1014 | * unresolved symbols (since there'll be too many ;) */ |
| 1015 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | mod->skip = 1; |
| 1018 | } |
| 1019 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1020 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
| 1021 | while (license) { |
| 1022 | if (license_is_gpl_compatible(license)) |
| 1023 | mod->gpl_compatible = 1; |
| 1024 | else { |
| 1025 | mod->gpl_compatible = 0; |
| 1026 | break; |
| 1027 | } |
| 1028 | license = get_next_modinfo(info.modinfo, info.modinfo_len, |
| 1029 | "license", license); |
| 1030 | } |
| 1031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 1033 | symname = info.strtab + sym->st_name; |
| 1034 | |
| 1035 | handle_modversions(mod, &info, sym, symname); |
| 1036 | handle_moddevtable(mod, &info, sym, symname); |
| 1037 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1038 | check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok); |
| 1039 | check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 1042 | if (version) |
| 1043 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 1044 | version - (char *)info.hdr); |
| 1045 | if (version || (all_versions && !is_vmlinux(modname))) |
| 1046 | get_src_version(modname, mod->srcversion, |
| 1047 | sizeof(mod->srcversion)-1); |
| 1048 | |
| 1049 | parse_elf_finish(&info); |
| 1050 | |
| 1051 | /* Our trick to get versioning for struct_module - it's |
| 1052 | * never passed as an argument to an exported function, so |
| 1053 | * the automatic versioning doesn't pick it up, but it's really |
| 1054 | * important anyhow */ |
| 1055 | if (modversions) |
| 1056 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); |
| 1057 | } |
| 1058 | |
| 1059 | #define SZ 500 |
| 1060 | |
| 1061 | /* We first write the generated file into memory using the |
| 1062 | * following helper, then compare to the file on disk and |
| 1063 | * only update the later if anything changed */ |
| 1064 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1065 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 1066 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
| 1068 | char tmp[SZ]; |
| 1069 | int len; |
| 1070 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | va_start(ap, fmt); |
| 1073 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1074 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | va_end(ap); |
| 1076 | } |
| 1077 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1078 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | { |
| 1080 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1081 | buf->size += len + SZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | buf->p = realloc(buf->p, buf->size); |
| 1083 | } |
| 1084 | strncpy(buf->p + buf->pos, s, len); |
| 1085 | buf->pos += len; |
| 1086 | } |
| 1087 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1088 | void check_license(struct module *mod) |
| 1089 | { |
| 1090 | struct symbol *s, *exp; |
| 1091 | |
| 1092 | for (s = mod->unres; s; s = s->next) { |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1093 | const char *basename; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1094 | if (mod->gpl_compatible == 1) { |
| 1095 | /* GPL-compatible modules may use all symbols */ |
| 1096 | continue; |
| 1097 | } |
| 1098 | exp = find_symbol(s->name); |
| 1099 | if (!exp || exp->module == mod) |
| 1100 | continue; |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1101 | basename = strrchr(mod->name, '/'); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1102 | if (basename) |
| 1103 | basename++; |
| 1104 | switch (exp->export) { |
| 1105 | case export_gpl: |
| 1106 | fatal("modpost: GPL-incompatible module %s " |
| 1107 | "uses GPL-only symbol '%s'\n", |
| 1108 | basename ? basename : mod->name, |
| 1109 | exp->name); |
| 1110 | break; |
| 1111 | case export_gpl_future: |
| 1112 | warn("modpost: GPL-incompatible module %s " |
| 1113 | "uses future GPL-only symbol '%s'\n", |
| 1114 | basename ? basename : mod->name, |
| 1115 | exp->name); |
| 1116 | break; |
| 1117 | case export_plain: /* ignore */ break; |
| 1118 | case export_unknown: /* ignore */ break; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1123 | /** |
| 1124 | * Header for the generated file |
| 1125 | **/ |
| 1126 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
| 1128 | buf_printf(b, "#include <linux/module.h>\n"); |
| 1129 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 1130 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 1131 | buf_printf(b, "\n"); |
| 1132 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 1133 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | buf_printf(b, "struct module __this_module\n"); |
| 1135 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Ustyugov Roman | f83b5e3 | 2005-09-23 08:42:11 +0400 | [diff] [blame] | 1136 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | if (mod->has_init) |
| 1138 | buf_printf(b, " .init = init_module,\n"); |
| 1139 | if (mod->has_cleanup) |
| 1140 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
| 1141 | " .exit = cleanup_module,\n" |
| 1142 | "#endif\n"); |
| 1143 | buf_printf(b, "};\n"); |
| 1144 | } |
| 1145 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1146 | /** |
| 1147 | * Record CRCs for unresolved symbols |
| 1148 | **/ |
| 1149 | static void add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
| 1151 | struct symbol *s, *exp; |
| 1152 | |
| 1153 | for (s = mod->unres; s; s = s->next) { |
| 1154 | exp = find_symbol(s->name); |
| 1155 | if (!exp || exp->module == mod) { |
| 1156 | if (have_vmlinux && !s->weak) |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 1157 | warn("\"%s\" [%s.ko] undefined!\n", |
| 1158 | s->name, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | continue; |
| 1160 | } |
| 1161 | s->module = exp->module; |
| 1162 | s->crc_valid = exp->crc_valid; |
| 1163 | s->crc = exp->crc; |
| 1164 | } |
| 1165 | |
| 1166 | if (!modversions) |
| 1167 | return; |
| 1168 | |
| 1169 | buf_printf(b, "\n"); |
| 1170 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
| 1171 | buf_printf(b, "__attribute_used__\n"); |
| 1172 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 1173 | |
| 1174 | for (s = mod->unres; s; s = s->next) { |
| 1175 | if (!s->module) { |
| 1176 | continue; |
| 1177 | } |
| 1178 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 1179 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | s->name, mod->name); |
| 1181 | continue; |
| 1182 | } |
| 1183 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); |
| 1184 | } |
| 1185 | |
| 1186 | buf_printf(b, "};\n"); |
| 1187 | } |
| 1188 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1189 | static void add_depends(struct buffer *b, struct module *mod, |
| 1190 | struct module *modules) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | { |
| 1192 | struct symbol *s; |
| 1193 | struct module *m; |
| 1194 | int first = 1; |
| 1195 | |
| 1196 | for (m = modules; m; m = m->next) { |
| 1197 | m->seen = is_vmlinux(m->name); |
| 1198 | } |
| 1199 | |
| 1200 | buf_printf(b, "\n"); |
| 1201 | buf_printf(b, "static const char __module_depends[]\n"); |
| 1202 | buf_printf(b, "__attribute_used__\n"); |
| 1203 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 1204 | buf_printf(b, "\"depends="); |
| 1205 | for (s = mod->unres; s; s = s->next) { |
| 1206 | if (!s->module) |
| 1207 | continue; |
| 1208 | |
| 1209 | if (s->module->seen) |
| 1210 | continue; |
| 1211 | |
| 1212 | s->module->seen = 1; |
| 1213 | buf_printf(b, "%s%s", first ? "" : ",", |
| 1214 | strrchr(s->module->name, '/') + 1); |
| 1215 | first = 0; |
| 1216 | } |
| 1217 | buf_printf(b, "\";\n"); |
| 1218 | } |
| 1219 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1220 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | { |
| 1222 | if (mod->srcversion[0]) { |
| 1223 | buf_printf(b, "\n"); |
| 1224 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 1225 | mod->srcversion); |
| 1226 | } |
| 1227 | } |
| 1228 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1229 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | { |
| 1231 | char *tmp; |
| 1232 | FILE *file; |
| 1233 | struct stat st; |
| 1234 | |
| 1235 | file = fopen(fname, "r"); |
| 1236 | if (!file) |
| 1237 | goto write; |
| 1238 | |
| 1239 | if (fstat(fileno(file), &st) < 0) |
| 1240 | goto close_write; |
| 1241 | |
| 1242 | if (st.st_size != b->pos) |
| 1243 | goto close_write; |
| 1244 | |
| 1245 | tmp = NOFAIL(malloc(b->pos)); |
| 1246 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 1247 | goto free_write; |
| 1248 | |
| 1249 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 1250 | goto free_write; |
| 1251 | |
| 1252 | free(tmp); |
| 1253 | fclose(file); |
| 1254 | return; |
| 1255 | |
| 1256 | free_write: |
| 1257 | free(tmp); |
| 1258 | close_write: |
| 1259 | fclose(file); |
| 1260 | write: |
| 1261 | file = fopen(fname, "w"); |
| 1262 | if (!file) { |
| 1263 | perror(fname); |
| 1264 | exit(1); |
| 1265 | } |
| 1266 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 1267 | perror(fname); |
| 1268 | exit(1); |
| 1269 | } |
| 1270 | fclose(file); |
| 1271 | } |
| 1272 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1273 | /* parse Module.symvers file. line format: |
| 1274 | * 0x12345678<tab>symbol<tab>module[<tab>export] |
| 1275 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1276 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
| 1278 | unsigned long size, pos = 0; |
| 1279 | void *file = grab_file(fname, &size); |
| 1280 | char *line; |
| 1281 | |
| 1282 | if (!file) |
| 1283 | /* No symbol versions, silently ignore */ |
| 1284 | return; |
| 1285 | |
| 1286 | while ((line = get_next_line(&pos, file, size))) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1287 | char *symname, *modname, *d, *export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | unsigned int crc; |
| 1289 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1290 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | if (!(symname = strchr(line, '\t'))) |
| 1293 | goto fail; |
| 1294 | *symname++ = '\0'; |
| 1295 | if (!(modname = strchr(symname, '\t'))) |
| 1296 | goto fail; |
| 1297 | *modname++ = '\0'; |
Laurent Riffard | 9ac545b | 2006-06-11 08:02:06 +0200 | [diff] [blame] | 1298 | if ((export = strchr(modname, '\t')) != NULL) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1299 | *export++ = '\0'; |
| 1300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | crc = strtoul(line, &d, 16); |
| 1302 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 1303 | goto fail; |
| 1304 | |
| 1305 | if (!(mod = find_module(modname))) { |
| 1306 | if (is_vmlinux(modname)) { |
| 1307 | have_vmlinux = 1; |
| 1308 | } |
| 1309 | mod = new_module(NOFAIL(strdup(modname))); |
| 1310 | mod->skip = 1; |
| 1311 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1312 | s = sym_add_exported(symname, mod, export_no(export)); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 1313 | s->kernel = kernel; |
| 1314 | s->preloaded = 1; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1315 | sym_update_crc(symname, mod, crc, export_no(export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | return; |
| 1318 | fail: |
| 1319 | fatal("parse error in symbol dump file\n"); |
| 1320 | } |
| 1321 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1322 | /* For normal builds always dump all symbols. |
| 1323 | * For external modules only dump symbols |
| 1324 | * that are not read from kernel Module.symvers. |
| 1325 | **/ |
| 1326 | static int dump_sym(struct symbol *sym) |
| 1327 | { |
| 1328 | if (!external_module) |
| 1329 | return 1; |
| 1330 | if (sym->vmlinux || sym->kernel) |
| 1331 | return 0; |
| 1332 | return 1; |
| 1333 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1334 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1335 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
| 1337 | struct buffer buf = { }; |
| 1338 | struct symbol *symbol; |
| 1339 | int n; |
| 1340 | |
| 1341 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 1342 | symbol = symbolhash[n]; |
| 1343 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1344 | if (dump_sym(symbol)) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1345 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1346 | symbol->crc, symbol->name, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1347 | symbol->module->name, |
| 1348 | export_str(symbol->export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | symbol = symbol->next; |
| 1350 | } |
| 1351 | } |
| 1352 | write_if_changed(&buf, fname); |
| 1353 | } |
| 1354 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1355 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | { |
| 1357 | struct module *mod; |
| 1358 | struct buffer buf = { }; |
| 1359 | char fname[SZ]; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1360 | char *kernel_read = NULL, *module_read = NULL; |
| 1361 | char *dump_write = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | int opt; |
| 1363 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1364 | while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | switch(opt) { |
| 1366 | case 'i': |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1367 | kernel_read = optarg; |
| 1368 | break; |
| 1369 | case 'I': |
| 1370 | module_read = optarg; |
| 1371 | external_module = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | break; |
| 1373 | case 'm': |
| 1374 | modversions = 1; |
| 1375 | break; |
| 1376 | case 'o': |
| 1377 | dump_write = optarg; |
| 1378 | break; |
| 1379 | case 'a': |
| 1380 | all_versions = 1; |
| 1381 | break; |
| 1382 | default: |
| 1383 | exit(1); |
| 1384 | } |
| 1385 | } |
| 1386 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1387 | if (kernel_read) |
| 1388 | read_dump(kernel_read, 1); |
| 1389 | if (module_read) |
| 1390 | read_dump(module_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | while (optind < argc) { |
| 1393 | read_symbols(argv[optind++]); |
| 1394 | } |
| 1395 | |
| 1396 | for (mod = modules; mod; mod = mod->next) { |
| 1397 | if (mod->skip) |
| 1398 | continue; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1399 | check_license(mod); |
| 1400 | } |
| 1401 | |
| 1402 | for (mod = modules; mod; mod = mod->next) { |
| 1403 | if (mod->skip) |
| 1404 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | buf.pos = 0; |
| 1407 | |
| 1408 | add_header(&buf, mod); |
| 1409 | add_versions(&buf, mod); |
| 1410 | add_depends(&buf, mod, modules); |
| 1411 | add_moddevtable(&buf, mod); |
| 1412 | add_srcversion(&buf, mod); |
| 1413 | |
| 1414 | sprintf(fname, "%s.mod.c", mod->name); |
| 1415 | write_if_changed(&buf, fname); |
| 1416 | } |
| 1417 | |
| 1418 | if (dump_write) |
| 1419 | write_dump(dump_write); |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |