blob: f8b42ab0724be194a258a00e4e24f814ad3e7475 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100419 if (sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100420 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
421 "sizeof(*hrd)=%zu\n", filename,
422 (unsigned long)sechdrs[i].sh_offset,
423 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100424 return 0;
425 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700426 secname = secstrings + sechdrs[i].sh_name;
427 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
429 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700430 } else if (strcmp(secname, "__ksymtab") == 0)
431 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200432 else if (strcmp(secname, "__ksymtab_unused") == 0)
433 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700434 else if (strcmp(secname, "__ksymtab_gpl") == 0)
435 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200436 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
437 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700438 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
439 info->export_gpl_future_sec = i;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -0800440 else if (strcmp(secname, "__markers_strings") == 0)
441 info->markers_strings_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (sechdrs[i].sh_type != SHT_SYMTAB)
444 continue;
445
446 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100447 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100449 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 sechdrs[sechdrs[i].sh_link].sh_offset;
451 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100452 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100453 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* Fix endianness in symbols */
456 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
457 sym->st_shndx = TO_NATIVE(sym->st_shndx);
458 sym->st_name = TO_NATIVE(sym->st_name);
459 sym->st_value = TO_NATIVE(sym->st_value);
460 sym->st_size = TO_NATIVE(sym->st_size);
461 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100462 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100465static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 release_file(info->hdr, info->size);
468}
469
Luke Yangf7b05e62006-03-02 18:35:31 +0800470#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
471#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100473static void handle_modversions(struct module *mod, struct elf_info *info,
474 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700477 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 switch (sym->st_shndx) {
480 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100481 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case SHN_ABS:
484 /* CRC'd symbol */
485 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
486 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700487 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
488 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 break;
491 case SHN_UNDEF:
492 /* undefined symbol */
493 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
494 ELF_ST_BIND(sym->st_info) != STB_WEAK)
495 break;
496 /* ignore global offset table */
497 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
498 break;
499 /* ignore __this_module, it will be resolved shortly */
500 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
501 break;
Ben Colline8d529012005-08-19 13:44:57 -0700502/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
503#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
504/* add compatibility with older glibc */
505#ifndef STT_SPARC_REGISTER
506#define STT_SPARC_REGISTER STT_REGISTER
507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (info->hdr->e_machine == EM_SPARC ||
509 info->hdr->e_machine == EM_SPARCV9) {
510 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700511 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100513 if (symname[0] == '.') {
514 char *munged = strdup(symname);
515 munged[0] = '_';
516 munged[1] = toupper(munged[1]);
517 symname = munged;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100523 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
524 mod->unres =
525 alloc_symbol(symname +
526 strlen(MODULE_SYMBOL_PREFIX),
527 ELF_ST_BIND(sym->st_info) == STB_WEAK,
528 mod->unres);
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 default:
532 /* All exported symbols */
533 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700534 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
535 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
538 mod->has_init = 1;
539 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
540 mod->has_cleanup = 1;
541 break;
542 }
543}
544
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100545/**
546 * Parse tag=value strings from .modinfo section
547 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static char *next_string(char *string, unsigned long *secsize)
549{
550 /* Skip non-zero chars */
551 while (string[0]) {
552 string++;
553 if ((*secsize)-- <= 1)
554 return NULL;
555 }
556
557 /* Skip any zero padding. */
558 while (!string[0]) {
559 string++;
560 if ((*secsize)-- <= 1)
561 return NULL;
562 }
563 return string;
564}
565
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200566static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
567 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 char *p;
570 unsigned int taglen = strlen(tag);
571 unsigned long size = modinfo_len;
572
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200573 if (info) {
574 size -= info - (char *)modinfo;
575 modinfo = next_string(info, &size);
576 }
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 for (p = modinfo; p; p = next_string(p, &size)) {
579 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
580 return p + taglen + 1;
581 }
582 return NULL;
583}
584
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200585static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
586 const char *tag)
587
588{
589 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
590}
591
Sam Ravnborg93684d32006-02-19 11:53:35 +0100592/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100593 * Test if string s ends in string sub
594 * return 0 if match
595 **/
596static int strrcmp(const char *s, const char *sub)
597{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100598 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100599
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100600 if (!s || !sub)
601 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100602
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100603 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100604 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100605
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100606 if ((slen == 0) || (sublen == 0))
607 return 1;
608
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100609 if (sublen > slen)
610 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100611
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100612 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100613}
614
Sam Ravnborgff13f922008-01-23 19:54:27 +0100615static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
616{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100617 if (sym)
618 return elf->strtab + sym->st_name;
619 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100620 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100621}
622
623static const char *sec_name(struct elf_info *elf, int shndx)
624{
625 Elf_Shdr *sechdrs = elf->sechdrs;
626 return (void *)elf->hdr +
627 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
628 sechdrs[shndx].sh_name;
629}
630
631static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
632{
633 return (void *)elf->hdr +
634 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
635 sechdr->sh_name;
636}
637
Sam Ravnborg10668222008-01-13 22:21:31 +0100638/* if sym is empty or point to a string
639 * like ".[0-9]+" then return 1.
640 * This is the optional prefix added by ld to some sections
641 */
642static int number_prefix(const char *sym)
643{
644 if (*sym++ == '\0')
645 return 1;
646 if (*sym != '.')
647 return 0;
648 do {
649 char c = *sym++;
650 if (c < '0' || c > '9')
651 return 0;
652 } while (*sym);
653 return 1;
654}
655
656/* The pattern is an array of simple patterns.
657 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100658 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100659 * "foo*" will match a string that begins with "foo"
660 * "foo$" will match a string equal to "foo" or "foo.1"
661 * where the '1' can be any number including several digits.
662 * The $ syntax is for sections where ld append a dot number
663 * to make section name unique.
664 */
665int match(const char *sym, const char * const pat[])
666{
667 const char *p;
668 while (*pat) {
669 p = *pat++;
670 const char *endp = p + strlen(p) - 1;
671
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100672 /* "*foo" */
673 if (*p == '*') {
674 if (strrcmp(sym, p + 1) == 0)
675 return 1;
676 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100677 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100678 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100679 if (strncmp(sym, p, strlen(p) - 1) == 0)
680 return 1;
681 }
682 /* "foo$" */
683 else if (*endp == '$') {
684 if (strncmp(sym, p, strlen(p) - 1) == 0) {
685 if (number_prefix(sym + strlen(p) - 1))
686 return 1;
687 }
688 }
689 /* no wildcards */
690 else {
691 if (strcmp(p, sym) == 0)
692 return 1;
693 }
694 }
695 /* no match */
696 return 0;
697}
698
Sam Ravnborg10668222008-01-13 22:21:31 +0100699/* sections that we do not want to do full section mismatch check on */
700static const char *section_white_list[] =
701 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
702
Sam Ravnborge241a632008-01-28 20:13:13 +0100703/*
704 * Is this section one we do not want to check?
705 * This is often debug sections.
706 * If we are going to check this section then
707 * test if section name ends with a dot and a number.
708 * This is used to find sections where the linker have
709 * appended a dot-number to make the name unique.
710 * The cause of this is often a section specified in assembler
711 * without "ax" / "aw" and the same section used in .c
712 * code where gcc add these.
713 */
714static int check_section(const char *modname, const char *sec)
715{
716 const char *e = sec + strlen(sec) - 1;
717 if (match(sec, section_white_list))
718 return 1;
719
720 if (*e && isdigit(*e)) {
721 /* consume all digits */
722 while (*e && e != sec && isdigit(*e))
723 e--;
724 if (*e == '.') {
725 warn("%s (%s): unexpected section name.\n"
726 "The (.[number]+) following section name are "
727 "ld generated and not expected.\n"
728 "Did you forget to use \"ax\"/\"aw\" "
729 "in a .S file?\n"
730 "Note that for example <linux/init.h> contains\n"
731 "section definitions for use in .S files.\n\n",
732 modname, sec);
733 }
734 }
735 return 0;
736}
737
738
739
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100740#define ALL_INIT_DATA_SECTIONS \
741 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
742#define ALL_EXIT_DATA_SECTIONS \
743 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100744
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100745#define ALL_INIT_TEXT_SECTIONS \
746 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
747#define ALL_EXIT_TEXT_SECTIONS \
748 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100749
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100750#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
751#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100752
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100753#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100754#define TEXT_SECTIONS ".text$"
755
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100756#define INIT_SECTIONS ".init.data$", ".init.text$"
757#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
758#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
759#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
760
761#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
762#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
763#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
764#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
765
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100766/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100767static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100768
769/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100770static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100771
772/* All init and exit sections (code + data) */
773static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100774 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100775
776/* data section */
777static const char *data_sections[] = { DATA_SECTIONS, NULL };
778
779/* sections that may refer to an init/exit section with no warning */
780static const char *initref_sections[] =
781{
782 ".text.init.refok*",
783 ".exit.text.refok*",
784 ".data.init.refok*",
785 NULL
786};
787
788
789/* symbols in .data that may refer to init/exit sections */
790static const char *symbol_white_list[] =
791{
792 "*driver",
793 "*_template", /* scsi uses *_template a lot */
794 "*_timer", /* arm uses ops structures named _timer a lot */
795 "*_sht", /* scsi also used *_sht to some extent */
796 "*_ops",
797 "*_probe",
798 "*_probe_one",
799 "*_console",
800 NULL
801};
802
803static const char *head_sections[] = { ".head.text*", NULL };
804static const char *linker_symbols[] =
805 { "__init_begin", "_sinittext", "_einittext", NULL };
806
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100807enum mismatch {
808 NO_MISMATCH,
809 TEXT_TO_INIT,
810 DATA_TO_INIT,
811 TEXT_TO_EXIT,
812 DATA_TO_EXIT,
813 XXXINIT_TO_INIT,
814 XXXEXIT_TO_EXIT,
815 INIT_TO_EXIT,
816 EXIT_TO_INIT,
817 EXPORT_TO_INIT_EXIT,
818};
819
Sam Ravnborg10668222008-01-13 22:21:31 +0100820struct sectioncheck {
821 const char *fromsec[20];
822 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100823 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100824};
825
826const struct sectioncheck sectioncheck[] = {
827/* Do not reference init/exit code/data from
828 * normal code and data
829 */
830{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100831 .fromsec = { TEXT_SECTIONS, NULL },
832 .tosec = { ALL_INIT_SECTIONS, NULL },
833 .mismatch = TEXT_TO_INIT,
834},
835{
836 .fromsec = { DATA_SECTIONS, NULL },
837 .tosec = { ALL_INIT_SECTIONS, NULL },
838 .mismatch = DATA_TO_INIT,
839},
840{
841 .fromsec = { TEXT_SECTIONS, NULL },
842 .tosec = { ALL_EXIT_SECTIONS, NULL },
843 .mismatch = TEXT_TO_EXIT,
844},
845{
846 .fromsec = { DATA_SECTIONS, NULL },
847 .tosec = { ALL_EXIT_SECTIONS, NULL },
848 .mismatch = DATA_TO_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100849},
850/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
851{
852 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100853 .tosec = { INIT_SECTIONS, NULL },
854 .mismatch = XXXINIT_TO_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100855},
856/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
857{
858 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100859 .tosec = { EXIT_SECTIONS, NULL },
860 .mismatch = XXXEXIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100861},
862/* Do not use exit code/data from init code */
863{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100864 .fromsec = { ALL_INIT_SECTIONS, NULL },
865 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100866 .mismatch = INIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100867},
868/* Do not use init code/data from exit code */
869{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100870 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100871 .tosec = { ALL_INIT_SECTIONS, NULL },
872 .mismatch = EXIT_TO_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100873},
874/* Do not export init/exit functions or data */
875{
876 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +0100877 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100878 .mismatch = EXPORT_TO_INIT_EXIT
Sam Ravnborg10668222008-01-13 22:21:31 +0100879}
880};
881
882static int section_mismatch(const char *fromsec, const char *tosec)
883{
884 int i;
885 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
886 const struct sectioncheck *check = &sectioncheck[0];
887
888 for (i = 0; i < elems; i++) {
889 if (match(fromsec, check->fromsec) &&
890 match(tosec, check->tosec))
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100891 return check->mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100892 check++;
893 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100894 return NO_MISMATCH;
Sam Ravnborg10668222008-01-13 22:21:31 +0100895}
896
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100897/**
898 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200899 *
900 * Pattern 0:
901 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
902 * The pattern is identified by:
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200903 * fromsec = .text.init.refok* | .data.init.refok*
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200904 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100905 * Pattern 1:
906 * If a module parameter is declared __initdata and permissions=0
907 * then this is legal despite the warning generated.
908 * We cannot see value of permissions here, so just ignore
909 * this pattern.
910 * The pattern is identified by:
911 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100912 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100913 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100914 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100915 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700916 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100917 * add, remove, probe functions etc.
918 * These functions may often be marked __init and we do not want to
919 * warn here.
920 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200921 * tosec = init or exit section
922 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100923 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
924 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100925 *
926 * Pattern 3:
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100927 * Whitelist all refereces from .text.head to .init.data
928 * Whitelist all refereces from .text.head to .init.text
929 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200930 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100931 * Some symbols belong to init section but still it is ok to reference
932 * these from non-init sections as these symbols don't have any memory
933 * allocated for them and symbol address and value are same. So even
934 * if init section is freed, its ok to reference those symbols.
935 * For ex. symbols marking the init section boundaries.
936 * This pattern is identified by
937 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100938 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100939 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100940static int secref_whitelist(const char *fromsec, const char *fromsym,
941 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100942{
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200943 /* Check for pattern 0 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100944 if (match(fromsec, initref_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100945 return 0;
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200946
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100947 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100948 if (match(tosec, init_data_sections) &&
949 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100950 (strncmp(fromsym, "__param", strlen("__param")) == 0))
951 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100952
953 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100954 if (match(tosec, init_exit_sections) &&
955 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100956 match(fromsym, symbol_white_list))
957 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100958
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100959 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100960 if (match(fromsec, head_sections) &&
961 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100962 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100963
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200964 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100965 if (match(tosym, linker_symbols))
966 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100967
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100968 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100969}
970
971/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100972 * Find symbol based on relocation record info.
973 * In some cases the symbol supplied is a valid symbol so
974 * return refsym. If st_name != 0 we assume this is a valid symbol.
975 * In other cases the symbol needs to be looked up in the symbol table
976 * based on section and address.
977 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100978static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100979 Elf_Sym *relsym)
980{
981 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100982 Elf_Sym *near = NULL;
983 Elf64_Sword distance = 20;
984 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100985
986 if (relsym->st_name != 0)
987 return relsym;
988 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
989 if (sym->st_shndx != relsym->st_shndx)
990 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900991 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
992 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100993 if (sym->st_value == addr)
994 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100995 /* Find a symbol nearby - addr are maybe negative */
996 d = sym->st_value - addr;
997 if (d < 0)
998 d = addr - sym->st_value;
999 if (d < distance) {
1000 distance = d;
1001 near = sym;
1002 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001003 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001004 /* We need a close match */
1005 if (distance < 20)
1006 return near;
1007 else
1008 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001009}
1010
David Brownellda68d612007-02-20 13:58:16 -08001011static inline int is_arm_mapping_symbol(const char *str)
1012{
1013 return str[0] == '$' && strchr("atd", str[1])
1014 && (str[2] == '\0' || str[2] == '.');
1015}
1016
1017/*
1018 * If there's no name there, ignore it; likewise, ignore it if it's
1019 * one of the magic symbols emitted used by current ARM tools.
1020 *
1021 * Otherwise if find_symbols_between() returns those symbols, they'll
1022 * fail the whitelist tests and cause lots of false alarms ... fixable
1023 * only by merging __exit and __init sections into __text, bloating
1024 * the kernel (which is especially evil on embedded platforms).
1025 */
1026static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1027{
1028 const char *name = elf->strtab + sym->st_name;
1029
1030 if (!name || !strlen(name))
1031 return 0;
1032 return !is_arm_mapping_symbol(name);
1033}
1034
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001035/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001036 * Find symbols before or equal addr and after addr - in the section sec.
1037 * If we find two symbols with equal offset prefer one with a valid name.
1038 * The ELF format may have a better way to detect what type of symbol
1039 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001040 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001041static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1042 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001043{
1044 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001045 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001046 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001047
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001048 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1049 const char *symsec;
1050
1051 if (sym->st_shndx >= SHN_LORESERVE)
1052 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001053 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001054 if (strcmp(symsec, sec) != 0)
1055 continue;
David Brownellda68d612007-02-20 13:58:16 -08001056 if (!is_valid_name(elf, sym))
1057 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001058 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001059 if ((addr - sym->st_value) < distance) {
1060 distance = addr - sym->st_value;
1061 near = sym;
1062 } else if ((addr - sym->st_value) == distance) {
1063 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001064 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001065 }
1066 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001067 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001068}
1069
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001070/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001071 * Convert a section name to the function/data attribute
1072 * .init.text => __init
1073 * .cpuinit.data => __cpudata
1074 * .memexitconst => __memconst
1075 * etc.
1076*/
1077static char *sec2annotation(const char *s)
1078{
1079 if (match(s, init_exit_sections)) {
1080 char *p = malloc(20);
1081 char *r = p;
1082
1083 *p++ = '_';
1084 *p++ = '_';
1085 if (*s == '.')
1086 s++;
1087 while (*s && *s != '.')
1088 *p++ = *s++;
1089 *p = '\0';
1090 if (*s == '.')
1091 s++;
1092 if (strstr(s, "rodata") != NULL)
1093 strcat(p, "const ");
1094 else if (strstr(s, "data") != NULL)
1095 strcat(p, "data ");
1096 else
1097 strcat(p, " ");
1098 return r; /* we leak her but we do not care */
1099 } else {
1100 return "";
1101 }
1102}
1103
1104static int is_function(Elf_Sym *sym)
1105{
1106 if (sym)
1107 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1108 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001109 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001110}
1111
1112/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001113 * Print a warning about a section mismatch.
1114 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001115 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001116 */
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001117static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001118 const char *fromsec,
1119 unsigned long long fromaddr,
1120 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001121 int from_is_func,
1122 const char *tosec, const char *tosym,
1123 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001124{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001125 const char *from, *from_p;
1126 const char *to, *to_p;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001127
1128 switch (from_is_func) {
1129 case 0: from = "variable"; from_p = ""; break;
1130 case 1: from = "function"; from_p = "()"; break;
1131 default: from = "(unknown reference)"; from_p = ""; break;
1132 }
1133 switch (to_is_func) {
1134 case 0: to = "variable"; to_p = ""; break;
1135 case 1: to = "function"; to_p = "()"; break;
1136 default: to = "(unknown reference)"; to_p = ""; break;
1137 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001138
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001139 sec_mismatch_count++;
1140 if (!sec_mismatch_verbose)
1141 return;
1142
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001143 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1144 "to the %s %s:%s%s\n",
1145 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1146 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001147
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001148 switch (mismatch) {
1149 case TEXT_TO_INIT:
1150 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001151 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001152 "the %s %s%s%s.\n"
1153 "This is often because %s lacks a %s\n"
1154 "annotation or the annotation of %s is wrong.\n",
1155 sec2annotation(fromsec), fromsym,
1156 to, sec2annotation(tosec), tosym, to_p,
1157 fromsym, sec2annotation(tosec), tosym);
1158 break;
1159 case DATA_TO_INIT: {
1160 const char **s = symbol_white_list;
1161 fprintf(stderr,
1162 "The variable %s references\n"
1163 "the %s %s%s%s\n"
1164 "If the reference is valid then annotate the\n"
1165 "variable with __init* (see linux/init.h) "
1166 "or name the variable:\n",
1167 fromsym, to, sec2annotation(tosec), tosym, to_p);
1168 while (*s)
1169 fprintf(stderr, "%s, ", *s++);
1170 fprintf(stderr, "\n");
1171 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001172 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001173 case TEXT_TO_EXIT:
1174 fprintf(stderr,
1175 "The function %s() references a %s in an exit section.\n"
1176 "Often the %s %s%s has valid usage outside the exit section\n"
1177 "and the fix is to remove the %sannotation of %s.\n",
1178 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1179 break;
1180 case DATA_TO_EXIT: {
1181 const char **s = symbol_white_list;
1182 fprintf(stderr,
1183 "The variable %s references\n"
1184 "the %s %s%s%s\n"
1185 "If the reference is valid then annotate the\n"
1186 "variable with __exit* (see linux/init.h) or "
1187 "name the variable:\n",
1188 fromsym, to, sec2annotation(tosec), tosym, to_p);
1189 while (*s)
1190 fprintf(stderr, "%s, ", *s++);
1191 fprintf(stderr, "\n");
1192 break;
1193 }
1194 case XXXINIT_TO_INIT:
1195 case XXXEXIT_TO_EXIT:
1196 fprintf(stderr,
1197 "The %s %s%s%s references\n"
1198 "a %s %s%s%s.\n"
1199 "If %s is only used by %s then\n"
1200 "annotate %s with a matching annotation.\n",
1201 from, sec2annotation(fromsec), fromsym, from_p,
1202 to, sec2annotation(tosec), tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001203 tosym, fromsym, tosym);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001204 break;
1205 case INIT_TO_EXIT:
1206 fprintf(stderr,
1207 "The %s %s%s%s references\n"
1208 "a %s %s%s%s.\n"
1209 "This is often seen when error handling "
1210 "in the init function\n"
1211 "uses functionality in the exit path.\n"
1212 "The fix is often to remove the %sannotation of\n"
1213 "%s%s so it may be used outside an exit section.\n",
1214 from, sec2annotation(fromsec), fromsym, from_p,
1215 to, sec2annotation(tosec), tosym, to_p,
1216 sec2annotation(tosec), tosym, to_p);
1217 break;
1218 case EXIT_TO_INIT:
1219 fprintf(stderr,
1220 "The %s %s%s%s references\n"
1221 "a %s %s%s%s.\n"
1222 "This is often seen when error handling "
1223 "in the exit function\n"
1224 "uses functionality in the init path.\n"
1225 "The fix is often to remove the %sannotation of\n"
1226 "%s%s so it may be used outside an init section.\n",
1227 from, sec2annotation(fromsec), fromsym, from_p,
1228 to, sec2annotation(tosec), tosym, to_p,
1229 sec2annotation(tosec), tosym, to_p);
1230 break;
1231 case EXPORT_TO_INIT_EXIT:
1232 fprintf(stderr,
1233 "The symbol %s is exported and annotated %s\n"
1234 "Fix this by removing the %sannotation of %s "
1235 "or drop the export.\n",
1236 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1237 case NO_MISMATCH:
1238 /* To get warnings on missing members */
1239 break;
1240 }
1241 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001242}
1243
1244static void check_section_mismatch(const char *modname, struct elf_info *elf,
1245 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1246{
1247 const char *tosec;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001248 enum mismatch mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001249
1250 tosec = sec_name(elf, sym->st_shndx);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001251 mismatch = section_mismatch(fromsec, tosec);
1252 if (mismatch != NO_MISMATCH) {
1253 Elf_Sym *to;
1254 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001255 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001256 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001257
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001258 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1259 fromsym = sym_name(elf, from);
1260 to = find_elf_symbol(elf, r->r_addend, sym);
1261 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001262
1263 /* check whitelist - we may ignore it */
1264 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001265 report_sec_mismatch(modname, mismatch,
1266 fromsec, r->r_offset, fromsym,
1267 is_function(from), tosec, tosym,
1268 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001269 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001270 }
1271}
1272
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001273static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001274 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001275{
1276 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001277 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001278
1279 return (void *)elf->hdr + sechdrs[section].sh_offset +
1280 (r->r_offset - sechdrs[section].sh_addr);
1281}
1282
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001283static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001284{
1285 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001286 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001287
1288 switch (r_typ) {
1289 case R_386_32:
1290 r->r_addend = TO_NATIVE(*location);
1291 break;
1292 case R_386_PC32:
1293 r->r_addend = TO_NATIVE(*location) + 4;
1294 /* For CONFIG_RELOCATABLE=y */
1295 if (elf->hdr->e_type == ET_EXEC)
1296 r->r_addend += r->r_offset;
1297 break;
1298 }
1299 return 0;
1300}
1301
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001302static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001303{
1304 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1305
1306 switch (r_typ) {
1307 case R_ARM_ABS32:
1308 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001309 r->r_addend = (int)(long)
1310 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001311 break;
1312 case R_ARM_PC24:
1313 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001314 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001315 sechdr->sh_offset +
1316 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001317 break;
1318 default:
1319 return 1;
1320 }
1321 return 0;
1322}
1323
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001324static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001325{
1326 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001327 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001328 unsigned int inst;
1329
1330 if (r_typ == R_MIPS_HI16)
1331 return 1; /* skip this */
1332 inst = TO_NATIVE(*location);
1333 switch (r_typ) {
1334 case R_MIPS_LO16:
1335 r->r_addend = inst & 0xffff;
1336 break;
1337 case R_MIPS_26:
1338 r->r_addend = (inst & 0x03ffffff) << 2;
1339 break;
1340 case R_MIPS_32:
1341 r->r_addend = inst;
1342 break;
1343 }
1344 return 0;
1345}
1346
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001347static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001348 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001349{
1350 Elf_Sym *sym;
1351 Elf_Rela *rela;
1352 Elf_Rela r;
1353 unsigned int r_sym;
1354 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001355
Sam Ravnborgff13f922008-01-23 19:54:27 +01001356 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001357 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1358
Sam Ravnborgff13f922008-01-23 19:54:27 +01001359 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001360 fromsec += strlen(".rela");
1361 /* if from section (name) is know good then skip it */
Sam Ravnborge241a632008-01-28 20:13:13 +01001362 if (check_section(modname, fromsec))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001363 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001364
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001365 for (rela = start; rela < stop; rela++) {
1366 r.r_offset = TO_NATIVE(rela->r_offset);
1367#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001368 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001369 unsigned int r_typ;
1370 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1371 r_sym = TO_NATIVE(r_sym);
1372 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1373 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1374 } else {
1375 r.r_info = TO_NATIVE(rela->r_info);
1376 r_sym = ELF_R_SYM(r.r_info);
1377 }
1378#else
1379 r.r_info = TO_NATIVE(rela->r_info);
1380 r_sym = ELF_R_SYM(r.r_info);
1381#endif
1382 r.r_addend = TO_NATIVE(rela->r_addend);
1383 sym = elf->symtab_start + r_sym;
1384 /* Skip special sections */
1385 if (sym->st_shndx >= SHN_LORESERVE)
1386 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001387 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001388 }
1389}
1390
1391static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001392 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001393{
1394 Elf_Sym *sym;
1395 Elf_Rel *rel;
1396 Elf_Rela r;
1397 unsigned int r_sym;
1398 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001399
Sam Ravnborgff13f922008-01-23 19:54:27 +01001400 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001401 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1402
Sam Ravnborgff13f922008-01-23 19:54:27 +01001403 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001404 fromsec += strlen(".rel");
1405 /* if from section (name) is know good then skip it */
Sam Ravnborge241a632008-01-28 20:13:13 +01001406 if (check_section(modname, fromsec))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001407 return;
1408
1409 for (rel = start; rel < stop; rel++) {
1410 r.r_offset = TO_NATIVE(rel->r_offset);
1411#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001412 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001413 unsigned int r_typ;
1414 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1415 r_sym = TO_NATIVE(r_sym);
1416 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1417 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1418 } else {
1419 r.r_info = TO_NATIVE(rel->r_info);
1420 r_sym = ELF_R_SYM(r.r_info);
1421 }
1422#else
1423 r.r_info = TO_NATIVE(rel->r_info);
1424 r_sym = ELF_R_SYM(r.r_info);
1425#endif
1426 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001427 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001428 case EM_386:
1429 if (addend_386_rel(elf, sechdr, &r))
1430 continue;
1431 break;
1432 case EM_ARM:
1433 if (addend_arm_rel(elf, sechdr, &r))
1434 continue;
1435 break;
1436 case EM_MIPS:
1437 if (addend_mips_rel(elf, sechdr, &r))
1438 continue;
1439 break;
1440 }
1441 sym = elf->symtab_start + r_sym;
1442 /* Skip special sections */
1443 if (sym->st_shndx >= SHN_LORESERVE)
1444 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001445 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001446 }
1447}
1448
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001449/**
1450 * A module includes a number of sections that are discarded
1451 * either when loaded or when used as built-in.
1452 * For loaded modules all functions marked __init and all data
1453 * marked __initdata will be discarded when the module has been intialized.
1454 * Likewise for modules used built-in the sections marked __exit
1455 * are discarded because __exit marked function are supposed to be called
1456 * only when a moduel is unloaded which never happes for built-in modules.
1457 * The check_sec_ref() function traverses all relocation records
1458 * to find all references to a section that reference a section that will
1459 * be discarded and warns about it.
1460 **/
1461static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001462 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001463{
1464 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001465 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001466
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001467 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001468 for (i = 0; i < elf->hdr->e_shnum; i++) {
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001469 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001470 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001471 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001472 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001473 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001474 }
1475}
1476
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001477static void get_markers(struct elf_info *info, struct module *mod)
1478{
1479 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1480 const char *strings = (const char *) info->hdr + sh->sh_offset;
1481 const Elf_Sym *sym, *first_sym, *last_sym;
1482 size_t n;
1483
1484 if (!info->markers_strings_sec)
1485 return;
1486
1487 /*
1488 * First count the strings. We look for all the symbols defined
1489 * in the __markers_strings section named __mstrtab_*. For
1490 * these local names, the compiler puts a random .NNN suffix on,
1491 * so the names don't correspond exactly.
1492 */
1493 first_sym = last_sym = NULL;
1494 n = 0;
1495 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1496 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1497 sym->st_shndx == info->markers_strings_sec &&
1498 !strncmp(info->strtab + sym->st_name,
1499 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1500 if (first_sym == NULL)
1501 first_sym = sym;
1502 last_sym = sym;
1503 ++n;
1504 }
1505
1506 if (n == 0)
1507 return;
1508
1509 /*
1510 * Now collect each name and format into a line for the output.
1511 * Lines look like:
1512 * marker_name vmlinux marker %s format %d
1513 * The format string after the second \t can use whitespace.
1514 */
1515 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1516 mod->nmarkers = n;
1517
1518 n = 0;
1519 for (sym = first_sym; sym <= last_sym; sym++)
1520 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1521 sym->st_shndx == info->markers_strings_sec &&
1522 !strncmp(info->strtab + sym->st_name,
1523 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1524 const char *name = strings + sym->st_value;
1525 const char *fmt = strchr(name, '\0') + 1;
1526 char *line = NULL;
1527 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1528 NOFAIL(line);
1529 mod->markers[n++] = line;
1530 }
1531}
1532
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001533static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 const char *symname;
1536 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001537 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct module *mod;
1539 struct elf_info info = { };
1540 Elf_Sym *sym;
1541
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001542 if (!parse_elf(&info, modname))
1543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 mod = new_module(modname);
1546
1547 /* When there's no vmlinux, don't print warnings about
1548 * unresolved symbols (since there'll be too many ;) */
1549 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 mod->skip = 1;
1552 }
1553
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001554 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborgeed7d272008-04-25 20:13:30 +02001555 if (!license && !is_vmlinux(modname))
1556 fatal("modpost: missing MODULE_LICENSE() in %s\n"
1557 "see include/linux/module.h for "
1558 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001559 while (license) {
1560 if (license_is_gpl_compatible(license))
1561 mod->gpl_compatible = 1;
1562 else {
1563 mod->gpl_compatible = 0;
1564 break;
1565 }
1566 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1567 "license", license);
1568 }
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1571 symname = info.strtab + sym->st_name;
1572
1573 handle_modversions(mod, &info, sym, symname);
1574 handle_moddevtable(mod, &info, sym, symname);
1575 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001576 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001577 (is_vmlinux(modname) && vmlinux_section_warnings))
1578 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1581 if (version)
1582 maybe_frob_rcs_version(modname, version, info.modinfo,
1583 version - (char *)info.hdr);
1584 if (version || (all_versions && !is_vmlinux(modname)))
1585 get_src_version(modname, mod->srcversion,
1586 sizeof(mod->srcversion)-1);
1587
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001588 get_markers(&info, mod);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 parse_elf_finish(&info);
1591
1592 /* Our trick to get versioning for struct_module - it's
1593 * never passed as an argument to an exported function, so
1594 * the automatic versioning doesn't pick it up, but it's really
1595 * important anyhow */
1596 if (modversions)
1597 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1598}
1599
1600#define SZ 500
1601
1602/* We first write the generated file into memory using the
1603 * following helper, then compare to the file on disk and
1604 * only update the later if anything changed */
1605
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001606void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1607 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 char tmp[SZ];
1610 int len;
1611 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 va_start(ap, fmt);
1614 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001615 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 va_end(ap);
1617}
1618
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001619void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001622 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 buf->p = realloc(buf->p, buf->size);
1624 }
1625 strncpy(buf->p + buf->pos, s, len);
1626 buf->pos += len;
1627}
1628
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001629static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1630{
1631 const char *e = is_vmlinux(m) ?"":".ko";
1632
1633 switch (exp) {
1634 case export_gpl:
1635 fatal("modpost: GPL-incompatible module %s%s "
1636 "uses GPL-only symbol '%s'\n", m, e, s);
1637 break;
1638 case export_unused_gpl:
1639 fatal("modpost: GPL-incompatible module %s%s "
1640 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1641 break;
1642 case export_gpl_future:
1643 warn("modpost: GPL-incompatible module %s%s "
1644 "uses future GPL-only symbol '%s'\n", m, e, s);
1645 break;
1646 case export_plain:
1647 case export_unused:
1648 case export_unknown:
1649 /* ignore */
1650 break;
1651 }
1652}
1653
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001654static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001655{
1656 const char *e = is_vmlinux(m) ?"":".ko";
1657
1658 switch (exp) {
1659 case export_unused:
1660 case export_unused_gpl:
1661 warn("modpost: module %s%s "
1662 "uses symbol '%s' marked UNUSED\n", m, e, s);
1663 break;
1664 default:
1665 /* ignore */
1666 break;
1667 }
1668}
1669
1670static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001671{
1672 struct symbol *s, *exp;
1673
1674 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001675 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001676 exp = find_symbol(s->name);
1677 if (!exp || exp->module == mod)
1678 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001679 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001680 if (basename)
1681 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001682 else
1683 basename = mod->name;
1684 if (!mod->gpl_compatible)
1685 check_for_gpl_usage(exp->export, basename, exp->name);
1686 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001687 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001688}
1689
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001690/**
1691 * Header for the generated file
1692 **/
1693static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 buf_printf(b, "#include <linux/module.h>\n");
1696 buf_printf(b, "#include <linux/vermagic.h>\n");
1697 buf_printf(b, "#include <linux/compiler.h>\n");
1698 buf_printf(b, "\n");
1699 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1700 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 buf_printf(b, "struct module __this_module\n");
1702 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001703 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (mod->has_init)
1705 buf_printf(b, " .init = init_module,\n");
1706 if (mod->has_cleanup)
1707 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1708 " .exit = cleanup_module,\n"
1709 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001710 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 buf_printf(b, "};\n");
1712}
1713
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001714/**
1715 * Record CRCs for unresolved symbols
1716 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001717static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001720 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 for (s = mod->unres; s; s = s->next) {
1723 exp = find_symbol(s->name);
1724 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001725 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001726 if (warn_unresolved) {
1727 warn("\"%s\" [%s.ko] undefined!\n",
1728 s->name, mod->name);
1729 } else {
1730 merror("\"%s\" [%s.ko] undefined!\n",
1731 s->name, mod->name);
1732 err = 1;
1733 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 continue;
1736 }
1737 s->module = exp->module;
1738 s->crc_valid = exp->crc_valid;
1739 s->crc = exp->crc;
1740 }
1741
1742 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 buf_printf(b, "\n");
1746 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001747 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1749
1750 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001751 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001754 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 s->name, mod->name);
1756 continue;
1757 }
1758 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1759 }
1760
1761 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001762
1763 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001766static void add_depends(struct buffer *b, struct module *mod,
1767 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 struct symbol *s;
1770 struct module *m;
1771 int first = 1;
1772
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001773 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 buf_printf(b, "\n");
1777 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001778 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1780 buf_printf(b, "\"depends=");
1781 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001782 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!s->module)
1784 continue;
1785
1786 if (s->module->seen)
1787 continue;
1788
1789 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001790 p = strrchr(s->module->name, '/');
1791 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001792 p++;
1793 else
1794 p = s->module->name;
1795 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 first = 0;
1797 }
1798 buf_printf(b, "\";\n");
1799}
1800
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001801static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 if (mod->srcversion[0]) {
1804 buf_printf(b, "\n");
1805 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1806 mod->srcversion);
1807 }
1808}
1809
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001810static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 char *tmp;
1813 FILE *file;
1814 struct stat st;
1815
1816 file = fopen(fname, "r");
1817 if (!file)
1818 goto write;
1819
1820 if (fstat(fileno(file), &st) < 0)
1821 goto close_write;
1822
1823 if (st.st_size != b->pos)
1824 goto close_write;
1825
1826 tmp = NOFAIL(malloc(b->pos));
1827 if (fread(tmp, 1, b->pos, file) != b->pos)
1828 goto free_write;
1829
1830 if (memcmp(tmp, b->p, b->pos) != 0)
1831 goto free_write;
1832
1833 free(tmp);
1834 fclose(file);
1835 return;
1836
1837 free_write:
1838 free(tmp);
1839 close_write:
1840 fclose(file);
1841 write:
1842 file = fopen(fname, "w");
1843 if (!file) {
1844 perror(fname);
1845 exit(1);
1846 }
1847 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1848 perror(fname);
1849 exit(1);
1850 }
1851 fclose(file);
1852}
1853
Ram Paibd5cbce2006-06-08 22:12:53 -07001854/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001855 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001856 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001857static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 unsigned long size, pos = 0;
1860 void *file = grab_file(fname, &size);
1861 char *line;
1862
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001863 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* No symbol versions, silently ignore */
1865 return;
1866
1867 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001868 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 unsigned int crc;
1870 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001871 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 if (!(symname = strchr(line, '\t')))
1874 goto fail;
1875 *symname++ = '\0';
1876 if (!(modname = strchr(symname, '\t')))
1877 goto fail;
1878 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001879 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001880 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001881 if (export && ((end = strchr(export, '\t')) != NULL))
1882 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 crc = strtoul(line, &d, 16);
1884 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1885 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001886 mod = find_module(modname);
1887 if (!mod) {
1888 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 mod = new_module(NOFAIL(strdup(modname)));
1891 mod->skip = 1;
1892 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001893 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001894 s->kernel = kernel;
1895 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001896 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 return;
1899fail:
1900 fatal("parse error in symbol dump file\n");
1901}
1902
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001903/* For normal builds always dump all symbols.
1904 * For external modules only dump symbols
1905 * that are not read from kernel Module.symvers.
1906 **/
1907static int dump_sym(struct symbol *sym)
1908{
1909 if (!external_module)
1910 return 1;
1911 if (sym->vmlinux || sym->kernel)
1912 return 0;
1913 return 1;
1914}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001915
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001916static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct buffer buf = { };
1919 struct symbol *symbol;
1920 int n;
1921
1922 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1923 symbol = symbolhash[n];
1924 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001925 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001926 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001927 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001928 symbol->module->name,
1929 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 symbol = symbol->next;
1931 }
1932 }
1933 write_if_changed(&buf, fname);
1934}
1935
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001936static void add_marker(struct module *mod, const char *name, const char *fmt)
1937{
1938 char *line = NULL;
1939 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1940 NOFAIL(line);
1941
1942 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1943 sizeof mod->markers[0])));
1944 mod->markers[mod->nmarkers++] = line;
1945}
1946
1947static void read_markers(const char *fname)
1948{
1949 unsigned long size, pos = 0;
1950 void *file = grab_file(fname, &size);
1951 char *line;
1952
1953 if (!file) /* No old markers, silently ignore */
1954 return;
1955
1956 while ((line = get_next_line(&pos, file, size))) {
1957 char *marker, *modname, *fmt;
1958 struct module *mod;
1959
1960 marker = line;
1961 modname = strchr(marker, '\t');
1962 if (!modname)
1963 goto fail;
1964 *modname++ = '\0';
1965 fmt = strchr(modname, '\t');
1966 if (!fmt)
1967 goto fail;
1968 *fmt++ = '\0';
1969 if (*marker == '\0' || *modname == '\0')
1970 goto fail;
1971
1972 mod = find_module(modname);
1973 if (!mod) {
1974 if (is_vmlinux(modname))
1975 have_vmlinux = 1;
1976 mod = new_module(NOFAIL(strdup(modname)));
1977 mod->skip = 1;
1978 }
1979
1980 add_marker(mod, marker, fmt);
1981 }
1982 return;
1983fail:
1984 fatal("parse error in markers list file\n");
1985}
1986
1987static int compare_strings(const void *a, const void *b)
1988{
1989 return strcmp(*(const char **) a, *(const char **) b);
1990}
1991
1992static void write_markers(const char *fname)
1993{
1994 struct buffer buf = { };
1995 struct module *mod;
1996 size_t i;
1997
1998 for (mod = modules; mod; mod = mod->next)
1999 if ((!external_module || !mod->skip) && mod->markers != NULL) {
2000 /*
2001 * Sort the strings so we can skip duplicates when
2002 * we write them out.
2003 */
2004 qsort(mod->markers, mod->nmarkers,
2005 sizeof mod->markers[0], &compare_strings);
2006 for (i = 0; i < mod->nmarkers; ++i) {
2007 char *line = mod->markers[i];
2008 buf_write(&buf, line, strlen(line));
2009 while (i + 1 < mod->nmarkers &&
2010 !strcmp(mod->markers[i],
2011 mod->markers[i + 1]))
2012 free(mod->markers[i++]);
2013 free(mod->markers[i]);
2014 }
2015 free(mod->markers);
2016 mod->markers = NULL;
2017 }
2018
2019 write_if_changed(&buf, fname);
2020}
2021
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002022struct ext_sym_list {
2023 struct ext_sym_list *next;
2024 const char *file;
2025};
2026
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002027int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct module *mod;
2030 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002031 char *kernel_read = NULL, *module_read = NULL;
2032 char *dump_write = NULL;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002033 char *markers_read = NULL;
2034 char *markers_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002036 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002037 struct ext_sym_list *extsym_iter;
2038 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002040 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002041 switch (opt) {
2042 case 'i':
2043 kernel_read = optarg;
2044 break;
2045 case 'I':
2046 module_read = optarg;
2047 external_module = 1;
2048 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002049 case 'c':
2050 cross_build = 1;
2051 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002052 case 'e':
2053 external_module = 1;
2054 extsym_iter =
2055 NOFAIL(malloc(sizeof(*extsym_iter)));
2056 extsym_iter->next = extsym_start;
2057 extsym_iter->file = optarg;
2058 extsym_start = extsym_iter;
2059 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002060 case 'm':
2061 modversions = 1;
2062 break;
2063 case 'o':
2064 dump_write = optarg;
2065 break;
2066 case 'a':
2067 all_versions = 1;
2068 break;
2069 case 's':
2070 vmlinux_section_warnings = 0;
2071 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002072 case 'S':
2073 sec_mismatch_verbose = 0;
2074 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002075 case 'w':
2076 warn_unresolved = 1;
2077 break;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002078 case 'M':
2079 markers_write = optarg;
2080 break;
2081 case 'K':
2082 markers_read = optarg;
2083 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002084 default:
2085 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 }
2088
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002089 if (kernel_read)
2090 read_dump(kernel_read, 1);
2091 if (module_read)
2092 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002093 while (extsym_start) {
2094 read_dump(extsym_start->file, 0);
2095 extsym_iter = extsym_start->next;
2096 free(extsym_start);
2097 extsym_start = extsym_iter;
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002100 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 for (mod = modules; mod; mod = mod->next) {
2104 if (mod->skip)
2105 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002106 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002107 }
2108
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002109 err = 0;
2110
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002111 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002112 char fname[strlen(mod->name) + 10];
2113
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002114 if (mod->skip)
2115 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 buf.pos = 0;
2118
2119 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002120 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 add_depends(&buf, mod, modules);
2122 add_moddevtable(&buf, mod);
2123 add_srcversion(&buf, mod);
2124
2125 sprintf(fname, "%s.mod.c", mod->name);
2126 write_if_changed(&buf, fname);
2127 }
2128
2129 if (dump_write)
2130 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002131 if (sec_mismatch_count && !sec_mismatch_verbose)
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01002132 warn("modpost: Found %d section mismatch(es).\n"
2133 "To see full details build your kernel with:\n"
2134 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2135 sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002137 if (markers_read)
2138 read_markers(markers_read);
2139
2140 if (markers_write)
2141 write_markers(markers_write);
2142
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002143 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}