blob: 4522948a012e0fb1d772949679a8c2a9af529345 [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 */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200387 hdr->e_type = TO_NATIVE(hdr->e_type);
388 hdr->e_machine = TO_NATIVE(hdr->e_machine);
389 hdr->e_version = TO_NATIVE(hdr->e_version);
390 hdr->e_entry = TO_NATIVE(hdr->e_entry);
391 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
392 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
393 hdr->e_flags = TO_NATIVE(hdr->e_flags);
394 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
395 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
396 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
397 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
398 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
399 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sechdrs = (void *)hdr + hdr->e_shoff;
401 info->sechdrs = sechdrs;
402
Petr Stetiara83710e2007-08-27 12:15:07 +0200403 /* Check if file offset is correct */
404 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100405 fatal("section header offset=%lu in file '%s' is bigger than "
406 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
407 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200408 return 0;
409 }
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* Fix endianness in section headers */
412 for (i = 0; i < hdr->e_shnum; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200413 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
414 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
415 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
416 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
417 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
418 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
419 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
420 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
421 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
422 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 /* Find symbol table. */
425 for (i = 1; i < hdr->e_shnum; i++) {
426 const char *secstrings
427 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700428 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900429 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Tejun Heo56fc82c2009-02-06 00:48:02 +0900431 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100432 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
433 "sizeof(*hrd)=%zu\n", filename,
434 (unsigned long)sechdrs[i].sh_offset,
435 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100436 return 0;
437 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700438 secname = secstrings + sechdrs[i].sh_name;
439 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900440 if (nobits)
441 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
443 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700444 } else if (strcmp(secname, "__ksymtab") == 0)
445 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200446 else if (strcmp(secname, "__ksymtab_unused") == 0)
447 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700448 else if (strcmp(secname, "__ksymtab_gpl") == 0)
449 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200450 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
451 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700452 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
453 info->export_gpl_future_sec = i;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -0800454 else if (strcmp(secname, "__markers_strings") == 0)
455 info->markers_strings_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (sechdrs[i].sh_type != SHT_SYMTAB)
458 continue;
459
460 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100461 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100463 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 sechdrs[sechdrs[i].sh_link].sh_offset;
465 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100466 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100467 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Fix endianness in symbols */
470 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
471 sym->st_shndx = TO_NATIVE(sym->st_shndx);
472 sym->st_name = TO_NATIVE(sym->st_name);
473 sym->st_value = TO_NATIVE(sym->st_value);
474 sym->st_size = TO_NATIVE(sym->st_size);
475 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100476 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100479static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 release_file(info->hdr, info->size);
482}
483
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200484static int ignore_undef_symbol(struct elf_info *info, const char *symname)
485{
486 /* ignore __this_module, it will be resolved shortly */
487 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
488 return 1;
489 /* ignore global offset table */
490 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
491 return 1;
492 if (info->hdr->e_machine == EM_PPC)
493 /* Special register function linked on all modules during final link of .ko */
494 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
495 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
496 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
497 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
498 return 1;
499 /* Do not ignore this symbol */
500 return 0;
501}
502
Luke Yangf7b05e62006-03-02 18:35:31 +0800503#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
504#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100506static void handle_modversions(struct module *mod, struct elf_info *info,
507 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700510 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 switch (sym->st_shndx) {
513 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100514 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case SHN_ABS:
517 /* CRC'd symbol */
518 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
519 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700520 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
521 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 break;
524 case SHN_UNDEF:
525 /* undefined symbol */
526 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
527 ELF_ST_BIND(sym->st_info) != STB_WEAK)
528 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200529 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
Ben Colline8d529012005-08-19 13:44:57 -0700531/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
532#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
533/* add compatibility with older glibc */
534#ifndef STT_SPARC_REGISTER
535#define STT_SPARC_REGISTER STT_REGISTER
536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (info->hdr->e_machine == EM_SPARC ||
538 info->hdr->e_machine == EM_SPARCV9) {
539 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700540 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100542 if (symname[0] == '.') {
543 char *munged = strdup(symname);
544 munged[0] = '_';
545 munged[1] = toupper(munged[1]);
546 symname = munged;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100552 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
553 mod->unres =
554 alloc_symbol(symname +
555 strlen(MODULE_SYMBOL_PREFIX),
556 ELF_ST_BIND(sym->st_info) == STB_WEAK,
557 mod->unres);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 default:
561 /* All exported symbols */
562 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700563 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
564 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
567 mod->has_init = 1;
568 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
569 mod->has_cleanup = 1;
570 break;
571 }
572}
573
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100574/**
575 * Parse tag=value strings from .modinfo section
576 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static char *next_string(char *string, unsigned long *secsize)
578{
579 /* Skip non-zero chars */
580 while (string[0]) {
581 string++;
582 if ((*secsize)-- <= 1)
583 return NULL;
584 }
585
586 /* Skip any zero padding. */
587 while (!string[0]) {
588 string++;
589 if ((*secsize)-- <= 1)
590 return NULL;
591 }
592 return string;
593}
594
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200595static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
596 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 char *p;
599 unsigned int taglen = strlen(tag);
600 unsigned long size = modinfo_len;
601
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200602 if (info) {
603 size -= info - (char *)modinfo;
604 modinfo = next_string(info, &size);
605 }
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 for (p = modinfo; p; p = next_string(p, &size)) {
608 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
609 return p + taglen + 1;
610 }
611 return NULL;
612}
613
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200614static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
615 const char *tag)
616
617{
618 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
619}
620
Sam Ravnborg93684d32006-02-19 11:53:35 +0100621/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100622 * Test if string s ends in string sub
623 * return 0 if match
624 **/
625static int strrcmp(const char *s, const char *sub)
626{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100627 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100628
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100629 if (!s || !sub)
630 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100631
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100632 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100633 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100634
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100635 if ((slen == 0) || (sublen == 0))
636 return 1;
637
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100638 if (sublen > slen)
639 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100640
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100641 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100642}
643
Sam Ravnborgff13f922008-01-23 19:54:27 +0100644static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
645{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100646 if (sym)
647 return elf->strtab + sym->st_name;
648 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100649 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100650}
651
652static const char *sec_name(struct elf_info *elf, int shndx)
653{
654 Elf_Shdr *sechdrs = elf->sechdrs;
655 return (void *)elf->hdr +
656 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
657 sechdrs[shndx].sh_name;
658}
659
660static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
661{
662 return (void *)elf->hdr +
663 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
664 sechdr->sh_name;
665}
666
Sam Ravnborg10668222008-01-13 22:21:31 +0100667/* if sym is empty or point to a string
668 * like ".[0-9]+" then return 1.
669 * This is the optional prefix added by ld to some sections
670 */
671static int number_prefix(const char *sym)
672{
673 if (*sym++ == '\0')
674 return 1;
675 if (*sym != '.')
676 return 0;
677 do {
678 char c = *sym++;
679 if (c < '0' || c > '9')
680 return 0;
681 } while (*sym);
682 return 1;
683}
684
685/* The pattern is an array of simple patterns.
686 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100687 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100688 * "foo*" will match a string that begins with "foo"
689 * "foo$" will match a string equal to "foo" or "foo.1"
690 * where the '1' can be any number including several digits.
691 * The $ syntax is for sections where ld append a dot number
692 * to make section name unique.
693 */
694int match(const char *sym, const char * const pat[])
695{
696 const char *p;
697 while (*pat) {
698 p = *pat++;
699 const char *endp = p + strlen(p) - 1;
700
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100701 /* "*foo" */
702 if (*p == '*') {
703 if (strrcmp(sym, p + 1) == 0)
704 return 1;
705 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100706 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100707 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100708 if (strncmp(sym, p, strlen(p) - 1) == 0)
709 return 1;
710 }
711 /* "foo$" */
712 else if (*endp == '$') {
713 if (strncmp(sym, p, strlen(p) - 1) == 0) {
714 if (number_prefix(sym + strlen(p) - 1))
715 return 1;
716 }
717 }
718 /* no wildcards */
719 else {
720 if (strcmp(p, sym) == 0)
721 return 1;
722 }
723 }
724 /* no match */
725 return 0;
726}
727
Sam Ravnborg10668222008-01-13 22:21:31 +0100728/* sections that we do not want to do full section mismatch check on */
729static const char *section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200730{
731 ".comment*",
732 ".debug*",
733 ".mdebug*", /* alpha, score, mips etc. */
734 ".pdr", /* alpha, score, mips etc. */
735 ".stab*",
736 ".note*",
737 ".got*",
738 ".toc*",
739 NULL
740};
Sam Ravnborg10668222008-01-13 22:21:31 +0100741
Sam Ravnborge241a632008-01-28 20:13:13 +0100742/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400743 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100744 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400745 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100746 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400747static void check_section(const char *modname, struct elf_info *elf,
748 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100749{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400750 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100751
Anders Kaseorgb614a692009-04-23 16:49:33 -0400752 if (sechdr->sh_type == SHT_PROGBITS &&
753 !(sechdr->sh_flags & SHF_ALLOC) &&
754 !match(sec, section_white_list)) {
755 warn("%s (%s): unexpected non-allocatable section.\n"
756 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
757 "Note that for example <linux/init.h> contains\n"
758 "section definitions for use in .S files.\n\n",
759 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100760 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100761}
762
763
764
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100765#define ALL_INIT_DATA_SECTIONS \
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000766 ".init.setup$", ".init.rodata$", \
767 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$" \
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100768 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
769#define ALL_EXIT_DATA_SECTIONS \
770 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100771
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100772#define ALL_INIT_TEXT_SECTIONS \
773 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
774#define ALL_EXIT_TEXT_SECTIONS \
775 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100776
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000777#define ALL_INIT_SECTIONS INIT_SECTIONS, DEV_INIT_SECTIONS, \
778 CPU_INIT_SECTIONS, MEM_INIT_SECTIONS
779#define ALL_EXIT_SECTIONS EXIT_SECTIONS, DEV_EXIT_SECTIONS, \
780 CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100781
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100782#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100783#define TEXT_SECTIONS ".text$"
784
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000785#define INIT_SECTIONS ".init.*"
786#define DEV_INIT_SECTIONS ".devinit.*"
787#define CPU_INIT_SECTIONS ".cpuinit.*"
788#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100789
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000790#define EXIT_SECTIONS ".exit.*"
791#define DEV_EXIT_SECTIONS ".devexit.*"
792#define CPU_EXIT_SECTIONS ".cpuexit.*"
793#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100794
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100795/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100796static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100797
798/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100799static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100800
801/* All init and exit sections (code + data) */
802static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100803 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100804
805/* data section */
806static const char *data_sections[] = { DATA_SECTIONS, NULL };
807
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100808
809/* symbols in .data that may refer to init/exit sections */
810static const char *symbol_white_list[] =
811{
812 "*driver",
813 "*_template", /* scsi uses *_template a lot */
814 "*_timer", /* arm uses ops structures named _timer a lot */
815 "*_sht", /* scsi also used *_sht to some extent */
816 "*_ops",
817 "*_probe",
818 "*_probe_one",
819 "*_console",
820 NULL
821};
822
823static const char *head_sections[] = { ".head.text*", NULL };
824static const char *linker_symbols[] =
825 { "__init_begin", "_sinittext", "_einittext", NULL };
826
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100827enum mismatch {
828 NO_MISMATCH,
829 TEXT_TO_INIT,
830 DATA_TO_INIT,
831 TEXT_TO_EXIT,
832 DATA_TO_EXIT,
833 XXXINIT_TO_INIT,
834 XXXEXIT_TO_EXIT,
835 INIT_TO_EXIT,
836 EXIT_TO_INIT,
837 EXPORT_TO_INIT_EXIT,
838};
839
Sam Ravnborg10668222008-01-13 22:21:31 +0100840struct sectioncheck {
841 const char *fromsec[20];
842 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100843 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100844};
845
846const struct sectioncheck sectioncheck[] = {
847/* Do not reference init/exit code/data from
848 * normal code and data
849 */
850{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100851 .fromsec = { TEXT_SECTIONS, NULL },
852 .tosec = { ALL_INIT_SECTIONS, NULL },
853 .mismatch = TEXT_TO_INIT,
854},
855{
856 .fromsec = { DATA_SECTIONS, NULL },
857 .tosec = { ALL_INIT_SECTIONS, NULL },
858 .mismatch = DATA_TO_INIT,
859},
860{
861 .fromsec = { TEXT_SECTIONS, NULL },
862 .tosec = { ALL_EXIT_SECTIONS, NULL },
863 .mismatch = TEXT_TO_EXIT,
864},
865{
866 .fromsec = { DATA_SECTIONS, NULL },
867 .tosec = { ALL_EXIT_SECTIONS, NULL },
868 .mismatch = DATA_TO_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100869},
870/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
871{
872 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100873 .tosec = { INIT_SECTIONS, NULL },
874 .mismatch = XXXINIT_TO_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100875},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000876/* Do not reference cpuinit code/data from meminit code/data */
877{
878 .fromsec = { MEM_INIT_SECTIONS, NULL },
879 .tosec = { CPU_INIT_SECTIONS, NULL },
880 .mismatch = XXXINIT_TO_INIT,
881},
882/* Do not reference meminit code/data from cpuinit code/data */
883{
884 .fromsec = { CPU_INIT_SECTIONS, NULL },
885 .tosec = { MEM_INIT_SECTIONS, NULL },
886 .mismatch = XXXINIT_TO_INIT,
887},
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100888/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
889{
890 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100891 .tosec = { EXIT_SECTIONS, NULL },
892 .mismatch = XXXEXIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100893},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000894/* Do not reference cpuexit code/data from memexit code/data */
895{
896 .fromsec = { MEM_EXIT_SECTIONS, NULL },
897 .tosec = { CPU_EXIT_SECTIONS, NULL },
898 .mismatch = XXXEXIT_TO_EXIT,
899},
900/* Do not reference memexit code/data from cpuexit code/data */
901{
902 .fromsec = { CPU_EXIT_SECTIONS, NULL },
903 .tosec = { MEM_EXIT_SECTIONS, NULL },
904 .mismatch = XXXEXIT_TO_EXIT,
905},
Sam Ravnborg10668222008-01-13 22:21:31 +0100906/* Do not use exit code/data from init code */
907{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100908 .fromsec = { ALL_INIT_SECTIONS, NULL },
909 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100910 .mismatch = INIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100911},
912/* Do not use init code/data from exit code */
913{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100914 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100915 .tosec = { ALL_INIT_SECTIONS, NULL },
916 .mismatch = EXIT_TO_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100917},
918/* Do not export init/exit functions or data */
919{
920 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +0100921 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100922 .mismatch = EXPORT_TO_INIT_EXIT
Sam Ravnborg10668222008-01-13 22:21:31 +0100923}
924};
925
926static int section_mismatch(const char *fromsec, const char *tosec)
927{
928 int i;
929 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
930 const struct sectioncheck *check = &sectioncheck[0];
931
932 for (i = 0; i < elems; i++) {
933 if (match(fromsec, check->fromsec) &&
934 match(tosec, check->tosec))
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100935 return check->mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100936 check++;
937 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100938 return NO_MISMATCH;
Sam Ravnborg10668222008-01-13 22:21:31 +0100939}
940
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100941/**
942 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200943 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100944 * Pattern 1:
945 * If a module parameter is declared __initdata and permissions=0
946 * then this is legal despite the warning generated.
947 * We cannot see value of permissions here, so just ignore
948 * this pattern.
949 * The pattern is identified by:
950 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100951 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100952 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100953 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100954 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700955 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100956 * add, remove, probe functions etc.
957 * These functions may often be marked __init and we do not want to
958 * warn here.
959 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200960 * tosec = init or exit section
961 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100962 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
963 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100964 *
965 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +0200966 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100967 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200968 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100969 * Some symbols belong to init section but still it is ok to reference
970 * these from non-init sections as these symbols don't have any memory
971 * allocated for them and symbol address and value are same. So even
972 * if init section is freed, its ok to reference those symbols.
973 * For ex. symbols marking the init section boundaries.
974 * This pattern is identified by
975 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100976 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100977 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100978static int secref_whitelist(const char *fromsec, const char *fromsym,
979 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100980{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100981 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100982 if (match(tosec, init_data_sections) &&
983 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100984 (strncmp(fromsym, "__param", strlen("__param")) == 0))
985 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100986
987 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100988 if (match(tosec, init_exit_sections) &&
989 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100990 match(fromsym, symbol_white_list))
991 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100992
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100993 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994 if (match(fromsec, head_sections) &&
995 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100996 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100997
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200998 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100999 if (match(tosym, linker_symbols))
1000 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001001
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001002 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001003}
1004
1005/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001006 * Find symbol based on relocation record info.
1007 * In some cases the symbol supplied is a valid symbol so
1008 * return refsym. If st_name != 0 we assume this is a valid symbol.
1009 * In other cases the symbol needs to be looked up in the symbol table
1010 * based on section and address.
1011 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001012static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001013 Elf_Sym *relsym)
1014{
1015 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001016 Elf_Sym *near = NULL;
1017 Elf64_Sword distance = 20;
1018 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001019
1020 if (relsym->st_name != 0)
1021 return relsym;
1022 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1023 if (sym->st_shndx != relsym->st_shndx)
1024 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001025 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1026 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001027 if (sym->st_value == addr)
1028 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001029 /* Find a symbol nearby - addr are maybe negative */
1030 d = sym->st_value - addr;
1031 if (d < 0)
1032 d = addr - sym->st_value;
1033 if (d < distance) {
1034 distance = d;
1035 near = sym;
1036 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001037 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001038 /* We need a close match */
1039 if (distance < 20)
1040 return near;
1041 else
1042 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001043}
1044
David Brownellda68d612007-02-20 13:58:16 -08001045static inline int is_arm_mapping_symbol(const char *str)
1046{
1047 return str[0] == '$' && strchr("atd", str[1])
1048 && (str[2] == '\0' || str[2] == '.');
1049}
1050
1051/*
1052 * If there's no name there, ignore it; likewise, ignore it if it's
1053 * one of the magic symbols emitted used by current ARM tools.
1054 *
1055 * Otherwise if find_symbols_between() returns those symbols, they'll
1056 * fail the whitelist tests and cause lots of false alarms ... fixable
1057 * only by merging __exit and __init sections into __text, bloating
1058 * the kernel (which is especially evil on embedded platforms).
1059 */
1060static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1061{
1062 const char *name = elf->strtab + sym->st_name;
1063
1064 if (!name || !strlen(name))
1065 return 0;
1066 return !is_arm_mapping_symbol(name);
1067}
1068
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001069/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001070 * Find symbols before or equal addr and after addr - in the section sec.
1071 * If we find two symbols with equal offset prefer one with a valid name.
1072 * The ELF format may have a better way to detect what type of symbol
1073 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001074 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001075static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1076 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001077{
1078 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001079 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001080 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001081
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001082 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1083 const char *symsec;
1084
1085 if (sym->st_shndx >= SHN_LORESERVE)
1086 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001087 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001088 if (strcmp(symsec, sec) != 0)
1089 continue;
David Brownellda68d612007-02-20 13:58:16 -08001090 if (!is_valid_name(elf, sym))
1091 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001092 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001093 if ((addr - sym->st_value) < distance) {
1094 distance = addr - sym->st_value;
1095 near = sym;
1096 } else if ((addr - sym->st_value) == distance) {
1097 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001098 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001099 }
1100 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001101 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001102}
1103
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001104/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001105 * Convert a section name to the function/data attribute
1106 * .init.text => __init
1107 * .cpuinit.data => __cpudata
1108 * .memexitconst => __memconst
1109 * etc.
1110*/
1111static char *sec2annotation(const char *s)
1112{
1113 if (match(s, init_exit_sections)) {
1114 char *p = malloc(20);
1115 char *r = p;
1116
1117 *p++ = '_';
1118 *p++ = '_';
1119 if (*s == '.')
1120 s++;
1121 while (*s && *s != '.')
1122 *p++ = *s++;
1123 *p = '\0';
1124 if (*s == '.')
1125 s++;
1126 if (strstr(s, "rodata") != NULL)
1127 strcat(p, "const ");
1128 else if (strstr(s, "data") != NULL)
1129 strcat(p, "data ");
1130 else
1131 strcat(p, " ");
1132 return r; /* we leak her but we do not care */
1133 } else {
1134 return "";
1135 }
1136}
1137
1138static int is_function(Elf_Sym *sym)
1139{
1140 if (sym)
1141 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1142 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001143 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001144}
1145
1146/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001147 * Print a warning about a section mismatch.
1148 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001149 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001150 */
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001151static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001152 const char *fromsec,
1153 unsigned long long fromaddr,
1154 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001155 int from_is_func,
1156 const char *tosec, const char *tosym,
1157 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001158{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001159 const char *from, *from_p;
1160 const char *to, *to_p;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001161
1162 switch (from_is_func) {
1163 case 0: from = "variable"; from_p = ""; break;
1164 case 1: from = "function"; from_p = "()"; break;
1165 default: from = "(unknown reference)"; from_p = ""; break;
1166 }
1167 switch (to_is_func) {
1168 case 0: to = "variable"; to_p = ""; break;
1169 case 1: to = "function"; to_p = "()"; break;
1170 default: to = "(unknown reference)"; to_p = ""; break;
1171 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001172
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001173 sec_mismatch_count++;
1174 if (!sec_mismatch_verbose)
1175 return;
1176
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001177 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1178 "to the %s %s:%s%s\n",
1179 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1180 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001181
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001182 switch (mismatch) {
1183 case TEXT_TO_INIT:
1184 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001185 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001186 "the %s %s%s%s.\n"
1187 "This is often because %s lacks a %s\n"
1188 "annotation or the annotation of %s is wrong.\n",
1189 sec2annotation(fromsec), fromsym,
1190 to, sec2annotation(tosec), tosym, to_p,
1191 fromsym, sec2annotation(tosec), tosym);
1192 break;
1193 case DATA_TO_INIT: {
1194 const char **s = symbol_white_list;
1195 fprintf(stderr,
1196 "The variable %s references\n"
1197 "the %s %s%s%s\n"
1198 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001199 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001200 "or name the variable:\n",
1201 fromsym, to, sec2annotation(tosec), tosym, to_p);
1202 while (*s)
1203 fprintf(stderr, "%s, ", *s++);
1204 fprintf(stderr, "\n");
1205 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001206 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001207 case TEXT_TO_EXIT:
1208 fprintf(stderr,
1209 "The function %s() references a %s in an exit section.\n"
1210 "Often the %s %s%s has valid usage outside the exit section\n"
1211 "and the fix is to remove the %sannotation of %s.\n",
1212 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1213 break;
1214 case DATA_TO_EXIT: {
1215 const char **s = symbol_white_list;
1216 fprintf(stderr,
1217 "The variable %s references\n"
1218 "the %s %s%s%s\n"
1219 "If the reference is valid then annotate the\n"
1220 "variable with __exit* (see linux/init.h) or "
1221 "name the variable:\n",
1222 fromsym, to, sec2annotation(tosec), tosym, to_p);
1223 while (*s)
1224 fprintf(stderr, "%s, ", *s++);
1225 fprintf(stderr, "\n");
1226 break;
1227 }
1228 case XXXINIT_TO_INIT:
1229 case XXXEXIT_TO_EXIT:
1230 fprintf(stderr,
1231 "The %s %s%s%s references\n"
1232 "a %s %s%s%s.\n"
1233 "If %s is only used by %s then\n"
1234 "annotate %s with a matching annotation.\n",
1235 from, sec2annotation(fromsec), fromsym, from_p,
1236 to, sec2annotation(tosec), tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001237 tosym, fromsym, tosym);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001238 break;
1239 case INIT_TO_EXIT:
1240 fprintf(stderr,
1241 "The %s %s%s%s references\n"
1242 "a %s %s%s%s.\n"
1243 "This is often seen when error handling "
1244 "in the init function\n"
1245 "uses functionality in the exit path.\n"
1246 "The fix is often to remove the %sannotation of\n"
1247 "%s%s so it may be used outside an exit section.\n",
1248 from, sec2annotation(fromsec), fromsym, from_p,
1249 to, sec2annotation(tosec), tosym, to_p,
1250 sec2annotation(tosec), tosym, to_p);
1251 break;
1252 case EXIT_TO_INIT:
1253 fprintf(stderr,
1254 "The %s %s%s%s references\n"
1255 "a %s %s%s%s.\n"
1256 "This is often seen when error handling "
1257 "in the exit function\n"
1258 "uses functionality in the init path.\n"
1259 "The fix is often to remove the %sannotation of\n"
1260 "%s%s so it may be used outside an init section.\n",
1261 from, sec2annotation(fromsec), fromsym, from_p,
1262 to, sec2annotation(tosec), tosym, to_p,
1263 sec2annotation(tosec), tosym, to_p);
1264 break;
1265 case EXPORT_TO_INIT_EXIT:
1266 fprintf(stderr,
1267 "The symbol %s is exported and annotated %s\n"
1268 "Fix this by removing the %sannotation of %s "
1269 "or drop the export.\n",
1270 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1271 case NO_MISMATCH:
1272 /* To get warnings on missing members */
1273 break;
1274 }
1275 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001276}
1277
1278static void check_section_mismatch(const char *modname, struct elf_info *elf,
1279 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1280{
1281 const char *tosec;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001282 enum mismatch mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001283
1284 tosec = sec_name(elf, sym->st_shndx);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001285 mismatch = section_mismatch(fromsec, tosec);
1286 if (mismatch != NO_MISMATCH) {
1287 Elf_Sym *to;
1288 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001289 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001290 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001291
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001292 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1293 fromsym = sym_name(elf, from);
1294 to = find_elf_symbol(elf, r->r_addend, sym);
1295 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001296
1297 /* check whitelist - we may ignore it */
1298 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001299 report_sec_mismatch(modname, mismatch,
1300 fromsec, r->r_offset, fromsym,
1301 is_function(from), tosec, tosym,
1302 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001303 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001304 }
1305}
1306
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001307static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001308 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001309{
1310 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001311 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001312
1313 return (void *)elf->hdr + sechdrs[section].sh_offset +
1314 (r->r_offset - sechdrs[section].sh_addr);
1315}
1316
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001317static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001318{
1319 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001320 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001321
1322 switch (r_typ) {
1323 case R_386_32:
1324 r->r_addend = TO_NATIVE(*location);
1325 break;
1326 case R_386_PC32:
1327 r->r_addend = TO_NATIVE(*location) + 4;
1328 /* For CONFIG_RELOCATABLE=y */
1329 if (elf->hdr->e_type == ET_EXEC)
1330 r->r_addend += r->r_offset;
1331 break;
1332 }
1333 return 0;
1334}
1335
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001336static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001337{
1338 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1339
1340 switch (r_typ) {
1341 case R_ARM_ABS32:
1342 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001343 r->r_addend = (int)(long)
1344 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001345 break;
1346 case R_ARM_PC24:
1347 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001348 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001349 sechdr->sh_offset +
1350 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001351 break;
1352 default:
1353 return 1;
1354 }
1355 return 0;
1356}
1357
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001358static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001359{
1360 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001361 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001362 unsigned int inst;
1363
1364 if (r_typ == R_MIPS_HI16)
1365 return 1; /* skip this */
1366 inst = TO_NATIVE(*location);
1367 switch (r_typ) {
1368 case R_MIPS_LO16:
1369 r->r_addend = inst & 0xffff;
1370 break;
1371 case R_MIPS_26:
1372 r->r_addend = (inst & 0x03ffffff) << 2;
1373 break;
1374 case R_MIPS_32:
1375 r->r_addend = inst;
1376 break;
1377 }
1378 return 0;
1379}
1380
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001381static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001382 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001383{
1384 Elf_Sym *sym;
1385 Elf_Rela *rela;
1386 Elf_Rela r;
1387 unsigned int r_sym;
1388 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001389
Sam Ravnborgff13f922008-01-23 19:54:27 +01001390 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001391 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1392
Sam Ravnborgff13f922008-01-23 19:54:27 +01001393 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001394 fromsec += strlen(".rela");
1395 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001396 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001397 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001398
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001399 for (rela = start; rela < stop; rela++) {
1400 r.r_offset = TO_NATIVE(rela->r_offset);
1401#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001402 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001403 unsigned int r_typ;
1404 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1405 r_sym = TO_NATIVE(r_sym);
1406 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1407 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1408 } else {
1409 r.r_info = TO_NATIVE(rela->r_info);
1410 r_sym = ELF_R_SYM(r.r_info);
1411 }
1412#else
1413 r.r_info = TO_NATIVE(rela->r_info);
1414 r_sym = ELF_R_SYM(r.r_info);
1415#endif
1416 r.r_addend = TO_NATIVE(rela->r_addend);
1417 sym = elf->symtab_start + r_sym;
1418 /* Skip special sections */
1419 if (sym->st_shndx >= SHN_LORESERVE)
1420 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001421 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001422 }
1423}
1424
1425static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001426 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001427{
1428 Elf_Sym *sym;
1429 Elf_Rel *rel;
1430 Elf_Rela r;
1431 unsigned int r_sym;
1432 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001433
Sam Ravnborgff13f922008-01-23 19:54:27 +01001434 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001435 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1436
Sam Ravnborgff13f922008-01-23 19:54:27 +01001437 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001438 fromsec += strlen(".rel");
1439 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001440 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001441 return;
1442
1443 for (rel = start; rel < stop; rel++) {
1444 r.r_offset = TO_NATIVE(rel->r_offset);
1445#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001446 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001447 unsigned int r_typ;
1448 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1449 r_sym = TO_NATIVE(r_sym);
1450 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1451 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1452 } else {
1453 r.r_info = TO_NATIVE(rel->r_info);
1454 r_sym = ELF_R_SYM(r.r_info);
1455 }
1456#else
1457 r.r_info = TO_NATIVE(rel->r_info);
1458 r_sym = ELF_R_SYM(r.r_info);
1459#endif
1460 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001461 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001462 case EM_386:
1463 if (addend_386_rel(elf, sechdr, &r))
1464 continue;
1465 break;
1466 case EM_ARM:
1467 if (addend_arm_rel(elf, sechdr, &r))
1468 continue;
1469 break;
1470 case EM_MIPS:
1471 if (addend_mips_rel(elf, sechdr, &r))
1472 continue;
1473 break;
1474 }
1475 sym = elf->symtab_start + r_sym;
1476 /* Skip special sections */
1477 if (sym->st_shndx >= SHN_LORESERVE)
1478 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001479 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001480 }
1481}
1482
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001483/**
1484 * A module includes a number of sections that are discarded
1485 * either when loaded or when used as built-in.
1486 * For loaded modules all functions marked __init and all data
1487 * marked __initdata will be discarded when the module has been intialized.
1488 * Likewise for modules used built-in the sections marked __exit
1489 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001490 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001491 * The check_sec_ref() function traverses all relocation records
1492 * to find all references to a section that reference a section that will
1493 * be discarded and warns about it.
1494 **/
1495static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001496 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001497{
1498 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001499 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001500
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001501 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001502 for (i = 0; i < elf->hdr->e_shnum; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001503 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001504 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001505 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001506 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001507 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001508 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001509 }
1510}
1511
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001512static void get_markers(struct elf_info *info, struct module *mod)
1513{
1514 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1515 const char *strings = (const char *) info->hdr + sh->sh_offset;
1516 const Elf_Sym *sym, *first_sym, *last_sym;
1517 size_t n;
1518
1519 if (!info->markers_strings_sec)
1520 return;
1521
1522 /*
1523 * First count the strings. We look for all the symbols defined
1524 * in the __markers_strings section named __mstrtab_*. For
1525 * these local names, the compiler puts a random .NNN suffix on,
1526 * so the names don't correspond exactly.
1527 */
1528 first_sym = last_sym = NULL;
1529 n = 0;
1530 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1531 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1532 sym->st_shndx == info->markers_strings_sec &&
1533 !strncmp(info->strtab + sym->st_name,
1534 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1535 if (first_sym == NULL)
1536 first_sym = sym;
1537 last_sym = sym;
1538 ++n;
1539 }
1540
1541 if (n == 0)
1542 return;
1543
1544 /*
1545 * Now collect each name and format into a line for the output.
1546 * Lines look like:
1547 * marker_name vmlinux marker %s format %d
1548 * The format string after the second \t can use whitespace.
1549 */
1550 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1551 mod->nmarkers = n;
1552
1553 n = 0;
1554 for (sym = first_sym; sym <= last_sym; sym++)
1555 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1556 sym->st_shndx == info->markers_strings_sec &&
1557 !strncmp(info->strtab + sym->st_name,
1558 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1559 const char *name = strings + sym->st_value;
1560 const char *fmt = strchr(name, '\0') + 1;
1561 char *line = NULL;
1562 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1563 NOFAIL(line);
1564 mod->markers[n++] = line;
1565 }
1566}
1567
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001568static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 const char *symname;
1571 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001572 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct module *mod;
1574 struct elf_info info = { };
1575 Elf_Sym *sym;
1576
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001577 if (!parse_elf(&info, modname))
1578 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 mod = new_module(modname);
1581
1582 /* When there's no vmlinux, don't print warnings about
1583 * unresolved symbols (since there'll be too many ;) */
1584 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 mod->skip = 1;
1587 }
1588
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001589 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001590 if (info.modinfo && !license && !is_vmlinux(modname))
1591 warn("modpost: missing MODULE_LICENSE() in %s\n"
1592 "see include/linux/module.h for "
1593 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001594 while (license) {
1595 if (license_is_gpl_compatible(license))
1596 mod->gpl_compatible = 1;
1597 else {
1598 mod->gpl_compatible = 0;
1599 break;
1600 }
1601 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1602 "license", license);
1603 }
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1606 symname = info.strtab + sym->st_name;
1607
1608 handle_modversions(mod, &info, sym, symname);
1609 handle_moddevtable(mod, &info, sym, symname);
1610 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001611 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001612 (is_vmlinux(modname) && vmlinux_section_warnings))
1613 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1616 if (version)
1617 maybe_frob_rcs_version(modname, version, info.modinfo,
1618 version - (char *)info.hdr);
1619 if (version || (all_versions && !is_vmlinux(modname)))
1620 get_src_version(modname, mod->srcversion,
1621 sizeof(mod->srcversion)-1);
1622
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001623 get_markers(&info, mod);
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 parse_elf_finish(&info);
1626
Rusty Russell8c8ef422009-03-31 13:05:34 -06001627 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * never passed as an argument to an exported function, so
1629 * the automatic versioning doesn't pick it up, but it's really
1630 * important anyhow */
1631 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001632 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635#define SZ 500
1636
1637/* We first write the generated file into memory using the
1638 * following helper, then compare to the file on disk and
1639 * only update the later if anything changed */
1640
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001641void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1642 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 char tmp[SZ];
1645 int len;
1646 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 va_start(ap, fmt);
1649 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001650 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 va_end(ap);
1652}
1653
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001654void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001657 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 buf->p = realloc(buf->p, buf->size);
1659 }
1660 strncpy(buf->p + buf->pos, s, len);
1661 buf->pos += len;
1662}
1663
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001664static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1665{
1666 const char *e = is_vmlinux(m) ?"":".ko";
1667
1668 switch (exp) {
1669 case export_gpl:
1670 fatal("modpost: GPL-incompatible module %s%s "
1671 "uses GPL-only symbol '%s'\n", m, e, s);
1672 break;
1673 case export_unused_gpl:
1674 fatal("modpost: GPL-incompatible module %s%s "
1675 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1676 break;
1677 case export_gpl_future:
1678 warn("modpost: GPL-incompatible module %s%s "
1679 "uses future GPL-only symbol '%s'\n", m, e, s);
1680 break;
1681 case export_plain:
1682 case export_unused:
1683 case export_unknown:
1684 /* ignore */
1685 break;
1686 }
1687}
1688
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001689static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001690{
1691 const char *e = is_vmlinux(m) ?"":".ko";
1692
1693 switch (exp) {
1694 case export_unused:
1695 case export_unused_gpl:
1696 warn("modpost: module %s%s "
1697 "uses symbol '%s' marked UNUSED\n", m, e, s);
1698 break;
1699 default:
1700 /* ignore */
1701 break;
1702 }
1703}
1704
1705static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001706{
1707 struct symbol *s, *exp;
1708
1709 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001710 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001711 exp = find_symbol(s->name);
1712 if (!exp || exp->module == mod)
1713 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001714 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001715 if (basename)
1716 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001717 else
1718 basename = mod->name;
1719 if (!mod->gpl_compatible)
1720 check_for_gpl_usage(exp->export, basename, exp->name);
1721 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001722 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001723}
1724
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001725/**
1726 * Header for the generated file
1727 **/
1728static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 buf_printf(b, "#include <linux/module.h>\n");
1731 buf_printf(b, "#include <linux/vermagic.h>\n");
1732 buf_printf(b, "#include <linux/compiler.h>\n");
1733 buf_printf(b, "\n");
1734 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1735 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 buf_printf(b, "struct module __this_module\n");
1737 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001738 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (mod->has_init)
1740 buf_printf(b, " .init = init_module,\n");
1741 if (mod->has_cleanup)
1742 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1743 " .exit = cleanup_module,\n"
1744 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001745 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 buf_printf(b, "};\n");
1747}
1748
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001749void add_staging_flag(struct buffer *b, const char *name)
1750{
1751 static const char *staging_dir = "drivers/staging";
1752
1753 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1754 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1755}
1756
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001757/**
1758 * Record CRCs for unresolved symbols
1759 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001760static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001763 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 for (s = mod->unres; s; s = s->next) {
1766 exp = find_symbol(s->name);
1767 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001768 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001769 if (warn_unresolved) {
1770 warn("\"%s\" [%s.ko] undefined!\n",
1771 s->name, mod->name);
1772 } else {
1773 merror("\"%s\" [%s.ko] undefined!\n",
1774 s->name, mod->name);
1775 err = 1;
1776 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 continue;
1779 }
1780 s->module = exp->module;
1781 s->crc_valid = exp->crc_valid;
1782 s->crc = exp->crc;
1783 }
1784
1785 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 buf_printf(b, "\n");
1789 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001790 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1792
1793 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001794 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001797 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 s->name, mod->name);
1799 continue;
1800 }
1801 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1802 }
1803
1804 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001805
1806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001809static void add_depends(struct buffer *b, struct module *mod,
1810 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct symbol *s;
1813 struct module *m;
1814 int first = 1;
1815
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001816 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 buf_printf(b, "\n");
1820 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001821 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1823 buf_printf(b, "\"depends=");
1824 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001825 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (!s->module)
1827 continue;
1828
1829 if (s->module->seen)
1830 continue;
1831
1832 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001833 p = strrchr(s->module->name, '/');
1834 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001835 p++;
1836 else
1837 p = s->module->name;
1838 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 first = 0;
1840 }
1841 buf_printf(b, "\";\n");
1842}
1843
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001844static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 if (mod->srcversion[0]) {
1847 buf_printf(b, "\n");
1848 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1849 mod->srcversion);
1850 }
1851}
1852
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001853static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 char *tmp;
1856 FILE *file;
1857 struct stat st;
1858
1859 file = fopen(fname, "r");
1860 if (!file)
1861 goto write;
1862
1863 if (fstat(fileno(file), &st) < 0)
1864 goto close_write;
1865
1866 if (st.st_size != b->pos)
1867 goto close_write;
1868
1869 tmp = NOFAIL(malloc(b->pos));
1870 if (fread(tmp, 1, b->pos, file) != b->pos)
1871 goto free_write;
1872
1873 if (memcmp(tmp, b->p, b->pos) != 0)
1874 goto free_write;
1875
1876 free(tmp);
1877 fclose(file);
1878 return;
1879
1880 free_write:
1881 free(tmp);
1882 close_write:
1883 fclose(file);
1884 write:
1885 file = fopen(fname, "w");
1886 if (!file) {
1887 perror(fname);
1888 exit(1);
1889 }
1890 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1891 perror(fname);
1892 exit(1);
1893 }
1894 fclose(file);
1895}
1896
Ram Paibd5cbce2006-06-08 22:12:53 -07001897/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001898 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001899 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001900static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 unsigned long size, pos = 0;
1903 void *file = grab_file(fname, &size);
1904 char *line;
1905
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001906 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* No symbol versions, silently ignore */
1908 return;
1909
1910 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001911 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 unsigned int crc;
1913 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001914 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (!(symname = strchr(line, '\t')))
1917 goto fail;
1918 *symname++ = '\0';
1919 if (!(modname = strchr(symname, '\t')))
1920 goto fail;
1921 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001922 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001923 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001924 if (export && ((end = strchr(export, '\t')) != NULL))
1925 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 crc = strtoul(line, &d, 16);
1927 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1928 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001929 mod = find_module(modname);
1930 if (!mod) {
1931 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00001933 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 mod->skip = 1;
1935 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001936 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001937 s->kernel = kernel;
1938 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001939 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 return;
1942fail:
1943 fatal("parse error in symbol dump file\n");
1944}
1945
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001946/* For normal builds always dump all symbols.
1947 * For external modules only dump symbols
1948 * that are not read from kernel Module.symvers.
1949 **/
1950static int dump_sym(struct symbol *sym)
1951{
1952 if (!external_module)
1953 return 1;
1954 if (sym->vmlinux || sym->kernel)
1955 return 0;
1956 return 1;
1957}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001958
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001959static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct buffer buf = { };
1962 struct symbol *symbol;
1963 int n;
1964
1965 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1966 symbol = symbolhash[n];
1967 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001968 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001969 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001970 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001971 symbol->module->name,
1972 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 symbol = symbol->next;
1974 }
1975 }
1976 write_if_changed(&buf, fname);
1977}
1978
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001979static void add_marker(struct module *mod, const char *name, const char *fmt)
1980{
1981 char *line = NULL;
1982 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1983 NOFAIL(line);
1984
1985 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1986 sizeof mod->markers[0])));
1987 mod->markers[mod->nmarkers++] = line;
1988}
1989
1990static void read_markers(const char *fname)
1991{
1992 unsigned long size, pos = 0;
1993 void *file = grab_file(fname, &size);
1994 char *line;
1995
1996 if (!file) /* No old markers, silently ignore */
1997 return;
1998
1999 while ((line = get_next_line(&pos, file, size))) {
2000 char *marker, *modname, *fmt;
2001 struct module *mod;
2002
2003 marker = line;
2004 modname = strchr(marker, '\t');
2005 if (!modname)
2006 goto fail;
2007 *modname++ = '\0';
2008 fmt = strchr(modname, '\t');
2009 if (!fmt)
2010 goto fail;
2011 *fmt++ = '\0';
2012 if (*marker == '\0' || *modname == '\0')
2013 goto fail;
2014
2015 mod = find_module(modname);
2016 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002017 mod = new_module(modname);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002018 mod->skip = 1;
2019 }
Mathieu Desnoyers87f3b6b2008-10-06 09:30:12 -04002020 if (is_vmlinux(modname)) {
2021 have_vmlinux = 1;
2022 mod->skip = 0;
2023 }
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002024
Mathieu Desnoyersd35cb362008-07-21 14:21:38 -07002025 if (!mod->skip)
2026 add_marker(mod, marker, fmt);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002027 }
Cedric Hombourger99e3a1e2009-04-25 09:38:21 +02002028 release_file(file, size);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002029 return;
2030fail:
2031 fatal("parse error in markers list file\n");
2032}
2033
2034static int compare_strings(const void *a, const void *b)
2035{
2036 return strcmp(*(const char **) a, *(const char **) b);
2037}
2038
2039static void write_markers(const char *fname)
2040{
2041 struct buffer buf = { };
2042 struct module *mod;
2043 size_t i;
2044
2045 for (mod = modules; mod; mod = mod->next)
2046 if ((!external_module || !mod->skip) && mod->markers != NULL) {
2047 /*
2048 * Sort the strings so we can skip duplicates when
2049 * we write them out.
2050 */
2051 qsort(mod->markers, mod->nmarkers,
2052 sizeof mod->markers[0], &compare_strings);
2053 for (i = 0; i < mod->nmarkers; ++i) {
2054 char *line = mod->markers[i];
2055 buf_write(&buf, line, strlen(line));
2056 while (i + 1 < mod->nmarkers &&
2057 !strcmp(mod->markers[i],
2058 mod->markers[i + 1]))
2059 free(mod->markers[i++]);
2060 free(mod->markers[i]);
2061 }
2062 free(mod->markers);
2063 mod->markers = NULL;
2064 }
2065
2066 write_if_changed(&buf, fname);
2067}
2068
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002069struct ext_sym_list {
2070 struct ext_sym_list *next;
2071 const char *file;
2072};
2073
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002074int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct module *mod;
2077 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002078 char *kernel_read = NULL, *module_read = NULL;
2079 char *dump_write = NULL;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002080 char *markers_read = NULL;
2081 char *markers_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002083 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002084 struct ext_sym_list *extsym_iter;
2085 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002087 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002088 switch (opt) {
2089 case 'i':
2090 kernel_read = optarg;
2091 break;
2092 case 'I':
2093 module_read = optarg;
2094 external_module = 1;
2095 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002096 case 'c':
2097 cross_build = 1;
2098 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002099 case 'e':
2100 external_module = 1;
2101 extsym_iter =
2102 NOFAIL(malloc(sizeof(*extsym_iter)));
2103 extsym_iter->next = extsym_start;
2104 extsym_iter->file = optarg;
2105 extsym_start = extsym_iter;
2106 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002107 case 'm':
2108 modversions = 1;
2109 break;
2110 case 'o':
2111 dump_write = optarg;
2112 break;
2113 case 'a':
2114 all_versions = 1;
2115 break;
2116 case 's':
2117 vmlinux_section_warnings = 0;
2118 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002119 case 'S':
2120 sec_mismatch_verbose = 0;
2121 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002122 case 'w':
2123 warn_unresolved = 1;
2124 break;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002125 case 'M':
2126 markers_write = optarg;
2127 break;
2128 case 'K':
2129 markers_read = optarg;
2130 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002131 default:
2132 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 }
2135
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002136 if (kernel_read)
2137 read_dump(kernel_read, 1);
2138 if (module_read)
2139 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002140 while (extsym_start) {
2141 read_dump(extsym_start->file, 0);
2142 extsym_iter = extsym_start->next;
2143 free(extsym_start);
2144 extsym_start = extsym_iter;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002147 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 for (mod = modules; mod; mod = mod->next) {
2151 if (mod->skip)
2152 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002153 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002154 }
2155
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002156 err = 0;
2157
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002158 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002159 char fname[strlen(mod->name) + 10];
2160
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002161 if (mod->skip)
2162 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 buf.pos = 0;
2165
2166 add_header(&buf, mod);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002167 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002168 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 add_depends(&buf, mod, modules);
2170 add_moddevtable(&buf, mod);
2171 add_srcversion(&buf, mod);
2172
2173 sprintf(fname, "%s.mod.c", mod->name);
2174 write_if_changed(&buf, fname);
2175 }
2176
2177 if (dump_write)
2178 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002179 if (sec_mismatch_count && !sec_mismatch_verbose)
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01002180 warn("modpost: Found %d section mismatch(es).\n"
2181 "To see full details build your kernel with:\n"
2182 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2183 sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002185 if (markers_read)
2186 read_markers(markers_read);
2187
2188 if (markers_write)
2189 write_markers(markers_write);
2190
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}