modpost.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006 Sam Ravnborg
  6. * Based in part on module-init-tools/depmod.c,file2alias
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * Usage: modpost vmlinux module1.o module2.o ...
  12. */
  13. #include <ctype.h>
  14. #include "modpost.h"
  15. /* Are we using CONFIG_MODVERSIONS? */
  16. int modversions = 0;
  17. /* Warn about undefined symbols? (do so if we have vmlinux) */
  18. int have_vmlinux = 0;
  19. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  20. static int all_versions = 0;
  21. /* If we are modposting external module set to 1 */
  22. static int external_module = 0;
  23. void fatal(const char *fmt, ...)
  24. {
  25. va_list arglist;
  26. fprintf(stderr, "FATAL: ");
  27. va_start(arglist, fmt);
  28. vfprintf(stderr, fmt, arglist);
  29. va_end(arglist);
  30. exit(1);
  31. }
  32. void warn(const char *fmt, ...)
  33. {
  34. va_list arglist;
  35. fprintf(stderr, "WARNING: ");
  36. va_start(arglist, fmt);
  37. vfprintf(stderr, fmt, arglist);
  38. va_end(arglist);
  39. }
  40. static int is_vmlinux(const char *modname)
  41. {
  42. const char *myname;
  43. if ((myname = strrchr(modname, '/')))
  44. myname++;
  45. else
  46. myname = modname;
  47. return strcmp(myname, "vmlinux") == 0;
  48. }
  49. void *do_nofail(void *ptr, const char *expr)
  50. {
  51. if (!ptr) {
  52. fatal("modpost: Memory allocation failure: %s.\n", expr);
  53. }
  54. return ptr;
  55. }
  56. /* A list of all modules we processed */
  57. static struct module *modules;
  58. static struct module *find_module(char *modname)
  59. {
  60. struct module *mod;
  61. for (mod = modules; mod; mod = mod->next)
  62. if (strcmp(mod->name, modname) == 0)
  63. break;
  64. return mod;
  65. }
  66. static struct module *new_module(char *modname)
  67. {
  68. struct module *mod;
  69. char *p, *s;
  70. mod = NOFAIL(malloc(sizeof(*mod)));
  71. memset(mod, 0, sizeof(*mod));
  72. p = NOFAIL(strdup(modname));
  73. /* strip trailing .o */
  74. if ((s = strrchr(p, '.')) != NULL)
  75. if (strcmp(s, ".o") == 0)
  76. *s = '\0';
  77. /* add to list */
  78. mod->name = p;
  79. mod->next = modules;
  80. modules = mod;
  81. return mod;
  82. }
  83. /* A hash of all exported symbols,
  84. * struct symbol is also used for lists of unresolved symbols */
  85. #define SYMBOL_HASH_SIZE 1024
  86. struct symbol {
  87. struct symbol *next;
  88. struct module *module;
  89. unsigned int crc;
  90. int crc_valid;
  91. unsigned int weak:1;
  92. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  93. unsigned int kernel:1; /* 1 if symbol is from kernel
  94. * (only for external modules) **/
  95. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  96. char name[0];
  97. };
  98. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  99. /* This is based on the hash agorithm from gdbm, via tdb */
  100. static inline unsigned int tdb_hash(const char *name)
  101. {
  102. unsigned value; /* Used to compute the hash value. */
  103. unsigned i; /* Used to cycle through random values. */
  104. /* Set the initial value from the key size. */
  105. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  106. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  107. return (1103515243 * value + 12345);
  108. }
  109. /**
  110. * Allocate a new symbols for use in the hash of exported symbols or
  111. * the list of unresolved symbols per module
  112. **/
  113. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  114. struct symbol *next)
  115. {
  116. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  117. memset(s, 0, sizeof(*s));
  118. strcpy(s->name, name);
  119. s->weak = weak;
  120. s->next = next;
  121. return s;
  122. }
  123. /* For the hash of exported symbols */
  124. static struct symbol *new_symbol(const char *name, struct module *module)
  125. {
  126. unsigned int hash;
  127. struct symbol *new;
  128. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  129. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  130. new->module = module;
  131. return new;
  132. }
  133. static struct symbol *find_symbol(const char *name)
  134. {
  135. struct symbol *s;
  136. /* For our purposes, .foo matches foo. PPC64 needs this. */
  137. if (name[0] == '.')
  138. name++;
  139. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  140. if (strcmp(s->name, name) == 0)
  141. return s;
  142. }
  143. return NULL;
  144. }
  145. /**
  146. * Add an exported symbol - it may have already been added without a
  147. * CRC, in this case just update the CRC
  148. **/
  149. static struct symbol *sym_add_exported(const char *name, struct module *mod)
  150. {
  151. struct symbol *s = find_symbol(name);
  152. if (!s) {
  153. s = new_symbol(name, mod);
  154. } else {
  155. if (!s->preloaded) {
  156. warn("%s: '%s' exported twice. Previous export "
  157. "was in %s%s\n", mod->name, name,
  158. s->module->name,
  159. is_vmlinux(s->module->name) ?"":".ko");
  160. }
  161. }
  162. s->preloaded = 0;
  163. s->vmlinux = is_vmlinux(mod->name);
  164. s->kernel = 0;
  165. return s;
  166. }
  167. static void sym_update_crc(const char *name, struct module *mod,
  168. unsigned int crc)
  169. {
  170. struct symbol *s = find_symbol(name);
  171. if (!s)
  172. s = new_symbol(name, mod);
  173. s->crc = crc;
  174. s->crc_valid = 1;
  175. }
  176. void *grab_file(const char *filename, unsigned long *size)
  177. {
  178. struct stat st;
  179. void *map;
  180. int fd;
  181. fd = open(filename, O_RDONLY);
  182. if (fd < 0 || fstat(fd, &st) != 0)
  183. return NULL;
  184. *size = st.st_size;
  185. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  186. close(fd);
  187. if (map == MAP_FAILED)
  188. return NULL;
  189. return map;
  190. }
  191. /**
  192. * Return a copy of the next line in a mmap'ed file.
  193. * spaces in the beginning of the line is trimmed away.
  194. * Return a pointer to a static buffer.
  195. **/
  196. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  197. {
  198. static char line[4096];
  199. int skip = 1;
  200. size_t len = 0;
  201. signed char *p = (signed char *)file + *pos;
  202. char *s = line;
  203. for (; *pos < size ; (*pos)++)
  204. {
  205. if (skip && isspace(*p)) {
  206. p++;
  207. continue;
  208. }
  209. skip = 0;
  210. if (*p != '\n' && (*pos < size)) {
  211. len++;
  212. *s++ = *p++;
  213. if (len > 4095)
  214. break; /* Too long, stop */
  215. } else {
  216. /* End of string */
  217. *s = '\0';
  218. return line;
  219. }
  220. }
  221. /* End of buffer */
  222. return NULL;
  223. }
  224. void release_file(void *file, unsigned long size)
  225. {
  226. munmap(file, size);
  227. }
  228. static void parse_elf(struct elf_info *info, const char *filename)
  229. {
  230. unsigned int i;
  231. Elf_Ehdr *hdr = info->hdr;
  232. Elf_Shdr *sechdrs;
  233. Elf_Sym *sym;
  234. hdr = grab_file(filename, &info->size);
  235. if (!hdr) {
  236. perror(filename);
  237. abort();
  238. }
  239. info->hdr = hdr;
  240. if (info->size < sizeof(*hdr))
  241. goto truncated;
  242. /* Fix endianness in ELF header */
  243. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  244. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  245. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  246. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  247. sechdrs = (void *)hdr + hdr->e_shoff;
  248. info->sechdrs = sechdrs;
  249. /* Fix endianness in section headers */
  250. for (i = 0; i < hdr->e_shnum; i++) {
  251. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  252. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  253. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  254. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  255. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  256. }
  257. /* Find symbol table. */
  258. for (i = 1; i < hdr->e_shnum; i++) {
  259. const char *secstrings
  260. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  261. if (sechdrs[i].sh_offset > info->size)
  262. goto truncated;
  263. if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
  264. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  265. info->modinfo_len = sechdrs[i].sh_size;
  266. }
  267. if (sechdrs[i].sh_type != SHT_SYMTAB)
  268. continue;
  269. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  270. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  271. + sechdrs[i].sh_size;
  272. info->strtab = (void *)hdr +
  273. sechdrs[sechdrs[i].sh_link].sh_offset;
  274. }
  275. if (!info->symtab_start) {
  276. fatal("%s has no symtab?\n", filename);
  277. }
  278. /* Fix endianness in symbols */
  279. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  280. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  281. sym->st_name = TO_NATIVE(sym->st_name);
  282. sym->st_value = TO_NATIVE(sym->st_value);
  283. sym->st_size = TO_NATIVE(sym->st_size);
  284. }
  285. return;
  286. truncated:
  287. fatal("%s is truncated.\n", filename);
  288. }
  289. static void parse_elf_finish(struct elf_info *info)
  290. {
  291. release_file(info->hdr, info->size);
  292. }
  293. #define CRC_PFX "__crc_"
  294. #define KSYMTAB_PFX "__ksymtab_"
  295. static void handle_modversions(struct module *mod, struct elf_info *info,
  296. Elf_Sym *sym, const char *symname)
  297. {
  298. unsigned int crc;
  299. switch (sym->st_shndx) {
  300. case SHN_COMMON:
  301. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  302. break;
  303. case SHN_ABS:
  304. /* CRC'd symbol */
  305. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  306. crc = (unsigned int) sym->st_value;
  307. sym_update_crc(symname + strlen(CRC_PFX), mod, crc);
  308. }
  309. break;
  310. case SHN_UNDEF:
  311. /* undefined symbol */
  312. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  313. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  314. break;
  315. /* ignore global offset table */
  316. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  317. break;
  318. /* ignore __this_module, it will be resolved shortly */
  319. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  320. break;
  321. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  322. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  323. /* add compatibility with older glibc */
  324. #ifndef STT_SPARC_REGISTER
  325. #define STT_SPARC_REGISTER STT_REGISTER
  326. #endif
  327. if (info->hdr->e_machine == EM_SPARC ||
  328. info->hdr->e_machine == EM_SPARCV9) {
  329. /* Ignore register directives. */
  330. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  331. break;
  332. if (symname[0] == '.') {
  333. char *munged = strdup(symname);
  334. munged[0] = '_';
  335. munged[1] = toupper(munged[1]);
  336. symname = munged;
  337. }
  338. }
  339. #endif
  340. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  341. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  342. mod->unres = alloc_symbol(symname +
  343. strlen(MODULE_SYMBOL_PREFIX),
  344. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  345. mod->unres);
  346. break;
  347. default:
  348. /* All exported symbols */
  349. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  350. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
  351. }
  352. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  353. mod->has_init = 1;
  354. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  355. mod->has_cleanup = 1;
  356. break;
  357. }
  358. }
  359. /**
  360. * Parse tag=value strings from .modinfo section
  361. **/
  362. static char *next_string(char *string, unsigned long *secsize)
  363. {
  364. /* Skip non-zero chars */
  365. while (string[0]) {
  366. string++;
  367. if ((*secsize)-- <= 1)
  368. return NULL;
  369. }
  370. /* Skip any zero padding. */
  371. while (!string[0]) {
  372. string++;
  373. if ((*secsize)-- <= 1)
  374. return NULL;
  375. }
  376. return string;
  377. }
  378. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  379. const char *tag)
  380. {
  381. char *p;
  382. unsigned int taglen = strlen(tag);
  383. unsigned long size = modinfo_len;
  384. for (p = modinfo; p; p = next_string(p, &size)) {
  385. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  386. return p + taglen + 1;
  387. }
  388. return NULL;
  389. }
  390. /**
  391. * Test if string s ends in string sub
  392. * return 0 if match
  393. **/
  394. static int strrcmp(const char *s, const char *sub)
  395. {
  396. int slen, sublen;
  397. if (!s || !sub)
  398. return 1;
  399. slen = strlen(s);
  400. sublen = strlen(sub);
  401. if ((slen == 0) || (sublen == 0))
  402. return 1;
  403. if (sublen > slen)
  404. return 1;
  405. return memcmp(s + slen - sublen, sub, sublen);
  406. }
  407. /**
  408. * Whitelist to allow certain references to pass with no warning.
  409. * Pattern 1:
  410. * If a module parameter is declared __initdata and permissions=0
  411. * then this is legal despite the warning generated.
  412. * We cannot see value of permissions here, so just ignore
  413. * this pattern.
  414. * The pattern is identified by:
  415. * tosec = .init.data
  416. * fromsec = .data*
  417. * atsym =__param*
  418. *
  419. * Pattern 2:
  420. * Many drivers utilise a *_driver container with references to
  421. * add, remove, probe functions etc.
  422. * These functions may often be marked __init and we do not want to
  423. * warn here.
  424. * the pattern is identified by:
  425. * tosec = .init.text | .exit.text
  426. * fromsec = .data
  427. * atsym = *_driver, *_ops, *_probe, *probe_one
  428. **/
  429. static int secref_whitelist(const char *tosec, const char *fromsec,
  430. const char *atsym)
  431. {
  432. int f1 = 1, f2 = 1;
  433. const char **s;
  434. const char *pat2sym[] = {
  435. "_driver",
  436. "_ops",
  437. "_probe",
  438. "_probe_one",
  439. NULL
  440. };
  441. /* Check for pattern 1 */
  442. if (strcmp(tosec, ".init.data") != 0)
  443. f1 = 0;
  444. if (strncmp(fromsec, ".data", strlen(".data")) != 0)
  445. f1 = 0;
  446. if (strncmp(atsym, "__param", strlen("__param")) != 0)
  447. f1 = 0;
  448. if (f1)
  449. return f1;
  450. /* Check for pattern 2 */
  451. if ((strcmp(tosec, ".init.text") != 0) &&
  452. (strcmp(tosec, ".exit.text") != 0))
  453. f2 = 0;
  454. if (strcmp(fromsec, ".data") != 0)
  455. f2 = 0;
  456. for (s = pat2sym; *s; s++)
  457. if (strrcmp(atsym, *s) == 0)
  458. f1 = 1;
  459. return f1 && f2;
  460. }
  461. /**
  462. * Find symbol based on relocation record info.
  463. * In some cases the symbol supplied is a valid symbol so
  464. * return refsym. If st_name != 0 we assume this is a valid symbol.
  465. * In other cases the symbol needs to be looked up in the symbol table
  466. * based on section and address.
  467. * **/
  468. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  469. Elf_Sym *relsym)
  470. {
  471. Elf_Sym *sym;
  472. if (relsym->st_name != 0)
  473. return relsym;
  474. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  475. if (sym->st_shndx != relsym->st_shndx)
  476. continue;
  477. if (sym->st_value == addr)
  478. return sym;
  479. }
  480. return NULL;
  481. }
  482. /*
  483. * Find symbols before or equal addr and after addr - in the section sec.
  484. * If we find two symbols with equal offset prefer one with a valid name.
  485. * The ELF format may have a better way to detect what type of symbol
  486. * it is, but this works for now.
  487. **/
  488. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  489. const char *sec,
  490. Elf_Sym **before, Elf_Sym **after)
  491. {
  492. Elf_Sym *sym;
  493. Elf_Ehdr *hdr = elf->hdr;
  494. Elf_Addr beforediff = ~0;
  495. Elf_Addr afterdiff = ~0;
  496. const char *secstrings = (void *)hdr +
  497. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  498. *before = NULL;
  499. *after = NULL;
  500. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  501. const char *symsec;
  502. if (sym->st_shndx >= SHN_LORESERVE)
  503. continue;
  504. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  505. if (strcmp(symsec, sec) != 0)
  506. continue;
  507. if (sym->st_value <= addr) {
  508. if ((addr - sym->st_value) < beforediff) {
  509. beforediff = addr - sym->st_value;
  510. *before = sym;
  511. }
  512. else if ((addr - sym->st_value) == beforediff) {
  513. /* equal offset, valid name? */
  514. const char *name = elf->strtab + sym->st_name;
  515. if (name && strlen(name))
  516. *before = sym;
  517. }
  518. }
  519. else
  520. {
  521. if ((sym->st_value - addr) < afterdiff) {
  522. afterdiff = sym->st_value - addr;
  523. *after = sym;
  524. }
  525. else if ((sym->st_value - addr) == afterdiff) {
  526. /* equal offset, valid name? */
  527. const char *name = elf->strtab + sym->st_name;
  528. if (name && strlen(name))
  529. *after = sym;
  530. }
  531. }
  532. }
  533. }
  534. /**
  535. * Print a warning about a section mismatch.
  536. * Try to find symbols near it so user can find it.
  537. * Check whitelist before warning - it may be a false positive.
  538. **/
  539. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  540. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  541. {
  542. const char *refsymname = "";
  543. Elf_Sym *before, *after;
  544. Elf_Sym *refsym;
  545. Elf_Ehdr *hdr = elf->hdr;
  546. Elf_Shdr *sechdrs = elf->sechdrs;
  547. const char *secstrings = (void *)hdr +
  548. sechdrs[hdr->e_shstrndx].sh_offset;
  549. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  550. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  551. refsym = find_elf_symbol(elf, r.r_addend, sym);
  552. if (refsym && strlen(elf->strtab + refsym->st_name))
  553. refsymname = elf->strtab + refsym->st_name;
  554. /* check whitelist - we may ignore it */
  555. if (before &&
  556. secref_whitelist(secname, fromsec, elf->strtab + before->st_name))
  557. return;
  558. if (before && after) {
  559. warn("%s - Section mismatch: reference to %s:%s from %s "
  560. "between '%s' (at offset 0x%llx) and '%s'\n",
  561. modname, secname, refsymname, fromsec,
  562. elf->strtab + before->st_name,
  563. (long long)r.r_offset,
  564. elf->strtab + after->st_name);
  565. } else if (before) {
  566. warn("%s - Section mismatch: reference to %s:%s from %s "
  567. "after '%s' (at offset 0x%llx)\n",
  568. modname, secname, refsymname, fromsec,
  569. elf->strtab + before->st_name,
  570. (long long)r.r_offset);
  571. } else if (after) {
  572. warn("%s - Section mismatch: reference to %s:%s from %s "
  573. "before '%s' (at offset -0x%llx)\n",
  574. modname, secname, refsymname, fromsec,
  575. elf->strtab + before->st_name,
  576. (long long)r.r_offset);
  577. } else {
  578. warn("%s - Section mismatch: reference to %s:%s from %s "
  579. "(offset 0x%llx)\n",
  580. modname, secname, fromsec, refsymname,
  581. (long long)r.r_offset);
  582. }
  583. }
  584. /**
  585. * A module includes a number of sections that are discarded
  586. * either when loaded or when used as built-in.
  587. * For loaded modules all functions marked __init and all data
  588. * marked __initdata will be discarded when the module has been intialized.
  589. * Likewise for modules used built-in the sections marked __exit
  590. * are discarded because __exit marked function are supposed to be called
  591. * only when a moduel is unloaded which never happes for built-in modules.
  592. * The check_sec_ref() function traverses all relocation records
  593. * to find all references to a section that reference a section that will
  594. * be discarded and warns about it.
  595. **/
  596. static void check_sec_ref(struct module *mod, const char *modname,
  597. struct elf_info *elf,
  598. int section(const char*),
  599. int section_ref_ok(const char *))
  600. {
  601. int i;
  602. Elf_Sym *sym;
  603. Elf_Ehdr *hdr = elf->hdr;
  604. Elf_Shdr *sechdrs = elf->sechdrs;
  605. const char *secstrings = (void *)hdr +
  606. sechdrs[hdr->e_shstrndx].sh_offset;
  607. /* Walk through all sections */
  608. for (i = 0; i < hdr->e_shnum; i++) {
  609. Elf_Rela *rela;
  610. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  611. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  612. const char *name = secstrings + sechdrs[i].sh_name +
  613. strlen(".rela");
  614. /* We want to process only relocation sections and not .init */
  615. if (section_ref_ok(name) || (sechdrs[i].sh_type != SHT_RELA))
  616. continue;
  617. for (rela = start; rela < stop; rela++) {
  618. Elf_Rela r;
  619. const char *secname;
  620. r.r_offset = TO_NATIVE(rela->r_offset);
  621. r.r_info = TO_NATIVE(rela->r_info);
  622. r.r_addend = TO_NATIVE(rela->r_addend);
  623. sym = elf->symtab_start + ELF_R_SYM(r.r_info);
  624. /* Skip special sections */
  625. if (sym->st_shndx >= SHN_LORESERVE)
  626. continue;
  627. secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  628. if (section(secname))
  629. warn_sec_mismatch(modname, name, elf, sym, r);
  630. }
  631. }
  632. }
  633. /**
  634. * Functions used only during module init is marked __init and is stored in
  635. * a .init.text section. Likewise data is marked __initdata and stored in
  636. * a .init.data section.
  637. * If this section is one of these sections return 1
  638. * See include/linux/init.h for the details
  639. **/
  640. static int init_section(const char *name)
  641. {
  642. if (strcmp(name, ".init") == 0)
  643. return 1;
  644. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  645. return 1;
  646. return 0;
  647. }
  648. /**
  649. * Identify sections from which references to a .init section is OK.
  650. *
  651. * Unfortunately references to read only data that referenced .init
  652. * sections had to be excluded. Almost all of these are false
  653. * positives, they are created by gcc. The downside of excluding rodata
  654. * is that there really are some user references from rodata to
  655. * init code, e.g. drivers/video/vgacon.c:
  656. *
  657. * const struct consw vga_con = {
  658. * con_startup: vgacon_startup,
  659. *
  660. * where vgacon_startup is __init. If you want to wade through the false
  661. * positives, take out the check for rodata.
  662. **/
  663. static int init_section_ref_ok(const char *name)
  664. {
  665. const char **s;
  666. /* Absolute section names */
  667. const char *namelist1[] = {
  668. ".init",
  669. ".opd", /* see comment [OPD] at exit_section_ref_ok() */
  670. ".toc1", /* used by ppc64 */
  671. ".stab",
  672. ".rodata",
  673. ".text.lock",
  674. "__bug_table", /* used by powerpc for BUG() */
  675. ".pci_fixup_header",
  676. ".pci_fixup_final",
  677. ".pdr",
  678. "__param",
  679. NULL
  680. };
  681. /* Start of section names */
  682. const char *namelist2[] = {
  683. ".init.",
  684. ".altinstructions",
  685. ".eh_frame",
  686. ".debug",
  687. NULL
  688. };
  689. /* part of section name */
  690. const char *namelist3 [] = {
  691. ".unwind", /* sample: IA_64.unwind.init.text */
  692. NULL
  693. };
  694. for (s = namelist1; *s; s++)
  695. if (strcmp(*s, name) == 0)
  696. return 1;
  697. for (s = namelist2; *s; s++)
  698. if (strncmp(*s, name, strlen(*s)) == 0)
  699. return 1;
  700. for (s = namelist3; *s; s++)
  701. if (strstr(name, *s) != NULL)
  702. return 1;
  703. return 0;
  704. }
  705. /*
  706. * Functions used only during module exit is marked __exit and is stored in
  707. * a .exit.text section. Likewise data is marked __exitdata and stored in
  708. * a .exit.data section.
  709. * If this section is one of these sections return 1
  710. * See include/linux/init.h for the details
  711. **/
  712. static int exit_section(const char *name)
  713. {
  714. if (strcmp(name, ".exit.text") == 0)
  715. return 1;
  716. if (strcmp(name, ".exit.data") == 0)
  717. return 1;
  718. return 0;
  719. }
  720. /*
  721. * Identify sections from which references to a .exit section is OK.
  722. *
  723. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  724. * For our future {in}sanity, add a comment that this is the ppc .opd
  725. * section, not the ia64 .opd section.
  726. * ia64 .opd should not point to discarded sections.
  727. **/
  728. static int exit_section_ref_ok(const char *name)
  729. {
  730. const char **s;
  731. /* Absolute section names */
  732. const char *namelist1[] = {
  733. ".exit.text",
  734. ".exit.data",
  735. ".init.text",
  736. ".opd", /* See comment [OPD] */
  737. ".toc1", /* used by ppc64 */
  738. ".altinstructions",
  739. ".pdr",
  740. "__bug_table", /* used by powerpc for BUG() */
  741. ".exitcall.exit",
  742. ".eh_frame",
  743. ".stab",
  744. NULL
  745. };
  746. /* Start of section names */
  747. const char *namelist2[] = {
  748. ".debug",
  749. NULL
  750. };
  751. /* part of section name */
  752. const char *namelist3 [] = {
  753. ".unwind", /* Sample: IA_64.unwind.exit.text */
  754. NULL
  755. };
  756. for (s = namelist1; *s; s++)
  757. if (strcmp(*s, name) == 0)
  758. return 1;
  759. for (s = namelist2; *s; s++)
  760. if (strncmp(*s, name, strlen(*s)) == 0)
  761. return 1;
  762. for (s = namelist3; *s; s++)
  763. if (strstr(name, *s) != NULL)
  764. return 1;
  765. return 0;
  766. }
  767. static void read_symbols(char *modname)
  768. {
  769. const char *symname;
  770. char *version;
  771. struct module *mod;
  772. struct elf_info info = { };
  773. Elf_Sym *sym;
  774. parse_elf(&info, modname);
  775. mod = new_module(modname);
  776. /* When there's no vmlinux, don't print warnings about
  777. * unresolved symbols (since there'll be too many ;) */
  778. if (is_vmlinux(modname)) {
  779. have_vmlinux = 1;
  780. mod->skip = 1;
  781. }
  782. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  783. symname = info.strtab + sym->st_name;
  784. handle_modversions(mod, &info, sym, symname);
  785. handle_moddevtable(mod, &info, sym, symname);
  786. }
  787. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  788. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  789. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  790. if (version)
  791. maybe_frob_rcs_version(modname, version, info.modinfo,
  792. version - (char *)info.hdr);
  793. if (version || (all_versions && !is_vmlinux(modname)))
  794. get_src_version(modname, mod->srcversion,
  795. sizeof(mod->srcversion)-1);
  796. parse_elf_finish(&info);
  797. /* Our trick to get versioning for struct_module - it's
  798. * never passed as an argument to an exported function, so
  799. * the automatic versioning doesn't pick it up, but it's really
  800. * important anyhow */
  801. if (modversions)
  802. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  803. }
  804. #define SZ 500
  805. /* We first write the generated file into memory using the
  806. * following helper, then compare to the file on disk and
  807. * only update the later if anything changed */
  808. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  809. const char *fmt, ...)
  810. {
  811. char tmp[SZ];
  812. int len;
  813. va_list ap;
  814. va_start(ap, fmt);
  815. len = vsnprintf(tmp, SZ, fmt, ap);
  816. if (buf->size - buf->pos < len + 1) {
  817. buf->size += 128;
  818. buf->p = realloc(buf->p, buf->size);
  819. }
  820. strncpy(buf->p + buf->pos, tmp, len + 1);
  821. buf->pos += len;
  822. va_end(ap);
  823. }
  824. void buf_write(struct buffer *buf, const char *s, int len)
  825. {
  826. if (buf->size - buf->pos < len) {
  827. buf->size += len;
  828. buf->p = realloc(buf->p, buf->size);
  829. }
  830. strncpy(buf->p + buf->pos, s, len);
  831. buf->pos += len;
  832. }
  833. /**
  834. * Header for the generated file
  835. **/
  836. static void add_header(struct buffer *b, struct module *mod)
  837. {
  838. buf_printf(b, "#include <linux/module.h>\n");
  839. buf_printf(b, "#include <linux/vermagic.h>\n");
  840. buf_printf(b, "#include <linux/compiler.h>\n");
  841. buf_printf(b, "\n");
  842. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  843. buf_printf(b, "\n");
  844. buf_printf(b, "struct module __this_module\n");
  845. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  846. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  847. if (mod->has_init)
  848. buf_printf(b, " .init = init_module,\n");
  849. if (mod->has_cleanup)
  850. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  851. " .exit = cleanup_module,\n"
  852. "#endif\n");
  853. buf_printf(b, "};\n");
  854. }
  855. /**
  856. * Record CRCs for unresolved symbols
  857. **/
  858. static void add_versions(struct buffer *b, struct module *mod)
  859. {
  860. struct symbol *s, *exp;
  861. for (s = mod->unres; s; s = s->next) {
  862. exp = find_symbol(s->name);
  863. if (!exp || exp->module == mod) {
  864. if (have_vmlinux && !s->weak)
  865. warn("\"%s\" [%s.ko] undefined!\n",
  866. s->name, mod->name);
  867. continue;
  868. }
  869. s->module = exp->module;
  870. s->crc_valid = exp->crc_valid;
  871. s->crc = exp->crc;
  872. }
  873. if (!modversions)
  874. return;
  875. buf_printf(b, "\n");
  876. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  877. buf_printf(b, "__attribute_used__\n");
  878. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  879. for (s = mod->unres; s; s = s->next) {
  880. if (!s->module) {
  881. continue;
  882. }
  883. if (!s->crc_valid) {
  884. warn("\"%s\" [%s.ko] has no CRC!\n",
  885. s->name, mod->name);
  886. continue;
  887. }
  888. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  889. }
  890. buf_printf(b, "};\n");
  891. }
  892. static void add_depends(struct buffer *b, struct module *mod,
  893. struct module *modules)
  894. {
  895. struct symbol *s;
  896. struct module *m;
  897. int first = 1;
  898. for (m = modules; m; m = m->next) {
  899. m->seen = is_vmlinux(m->name);
  900. }
  901. buf_printf(b, "\n");
  902. buf_printf(b, "static const char __module_depends[]\n");
  903. buf_printf(b, "__attribute_used__\n");
  904. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  905. buf_printf(b, "\"depends=");
  906. for (s = mod->unres; s; s = s->next) {
  907. if (!s->module)
  908. continue;
  909. if (s->module->seen)
  910. continue;
  911. s->module->seen = 1;
  912. buf_printf(b, "%s%s", first ? "" : ",",
  913. strrchr(s->module->name, '/') + 1);
  914. first = 0;
  915. }
  916. buf_printf(b, "\";\n");
  917. }
  918. static void add_srcversion(struct buffer *b, struct module *mod)
  919. {
  920. if (mod->srcversion[0]) {
  921. buf_printf(b, "\n");
  922. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  923. mod->srcversion);
  924. }
  925. }
  926. static void write_if_changed(struct buffer *b, const char *fname)
  927. {
  928. char *tmp;
  929. FILE *file;
  930. struct stat st;
  931. file = fopen(fname, "r");
  932. if (!file)
  933. goto write;
  934. if (fstat(fileno(file), &st) < 0)
  935. goto close_write;
  936. if (st.st_size != b->pos)
  937. goto close_write;
  938. tmp = NOFAIL(malloc(b->pos));
  939. if (fread(tmp, 1, b->pos, file) != b->pos)
  940. goto free_write;
  941. if (memcmp(tmp, b->p, b->pos) != 0)
  942. goto free_write;
  943. free(tmp);
  944. fclose(file);
  945. return;
  946. free_write:
  947. free(tmp);
  948. close_write:
  949. fclose(file);
  950. write:
  951. file = fopen(fname, "w");
  952. if (!file) {
  953. perror(fname);
  954. exit(1);
  955. }
  956. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  957. perror(fname);
  958. exit(1);
  959. }
  960. fclose(file);
  961. }
  962. static void read_dump(const char *fname, unsigned int kernel)
  963. {
  964. unsigned long size, pos = 0;
  965. void *file = grab_file(fname, &size);
  966. char *line;
  967. if (!file)
  968. /* No symbol versions, silently ignore */
  969. return;
  970. while ((line = get_next_line(&pos, file, size))) {
  971. char *symname, *modname, *d;
  972. unsigned int crc;
  973. struct module *mod;
  974. struct symbol *s;
  975. if (!(symname = strchr(line, '\t')))
  976. goto fail;
  977. *symname++ = '\0';
  978. if (!(modname = strchr(symname, '\t')))
  979. goto fail;
  980. *modname++ = '\0';
  981. if (strchr(modname, '\t'))
  982. goto fail;
  983. crc = strtoul(line, &d, 16);
  984. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  985. goto fail;
  986. if (!(mod = find_module(modname))) {
  987. if (is_vmlinux(modname)) {
  988. have_vmlinux = 1;
  989. }
  990. mod = new_module(NOFAIL(strdup(modname)));
  991. mod->skip = 1;
  992. }
  993. s = sym_add_exported(symname, mod);
  994. s->kernel = kernel;
  995. s->preloaded = 1;
  996. sym_update_crc(symname, mod, crc);
  997. }
  998. return;
  999. fail:
  1000. fatal("parse error in symbol dump file\n");
  1001. }
  1002. /* For normal builds always dump all symbols.
  1003. * For external modules only dump symbols
  1004. * that are not read from kernel Module.symvers.
  1005. **/
  1006. static int dump_sym(struct symbol *sym)
  1007. {
  1008. if (!external_module)
  1009. return 1;
  1010. if (sym->vmlinux || sym->kernel)
  1011. return 0;
  1012. return 1;
  1013. }
  1014. static void write_dump(const char *fname)
  1015. {
  1016. struct buffer buf = { };
  1017. struct symbol *symbol;
  1018. int n;
  1019. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1020. symbol = symbolhash[n];
  1021. while (symbol) {
  1022. if (dump_sym(symbol))
  1023. buf_printf(&buf, "0x%08x\t%s\t%s\n",
  1024. symbol->crc, symbol->name,
  1025. symbol->module->name);
  1026. symbol = symbol->next;
  1027. }
  1028. }
  1029. write_if_changed(&buf, fname);
  1030. }
  1031. int main(int argc, char **argv)
  1032. {
  1033. struct module *mod;
  1034. struct buffer buf = { };
  1035. char fname[SZ];
  1036. char *kernel_read = NULL, *module_read = NULL;
  1037. char *dump_write = NULL;
  1038. int opt;
  1039. while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
  1040. switch(opt) {
  1041. case 'i':
  1042. kernel_read = optarg;
  1043. break;
  1044. case 'I':
  1045. module_read = optarg;
  1046. external_module = 1;
  1047. break;
  1048. case 'm':
  1049. modversions = 1;
  1050. break;
  1051. case 'o':
  1052. dump_write = optarg;
  1053. break;
  1054. case 'a':
  1055. all_versions = 1;
  1056. break;
  1057. default:
  1058. exit(1);
  1059. }
  1060. }
  1061. if (kernel_read)
  1062. read_dump(kernel_read, 1);
  1063. if (module_read)
  1064. read_dump(module_read, 0);
  1065. while (optind < argc) {
  1066. read_symbols(argv[optind++]);
  1067. }
  1068. for (mod = modules; mod; mod = mod->next) {
  1069. if (mod->skip)
  1070. continue;
  1071. buf.pos = 0;
  1072. add_header(&buf, mod);
  1073. add_versions(&buf, mod);
  1074. add_depends(&buf, mod, modules);
  1075. add_moddevtable(&buf, mod);
  1076. add_srcversion(&buf, mod);
  1077. sprintf(fname, "%s.mod.c", mod->name);
  1078. write_if_changed(&buf, fname);
  1079. }
  1080. if (dump_write)
  1081. write_dump(dump_write);
  1082. return 0;
  1083. }