blob: 936b6f8e46ff2fc6663852a5f19e697d41e41296 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
17#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020018#include "../../include/linux/license.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/* Are we using CONFIG_MODVERSIONS? */
21int modversions = 0;
22/* Warn about undefined symbols? (do so if we have vmlinux) */
23int have_vmlinux = 0;
24/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
25static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010026/* If we are modposting external module set to 1 */
27static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020028/* Warn about section mismatch in vmlinux if set to 1 */
29static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070030/* Only warn about unresolved symbols */
31static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070032/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010033static int sec_mismatch_count = 0;
34static int sec_mismatch_verbose = 1;
35
Sam Ravnborgc96fca22006-07-01 11:44:23 +020036enum export {
37 export_plain, export_unused, export_gpl,
38 export_unused_gpl, export_gpl_future, export_unknown
39};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Andi Kleen6d9a89e2007-11-22 03:43:08 +010041#define PRINTF __attribute__ ((format (printf, 1, 2)))
42
43PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 va_list arglist;
46
47 fprintf(stderr, "FATAL: ");
48
49 va_start(arglist, fmt);
50 vfprintf(stderr, fmt, arglist);
51 va_end(arglist);
52
53 exit(1);
54}
55
Andi Kleen6d9a89e2007-11-22 03:43:08 +010056PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 va_list arglist;
59
60 fprintf(stderr, "WARNING: ");
61
62 va_start(arglist, fmt);
63 vfprintf(stderr, fmt, arglist);
64 va_end(arglist);
65}
66
Andi Kleen6d9a89e2007-11-22 03:43:08 +010067PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060068{
69 va_list arglist;
70
71 fprintf(stderr, "ERROR: ");
72
73 va_start(arglist, fmt);
74 vfprintf(stderr, fmt, arglist);
75 va_end(arglist);
76}
77
Sam Ravnborg040fcc82006-01-28 22:15:55 +010078static int is_vmlinux(const char *modname)
79{
80 const char *myname;
81
Sam Ravnborgdf578e72008-01-11 19:17:15 +010082 myname = strrchr(modname, '/');
83 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010084 myname++;
85 else
86 myname = modname;
87
Sam Ravnborg741f98f2007-07-17 10:54:06 +020088 return (strcmp(myname, "vmlinux") == 0) ||
89 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +010090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092void *do_nofail(void *ptr, const char *expr)
93{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010094 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return ptr;
98}
99
100/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct module *modules;
102
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100103static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct module *mod;
106
107 for (mod = modules; mod; mod = mod->next)
108 if (strcmp(mod->name, modname) == 0)
109 break;
110 return mod;
111}
112
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100113static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct module *mod;
116 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mod = NOFAIL(malloc(sizeof(*mod)));
119 memset(mod, 0, sizeof(*mod));
120 p = NOFAIL(strdup(modname));
121
122 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100123 s = strrchr(p, '.');
124 if (s != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (strcmp(s, ".o") == 0)
126 *s = '\0';
127
128 /* add to list */
129 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200130 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mod->next = modules;
132 modules = mod;
133
134 return mod;
135}
136
137/* A hash of all exported symbols,
138 * struct symbol is also used for lists of unresolved symbols */
139
140#define SYMBOL_HASH_SIZE 1024
141
142struct symbol {
143 struct symbol *next;
144 struct module *module;
145 unsigned int crc;
146 int crc_valid;
147 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100148 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
149 unsigned int kernel:1; /* 1 if symbol is from kernel
150 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100151 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700152 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 char name[0];
154};
155
156static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
157
158/* This is based on the hash agorithm from gdbm, via tdb */
159static inline unsigned int tdb_hash(const char *name)
160{
161 unsigned value; /* Used to compute the hash value. */
162 unsigned i; /* Used to cycle through random values. */
163
164 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100165 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
167
168 return (1103515243 * value + 12345);
169}
170
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100171/**
172 * Allocate a new symbols for use in the hash of exported symbols or
173 * the list of unresolved symbols per module
174 **/
175static struct symbol *alloc_symbol(const char *name, unsigned int weak,
176 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
179
180 memset(s, 0, sizeof(*s));
181 strcpy(s->name, name);
182 s->weak = weak;
183 s->next = next;
184 return s;
185}
186
187/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700188static struct symbol *new_symbol(const char *name, struct module *module,
189 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 unsigned int hash;
192 struct symbol *new;
193
194 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
195 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
196 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700197 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100198 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100201static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct symbol *s;
204
205 /* For our purposes, .foo matches foo. PPC64 needs this. */
206 if (name[0] == '.')
207 name++;
208
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100209 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (strcmp(s->name, name) == 0)
211 return s;
212 }
213 return NULL;
214}
215
Ram Paibd5cbce2006-06-08 22:12:53 -0700216static struct {
217 const char *str;
218 enum export export;
219} export_list[] = {
220 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200221 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700222 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200223 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700224 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
225 { .str = "(unknown)", .export = export_unknown },
226};
227
228
229static const char *export_str(enum export ex)
230{
231 return export_list[ex].str;
232}
233
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100234static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700235{
236 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100237
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200238 if (!s)
239 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700240 for (i = 0; export_list[i].export != export_unknown; i++) {
241 if (strcmp(export_list[i].str, s) == 0)
242 return export_list[i].export;
243 }
244 return export_unknown;
245}
246
247static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
248{
249 if (sec == elf->export_sec)
250 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200251 else if (sec == elf->export_unused_sec)
252 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700253 else if (sec == elf->export_gpl_sec)
254 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200255 else if (sec == elf->export_unused_gpl_sec)
256 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700257 else if (sec == elf->export_gpl_future_sec)
258 return export_gpl_future;
259 else
260 return export_unknown;
261}
262
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100263/**
264 * Add an exported symbol - it may have already been added without a
265 * CRC, in this case just update the CRC
266 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700267static struct symbol *sym_add_exported(const char *name, struct module *mod,
268 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct symbol *s = find_symbol(name);
271
272 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700273 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100274 } else {
275 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100276 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100277 "was in %s%s\n", mod->name, name,
278 s->module->name,
279 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700280 } else {
281 /* In case Modules.symvers was out of date */
282 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100285 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100286 s->vmlinux = is_vmlinux(mod->name);
287 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700288 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100289 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100292static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700293 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100294{
295 struct symbol *s = find_symbol(name);
296
297 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700298 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100299 s->crc = crc;
300 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100303void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct stat st;
306 void *map;
307 int fd;
308
309 fd = open(filename, O_RDONLY);
310 if (fd < 0 || fstat(fd, &st) != 0)
311 return NULL;
312
313 *size = st.st_size;
314 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
315 close(fd);
316
317 if (map == MAP_FAILED)
318 return NULL;
319 return map;
320}
321
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100322/**
323 * Return a copy of the next line in a mmap'ed file.
324 * spaces in the beginning of the line is trimmed away.
325 * Return a pointer to a static buffer.
326 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100327char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 static char line[4096];
330 int skip = 1;
331 size_t len = 0;
332 signed char *p = (signed char *)file + *pos;
333 char *s = line;
334
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100335 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (skip && isspace(*p)) {
337 p++;
338 continue;
339 }
340 skip = 0;
341 if (*p != '\n' && (*pos < size)) {
342 len++;
343 *s++ = *p++;
344 if (len > 4095)
345 break; /* Too long, stop */
346 } else {
347 /* End of string */
348 *s = '\0';
349 return line;
350 }
351 }
352 /* End of buffer */
353 return NULL;
354}
355
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100356void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 munmap(file, size);
359}
360
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100361static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100364 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 Elf_Shdr *sechdrs;
366 Elf_Sym *sym;
367
368 hdr = grab_file(filename, &info->size);
369 if (!hdr) {
370 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200371 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100374 if (info->size < sizeof(*hdr)) {
375 /* file too small, assume this is an empty .o file */
376 return 0;
377 }
378 /* Is this a valid ELF file? */
379 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
380 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
381 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
382 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
383 /* Not an ELF file - silently ignore it */
384 return 0;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Fix endianness in ELF header */
387 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
388 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
389 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
390 hdr->e_machine = TO_NATIVE(hdr->e_machine);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900391 hdr->e_type = TO_NATIVE(hdr->e_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sechdrs = (void *)hdr + hdr->e_shoff;
393 info->sechdrs = sechdrs;
394
Petr Stetiara83710e2007-08-27 12:15:07 +0200395 /* Check if file offset is correct */
396 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100397 fatal("section header offset=%lu in file '%s' is bigger than "
398 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
399 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200400 return 0;
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* Fix endianness in section headers */
404 for (i = 0; i < hdr->e_shnum; i++) {
405 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
406 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
407 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
408 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
409 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900410 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
411 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 /* Find symbol table. */
414 for (i = 1; i < hdr->e_shnum; i++) {
415 const char *secstrings
416 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700417 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900418 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Tejun Heo56fc82c2009-02-06 00:48:02 +0900420 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100421 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
422 "sizeof(*hrd)=%zu\n", filename,
423 (unsigned long)sechdrs[i].sh_offset,
424 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100425 return 0;
426 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700427 secname = secstrings + sechdrs[i].sh_name;
428 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900429 if (nobits)
430 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
432 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700433 } else if (strcmp(secname, "__ksymtab") == 0)
434 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200435 else if (strcmp(secname, "__ksymtab_unused") == 0)
436 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700437 else if (strcmp(secname, "__ksymtab_gpl") == 0)
438 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200439 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
440 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700441 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
442 info->export_gpl_future_sec = i;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -0800443 else if (strcmp(secname, "__markers_strings") == 0)
444 info->markers_strings_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (sechdrs[i].sh_type != SHT_SYMTAB)
447 continue;
448
449 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100450 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100452 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sechdrs[sechdrs[i].sh_link].sh_offset;
454 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100455 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100456 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* Fix endianness in symbols */
459 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
460 sym->st_shndx = TO_NATIVE(sym->st_shndx);
461 sym->st_name = TO_NATIVE(sym->st_name);
462 sym->st_value = TO_NATIVE(sym->st_value);
463 sym->st_size = TO_NATIVE(sym->st_size);
464 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100465 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100468static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 release_file(info->hdr, info->size);
471}
472
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200473static int ignore_undef_symbol(struct elf_info *info, const char *symname)
474{
475 /* ignore __this_module, it will be resolved shortly */
476 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
477 return 1;
478 /* ignore global offset table */
479 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
480 return 1;
481 if (info->hdr->e_machine == EM_PPC)
482 /* Special register function linked on all modules during final link of .ko */
483 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
484 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
485 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
486 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
487 return 1;
488 /* Do not ignore this symbol */
489 return 0;
490}
491
Luke Yangf7b05e62006-03-02 18:35:31 +0800492#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
493#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100495static void handle_modversions(struct module *mod, struct elf_info *info,
496 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700499 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 switch (sym->st_shndx) {
502 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100503 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case SHN_ABS:
506 /* CRC'd symbol */
507 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
508 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700509 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
510 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 break;
513 case SHN_UNDEF:
514 /* undefined symbol */
515 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
516 ELF_ST_BIND(sym->st_info) != STB_WEAK)
517 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200518 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
Ben Colline8d529012005-08-19 13:44:57 -0700520/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
521#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
522/* add compatibility with older glibc */
523#ifndef STT_SPARC_REGISTER
524#define STT_SPARC_REGISTER STT_REGISTER
525#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (info->hdr->e_machine == EM_SPARC ||
527 info->hdr->e_machine == EM_SPARCV9) {
528 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700529 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100531 if (symname[0] == '.') {
532 char *munged = strdup(symname);
533 munged[0] = '_';
534 munged[1] = toupper(munged[1]);
535 symname = munged;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100541 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
542 mod->unres =
543 alloc_symbol(symname +
544 strlen(MODULE_SYMBOL_PREFIX),
545 ELF_ST_BIND(sym->st_info) == STB_WEAK,
546 mod->unres);
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 default:
550 /* All exported symbols */
551 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700552 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
553 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
556 mod->has_init = 1;
557 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
558 mod->has_cleanup = 1;
559 break;
560 }
561}
562
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100563/**
564 * Parse tag=value strings from .modinfo section
565 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static char *next_string(char *string, unsigned long *secsize)
567{
568 /* Skip non-zero chars */
569 while (string[0]) {
570 string++;
571 if ((*secsize)-- <= 1)
572 return NULL;
573 }
574
575 /* Skip any zero padding. */
576 while (!string[0]) {
577 string++;
578 if ((*secsize)-- <= 1)
579 return NULL;
580 }
581 return string;
582}
583
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200584static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
585 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 char *p;
588 unsigned int taglen = strlen(tag);
589 unsigned long size = modinfo_len;
590
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200591 if (info) {
592 size -= info - (char *)modinfo;
593 modinfo = next_string(info, &size);
594 }
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 for (p = modinfo; p; p = next_string(p, &size)) {
597 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
598 return p + taglen + 1;
599 }
600 return NULL;
601}
602
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200603static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
604 const char *tag)
605
606{
607 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
608}
609
Sam Ravnborg93684d32006-02-19 11:53:35 +0100610/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100611 * Test if string s ends in string sub
612 * return 0 if match
613 **/
614static int strrcmp(const char *s, const char *sub)
615{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100616 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100617
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100618 if (!s || !sub)
619 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100620
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100621 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100622 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100623
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100624 if ((slen == 0) || (sublen == 0))
625 return 1;
626
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100627 if (sublen > slen)
628 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100629
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100630 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100631}
632
Sam Ravnborgff13f922008-01-23 19:54:27 +0100633static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
634{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100635 if (sym)
636 return elf->strtab + sym->st_name;
637 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100638 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100639}
640
641static const char *sec_name(struct elf_info *elf, int shndx)
642{
643 Elf_Shdr *sechdrs = elf->sechdrs;
644 return (void *)elf->hdr +
645 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
646 sechdrs[shndx].sh_name;
647}
648
649static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
650{
651 return (void *)elf->hdr +
652 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
653 sechdr->sh_name;
654}
655
Sam Ravnborg10668222008-01-13 22:21:31 +0100656/* if sym is empty or point to a string
657 * like ".[0-9]+" then return 1.
658 * This is the optional prefix added by ld to some sections
659 */
660static int number_prefix(const char *sym)
661{
662 if (*sym++ == '\0')
663 return 1;
664 if (*sym != '.')
665 return 0;
666 do {
667 char c = *sym++;
668 if (c < '0' || c > '9')
669 return 0;
670 } while (*sym);
671 return 1;
672}
673
674/* The pattern is an array of simple patterns.
675 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100676 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100677 * "foo*" will match a string that begins with "foo"
678 * "foo$" will match a string equal to "foo" or "foo.1"
679 * where the '1' can be any number including several digits.
680 * The $ syntax is for sections where ld append a dot number
681 * to make section name unique.
682 */
683int match(const char *sym, const char * const pat[])
684{
685 const char *p;
686 while (*pat) {
687 p = *pat++;
688 const char *endp = p + strlen(p) - 1;
689
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100690 /* "*foo" */
691 if (*p == '*') {
692 if (strrcmp(sym, p + 1) == 0)
693 return 1;
694 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100695 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100696 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100697 if (strncmp(sym, p, strlen(p) - 1) == 0)
698 return 1;
699 }
700 /* "foo$" */
701 else if (*endp == '$') {
702 if (strncmp(sym, p, strlen(p) - 1) == 0) {
703 if (number_prefix(sym + strlen(p) - 1))
704 return 1;
705 }
706 }
707 /* no wildcards */
708 else {
709 if (strcmp(p, sym) == 0)
710 return 1;
711 }
712 }
713 /* no match */
714 return 0;
715}
716
Sam Ravnborg10668222008-01-13 22:21:31 +0100717/* sections that we do not want to do full section mismatch check on */
718static const char *section_white_list[] =
Anders Kaseorgb614a692009-04-23 16:49:33 -0400719 { ".comment", ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
Sam Ravnborg10668222008-01-13 22:21:31 +0100720
Sam Ravnborge241a632008-01-28 20:13:13 +0100721/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400722 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100723 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400724 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100725 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400726static void check_section(const char *modname, struct elf_info *elf,
727 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100728{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400729 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100730
Anders Kaseorgb614a692009-04-23 16:49:33 -0400731 if (sechdr->sh_type == SHT_PROGBITS &&
732 !(sechdr->sh_flags & SHF_ALLOC) &&
733 !match(sec, section_white_list)) {
734 warn("%s (%s): unexpected non-allocatable section.\n"
735 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
736 "Note that for example <linux/init.h> contains\n"
737 "section definitions for use in .S files.\n\n",
738 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100739 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100740}
741
742
743
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100744#define ALL_INIT_DATA_SECTIONS \
745 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
746#define ALL_EXIT_DATA_SECTIONS \
747 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100748
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100749#define ALL_INIT_TEXT_SECTIONS \
750 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
751#define ALL_EXIT_TEXT_SECTIONS \
752 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100753
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100754#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
755#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100756
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100757#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100758#define TEXT_SECTIONS ".text$"
759
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100760#define INIT_SECTIONS ".init.data$", ".init.text$"
761#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
762#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
763#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
764
765#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
766#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
767#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
768#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
769
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100770/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100771static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100772
773/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100774static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100775
776/* All init and exit sections (code + data) */
777static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100778 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100779
780/* data section */
781static const char *data_sections[] = { DATA_SECTIONS, NULL };
782
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100783
784/* symbols in .data that may refer to init/exit sections */
785static const char *symbol_white_list[] =
786{
787 "*driver",
788 "*_template", /* scsi uses *_template a lot */
789 "*_timer", /* arm uses ops structures named _timer a lot */
790 "*_sht", /* scsi also used *_sht to some extent */
791 "*_ops",
792 "*_probe",
793 "*_probe_one",
794 "*_console",
795 NULL
796};
797
798static const char *head_sections[] = { ".head.text*", NULL };
799static const char *linker_symbols[] =
800 { "__init_begin", "_sinittext", "_einittext", NULL };
801
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100802enum mismatch {
803 NO_MISMATCH,
804 TEXT_TO_INIT,
805 DATA_TO_INIT,
806 TEXT_TO_EXIT,
807 DATA_TO_EXIT,
808 XXXINIT_TO_INIT,
809 XXXEXIT_TO_EXIT,
810 INIT_TO_EXIT,
811 EXIT_TO_INIT,
812 EXPORT_TO_INIT_EXIT,
813};
814
Sam Ravnborg10668222008-01-13 22:21:31 +0100815struct sectioncheck {
816 const char *fromsec[20];
817 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100818 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100819};
820
821const struct sectioncheck sectioncheck[] = {
822/* Do not reference init/exit code/data from
823 * normal code and data
824 */
825{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100826 .fromsec = { TEXT_SECTIONS, NULL },
827 .tosec = { ALL_INIT_SECTIONS, NULL },
828 .mismatch = TEXT_TO_INIT,
829},
830{
831 .fromsec = { DATA_SECTIONS, NULL },
832 .tosec = { ALL_INIT_SECTIONS, NULL },
833 .mismatch = DATA_TO_INIT,
834},
835{
836 .fromsec = { TEXT_SECTIONS, NULL },
837 .tosec = { ALL_EXIT_SECTIONS, NULL },
838 .mismatch = TEXT_TO_EXIT,
839},
840{
841 .fromsec = { DATA_SECTIONS, NULL },
842 .tosec = { ALL_EXIT_SECTIONS, NULL },
843 .mismatch = DATA_TO_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100844},
845/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
846{
847 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100848 .tosec = { INIT_SECTIONS, NULL },
849 .mismatch = XXXINIT_TO_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100850},
851/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
852{
853 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100854 .tosec = { EXIT_SECTIONS, NULL },
855 .mismatch = XXXEXIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100856},
857/* Do not use exit code/data from init code */
858{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100859 .fromsec = { ALL_INIT_SECTIONS, NULL },
860 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100861 .mismatch = INIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100862},
863/* Do not use init code/data from exit code */
864{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100865 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100866 .tosec = { ALL_INIT_SECTIONS, NULL },
867 .mismatch = EXIT_TO_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100868},
869/* Do not export init/exit functions or data */
870{
871 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +0100872 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100873 .mismatch = EXPORT_TO_INIT_EXIT
Sam Ravnborg10668222008-01-13 22:21:31 +0100874}
875};
876
877static int section_mismatch(const char *fromsec, const char *tosec)
878{
879 int i;
880 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
881 const struct sectioncheck *check = &sectioncheck[0];
882
883 for (i = 0; i < elems; i++) {
884 if (match(fromsec, check->fromsec) &&
885 match(tosec, check->tosec))
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100886 return check->mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100887 check++;
888 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100889 return NO_MISMATCH;
Sam Ravnborg10668222008-01-13 22:21:31 +0100890}
891
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100892/**
893 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200894 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100895 * Pattern 1:
896 * If a module parameter is declared __initdata and permissions=0
897 * then this is legal despite the warning generated.
898 * We cannot see value of permissions here, so just ignore
899 * this pattern.
900 * The pattern is identified by:
901 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100902 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100903 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100904 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100905 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700906 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100907 * add, remove, probe functions etc.
908 * These functions may often be marked __init and we do not want to
909 * warn here.
910 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200911 * tosec = init or exit section
912 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100913 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
914 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100915 *
916 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +0200917 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100918 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200919 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100920 * Some symbols belong to init section but still it is ok to reference
921 * these from non-init sections as these symbols don't have any memory
922 * allocated for them and symbol address and value are same. So even
923 * if init section is freed, its ok to reference those symbols.
924 * For ex. symbols marking the init section boundaries.
925 * This pattern is identified by
926 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100927 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100928 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100929static int secref_whitelist(const char *fromsec, const char *fromsym,
930 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100931{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100932 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100933 if (match(tosec, init_data_sections) &&
934 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100935 (strncmp(fromsym, "__param", strlen("__param")) == 0))
936 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100937
938 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100939 if (match(tosec, init_exit_sections) &&
940 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100941 match(fromsym, symbol_white_list))
942 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100943
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100944 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100945 if (match(fromsec, head_sections) &&
946 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100947 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100948
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200949 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100950 if (match(tosym, linker_symbols))
951 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100952
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100953 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100954}
955
956/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100957 * Find symbol based on relocation record info.
958 * In some cases the symbol supplied is a valid symbol so
959 * return refsym. If st_name != 0 we assume this is a valid symbol.
960 * In other cases the symbol needs to be looked up in the symbol table
961 * based on section and address.
962 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100963static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100964 Elf_Sym *relsym)
965{
966 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100967 Elf_Sym *near = NULL;
968 Elf64_Sword distance = 20;
969 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100970
971 if (relsym->st_name != 0)
972 return relsym;
973 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
974 if (sym->st_shndx != relsym->st_shndx)
975 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900976 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
977 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100978 if (sym->st_value == addr)
979 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100980 /* Find a symbol nearby - addr are maybe negative */
981 d = sym->st_value - addr;
982 if (d < 0)
983 d = addr - sym->st_value;
984 if (d < distance) {
985 distance = d;
986 near = sym;
987 }
Sam Ravnborg93684d32006-02-19 11:53:35 +0100988 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100989 /* We need a close match */
990 if (distance < 20)
991 return near;
992 else
993 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100994}
995
David Brownellda68d612007-02-20 13:58:16 -0800996static inline int is_arm_mapping_symbol(const char *str)
997{
998 return str[0] == '$' && strchr("atd", str[1])
999 && (str[2] == '\0' || str[2] == '.');
1000}
1001
1002/*
1003 * If there's no name there, ignore it; likewise, ignore it if it's
1004 * one of the magic symbols emitted used by current ARM tools.
1005 *
1006 * Otherwise if find_symbols_between() returns those symbols, they'll
1007 * fail the whitelist tests and cause lots of false alarms ... fixable
1008 * only by merging __exit and __init sections into __text, bloating
1009 * the kernel (which is especially evil on embedded platforms).
1010 */
1011static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1012{
1013 const char *name = elf->strtab + sym->st_name;
1014
1015 if (!name || !strlen(name))
1016 return 0;
1017 return !is_arm_mapping_symbol(name);
1018}
1019
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001020/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001021 * Find symbols before or equal addr and after addr - in the section sec.
1022 * If we find two symbols with equal offset prefer one with a valid name.
1023 * The ELF format may have a better way to detect what type of symbol
1024 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001025 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001026static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1027 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001028{
1029 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001030 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001031 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001032
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001033 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1034 const char *symsec;
1035
1036 if (sym->st_shndx >= SHN_LORESERVE)
1037 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001038 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001039 if (strcmp(symsec, sec) != 0)
1040 continue;
David Brownellda68d612007-02-20 13:58:16 -08001041 if (!is_valid_name(elf, sym))
1042 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001043 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001044 if ((addr - sym->st_value) < distance) {
1045 distance = addr - sym->st_value;
1046 near = sym;
1047 } else if ((addr - sym->st_value) == distance) {
1048 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001049 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001050 }
1051 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001052 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001053}
1054
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001055/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001056 * Convert a section name to the function/data attribute
1057 * .init.text => __init
1058 * .cpuinit.data => __cpudata
1059 * .memexitconst => __memconst
1060 * etc.
1061*/
1062static char *sec2annotation(const char *s)
1063{
1064 if (match(s, init_exit_sections)) {
1065 char *p = malloc(20);
1066 char *r = p;
1067
1068 *p++ = '_';
1069 *p++ = '_';
1070 if (*s == '.')
1071 s++;
1072 while (*s && *s != '.')
1073 *p++ = *s++;
1074 *p = '\0';
1075 if (*s == '.')
1076 s++;
1077 if (strstr(s, "rodata") != NULL)
1078 strcat(p, "const ");
1079 else if (strstr(s, "data") != NULL)
1080 strcat(p, "data ");
1081 else
1082 strcat(p, " ");
1083 return r; /* we leak her but we do not care */
1084 } else {
1085 return "";
1086 }
1087}
1088
1089static int is_function(Elf_Sym *sym)
1090{
1091 if (sym)
1092 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1093 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001094 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001095}
1096
1097/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001098 * Print a warning about a section mismatch.
1099 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001100 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001101 */
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001102static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001103 const char *fromsec,
1104 unsigned long long fromaddr,
1105 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001106 int from_is_func,
1107 const char *tosec, const char *tosym,
1108 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001109{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001110 const char *from, *from_p;
1111 const char *to, *to_p;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001112
1113 switch (from_is_func) {
1114 case 0: from = "variable"; from_p = ""; break;
1115 case 1: from = "function"; from_p = "()"; break;
1116 default: from = "(unknown reference)"; from_p = ""; break;
1117 }
1118 switch (to_is_func) {
1119 case 0: to = "variable"; to_p = ""; break;
1120 case 1: to = "function"; to_p = "()"; break;
1121 default: to = "(unknown reference)"; to_p = ""; break;
1122 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001123
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001124 sec_mismatch_count++;
1125 if (!sec_mismatch_verbose)
1126 return;
1127
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001128 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1129 "to the %s %s:%s%s\n",
1130 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1131 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001132
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001133 switch (mismatch) {
1134 case TEXT_TO_INIT:
1135 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001136 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001137 "the %s %s%s%s.\n"
1138 "This is often because %s lacks a %s\n"
1139 "annotation or the annotation of %s is wrong.\n",
1140 sec2annotation(fromsec), fromsym,
1141 to, sec2annotation(tosec), tosym, to_p,
1142 fromsym, sec2annotation(tosec), tosym);
1143 break;
1144 case DATA_TO_INIT: {
1145 const char **s = symbol_white_list;
1146 fprintf(stderr,
1147 "The variable %s references\n"
1148 "the %s %s%s%s\n"
1149 "If the reference is valid then annotate the\n"
1150 "variable with __init* (see linux/init.h) "
1151 "or name the variable:\n",
1152 fromsym, to, sec2annotation(tosec), tosym, to_p);
1153 while (*s)
1154 fprintf(stderr, "%s, ", *s++);
1155 fprintf(stderr, "\n");
1156 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001157 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001158 case TEXT_TO_EXIT:
1159 fprintf(stderr,
1160 "The function %s() references a %s in an exit section.\n"
1161 "Often the %s %s%s has valid usage outside the exit section\n"
1162 "and the fix is to remove the %sannotation of %s.\n",
1163 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1164 break;
1165 case DATA_TO_EXIT: {
1166 const char **s = symbol_white_list;
1167 fprintf(stderr,
1168 "The variable %s references\n"
1169 "the %s %s%s%s\n"
1170 "If the reference is valid then annotate the\n"
1171 "variable with __exit* (see linux/init.h) or "
1172 "name the variable:\n",
1173 fromsym, to, sec2annotation(tosec), tosym, to_p);
1174 while (*s)
1175 fprintf(stderr, "%s, ", *s++);
1176 fprintf(stderr, "\n");
1177 break;
1178 }
1179 case XXXINIT_TO_INIT:
1180 case XXXEXIT_TO_EXIT:
1181 fprintf(stderr,
1182 "The %s %s%s%s references\n"
1183 "a %s %s%s%s.\n"
1184 "If %s is only used by %s then\n"
1185 "annotate %s with a matching annotation.\n",
1186 from, sec2annotation(fromsec), fromsym, from_p,
1187 to, sec2annotation(tosec), tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001188 tosym, fromsym, tosym);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001189 break;
1190 case INIT_TO_EXIT:
1191 fprintf(stderr,
1192 "The %s %s%s%s references\n"
1193 "a %s %s%s%s.\n"
1194 "This is often seen when error handling "
1195 "in the init function\n"
1196 "uses functionality in the exit path.\n"
1197 "The fix is often to remove the %sannotation of\n"
1198 "%s%s so it may be used outside an exit section.\n",
1199 from, sec2annotation(fromsec), fromsym, from_p,
1200 to, sec2annotation(tosec), tosym, to_p,
1201 sec2annotation(tosec), tosym, to_p);
1202 break;
1203 case EXIT_TO_INIT:
1204 fprintf(stderr,
1205 "The %s %s%s%s references\n"
1206 "a %s %s%s%s.\n"
1207 "This is often seen when error handling "
1208 "in the exit function\n"
1209 "uses functionality in the init path.\n"
1210 "The fix is often to remove the %sannotation of\n"
1211 "%s%s so it may be used outside an init section.\n",
1212 from, sec2annotation(fromsec), fromsym, from_p,
1213 to, sec2annotation(tosec), tosym, to_p,
1214 sec2annotation(tosec), tosym, to_p);
1215 break;
1216 case EXPORT_TO_INIT_EXIT:
1217 fprintf(stderr,
1218 "The symbol %s is exported and annotated %s\n"
1219 "Fix this by removing the %sannotation of %s "
1220 "or drop the export.\n",
1221 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1222 case NO_MISMATCH:
1223 /* To get warnings on missing members */
1224 break;
1225 }
1226 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001227}
1228
1229static void check_section_mismatch(const char *modname, struct elf_info *elf,
1230 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1231{
1232 const char *tosec;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001233 enum mismatch mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001234
1235 tosec = sec_name(elf, sym->st_shndx);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001236 mismatch = section_mismatch(fromsec, tosec);
1237 if (mismatch != NO_MISMATCH) {
1238 Elf_Sym *to;
1239 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001240 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001241 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001242
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001243 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1244 fromsym = sym_name(elf, from);
1245 to = find_elf_symbol(elf, r->r_addend, sym);
1246 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001247
1248 /* check whitelist - we may ignore it */
1249 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001250 report_sec_mismatch(modname, mismatch,
1251 fromsec, r->r_offset, fromsym,
1252 is_function(from), tosec, tosym,
1253 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001254 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001255 }
1256}
1257
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001258static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001259 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001260{
1261 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001262 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001263
1264 return (void *)elf->hdr + sechdrs[section].sh_offset +
1265 (r->r_offset - sechdrs[section].sh_addr);
1266}
1267
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001268static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001269{
1270 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001271 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001272
1273 switch (r_typ) {
1274 case R_386_32:
1275 r->r_addend = TO_NATIVE(*location);
1276 break;
1277 case R_386_PC32:
1278 r->r_addend = TO_NATIVE(*location) + 4;
1279 /* For CONFIG_RELOCATABLE=y */
1280 if (elf->hdr->e_type == ET_EXEC)
1281 r->r_addend += r->r_offset;
1282 break;
1283 }
1284 return 0;
1285}
1286
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001287static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001288{
1289 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1290
1291 switch (r_typ) {
1292 case R_ARM_ABS32:
1293 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001294 r->r_addend = (int)(long)
1295 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001296 break;
1297 case R_ARM_PC24:
1298 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001299 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001300 sechdr->sh_offset +
1301 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001302 break;
1303 default:
1304 return 1;
1305 }
1306 return 0;
1307}
1308
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001309static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001310{
1311 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001312 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001313 unsigned int inst;
1314
1315 if (r_typ == R_MIPS_HI16)
1316 return 1; /* skip this */
1317 inst = TO_NATIVE(*location);
1318 switch (r_typ) {
1319 case R_MIPS_LO16:
1320 r->r_addend = inst & 0xffff;
1321 break;
1322 case R_MIPS_26:
1323 r->r_addend = (inst & 0x03ffffff) << 2;
1324 break;
1325 case R_MIPS_32:
1326 r->r_addend = inst;
1327 break;
1328 }
1329 return 0;
1330}
1331
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001332static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001333 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001334{
1335 Elf_Sym *sym;
1336 Elf_Rela *rela;
1337 Elf_Rela r;
1338 unsigned int r_sym;
1339 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001340
Sam Ravnborgff13f922008-01-23 19:54:27 +01001341 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001342 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1343
Sam Ravnborgff13f922008-01-23 19:54:27 +01001344 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001345 fromsec += strlen(".rela");
1346 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001347 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001348 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001349
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001350 for (rela = start; rela < stop; rela++) {
1351 r.r_offset = TO_NATIVE(rela->r_offset);
1352#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001353 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001354 unsigned int r_typ;
1355 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1356 r_sym = TO_NATIVE(r_sym);
1357 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1358 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1359 } else {
1360 r.r_info = TO_NATIVE(rela->r_info);
1361 r_sym = ELF_R_SYM(r.r_info);
1362 }
1363#else
1364 r.r_info = TO_NATIVE(rela->r_info);
1365 r_sym = ELF_R_SYM(r.r_info);
1366#endif
1367 r.r_addend = TO_NATIVE(rela->r_addend);
1368 sym = elf->symtab_start + r_sym;
1369 /* Skip special sections */
1370 if (sym->st_shndx >= SHN_LORESERVE)
1371 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001372 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001373 }
1374}
1375
1376static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001377 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001378{
1379 Elf_Sym *sym;
1380 Elf_Rel *rel;
1381 Elf_Rela r;
1382 unsigned int r_sym;
1383 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001384
Sam Ravnborgff13f922008-01-23 19:54:27 +01001385 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001386 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1387
Sam Ravnborgff13f922008-01-23 19:54:27 +01001388 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001389 fromsec += strlen(".rel");
1390 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001391 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001392 return;
1393
1394 for (rel = start; rel < stop; rel++) {
1395 r.r_offset = TO_NATIVE(rel->r_offset);
1396#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001397 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001398 unsigned int r_typ;
1399 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1400 r_sym = TO_NATIVE(r_sym);
1401 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1402 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1403 } else {
1404 r.r_info = TO_NATIVE(rel->r_info);
1405 r_sym = ELF_R_SYM(r.r_info);
1406 }
1407#else
1408 r.r_info = TO_NATIVE(rel->r_info);
1409 r_sym = ELF_R_SYM(r.r_info);
1410#endif
1411 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001412 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001413 case EM_386:
1414 if (addend_386_rel(elf, sechdr, &r))
1415 continue;
1416 break;
1417 case EM_ARM:
1418 if (addend_arm_rel(elf, sechdr, &r))
1419 continue;
1420 break;
1421 case EM_MIPS:
1422 if (addend_mips_rel(elf, sechdr, &r))
1423 continue;
1424 break;
1425 }
1426 sym = elf->symtab_start + r_sym;
1427 /* Skip special sections */
1428 if (sym->st_shndx >= SHN_LORESERVE)
1429 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001430 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001431 }
1432}
1433
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001434/**
1435 * A module includes a number of sections that are discarded
1436 * either when loaded or when used as built-in.
1437 * For loaded modules all functions marked __init and all data
1438 * marked __initdata will be discarded when the module has been intialized.
1439 * Likewise for modules used built-in the sections marked __exit
1440 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001441 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001442 * The check_sec_ref() function traverses all relocation records
1443 * to find all references to a section that reference a section that will
1444 * be discarded and warns about it.
1445 **/
1446static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001447 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001448{
1449 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001450 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001451
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001452 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001453 for (i = 0; i < elf->hdr->e_shnum; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001454 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001455 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001456 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001457 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001458 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001459 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001460 }
1461}
1462
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001463static void get_markers(struct elf_info *info, struct module *mod)
1464{
1465 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1466 const char *strings = (const char *) info->hdr + sh->sh_offset;
1467 const Elf_Sym *sym, *first_sym, *last_sym;
1468 size_t n;
1469
1470 if (!info->markers_strings_sec)
1471 return;
1472
1473 /*
1474 * First count the strings. We look for all the symbols defined
1475 * in the __markers_strings section named __mstrtab_*. For
1476 * these local names, the compiler puts a random .NNN suffix on,
1477 * so the names don't correspond exactly.
1478 */
1479 first_sym = last_sym = NULL;
1480 n = 0;
1481 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1482 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1483 sym->st_shndx == info->markers_strings_sec &&
1484 !strncmp(info->strtab + sym->st_name,
1485 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1486 if (first_sym == NULL)
1487 first_sym = sym;
1488 last_sym = sym;
1489 ++n;
1490 }
1491
1492 if (n == 0)
1493 return;
1494
1495 /*
1496 * Now collect each name and format into a line for the output.
1497 * Lines look like:
1498 * marker_name vmlinux marker %s format %d
1499 * The format string after the second \t can use whitespace.
1500 */
1501 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1502 mod->nmarkers = n;
1503
1504 n = 0;
1505 for (sym = first_sym; sym <= last_sym; sym++)
1506 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1507 sym->st_shndx == info->markers_strings_sec &&
1508 !strncmp(info->strtab + sym->st_name,
1509 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1510 const char *name = strings + sym->st_value;
1511 const char *fmt = strchr(name, '\0') + 1;
1512 char *line = NULL;
1513 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1514 NOFAIL(line);
1515 mod->markers[n++] = line;
1516 }
1517}
1518
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001519static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 const char *symname;
1522 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001523 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct module *mod;
1525 struct elf_info info = { };
1526 Elf_Sym *sym;
1527
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001528 if (!parse_elf(&info, modname))
1529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 mod = new_module(modname);
1532
1533 /* When there's no vmlinux, don't print warnings about
1534 * unresolved symbols (since there'll be too many ;) */
1535 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 mod->skip = 1;
1538 }
1539
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001540 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001541 if (info.modinfo && !license && !is_vmlinux(modname))
1542 warn("modpost: missing MODULE_LICENSE() in %s\n"
1543 "see include/linux/module.h for "
1544 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001545 while (license) {
1546 if (license_is_gpl_compatible(license))
1547 mod->gpl_compatible = 1;
1548 else {
1549 mod->gpl_compatible = 0;
1550 break;
1551 }
1552 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1553 "license", license);
1554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1557 symname = info.strtab + sym->st_name;
1558
1559 handle_modversions(mod, &info, sym, symname);
1560 handle_moddevtable(mod, &info, sym, symname);
1561 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001562 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001563 (is_vmlinux(modname) && vmlinux_section_warnings))
1564 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1567 if (version)
1568 maybe_frob_rcs_version(modname, version, info.modinfo,
1569 version - (char *)info.hdr);
1570 if (version || (all_versions && !is_vmlinux(modname)))
1571 get_src_version(modname, mod->srcversion,
1572 sizeof(mod->srcversion)-1);
1573
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001574 get_markers(&info, mod);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 parse_elf_finish(&info);
1577
Rusty Russell8c8ef422009-03-31 13:05:34 -06001578 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * never passed as an argument to an exported function, so
1580 * the automatic versioning doesn't pick it up, but it's really
1581 * important anyhow */
1582 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001583 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586#define SZ 500
1587
1588/* We first write the generated file into memory using the
1589 * following helper, then compare to the file on disk and
1590 * only update the later if anything changed */
1591
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001592void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1593 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 char tmp[SZ];
1596 int len;
1597 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 va_start(ap, fmt);
1600 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001601 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 va_end(ap);
1603}
1604
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001605void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001608 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 buf->p = realloc(buf->p, buf->size);
1610 }
1611 strncpy(buf->p + buf->pos, s, len);
1612 buf->pos += len;
1613}
1614
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001615static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1616{
1617 const char *e = is_vmlinux(m) ?"":".ko";
1618
1619 switch (exp) {
1620 case export_gpl:
1621 fatal("modpost: GPL-incompatible module %s%s "
1622 "uses GPL-only symbol '%s'\n", m, e, s);
1623 break;
1624 case export_unused_gpl:
1625 fatal("modpost: GPL-incompatible module %s%s "
1626 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1627 break;
1628 case export_gpl_future:
1629 warn("modpost: GPL-incompatible module %s%s "
1630 "uses future GPL-only symbol '%s'\n", m, e, s);
1631 break;
1632 case export_plain:
1633 case export_unused:
1634 case export_unknown:
1635 /* ignore */
1636 break;
1637 }
1638}
1639
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001640static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001641{
1642 const char *e = is_vmlinux(m) ?"":".ko";
1643
1644 switch (exp) {
1645 case export_unused:
1646 case export_unused_gpl:
1647 warn("modpost: module %s%s "
1648 "uses symbol '%s' marked UNUSED\n", m, e, s);
1649 break;
1650 default:
1651 /* ignore */
1652 break;
1653 }
1654}
1655
1656static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001657{
1658 struct symbol *s, *exp;
1659
1660 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001661 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001662 exp = find_symbol(s->name);
1663 if (!exp || exp->module == mod)
1664 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001665 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001666 if (basename)
1667 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001668 else
1669 basename = mod->name;
1670 if (!mod->gpl_compatible)
1671 check_for_gpl_usage(exp->export, basename, exp->name);
1672 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001673 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001674}
1675
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001676/**
1677 * Header for the generated file
1678 **/
1679static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 buf_printf(b, "#include <linux/module.h>\n");
1682 buf_printf(b, "#include <linux/vermagic.h>\n");
1683 buf_printf(b, "#include <linux/compiler.h>\n");
1684 buf_printf(b, "\n");
1685 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1686 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 buf_printf(b, "struct module __this_module\n");
1688 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001689 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (mod->has_init)
1691 buf_printf(b, " .init = init_module,\n");
1692 if (mod->has_cleanup)
1693 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1694 " .exit = cleanup_module,\n"
1695 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001696 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 buf_printf(b, "};\n");
1698}
1699
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001700void add_staging_flag(struct buffer *b, const char *name)
1701{
1702 static const char *staging_dir = "drivers/staging";
1703
1704 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1705 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1706}
1707
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001708/**
1709 * Record CRCs for unresolved symbols
1710 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001711static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001714 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 for (s = mod->unres; s; s = s->next) {
1717 exp = find_symbol(s->name);
1718 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001719 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001720 if (warn_unresolved) {
1721 warn("\"%s\" [%s.ko] undefined!\n",
1722 s->name, mod->name);
1723 } else {
1724 merror("\"%s\" [%s.ko] undefined!\n",
1725 s->name, mod->name);
1726 err = 1;
1727 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 continue;
1730 }
1731 s->module = exp->module;
1732 s->crc_valid = exp->crc_valid;
1733 s->crc = exp->crc;
1734 }
1735
1736 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 buf_printf(b, "\n");
1740 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001741 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1743
1744 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001745 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001748 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 s->name, mod->name);
1750 continue;
1751 }
1752 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1753 }
1754
1755 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001756
1757 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001760static void add_depends(struct buffer *b, struct module *mod,
1761 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct symbol *s;
1764 struct module *m;
1765 int first = 1;
1766
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001767 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 buf_printf(b, "\n");
1771 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001772 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1774 buf_printf(b, "\"depends=");
1775 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001776 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!s->module)
1778 continue;
1779
1780 if (s->module->seen)
1781 continue;
1782
1783 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001784 p = strrchr(s->module->name, '/');
1785 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001786 p++;
1787 else
1788 p = s->module->name;
1789 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 first = 0;
1791 }
1792 buf_printf(b, "\";\n");
1793}
1794
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001795static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 if (mod->srcversion[0]) {
1798 buf_printf(b, "\n");
1799 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1800 mod->srcversion);
1801 }
1802}
1803
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001804static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 char *tmp;
1807 FILE *file;
1808 struct stat st;
1809
1810 file = fopen(fname, "r");
1811 if (!file)
1812 goto write;
1813
1814 if (fstat(fileno(file), &st) < 0)
1815 goto close_write;
1816
1817 if (st.st_size != b->pos)
1818 goto close_write;
1819
1820 tmp = NOFAIL(malloc(b->pos));
1821 if (fread(tmp, 1, b->pos, file) != b->pos)
1822 goto free_write;
1823
1824 if (memcmp(tmp, b->p, b->pos) != 0)
1825 goto free_write;
1826
1827 free(tmp);
1828 fclose(file);
1829 return;
1830
1831 free_write:
1832 free(tmp);
1833 close_write:
1834 fclose(file);
1835 write:
1836 file = fopen(fname, "w");
1837 if (!file) {
1838 perror(fname);
1839 exit(1);
1840 }
1841 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1842 perror(fname);
1843 exit(1);
1844 }
1845 fclose(file);
1846}
1847
Ram Paibd5cbce2006-06-08 22:12:53 -07001848/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001849 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001850 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001851static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 unsigned long size, pos = 0;
1854 void *file = grab_file(fname, &size);
1855 char *line;
1856
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001857 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* No symbol versions, silently ignore */
1859 return;
1860
1861 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001862 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned int crc;
1864 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001865 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 if (!(symname = strchr(line, '\t')))
1868 goto fail;
1869 *symname++ = '\0';
1870 if (!(modname = strchr(symname, '\t')))
1871 goto fail;
1872 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001873 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001874 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001875 if (export && ((end = strchr(export, '\t')) != NULL))
1876 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 crc = strtoul(line, &d, 16);
1878 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1879 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001880 mod = find_module(modname);
1881 if (!mod) {
1882 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00001884 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 mod->skip = 1;
1886 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001887 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001888 s->kernel = kernel;
1889 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001890 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892 return;
1893fail:
1894 fatal("parse error in symbol dump file\n");
1895}
1896
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001897/* For normal builds always dump all symbols.
1898 * For external modules only dump symbols
1899 * that are not read from kernel Module.symvers.
1900 **/
1901static int dump_sym(struct symbol *sym)
1902{
1903 if (!external_module)
1904 return 1;
1905 if (sym->vmlinux || sym->kernel)
1906 return 0;
1907 return 1;
1908}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001909
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001910static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct buffer buf = { };
1913 struct symbol *symbol;
1914 int n;
1915
1916 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1917 symbol = symbolhash[n];
1918 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001919 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001920 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001921 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001922 symbol->module->name,
1923 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 symbol = symbol->next;
1925 }
1926 }
1927 write_if_changed(&buf, fname);
1928}
1929
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001930static void add_marker(struct module *mod, const char *name, const char *fmt)
1931{
1932 char *line = NULL;
1933 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1934 NOFAIL(line);
1935
1936 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1937 sizeof mod->markers[0])));
1938 mod->markers[mod->nmarkers++] = line;
1939}
1940
1941static void read_markers(const char *fname)
1942{
1943 unsigned long size, pos = 0;
1944 void *file = grab_file(fname, &size);
1945 char *line;
1946
1947 if (!file) /* No old markers, silently ignore */
1948 return;
1949
1950 while ((line = get_next_line(&pos, file, size))) {
1951 char *marker, *modname, *fmt;
1952 struct module *mod;
1953
1954 marker = line;
1955 modname = strchr(marker, '\t');
1956 if (!modname)
1957 goto fail;
1958 *modname++ = '\0';
1959 fmt = strchr(modname, '\t');
1960 if (!fmt)
1961 goto fail;
1962 *fmt++ = '\0';
1963 if (*marker == '\0' || *modname == '\0')
1964 goto fail;
1965
1966 mod = find_module(modname);
1967 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00001968 mod = new_module(modname);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001969 mod->skip = 1;
1970 }
Mathieu Desnoyers87f3b6b2008-10-06 09:30:12 -04001971 if (is_vmlinux(modname)) {
1972 have_vmlinux = 1;
1973 mod->skip = 0;
1974 }
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001975
Mathieu Desnoyersd35cb362008-07-21 14:21:38 -07001976 if (!mod->skip)
1977 add_marker(mod, marker, fmt);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001978 }
Cedric Hombourger99e3a1e2009-04-25 09:38:21 +02001979 release_file(file, size);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001980 return;
1981fail:
1982 fatal("parse error in markers list file\n");
1983}
1984
1985static int compare_strings(const void *a, const void *b)
1986{
1987 return strcmp(*(const char **) a, *(const char **) b);
1988}
1989
1990static void write_markers(const char *fname)
1991{
1992 struct buffer buf = { };
1993 struct module *mod;
1994 size_t i;
1995
1996 for (mod = modules; mod; mod = mod->next)
1997 if ((!external_module || !mod->skip) && mod->markers != NULL) {
1998 /*
1999 * Sort the strings so we can skip duplicates when
2000 * we write them out.
2001 */
2002 qsort(mod->markers, mod->nmarkers,
2003 sizeof mod->markers[0], &compare_strings);
2004 for (i = 0; i < mod->nmarkers; ++i) {
2005 char *line = mod->markers[i];
2006 buf_write(&buf, line, strlen(line));
2007 while (i + 1 < mod->nmarkers &&
2008 !strcmp(mod->markers[i],
2009 mod->markers[i + 1]))
2010 free(mod->markers[i++]);
2011 free(mod->markers[i]);
2012 }
2013 free(mod->markers);
2014 mod->markers = NULL;
2015 }
2016
2017 write_if_changed(&buf, fname);
2018}
2019
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002020struct ext_sym_list {
2021 struct ext_sym_list *next;
2022 const char *file;
2023};
2024
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002025int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct module *mod;
2028 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002029 char *kernel_read = NULL, *module_read = NULL;
2030 char *dump_write = NULL;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002031 char *markers_read = NULL;
2032 char *markers_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002034 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002035 struct ext_sym_list *extsym_iter;
2036 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002038 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002039 switch (opt) {
2040 case 'i':
2041 kernel_read = optarg;
2042 break;
2043 case 'I':
2044 module_read = optarg;
2045 external_module = 1;
2046 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002047 case 'c':
2048 cross_build = 1;
2049 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002050 case 'e':
2051 external_module = 1;
2052 extsym_iter =
2053 NOFAIL(malloc(sizeof(*extsym_iter)));
2054 extsym_iter->next = extsym_start;
2055 extsym_iter->file = optarg;
2056 extsym_start = extsym_iter;
2057 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002058 case 'm':
2059 modversions = 1;
2060 break;
2061 case 'o':
2062 dump_write = optarg;
2063 break;
2064 case 'a':
2065 all_versions = 1;
2066 break;
2067 case 's':
2068 vmlinux_section_warnings = 0;
2069 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002070 case 'S':
2071 sec_mismatch_verbose = 0;
2072 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002073 case 'w':
2074 warn_unresolved = 1;
2075 break;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002076 case 'M':
2077 markers_write = optarg;
2078 break;
2079 case 'K':
2080 markers_read = optarg;
2081 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002082 default:
2083 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 }
2086
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002087 if (kernel_read)
2088 read_dump(kernel_read, 1);
2089 if (module_read)
2090 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002091 while (extsym_start) {
2092 read_dump(extsym_start->file, 0);
2093 extsym_iter = extsym_start->next;
2094 free(extsym_start);
2095 extsym_start = extsym_iter;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002098 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 for (mod = modules; mod; mod = mod->next) {
2102 if (mod->skip)
2103 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002104 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002105 }
2106
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002107 err = 0;
2108
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002109 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002110 char fname[strlen(mod->name) + 10];
2111
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002112 if (mod->skip)
2113 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 buf.pos = 0;
2116
2117 add_header(&buf, mod);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002118 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002119 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 add_depends(&buf, mod, modules);
2121 add_moddevtable(&buf, mod);
2122 add_srcversion(&buf, mod);
2123
2124 sprintf(fname, "%s.mod.c", mod->name);
2125 write_if_changed(&buf, fname);
2126 }
2127
2128 if (dump_write)
2129 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002130 if (sec_mismatch_count && !sec_mismatch_verbose)
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01002131 warn("modpost: Found %d section mismatch(es).\n"
2132 "To see full details build your kernel with:\n"
2133 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2134 sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002136 if (markers_read)
2137 read_markers(markers_read);
2138
2139 if (markers_write)
2140 write_markers(markers_write);
2141
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}