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 | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 5 | * Copyright 2006-2008 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; |
Sam Ravnborg | 8d8d828 | 2007-07-20 22:36:56 +0200 | [diff] [blame] | 26 | /* Warn about section mismatch in vmlinux if set to 1 */ |
| 27 | static int vmlinux_section_warnings = 1; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 28 | /* Only warn about unresolved symbols */ |
| 29 | static int warn_unresolved = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 30 | /* How a symbol is exported */ |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 31 | static int sec_mismatch_count = 0; |
| 32 | static int sec_mismatch_verbose = 1; |
| 33 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 34 | enum export { |
| 35 | export_plain, export_unused, export_gpl, |
| 36 | export_unused_gpl, export_gpl_future, export_unknown |
| 37 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 39 | #define PRINTF __attribute__ ((format (printf, 1, 2))) |
| 40 | |
| 41 | PRINTF void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | va_list arglist; |
| 44 | |
| 45 | fprintf(stderr, "FATAL: "); |
| 46 | |
| 47 | va_start(arglist, fmt); |
| 48 | vfprintf(stderr, fmt, arglist); |
| 49 | va_end(arglist); |
| 50 | |
| 51 | exit(1); |
| 52 | } |
| 53 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 54 | PRINTF void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | va_list arglist; |
| 57 | |
| 58 | fprintf(stderr, "WARNING: "); |
| 59 | |
| 60 | va_start(arglist, fmt); |
| 61 | vfprintf(stderr, fmt, arglist); |
| 62 | va_end(arglist); |
| 63 | } |
| 64 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 65 | PRINTF void merror(const char *fmt, ...) |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 66 | { |
| 67 | va_list arglist; |
| 68 | |
| 69 | fprintf(stderr, "ERROR: "); |
| 70 | |
| 71 | va_start(arglist, fmt); |
| 72 | vfprintf(stderr, fmt, arglist); |
| 73 | va_end(arglist); |
| 74 | } |
| 75 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 76 | static int is_vmlinux(const char *modname) |
| 77 | { |
| 78 | const char *myname; |
| 79 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 80 | myname = strrchr(modname, '/'); |
| 81 | if (myname) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 82 | myname++; |
| 83 | else |
| 84 | myname = modname; |
| 85 | |
Sam Ravnborg | 741f98f | 2007-07-17 10:54:06 +0200 | [diff] [blame] | 86 | return (strcmp(myname, "vmlinux") == 0) || |
| 87 | (strcmp(myname, "vmlinux.o") == 0); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | void *do_nofail(void *ptr, const char *expr) |
| 91 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 92 | if (!ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return ptr; |
| 96 | } |
| 97 | |
| 98 | /* A list of all modules we processed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static struct module *modules; |
| 100 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 101 | static struct module *find_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
| 103 | struct module *mod; |
| 104 | |
| 105 | for (mod = modules; mod; mod = mod->next) |
| 106 | if (strcmp(mod->name, modname) == 0) |
| 107 | break; |
| 108 | return mod; |
| 109 | } |
| 110 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 111 | static struct module *new_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | struct module *mod; |
| 114 | char *p, *s; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 117 | memset(mod, 0, sizeof(*mod)); |
| 118 | p = NOFAIL(strdup(modname)); |
| 119 | |
| 120 | /* strip trailing .o */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 121 | s = strrchr(p, '.'); |
| 122 | if (s != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | if (strcmp(s, ".o") == 0) |
| 124 | *s = '\0'; |
| 125 | |
| 126 | /* add to list */ |
| 127 | mod->name = p; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 128 | mod->gpl_compatible = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | mod->next = modules; |
| 130 | modules = mod; |
| 131 | |
| 132 | return mod; |
| 133 | } |
| 134 | |
| 135 | /* A hash of all exported symbols, |
| 136 | * struct symbol is also used for lists of unresolved symbols */ |
| 137 | |
| 138 | #define SYMBOL_HASH_SIZE 1024 |
| 139 | |
| 140 | struct symbol { |
| 141 | struct symbol *next; |
| 142 | struct module *module; |
| 143 | unsigned int crc; |
| 144 | int crc_valid; |
| 145 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 146 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 147 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 148 | * (only for external modules) **/ |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 149 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 150 | enum export export; /* Type of export */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | char name[0]; |
| 152 | }; |
| 153 | |
| 154 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 155 | |
| 156 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 157 | static inline unsigned int tdb_hash(const char *name) |
| 158 | { |
| 159 | unsigned value; /* Used to compute the hash value. */ |
| 160 | unsigned i; /* Used to cycle through random values. */ |
| 161 | |
| 162 | /* Set the initial value from the key size. */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 163 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 165 | |
| 166 | return (1103515243 * value + 12345); |
| 167 | } |
| 168 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 169 | /** |
| 170 | * Allocate a new symbols for use in the hash of exported symbols or |
| 171 | * the list of unresolved symbols per module |
| 172 | **/ |
| 173 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 174 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 177 | |
| 178 | memset(s, 0, sizeof(*s)); |
| 179 | strcpy(s->name, name); |
| 180 | s->weak = weak; |
| 181 | s->next = next; |
| 182 | return s; |
| 183 | } |
| 184 | |
| 185 | /* For the hash of exported symbols */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 186 | static struct symbol *new_symbol(const char *name, struct module *module, |
| 187 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | unsigned int hash; |
| 190 | struct symbol *new; |
| 191 | |
| 192 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 193 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 194 | new->module = module; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 195 | new->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 196 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 199 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
| 201 | struct symbol *s; |
| 202 | |
| 203 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 204 | if (name[0] == '.') |
| 205 | name++; |
| 206 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 207 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (strcmp(s->name, name) == 0) |
| 209 | return s; |
| 210 | } |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 214 | static struct { |
| 215 | const char *str; |
| 216 | enum export export; |
| 217 | } export_list[] = { |
| 218 | { .str = "EXPORT_SYMBOL", .export = export_plain }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 219 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 220 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 221 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 222 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
| 223 | { .str = "(unknown)", .export = export_unknown }, |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | static const char *export_str(enum export ex) |
| 228 | { |
| 229 | return export_list[ex].str; |
| 230 | } |
| 231 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 232 | static enum export export_no(const char *s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 233 | { |
| 234 | int i; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 235 | |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 236 | if (!s) |
| 237 | return export_unknown; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 238 | for (i = 0; export_list[i].export != export_unknown; i++) { |
| 239 | if (strcmp(export_list[i].str, s) == 0) |
| 240 | return export_list[i].export; |
| 241 | } |
| 242 | return export_unknown; |
| 243 | } |
| 244 | |
| 245 | static enum export export_from_sec(struct elf_info *elf, Elf_Section sec) |
| 246 | { |
| 247 | if (sec == elf->export_sec) |
| 248 | return export_plain; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 249 | else if (sec == elf->export_unused_sec) |
| 250 | return export_unused; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 251 | else if (sec == elf->export_gpl_sec) |
| 252 | return export_gpl; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 253 | else if (sec == elf->export_unused_gpl_sec) |
| 254 | return export_unused_gpl; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 255 | else if (sec == elf->export_gpl_future_sec) |
| 256 | return export_gpl_future; |
| 257 | else |
| 258 | return export_unknown; |
| 259 | } |
| 260 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 261 | /** |
| 262 | * Add an exported symbol - it may have already been added without a |
| 263 | * CRC, in this case just update the CRC |
| 264 | **/ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 265 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
| 266 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | struct symbol *s = find_symbol(name); |
| 269 | |
| 270 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 271 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 272 | } else { |
| 273 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 274 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 275 | "was in %s%s\n", mod->name, name, |
| 276 | s->module->name, |
| 277 | is_vmlinux(s->module->name) ?"":".ko"); |
Trent Piepho | 4b21960 | 2007-10-11 16:40:10 -0700 | [diff] [blame] | 278 | } else { |
| 279 | /* In case Modules.symvers was out of date */ |
| 280 | s->module = mod; |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 283 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 284 | s->vmlinux = is_vmlinux(mod->name); |
| 285 | s->kernel = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 286 | s->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 287 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 290 | static void sym_update_crc(const char *name, struct module *mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 291 | unsigned int crc, enum export export) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 292 | { |
| 293 | struct symbol *s = find_symbol(name); |
| 294 | |
| 295 | if (!s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 296 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 297 | s->crc = crc; |
| 298 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 301 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | struct stat st; |
| 304 | void *map; |
| 305 | int fd; |
| 306 | |
| 307 | fd = open(filename, O_RDONLY); |
| 308 | if (fd < 0 || fstat(fd, &st) != 0) |
| 309 | return NULL; |
| 310 | |
| 311 | *size = st.st_size; |
| 312 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 313 | close(fd); |
| 314 | |
| 315 | if (map == MAP_FAILED) |
| 316 | return NULL; |
| 317 | return map; |
| 318 | } |
| 319 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 320 | /** |
| 321 | * Return a copy of the next line in a mmap'ed file. |
| 322 | * spaces in the beginning of the line is trimmed away. |
| 323 | * Return a pointer to a static buffer. |
| 324 | **/ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 325 | char *get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | static char line[4096]; |
| 328 | int skip = 1; |
| 329 | size_t len = 0; |
| 330 | signed char *p = (signed char *)file + *pos; |
| 331 | char *s = line; |
| 332 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 333 | for (; *pos < size ; (*pos)++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (skip && isspace(*p)) { |
| 335 | p++; |
| 336 | continue; |
| 337 | } |
| 338 | skip = 0; |
| 339 | if (*p != '\n' && (*pos < size)) { |
| 340 | len++; |
| 341 | *s++ = *p++; |
| 342 | if (len > 4095) |
| 343 | break; /* Too long, stop */ |
| 344 | } else { |
| 345 | /* End of string */ |
| 346 | *s = '\0'; |
| 347 | return line; |
| 348 | } |
| 349 | } |
| 350 | /* End of buffer */ |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 354 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | munmap(file, size); |
| 357 | } |
| 358 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 359 | static int parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | unsigned int i; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 362 | Elf_Ehdr *hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | Elf_Shdr *sechdrs; |
| 364 | Elf_Sym *sym; |
| 365 | |
| 366 | hdr = grab_file(filename, &info->size); |
| 367 | if (!hdr) { |
| 368 | perror(filename); |
Sam Ravnborg | 6803dc0 | 2006-06-24 23:46:54 +0200 | [diff] [blame] | 369 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | info->hdr = hdr; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 372 | if (info->size < sizeof(*hdr)) { |
| 373 | /* file too small, assume this is an empty .o file */ |
| 374 | return 0; |
| 375 | } |
| 376 | /* Is this a valid ELF file? */ |
| 377 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || |
| 378 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || |
| 379 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || |
| 380 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { |
| 381 | /* Not an ELF file - silently ignore it */ |
| 382 | return 0; |
| 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* Fix endianness in ELF header */ |
| 385 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 386 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
| 387 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 388 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 389 | hdr->e_type = TO_NATIVE(hdr->e_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 391 | info->sechdrs = sechdrs; |
| 392 | |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 393 | /* Check if file offset is correct */ |
| 394 | if (hdr->e_shoff > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 395 | fatal("section header offset=%lu in file '%s' is bigger than " |
| 396 | "filesize=%lu\n", (unsigned long)hdr->e_shoff, |
| 397 | filename, info->size); |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* Fix endianness in section headers */ |
| 402 | for (i = 0; i < hdr->e_shnum; i++) { |
| 403 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 404 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 405 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 406 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 407 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 408 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); |
| 409 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | /* Find symbol table. */ |
| 412 | for (i = 1; i < hdr->e_shnum; i++) { |
| 413 | const char *secstrings |
| 414 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 415 | const char *secname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 417 | if (sechdrs[i].sh_offset > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 418 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
| 419 | "sizeof(*hrd)=%zu\n", filename, |
| 420 | (unsigned long)sechdrs[i].sh_offset, |
| 421 | sizeof(*hdr)); |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 422 | return 0; |
| 423 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 424 | secname = secstrings + sechdrs[i].sh_name; |
| 425 | if (strcmp(secname, ".modinfo") == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 427 | info->modinfo_len = sechdrs[i].sh_size; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 428 | } else if (strcmp(secname, "__ksymtab") == 0) |
| 429 | info->export_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 430 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
| 431 | info->export_unused_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 432 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
| 433 | info->export_gpl_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 434 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
| 435 | info->export_unused_gpl_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 436 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
| 437 | info->export_gpl_future_sec = i; |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
| 440 | continue; |
| 441 | |
| 442 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 443 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | + sechdrs[i].sh_size; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 445 | info->strtab = (void *)hdr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | sechdrs[sechdrs[i].sh_link].sh_offset; |
| 447 | } |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 448 | if (!info->symtab_start) |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 449 | fatal("%s has no symtab?\n", filename); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* Fix endianness in symbols */ |
| 452 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 453 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 454 | sym->st_name = TO_NATIVE(sym->st_name); |
| 455 | sym->st_value = TO_NATIVE(sym->st_value); |
| 456 | sym->st_size = TO_NATIVE(sym->st_size); |
| 457 | } |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 458 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 461 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | release_file(info->hdr, info->size); |
| 464 | } |
| 465 | |
Luke Yang | f7b05e6 | 2006-03-02 18:35:31 +0800 | [diff] [blame] | 466 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
| 467 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 469 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 470 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | unsigned int crc; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 473 | enum export export = export_from_sec(info, sym->st_shndx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | switch (sym->st_shndx) { |
| 476 | case SHN_COMMON: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 477 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | break; |
| 479 | case SHN_ABS: |
| 480 | /* CRC'd symbol */ |
| 481 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
| 482 | crc = (unsigned int) sym->st_value; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 483 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
| 484 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | break; |
| 487 | case SHN_UNDEF: |
| 488 | /* undefined symbol */ |
| 489 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 490 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 491 | break; |
| 492 | /* ignore global offset table */ |
| 493 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 494 | break; |
| 495 | /* ignore __this_module, it will be resolved shortly */ |
| 496 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) |
| 497 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 498 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 499 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 500 | /* add compatibility with older glibc */ |
| 501 | #ifndef STT_SPARC_REGISTER |
| 502 | #define STT_SPARC_REGISTER STT_REGISTER |
| 503 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (info->hdr->e_machine == EM_SPARC || |
| 505 | info->hdr->e_machine == EM_SPARCV9) { |
| 506 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 507 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 509 | if (symname[0] == '.') { |
| 510 | char *munged = strdup(symname); |
| 511 | munged[0] = '_'; |
| 512 | munged[1] = toupper(munged[1]); |
| 513 | symname = munged; |
| 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 519 | strlen(MODULE_SYMBOL_PREFIX)) == 0) { |
| 520 | mod->unres = |
| 521 | alloc_symbol(symname + |
| 522 | strlen(MODULE_SYMBOL_PREFIX), |
| 523 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 524 | mod->unres); |
| 525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | break; |
| 527 | default: |
| 528 | /* All exported symbols */ |
| 529 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 530 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
| 531 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) |
| 534 | mod->has_init = 1; |
| 535 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) |
| 536 | mod->has_cleanup = 1; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 541 | /** |
| 542 | * Parse tag=value strings from .modinfo section |
| 543 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | static char *next_string(char *string, unsigned long *secsize) |
| 545 | { |
| 546 | /* Skip non-zero chars */ |
| 547 | while (string[0]) { |
| 548 | string++; |
| 549 | if ((*secsize)-- <= 1) |
| 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | /* Skip any zero padding. */ |
| 554 | while (!string[0]) { |
| 555 | string++; |
| 556 | if ((*secsize)-- <= 1) |
| 557 | return NULL; |
| 558 | } |
| 559 | return string; |
| 560 | } |
| 561 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 562 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
| 563 | const char *tag, char *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | char *p; |
| 566 | unsigned int taglen = strlen(tag); |
| 567 | unsigned long size = modinfo_len; |
| 568 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 569 | if (info) { |
| 570 | size -= info - (char *)modinfo; |
| 571 | modinfo = next_string(info, &size); |
| 572 | } |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 575 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 576 | return p + taglen + 1; |
| 577 | } |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 581 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 582 | const char *tag) |
| 583 | |
| 584 | { |
| 585 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); |
| 586 | } |
| 587 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 588 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 589 | * Test if string s ends in string sub |
| 590 | * return 0 if match |
| 591 | **/ |
| 592 | static int strrcmp(const char *s, const char *sub) |
| 593 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 594 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 595 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 596 | if (!s || !sub) |
| 597 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 598 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 599 | slen = strlen(s); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 600 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 601 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 602 | if ((slen == 0) || (sublen == 0)) |
| 603 | return 1; |
| 604 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 605 | if (sublen > slen) |
| 606 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 607 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 608 | return memcmp(s + slen - sublen, sub, sublen); |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 611 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
| 612 | { |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 613 | if (sym) |
| 614 | return elf->strtab + sym->st_name; |
| 615 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame^] | 616 | return "(unknown)"; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static const char *sec_name(struct elf_info *elf, int shndx) |
| 620 | { |
| 621 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 622 | return (void *)elf->hdr + |
| 623 | elf->sechdrs[elf->hdr->e_shstrndx].sh_offset + |
| 624 | sechdrs[shndx].sh_name; |
| 625 | } |
| 626 | |
| 627 | static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) |
| 628 | { |
| 629 | return (void *)elf->hdr + |
| 630 | elf->sechdrs[elf->hdr->e_shstrndx].sh_offset + |
| 631 | sechdr->sh_name; |
| 632 | } |
| 633 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 634 | /* if sym is empty or point to a string |
| 635 | * like ".[0-9]+" then return 1. |
| 636 | * This is the optional prefix added by ld to some sections |
| 637 | */ |
| 638 | static int number_prefix(const char *sym) |
| 639 | { |
| 640 | if (*sym++ == '\0') |
| 641 | return 1; |
| 642 | if (*sym != '.') |
| 643 | return 0; |
| 644 | do { |
| 645 | char c = *sym++; |
| 646 | if (c < '0' || c > '9') |
| 647 | return 0; |
| 648 | } while (*sym); |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | /* The pattern is an array of simple patterns. |
| 653 | * "foo" will match an exact string equal to "foo" |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 654 | * "*foo" will match a string that ends with "foo" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 655 | * "foo*" will match a string that begins with "foo" |
| 656 | * "foo$" will match a string equal to "foo" or "foo.1" |
| 657 | * where the '1' can be any number including several digits. |
| 658 | * The $ syntax is for sections where ld append a dot number |
| 659 | * to make section name unique. |
| 660 | */ |
| 661 | int match(const char *sym, const char * const pat[]) |
| 662 | { |
| 663 | const char *p; |
| 664 | while (*pat) { |
| 665 | p = *pat++; |
| 666 | const char *endp = p + strlen(p) - 1; |
| 667 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 668 | /* "*foo" */ |
| 669 | if (*p == '*') { |
| 670 | if (strrcmp(sym, p + 1) == 0) |
| 671 | return 1; |
| 672 | } |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 673 | /* "foo*" */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 674 | else if (*endp == '*') { |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 675 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
| 676 | return 1; |
| 677 | } |
| 678 | /* "foo$" */ |
| 679 | else if (*endp == '$') { |
| 680 | if (strncmp(sym, p, strlen(p) - 1) == 0) { |
| 681 | if (number_prefix(sym + strlen(p) - 1)) |
| 682 | return 1; |
| 683 | } |
| 684 | } |
| 685 | /* no wildcards */ |
| 686 | else { |
| 687 | if (strcmp(p, sym) == 0) |
| 688 | return 1; |
| 689 | } |
| 690 | } |
| 691 | /* no match */ |
| 692 | return 0; |
| 693 | } |
| 694 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 695 | /* sections that we do not want to do full section mismatch check on */ |
| 696 | static const char *section_white_list[] = |
| 697 | { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL }; |
| 698 | |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 699 | /* |
| 700 | * Is this section one we do not want to check? |
| 701 | * This is often debug sections. |
| 702 | * If we are going to check this section then |
| 703 | * test if section name ends with a dot and a number. |
| 704 | * This is used to find sections where the linker have |
| 705 | * appended a dot-number to make the name unique. |
| 706 | * The cause of this is often a section specified in assembler |
| 707 | * without "ax" / "aw" and the same section used in .c |
| 708 | * code where gcc add these. |
| 709 | */ |
| 710 | static int check_section(const char *modname, const char *sec) |
| 711 | { |
| 712 | const char *e = sec + strlen(sec) - 1; |
| 713 | if (match(sec, section_white_list)) |
| 714 | return 1; |
| 715 | |
| 716 | if (*e && isdigit(*e)) { |
| 717 | /* consume all digits */ |
| 718 | while (*e && e != sec && isdigit(*e)) |
| 719 | e--; |
| 720 | if (*e == '.') { |
| 721 | warn("%s (%s): unexpected section name.\n" |
| 722 | "The (.[number]+) following section name are " |
| 723 | "ld generated and not expected.\n" |
| 724 | "Did you forget to use \"ax\"/\"aw\" " |
| 725 | "in a .S file?\n" |
| 726 | "Note that for example <linux/init.h> contains\n" |
| 727 | "section definitions for use in .S files.\n\n", |
| 728 | modname, sec); |
| 729 | } |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 736 | #define ALL_INIT_DATA_SECTIONS \ |
| 737 | ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$" |
| 738 | #define ALL_EXIT_DATA_SECTIONS \ |
| 739 | ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 740 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 741 | #define ALL_INIT_TEXT_SECTIONS \ |
| 742 | ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$" |
| 743 | #define ALL_EXIT_TEXT_SECTIONS \ |
| 744 | ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 745 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 746 | #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS |
| 747 | #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 748 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 749 | #define DATA_SECTIONS ".data$", ".data.rel$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 750 | #define TEXT_SECTIONS ".text$" |
| 751 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 752 | #define INIT_SECTIONS ".init.data$", ".init.text$" |
| 753 | #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$" |
| 754 | #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$" |
| 755 | #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$" |
| 756 | |
| 757 | #define EXIT_SECTIONS ".exit.data$", ".exit.text$" |
| 758 | #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$" |
| 759 | #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$" |
| 760 | #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$" |
| 761 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 762 | /* init data sections */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 763 | static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 764 | |
| 765 | /* all init sections */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 766 | static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 767 | |
| 768 | /* All init and exit sections (code + data) */ |
| 769 | static const char *init_exit_sections[] = |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 770 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 771 | |
| 772 | /* data section */ |
| 773 | static const char *data_sections[] = { DATA_SECTIONS, NULL }; |
| 774 | |
| 775 | /* sections that may refer to an init/exit section with no warning */ |
| 776 | static const char *initref_sections[] = |
| 777 | { |
| 778 | ".text.init.refok*", |
| 779 | ".exit.text.refok*", |
| 780 | ".data.init.refok*", |
| 781 | NULL |
| 782 | }; |
| 783 | |
| 784 | |
| 785 | /* symbols in .data that may refer to init/exit sections */ |
| 786 | static const char *symbol_white_list[] = |
| 787 | { |
| 788 | "*driver", |
| 789 | "*_template", /* scsi uses *_template a lot */ |
| 790 | "*_timer", /* arm uses ops structures named _timer a lot */ |
| 791 | "*_sht", /* scsi also used *_sht to some extent */ |
| 792 | "*_ops", |
| 793 | "*_probe", |
| 794 | "*_probe_one", |
| 795 | "*_console", |
| 796 | NULL |
| 797 | }; |
| 798 | |
| 799 | static const char *head_sections[] = { ".head.text*", NULL }; |
| 800 | static const char *linker_symbols[] = |
| 801 | { "__init_begin", "_sinittext", "_einittext", NULL }; |
| 802 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 803 | enum mismatch { |
| 804 | NO_MISMATCH, |
| 805 | TEXT_TO_INIT, |
| 806 | DATA_TO_INIT, |
| 807 | TEXT_TO_EXIT, |
| 808 | DATA_TO_EXIT, |
| 809 | XXXINIT_TO_INIT, |
| 810 | XXXEXIT_TO_EXIT, |
| 811 | INIT_TO_EXIT, |
| 812 | EXIT_TO_INIT, |
| 813 | EXPORT_TO_INIT_EXIT, |
| 814 | }; |
| 815 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 816 | struct sectioncheck { |
| 817 | const char *fromsec[20]; |
| 818 | const char *tosec[20]; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 819 | enum mismatch mismatch; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 820 | }; |
| 821 | |
| 822 | const struct sectioncheck sectioncheck[] = { |
| 823 | /* Do not reference init/exit code/data from |
| 824 | * normal code and data |
| 825 | */ |
| 826 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 827 | .fromsec = { TEXT_SECTIONS, NULL }, |
| 828 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
| 829 | .mismatch = TEXT_TO_INIT, |
| 830 | }, |
| 831 | { |
| 832 | .fromsec = { DATA_SECTIONS, NULL }, |
| 833 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
| 834 | .mismatch = DATA_TO_INIT, |
| 835 | }, |
| 836 | { |
| 837 | .fromsec = { TEXT_SECTIONS, NULL }, |
| 838 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
| 839 | .mismatch = TEXT_TO_EXIT, |
| 840 | }, |
| 841 | { |
| 842 | .fromsec = { DATA_SECTIONS, NULL }, |
| 843 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
| 844 | .mismatch = DATA_TO_EXIT, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 845 | }, |
| 846 | /* Do not reference init code/data from devinit/cpuinit/meminit code/data */ |
| 847 | { |
| 848 | .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 849 | .tosec = { INIT_SECTIONS, NULL }, |
| 850 | .mismatch = XXXINIT_TO_INIT, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 851 | }, |
| 852 | /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */ |
| 853 | { |
| 854 | .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 855 | .tosec = { EXIT_SECTIONS, NULL }, |
| 856 | .mismatch = XXXEXIT_TO_EXIT, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 857 | }, |
| 858 | /* Do not use exit code/data from init code */ |
| 859 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 860 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
| 861 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 862 | .mismatch = INIT_TO_EXIT, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 863 | }, |
| 864 | /* Do not use init code/data from exit code */ |
| 865 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 866 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 867 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
| 868 | .mismatch = EXIT_TO_INIT, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 869 | }, |
| 870 | /* Do not export init/exit functions or data */ |
| 871 | { |
| 872 | .fromsec = { "__ksymtab*", NULL }, |
Sam Ravnborg | fa95eb1 | 2008-02-02 23:30:22 +0100 | [diff] [blame] | 873 | .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 874 | .mismatch = EXPORT_TO_INIT_EXIT |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 875 | } |
| 876 | }; |
| 877 | |
| 878 | static int section_mismatch(const char *fromsec, const char *tosec) |
| 879 | { |
| 880 | int i; |
| 881 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); |
| 882 | const struct sectioncheck *check = §ioncheck[0]; |
| 883 | |
| 884 | for (i = 0; i < elems; i++) { |
| 885 | if (match(fromsec, check->fromsec) && |
| 886 | match(tosec, check->tosec)) |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 887 | return check->mismatch; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 888 | check++; |
| 889 | } |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 890 | return NO_MISMATCH; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 893 | /** |
| 894 | * Whitelist to allow certain references to pass with no warning. |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 895 | * |
| 896 | * Pattern 0: |
| 897 | * Do not warn if funtion/data are marked with __init_refok/__initdata_refok. |
| 898 | * The pattern is identified by: |
Sam Ravnborg | cb7e51d | 2007-07-25 22:24:52 +0200 | [diff] [blame] | 899 | * fromsec = .text.init.refok* | .data.init.refok* |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 900 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 901 | * Pattern 1: |
| 902 | * If a module parameter is declared __initdata and permissions=0 |
| 903 | * then this is legal despite the warning generated. |
| 904 | * We cannot see value of permissions here, so just ignore |
| 905 | * this pattern. |
| 906 | * The pattern is identified by: |
| 907 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 908 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 909 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 910 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 911 | * Pattern 2: |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 912 | * Many drivers utilise a *driver container with references to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 913 | * add, remove, probe functions etc. |
| 914 | * These functions may often be marked __init and we do not want to |
| 915 | * warn here. |
| 916 | * the pattern is identified by: |
Sam Ravnborg | 83cda2b | 2007-07-25 21:52:31 +0200 | [diff] [blame] | 917 | * tosec = init or exit section |
| 918 | * fromsec = data section |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 919 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
| 920 | * *probe_one, *_console, *_timer |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 921 | * |
| 922 | * Pattern 3: |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 923 | * Whitelist all refereces from .text.head to .init.data |
| 924 | * Whitelist all refereces from .text.head to .init.text |
| 925 | * |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 926 | * Pattern 4: |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 927 | * Some symbols belong to init section but still it is ok to reference |
| 928 | * these from non-init sections as these symbols don't have any memory |
| 929 | * allocated for them and symbol address and value are same. So even |
| 930 | * if init section is freed, its ok to reference those symbols. |
| 931 | * For ex. symbols marking the init section boundaries. |
| 932 | * This pattern is identified by |
| 933 | * refsymname = __init_begin, _sinittext, _einittext |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 934 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 935 | **/ |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 936 | static int secref_whitelist(const char *fromsec, const char *fromsym, |
| 937 | const char *tosec, const char *tosym) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 938 | { |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 939 | /* Check for pattern 0 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 940 | if (match(fromsec, initref_sections)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 941 | return 0; |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 942 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 943 | /* Check for pattern 1 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 944 | if (match(tosec, init_data_sections) && |
| 945 | match(fromsec, data_sections) && |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 946 | (strncmp(fromsym, "__param", strlen("__param")) == 0)) |
| 947 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 948 | |
| 949 | /* Check for pattern 2 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 950 | if (match(tosec, init_exit_sections) && |
| 951 | match(fromsec, data_sections) && |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 952 | match(fromsym, symbol_white_list)) |
| 953 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 954 | |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 955 | /* Check for pattern 3 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 956 | if (match(fromsec, head_sections) && |
| 957 | match(tosec, init_sections)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 958 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 959 | |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 960 | /* Check for pattern 4 */ |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 961 | if (match(tosym, linker_symbols)) |
| 962 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 963 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 964 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 968 | * Find symbol based on relocation record info. |
| 969 | * In some cases the symbol supplied is a valid symbol so |
| 970 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 971 | * In other cases the symbol needs to be looked up in the symbol table |
| 972 | * based on section and address. |
| 973 | * **/ |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 974 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 975 | Elf_Sym *relsym) |
| 976 | { |
| 977 | Elf_Sym *sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 978 | Elf_Sym *near = NULL; |
| 979 | Elf64_Sword distance = 20; |
| 980 | Elf64_Sword d; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 981 | |
| 982 | if (relsym->st_name != 0) |
| 983 | return relsym; |
| 984 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 985 | if (sym->st_shndx != relsym->st_shndx) |
| 986 | continue; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 987 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
| 988 | continue; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 989 | if (sym->st_value == addr) |
| 990 | return sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 991 | /* Find a symbol nearby - addr are maybe negative */ |
| 992 | d = sym->st_value - addr; |
| 993 | if (d < 0) |
| 994 | d = addr - sym->st_value; |
| 995 | if (d < distance) { |
| 996 | distance = d; |
| 997 | near = sym; |
| 998 | } |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 999 | } |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1000 | /* We need a close match */ |
| 1001 | if (distance < 20) |
| 1002 | return near; |
| 1003 | else |
| 1004 | return NULL; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 1007 | static inline int is_arm_mapping_symbol(const char *str) |
| 1008 | { |
| 1009 | return str[0] == '$' && strchr("atd", str[1]) |
| 1010 | && (str[2] == '\0' || str[2] == '.'); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * If there's no name there, ignore it; likewise, ignore it if it's |
| 1015 | * one of the magic symbols emitted used by current ARM tools. |
| 1016 | * |
| 1017 | * Otherwise if find_symbols_between() returns those symbols, they'll |
| 1018 | * fail the whitelist tests and cause lots of false alarms ... fixable |
| 1019 | * only by merging __exit and __init sections into __text, bloating |
| 1020 | * the kernel (which is especially evil on embedded platforms). |
| 1021 | */ |
| 1022 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) |
| 1023 | { |
| 1024 | const char *name = elf->strtab + sym->st_name; |
| 1025 | |
| 1026 | if (!name || !strlen(name)) |
| 1027 | return 0; |
| 1028 | return !is_arm_mapping_symbol(name); |
| 1029 | } |
| 1030 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1031 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1032 | * Find symbols before or equal addr and after addr - in the section sec. |
| 1033 | * If we find two symbols with equal offset prefer one with a valid name. |
| 1034 | * The ELF format may have a better way to detect what type of symbol |
| 1035 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1036 | **/ |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1037 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
| 1038 | const char *sec) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1039 | { |
| 1040 | Elf_Sym *sym; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1041 | Elf_Sym *near = NULL; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1042 | Elf_Addr distance = ~0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1043 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1044 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 1045 | const char *symsec; |
| 1046 | |
| 1047 | if (sym->st_shndx >= SHN_LORESERVE) |
| 1048 | continue; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1049 | symsec = sec_name(elf, sym->st_shndx); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1050 | if (strcmp(symsec, sec) != 0) |
| 1051 | continue; |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 1052 | if (!is_valid_name(elf, sym)) |
| 1053 | continue; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1054 | if (sym->st_value <= addr) { |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1055 | if ((addr - sym->st_value) < distance) { |
| 1056 | distance = addr - sym->st_value; |
| 1057 | near = sym; |
| 1058 | } else if ((addr - sym->st_value) == distance) { |
| 1059 | near = sym; |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1060 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1061 | } |
| 1062 | } |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1063 | return near; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1066 | /* |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1067 | * Convert a section name to the function/data attribute |
| 1068 | * .init.text => __init |
| 1069 | * .cpuinit.data => __cpudata |
| 1070 | * .memexitconst => __memconst |
| 1071 | * etc. |
| 1072 | */ |
| 1073 | static char *sec2annotation(const char *s) |
| 1074 | { |
| 1075 | if (match(s, init_exit_sections)) { |
| 1076 | char *p = malloc(20); |
| 1077 | char *r = p; |
| 1078 | |
| 1079 | *p++ = '_'; |
| 1080 | *p++ = '_'; |
| 1081 | if (*s == '.') |
| 1082 | s++; |
| 1083 | while (*s && *s != '.') |
| 1084 | *p++ = *s++; |
| 1085 | *p = '\0'; |
| 1086 | if (*s == '.') |
| 1087 | s++; |
| 1088 | if (strstr(s, "rodata") != NULL) |
| 1089 | strcat(p, "const "); |
| 1090 | else if (strstr(s, "data") != NULL) |
| 1091 | strcat(p, "data "); |
| 1092 | else |
| 1093 | strcat(p, " "); |
| 1094 | return r; /* we leak her but we do not care */ |
| 1095 | } else { |
| 1096 | return ""; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | static int is_function(Elf_Sym *sym) |
| 1101 | { |
| 1102 | if (sym) |
| 1103 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; |
| 1104 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame^] | 1105 | return -1; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1109 | * Print a warning about a section mismatch. |
| 1110 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1111 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1112 | */ |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1113 | static void report_sec_mismatch(const char *modname, enum mismatch mismatch, |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1114 | const char *fromsec, |
| 1115 | unsigned long long fromaddr, |
| 1116 | const char *fromsym, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1117 | int from_is_func, |
| 1118 | const char *tosec, const char *tosym, |
| 1119 | int to_is_func) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1120 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1121 | const char *from, *from_p; |
| 1122 | const char *to, *to_p; |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame^] | 1123 | |
| 1124 | switch (from_is_func) { |
| 1125 | case 0: from = "variable"; from_p = ""; break; |
| 1126 | case 1: from = "function"; from_p = "()"; break; |
| 1127 | default: from = "(unknown reference)"; from_p = ""; break; |
| 1128 | } |
| 1129 | switch (to_is_func) { |
| 1130 | case 0: to = "variable"; to_p = ""; break; |
| 1131 | case 1: to = "function"; to_p = "()"; break; |
| 1132 | default: to = "(unknown reference)"; to_p = ""; break; |
| 1133 | } |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1134 | |
Sam Ravnborg | e5f95c8 | 2008-02-02 18:57:18 +0100 | [diff] [blame] | 1135 | sec_mismatch_count++; |
| 1136 | if (!sec_mismatch_verbose) |
| 1137 | return; |
| 1138 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1139 | fprintf(stderr, "WARNING: %s(%s+0x%llx): Section mismatch in" |
| 1140 | " reference from the %s %s%s to the %s %s:%s%s\n", |
| 1141 | modname, fromsec, fromaddr, from, fromsym, from_p, |
| 1142 | to, tosec, tosym, to_p); |
| 1143 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1144 | switch (mismatch) { |
| 1145 | case TEXT_TO_INIT: |
| 1146 | fprintf(stderr, |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame^] | 1147 | "The function %s%s() references\n" |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1148 | "the %s %s%s%s.\n" |
| 1149 | "This is often because %s lacks a %s\n" |
| 1150 | "annotation or the annotation of %s is wrong.\n", |
| 1151 | sec2annotation(fromsec), fromsym, |
| 1152 | to, sec2annotation(tosec), tosym, to_p, |
| 1153 | fromsym, sec2annotation(tosec), tosym); |
| 1154 | break; |
| 1155 | case DATA_TO_INIT: { |
| 1156 | const char **s = symbol_white_list; |
| 1157 | fprintf(stderr, |
| 1158 | "The variable %s references\n" |
| 1159 | "the %s %s%s%s\n" |
| 1160 | "If the reference is valid then annotate the\n" |
| 1161 | "variable with __init* (see linux/init.h) " |
| 1162 | "or name the variable:\n", |
| 1163 | fromsym, to, sec2annotation(tosec), tosym, to_p); |
| 1164 | while (*s) |
| 1165 | fprintf(stderr, "%s, ", *s++); |
| 1166 | fprintf(stderr, "\n"); |
| 1167 | break; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1168 | } |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1169 | case TEXT_TO_EXIT: |
| 1170 | fprintf(stderr, |
| 1171 | "The function %s() references a %s in an exit section.\n" |
| 1172 | "Often the %s %s%s has valid usage outside the exit section\n" |
| 1173 | "and the fix is to remove the %sannotation of %s.\n", |
| 1174 | fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym); |
| 1175 | break; |
| 1176 | case DATA_TO_EXIT: { |
| 1177 | const char **s = symbol_white_list; |
| 1178 | fprintf(stderr, |
| 1179 | "The variable %s references\n" |
| 1180 | "the %s %s%s%s\n" |
| 1181 | "If the reference is valid then annotate the\n" |
| 1182 | "variable with __exit* (see linux/init.h) or " |
| 1183 | "name the variable:\n", |
| 1184 | fromsym, to, sec2annotation(tosec), tosym, to_p); |
| 1185 | while (*s) |
| 1186 | fprintf(stderr, "%s, ", *s++); |
| 1187 | fprintf(stderr, "\n"); |
| 1188 | break; |
| 1189 | } |
| 1190 | case XXXINIT_TO_INIT: |
| 1191 | case XXXEXIT_TO_EXIT: |
| 1192 | fprintf(stderr, |
| 1193 | "The %s %s%s%s references\n" |
| 1194 | "a %s %s%s%s.\n" |
| 1195 | "If %s is only used by %s then\n" |
| 1196 | "annotate %s with a matching annotation.\n", |
| 1197 | from, sec2annotation(fromsec), fromsym, from_p, |
| 1198 | to, sec2annotation(tosec), tosym, to_p, |
| 1199 | fromsym, tosym, fromsym); |
| 1200 | break; |
| 1201 | case INIT_TO_EXIT: |
| 1202 | fprintf(stderr, |
| 1203 | "The %s %s%s%s references\n" |
| 1204 | "a %s %s%s%s.\n" |
| 1205 | "This is often seen when error handling " |
| 1206 | "in the init function\n" |
| 1207 | "uses functionality in the exit path.\n" |
| 1208 | "The fix is often to remove the %sannotation of\n" |
| 1209 | "%s%s so it may be used outside an exit section.\n", |
| 1210 | from, sec2annotation(fromsec), fromsym, from_p, |
| 1211 | to, sec2annotation(tosec), tosym, to_p, |
| 1212 | sec2annotation(tosec), tosym, to_p); |
| 1213 | break; |
| 1214 | case EXIT_TO_INIT: |
| 1215 | fprintf(stderr, |
| 1216 | "The %s %s%s%s references\n" |
| 1217 | "a %s %s%s%s.\n" |
| 1218 | "This is often seen when error handling " |
| 1219 | "in the exit function\n" |
| 1220 | "uses functionality in the init path.\n" |
| 1221 | "The fix is often to remove the %sannotation of\n" |
| 1222 | "%s%s so it may be used outside an init section.\n", |
| 1223 | from, sec2annotation(fromsec), fromsym, from_p, |
| 1224 | to, sec2annotation(tosec), tosym, to_p, |
| 1225 | sec2annotation(tosec), tosym, to_p); |
| 1226 | break; |
| 1227 | case EXPORT_TO_INIT_EXIT: |
| 1228 | fprintf(stderr, |
| 1229 | "The symbol %s is exported and annotated %s\n" |
| 1230 | "Fix this by removing the %sannotation of %s " |
| 1231 | "or drop the export.\n", |
| 1232 | tosym, sec2annotation(tosec), sec2annotation(tosec), tosym); |
| 1233 | case NO_MISMATCH: |
| 1234 | /* To get warnings on missing members */ |
| 1235 | break; |
| 1236 | } |
| 1237 | fprintf(stderr, "\n"); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static void check_section_mismatch(const char *modname, struct elf_info *elf, |
| 1241 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) |
| 1242 | { |
| 1243 | const char *tosec; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1244 | enum mismatch mismatch; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1245 | |
| 1246 | tosec = sec_name(elf, sym->st_shndx); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1247 | mismatch = section_mismatch(fromsec, tosec); |
| 1248 | if (mismatch != NO_MISMATCH) { |
| 1249 | Elf_Sym *to; |
| 1250 | Elf_Sym *from; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1251 | const char *tosym; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1252 | const char *fromsym; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1253 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1254 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
| 1255 | fromsym = sym_name(elf, from); |
| 1256 | to = find_elf_symbol(elf, r->r_addend, sym); |
| 1257 | tosym = sym_name(elf, to); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1258 | |
| 1259 | /* check whitelist - we may ignore it */ |
| 1260 | if (secref_whitelist(fromsec, fromsym, tosec, tosym)) { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1261 | report_sec_mismatch(modname, mismatch, |
| 1262 | fromsec, r->r_offset, fromsym, |
| 1263 | is_function(from), tosec, tosym, |
| 1264 | is_function(to)); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1265 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1269 | static unsigned int *reloc_location(struct elf_info *elf, |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1270 | Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1271 | { |
| 1272 | Elf_Shdr *sechdrs = elf->sechdrs; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1273 | int section = sechdr->sh_info; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1274 | |
| 1275 | return (void *)elf->hdr + sechdrs[section].sh_offset + |
| 1276 | (r->r_offset - sechdrs[section].sh_addr); |
| 1277 | } |
| 1278 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1279 | static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1280 | { |
| 1281 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1282 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1283 | |
| 1284 | switch (r_typ) { |
| 1285 | case R_386_32: |
| 1286 | r->r_addend = TO_NATIVE(*location); |
| 1287 | break; |
| 1288 | case R_386_PC32: |
| 1289 | r->r_addend = TO_NATIVE(*location) + 4; |
| 1290 | /* For CONFIG_RELOCATABLE=y */ |
| 1291 | if (elf->hdr->e_type == ET_EXEC) |
| 1292 | r->r_addend += r->r_offset; |
| 1293 | break; |
| 1294 | } |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1298 | static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1299 | { |
| 1300 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 1301 | |
| 1302 | switch (r_typ) { |
| 1303 | case R_ARM_ABS32: |
| 1304 | /* From ARM ABI: (S + A) | T */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1305 | r->r_addend = (int)(long) |
| 1306 | (elf->symtab_start + ELF_R_SYM(r->r_info)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1307 | break; |
| 1308 | case R_ARM_PC24: |
| 1309 | /* From ARM ABI: ((S + A) | T) - P */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1310 | r->r_addend = (int)(long)(elf->hdr + |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1311 | sechdr->sh_offset + |
| 1312 | (r->r_offset - sechdr->sh_addr)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1313 | break; |
| 1314 | default: |
| 1315 | return 1; |
| 1316 | } |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1320 | static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1321 | { |
| 1322 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1323 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1324 | unsigned int inst; |
| 1325 | |
| 1326 | if (r_typ == R_MIPS_HI16) |
| 1327 | return 1; /* skip this */ |
| 1328 | inst = TO_NATIVE(*location); |
| 1329 | switch (r_typ) { |
| 1330 | case R_MIPS_LO16: |
| 1331 | r->r_addend = inst & 0xffff; |
| 1332 | break; |
| 1333 | case R_MIPS_26: |
| 1334 | r->r_addend = (inst & 0x03ffffff) << 2; |
| 1335 | break; |
| 1336 | case R_MIPS_32: |
| 1337 | r->r_addend = inst; |
| 1338 | break; |
| 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1343 | static void section_rela(const char *modname, struct elf_info *elf, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1344 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1345 | { |
| 1346 | Elf_Sym *sym; |
| 1347 | Elf_Rela *rela; |
| 1348 | Elf_Rela r; |
| 1349 | unsigned int r_sym; |
| 1350 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1351 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1352 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1353 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
| 1354 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1355 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1356 | fromsec += strlen(".rela"); |
| 1357 | /* if from section (name) is know good then skip it */ |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 1358 | if (check_section(modname, fromsec)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1359 | return; |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 1360 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1361 | for (rela = start; rela < stop; rela++) { |
| 1362 | r.r_offset = TO_NATIVE(rela->r_offset); |
| 1363 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1364 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1365 | unsigned int r_typ; |
| 1366 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); |
| 1367 | r_sym = TO_NATIVE(r_sym); |
| 1368 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); |
| 1369 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1370 | } else { |
| 1371 | r.r_info = TO_NATIVE(rela->r_info); |
| 1372 | r_sym = ELF_R_SYM(r.r_info); |
| 1373 | } |
| 1374 | #else |
| 1375 | r.r_info = TO_NATIVE(rela->r_info); |
| 1376 | r_sym = ELF_R_SYM(r.r_info); |
| 1377 | #endif |
| 1378 | r.r_addend = TO_NATIVE(rela->r_addend); |
| 1379 | sym = elf->symtab_start + r_sym; |
| 1380 | /* Skip special sections */ |
| 1381 | if (sym->st_shndx >= SHN_LORESERVE) |
| 1382 | continue; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1383 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | static void section_rel(const char *modname, struct elf_info *elf, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1388 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1389 | { |
| 1390 | Elf_Sym *sym; |
| 1391 | Elf_Rel *rel; |
| 1392 | Elf_Rela r; |
| 1393 | unsigned int r_sym; |
| 1394 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1395 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1396 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1397 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
| 1398 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1399 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1400 | fromsec += strlen(".rel"); |
| 1401 | /* if from section (name) is know good then skip it */ |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 1402 | if (check_section(modname, fromsec)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1403 | return; |
| 1404 | |
| 1405 | for (rel = start; rel < stop; rel++) { |
| 1406 | r.r_offset = TO_NATIVE(rel->r_offset); |
| 1407 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1408 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1409 | unsigned int r_typ; |
| 1410 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); |
| 1411 | r_sym = TO_NATIVE(r_sym); |
| 1412 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); |
| 1413 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1414 | } else { |
| 1415 | r.r_info = TO_NATIVE(rel->r_info); |
| 1416 | r_sym = ELF_R_SYM(r.r_info); |
| 1417 | } |
| 1418 | #else |
| 1419 | r.r_info = TO_NATIVE(rel->r_info); |
| 1420 | r_sym = ELF_R_SYM(r.r_info); |
| 1421 | #endif |
| 1422 | r.r_addend = 0; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1423 | switch (elf->hdr->e_machine) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1424 | case EM_386: |
| 1425 | if (addend_386_rel(elf, sechdr, &r)) |
| 1426 | continue; |
| 1427 | break; |
| 1428 | case EM_ARM: |
| 1429 | if (addend_arm_rel(elf, sechdr, &r)) |
| 1430 | continue; |
| 1431 | break; |
| 1432 | case EM_MIPS: |
| 1433 | if (addend_mips_rel(elf, sechdr, &r)) |
| 1434 | continue; |
| 1435 | break; |
| 1436 | } |
| 1437 | sym = elf->symtab_start + r_sym; |
| 1438 | /* Skip special sections */ |
| 1439 | if (sym->st_shndx >= SHN_LORESERVE) |
| 1440 | continue; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1441 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1445 | /** |
| 1446 | * A module includes a number of sections that are discarded |
| 1447 | * either when loaded or when used as built-in. |
| 1448 | * For loaded modules all functions marked __init and all data |
| 1449 | * marked __initdata will be discarded when the module has been intialized. |
| 1450 | * Likewise for modules used built-in the sections marked __exit |
| 1451 | * are discarded because __exit marked function are supposed to be called |
| 1452 | * only when a moduel is unloaded which never happes for built-in modules. |
| 1453 | * The check_sec_ref() function traverses all relocation records |
| 1454 | * to find all references to a section that reference a section that will |
| 1455 | * be discarded and warns about it. |
| 1456 | **/ |
| 1457 | static void check_sec_ref(struct module *mod, const char *modname, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1458 | struct elf_info *elf) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1459 | { |
| 1460 | int i; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1461 | Elf_Shdr *sechdrs = elf->sechdrs; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1462 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1463 | /* Walk through all sections */ |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1464 | for (i = 0; i < elf->hdr->e_shnum; i++) { |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1465 | /* We want to process only relocation sections and not .init */ |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1466 | if (sechdrs[i].sh_type == SHT_RELA) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1467 | section_rela(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1468 | else if (sechdrs[i].sh_type == SHT_REL) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1469 | section_rel(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1473 | static void read_symbols(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
| 1475 | const char *symname; |
| 1476 | char *version; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1477 | char *license; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | struct module *mod; |
| 1479 | struct elf_info info = { }; |
| 1480 | Elf_Sym *sym; |
| 1481 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 1482 | if (!parse_elf(&info, modname)) |
| 1483 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
| 1485 | mod = new_module(modname); |
| 1486 | |
| 1487 | /* When there's no vmlinux, don't print warnings about |
| 1488 | * unresolved symbols (since there'll be too many ;) */ |
| 1489 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | mod->skip = 1; |
| 1492 | } |
| 1493 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1494 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
| 1495 | while (license) { |
| 1496 | if (license_is_gpl_compatible(license)) |
| 1497 | mod->gpl_compatible = 1; |
| 1498 | else { |
| 1499 | mod->gpl_compatible = 0; |
| 1500 | break; |
| 1501 | } |
| 1502 | license = get_next_modinfo(info.modinfo, info.modinfo_len, |
| 1503 | "license", license); |
| 1504 | } |
| 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 1507 | symname = info.strtab + sym->st_name; |
| 1508 | |
| 1509 | handle_modversions(mod, &info, sym, symname); |
| 1510 | handle_moddevtable(mod, &info, sym, symname); |
| 1511 | } |
Sam Ravnborg | d1f25e6 | 2008-01-17 21:17:42 +0100 | [diff] [blame] | 1512 | if (!is_vmlinux(modname) || |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1513 | (is_vmlinux(modname) && vmlinux_section_warnings)) |
| 1514 | check_sec_ref(mod, modname, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 1517 | if (version) |
| 1518 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 1519 | version - (char *)info.hdr); |
| 1520 | if (version || (all_versions && !is_vmlinux(modname))) |
| 1521 | get_src_version(modname, mod->srcversion, |
| 1522 | sizeof(mod->srcversion)-1); |
| 1523 | |
| 1524 | parse_elf_finish(&info); |
| 1525 | |
| 1526 | /* Our trick to get versioning for struct_module - it's |
| 1527 | * never passed as an argument to an exported function, so |
| 1528 | * the automatic versioning doesn't pick it up, but it's really |
| 1529 | * important anyhow */ |
| 1530 | if (modversions) |
| 1531 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); |
| 1532 | } |
| 1533 | |
| 1534 | #define SZ 500 |
| 1535 | |
| 1536 | /* We first write the generated file into memory using the |
| 1537 | * following helper, then compare to the file on disk and |
| 1538 | * only update the later if anything changed */ |
| 1539 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1540 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 1541 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | { |
| 1543 | char tmp[SZ]; |
| 1544 | int len; |
| 1545 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | va_start(ap, fmt); |
| 1548 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1549 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | va_end(ap); |
| 1551 | } |
| 1552 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1553 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | { |
| 1555 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1556 | buf->size += len + SZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | buf->p = realloc(buf->p, buf->size); |
| 1558 | } |
| 1559 | strncpy(buf->p + buf->pos, s, len); |
| 1560 | buf->pos += len; |
| 1561 | } |
| 1562 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1563 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
| 1564 | { |
| 1565 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1566 | |
| 1567 | switch (exp) { |
| 1568 | case export_gpl: |
| 1569 | fatal("modpost: GPL-incompatible module %s%s " |
| 1570 | "uses GPL-only symbol '%s'\n", m, e, s); |
| 1571 | break; |
| 1572 | case export_unused_gpl: |
| 1573 | fatal("modpost: GPL-incompatible module %s%s " |
| 1574 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); |
| 1575 | break; |
| 1576 | case export_gpl_future: |
| 1577 | warn("modpost: GPL-incompatible module %s%s " |
| 1578 | "uses future GPL-only symbol '%s'\n", m, e, s); |
| 1579 | break; |
| 1580 | case export_plain: |
| 1581 | case export_unused: |
| 1582 | case export_unknown: |
| 1583 | /* ignore */ |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1588 | static void check_for_unused(enum export exp, const char *m, const char *s) |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1589 | { |
| 1590 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1591 | |
| 1592 | switch (exp) { |
| 1593 | case export_unused: |
| 1594 | case export_unused_gpl: |
| 1595 | warn("modpost: module %s%s " |
| 1596 | "uses symbol '%s' marked UNUSED\n", m, e, s); |
| 1597 | break; |
| 1598 | default: |
| 1599 | /* ignore */ |
| 1600 | break; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | static void check_exports(struct module *mod) |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1605 | { |
| 1606 | struct symbol *s, *exp; |
| 1607 | |
| 1608 | for (s = mod->unres; s; s = s->next) { |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1609 | const char *basename; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1610 | exp = find_symbol(s->name); |
| 1611 | if (!exp || exp->module == mod) |
| 1612 | continue; |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1613 | basename = strrchr(mod->name, '/'); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1614 | if (basename) |
| 1615 | basename++; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1616 | else |
| 1617 | basename = mod->name; |
| 1618 | if (!mod->gpl_compatible) |
| 1619 | check_for_gpl_usage(exp->export, basename, exp->name); |
| 1620 | check_for_unused(exp->export, basename, exp->name); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1621 | } |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1624 | /** |
| 1625 | * Header for the generated file |
| 1626 | **/ |
| 1627 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
| 1629 | buf_printf(b, "#include <linux/module.h>\n"); |
| 1630 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 1631 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 1632 | buf_printf(b, "\n"); |
| 1633 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 1634 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | buf_printf(b, "struct module __this_module\n"); |
| 1636 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Ustyugov Roman | f83b5e3 | 2005-09-23 08:42:11 +0400 | [diff] [blame] | 1637 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | if (mod->has_init) |
| 1639 | buf_printf(b, " .init = init_module,\n"); |
| 1640 | if (mod->has_cleanup) |
| 1641 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
| 1642 | " .exit = cleanup_module,\n" |
| 1643 | "#endif\n"); |
Roman Zippel | e61a1c1 | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 1644 | buf_printf(b, " .arch = MODULE_ARCH_INIT,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | buf_printf(b, "};\n"); |
| 1646 | } |
| 1647 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1648 | /** |
| 1649 | * Record CRCs for unresolved symbols |
| 1650 | **/ |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1651 | static int add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | { |
| 1653 | struct symbol *s, *exp; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1654 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
| 1656 | for (s = mod->unres; s; s = s->next) { |
| 1657 | exp = find_symbol(s->name); |
| 1658 | if (!exp || exp->module == mod) { |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1659 | if (have_vmlinux && !s->weak) { |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 1660 | if (warn_unresolved) { |
| 1661 | warn("\"%s\" [%s.ko] undefined!\n", |
| 1662 | s->name, mod->name); |
| 1663 | } else { |
| 1664 | merror("\"%s\" [%s.ko] undefined!\n", |
| 1665 | s->name, mod->name); |
| 1666 | err = 1; |
| 1667 | } |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | continue; |
| 1670 | } |
| 1671 | s->module = exp->module; |
| 1672 | s->crc_valid = exp->crc_valid; |
| 1673 | s->crc = exp->crc; |
| 1674 | } |
| 1675 | |
| 1676 | if (!modversions) |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1677 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
| 1679 | buf_printf(b, "\n"); |
| 1680 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 1681 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 1683 | |
| 1684 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1685 | if (!s->module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 1688 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | s->name, mod->name); |
| 1690 | continue; |
| 1691 | } |
| 1692 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); |
| 1693 | } |
| 1694 | |
| 1695 | buf_printf(b, "};\n"); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1696 | |
| 1697 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1700 | static void add_depends(struct buffer *b, struct module *mod, |
| 1701 | struct module *modules) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | { |
| 1703 | struct symbol *s; |
| 1704 | struct module *m; |
| 1705 | int first = 1; |
| 1706 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1707 | for (m = modules; m; m = m->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | m->seen = is_vmlinux(m->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
| 1710 | buf_printf(b, "\n"); |
| 1711 | buf_printf(b, "static const char __module_depends[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 1712 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 1714 | buf_printf(b, "\"depends="); |
| 1715 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1716 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | if (!s->module) |
| 1718 | continue; |
| 1719 | |
| 1720 | if (s->module->seen) |
| 1721 | continue; |
| 1722 | |
| 1723 | s->module->seen = 1; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1724 | p = strrchr(s->module->name, '/'); |
| 1725 | if (p) |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1726 | p++; |
| 1727 | else |
| 1728 | p = s->module->name; |
| 1729 | buf_printf(b, "%s%s", first ? "" : ",", p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | first = 0; |
| 1731 | } |
| 1732 | buf_printf(b, "\";\n"); |
| 1733 | } |
| 1734 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1735 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | { |
| 1737 | if (mod->srcversion[0]) { |
| 1738 | buf_printf(b, "\n"); |
| 1739 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 1740 | mod->srcversion); |
| 1741 | } |
| 1742 | } |
| 1743 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1744 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | { |
| 1746 | char *tmp; |
| 1747 | FILE *file; |
| 1748 | struct stat st; |
| 1749 | |
| 1750 | file = fopen(fname, "r"); |
| 1751 | if (!file) |
| 1752 | goto write; |
| 1753 | |
| 1754 | if (fstat(fileno(file), &st) < 0) |
| 1755 | goto close_write; |
| 1756 | |
| 1757 | if (st.st_size != b->pos) |
| 1758 | goto close_write; |
| 1759 | |
| 1760 | tmp = NOFAIL(malloc(b->pos)); |
| 1761 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 1762 | goto free_write; |
| 1763 | |
| 1764 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 1765 | goto free_write; |
| 1766 | |
| 1767 | free(tmp); |
| 1768 | fclose(file); |
| 1769 | return; |
| 1770 | |
| 1771 | free_write: |
| 1772 | free(tmp); |
| 1773 | close_write: |
| 1774 | fclose(file); |
| 1775 | write: |
| 1776 | file = fopen(fname, "w"); |
| 1777 | if (!file) { |
| 1778 | perror(fname); |
| 1779 | exit(1); |
| 1780 | } |
| 1781 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 1782 | perror(fname); |
| 1783 | exit(1); |
| 1784 | } |
| 1785 | fclose(file); |
| 1786 | } |
| 1787 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1788 | /* parse Module.symvers file. line format: |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1789 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1790 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1791 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | { |
| 1793 | unsigned long size, pos = 0; |
| 1794 | void *file = grab_file(fname, &size); |
| 1795 | char *line; |
| 1796 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1797 | if (!file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | /* No symbol versions, silently ignore */ |
| 1799 | return; |
| 1800 | |
| 1801 | while ((line = get_next_line(&pos, file, size))) { |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1802 | char *symname, *modname, *d, *export, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | unsigned int crc; |
| 1804 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1805 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | |
| 1807 | if (!(symname = strchr(line, '\t'))) |
| 1808 | goto fail; |
| 1809 | *symname++ = '\0'; |
| 1810 | if (!(modname = strchr(symname, '\t'))) |
| 1811 | goto fail; |
| 1812 | *modname++ = '\0'; |
Laurent Riffard | 9ac545b | 2006-06-11 08:02:06 +0200 | [diff] [blame] | 1813 | if ((export = strchr(modname, '\t')) != NULL) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1814 | *export++ = '\0'; |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1815 | if (export && ((end = strchr(export, '\t')) != NULL)) |
| 1816 | *end = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | crc = strtoul(line, &d, 16); |
| 1818 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 1819 | goto fail; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1820 | mod = find_module(modname); |
| 1821 | if (!mod) { |
| 1822 | if (is_vmlinux(modname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | mod = new_module(NOFAIL(strdup(modname))); |
| 1825 | mod->skip = 1; |
| 1826 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1827 | s = sym_add_exported(symname, mod, export_no(export)); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 1828 | s->kernel = kernel; |
| 1829 | s->preloaded = 1; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1830 | sym_update_crc(symname, mod, crc, export_no(export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | return; |
| 1833 | fail: |
| 1834 | fatal("parse error in symbol dump file\n"); |
| 1835 | } |
| 1836 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1837 | /* For normal builds always dump all symbols. |
| 1838 | * For external modules only dump symbols |
| 1839 | * that are not read from kernel Module.symvers. |
| 1840 | **/ |
| 1841 | static int dump_sym(struct symbol *sym) |
| 1842 | { |
| 1843 | if (!external_module) |
| 1844 | return 1; |
| 1845 | if (sym->vmlinux || sym->kernel) |
| 1846 | return 0; |
| 1847 | return 1; |
| 1848 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1849 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1850 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | { |
| 1852 | struct buffer buf = { }; |
| 1853 | struct symbol *symbol; |
| 1854 | int n; |
| 1855 | |
| 1856 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 1857 | symbol = symbolhash[n]; |
| 1858 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1859 | if (dump_sym(symbol)) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1860 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1861 | symbol->crc, symbol->name, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1862 | symbol->module->name, |
| 1863 | export_str(symbol->export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | symbol = symbol->next; |
| 1865 | } |
| 1866 | } |
| 1867 | write_if_changed(&buf, fname); |
| 1868 | } |
| 1869 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1870 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | { |
| 1872 | struct module *mod; |
| 1873 | struct buffer buf = { }; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1874 | char *kernel_read = NULL, *module_read = NULL; |
| 1875 | char *dump_write = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | int opt; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1877 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1879 | while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1880 | switch (opt) { |
| 1881 | case 'i': |
| 1882 | kernel_read = optarg; |
| 1883 | break; |
| 1884 | case 'I': |
| 1885 | module_read = optarg; |
| 1886 | external_module = 1; |
| 1887 | break; |
| 1888 | case 'm': |
| 1889 | modversions = 1; |
| 1890 | break; |
| 1891 | case 'o': |
| 1892 | dump_write = optarg; |
| 1893 | break; |
| 1894 | case 'a': |
| 1895 | all_versions = 1; |
| 1896 | break; |
| 1897 | case 's': |
| 1898 | vmlinux_section_warnings = 0; |
| 1899 | break; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1900 | case 'S': |
| 1901 | sec_mismatch_verbose = 0; |
| 1902 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1903 | case 'w': |
| 1904 | warn_unresolved = 1; |
| 1905 | break; |
| 1906 | default: |
| 1907 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1911 | if (kernel_read) |
| 1912 | read_dump(kernel_read, 1); |
| 1913 | if (module_read) |
| 1914 | read_dump(module_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1916 | while (optind < argc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | read_symbols(argv[optind++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | |
| 1919 | for (mod = modules; mod; mod = mod->next) { |
| 1920 | if (mod->skip) |
| 1921 | continue; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1922 | check_exports(mod); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1925 | err = 0; |
| 1926 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1927 | for (mod = modules; mod; mod = mod->next) { |
Andi Kleen | 666ab41 | 2007-11-22 03:43:10 +0100 | [diff] [blame] | 1928 | char fname[strlen(mod->name) + 10]; |
| 1929 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1930 | if (mod->skip) |
| 1931 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
| 1933 | buf.pos = 0; |
| 1934 | |
| 1935 | add_header(&buf, mod); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1936 | err |= add_versions(&buf, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | add_depends(&buf, mod, modules); |
| 1938 | add_moddevtable(&buf, mod); |
| 1939 | add_srcversion(&buf, mod); |
| 1940 | |
| 1941 | sprintf(fname, "%s.mod.c", mod->name); |
| 1942 | write_if_changed(&buf, fname); |
| 1943 | } |
| 1944 | |
| 1945 | if (dump_write) |
| 1946 | write_dump(dump_write); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1947 | if (sec_mismatch_count && !sec_mismatch_verbose) |
| 1948 | fprintf(stderr, "modpost: Found %d section mismatch(es).\n" |
Sam Ravnborg | e5f95c8 | 2008-02-02 18:57:18 +0100 | [diff] [blame] | 1949 | "To see full details build your kernel with:\n" |
| 1950 | "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n", |
| 1951 | sec_mismatch_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1953 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | } |