modpost.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. *
  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: duplicate symbol '%s' previous definition "
  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. * Find symbols before or equal addr and after addr - in the section sec
  392. **/
  393. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  394. const char *sec,
  395. Elf_Sym **before, Elf_Sym **after)
  396. {
  397. Elf_Sym *sym;
  398. Elf_Ehdr *hdr = elf->hdr;
  399. Elf_Addr beforediff = ~0;
  400. Elf_Addr afterdiff = ~0;
  401. const char *secstrings = (void *)hdr +
  402. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  403. *before = NULL;
  404. *after = NULL;
  405. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  406. const char *symsec;
  407. if (sym->st_shndx >= SHN_LORESERVE)
  408. continue;
  409. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  410. if (strcmp(symsec, sec) != 0)
  411. continue;
  412. if (sym->st_value <= addr) {
  413. if ((addr - sym->st_value) < beforediff) {
  414. beforediff = addr - sym->st_value;
  415. *before = sym;
  416. }
  417. }
  418. else
  419. {
  420. if ((sym->st_value - addr) < afterdiff) {
  421. afterdiff = sym->st_value - addr;
  422. *after = sym;
  423. }
  424. }
  425. }
  426. }
  427. /**
  428. * Print a warning about a section mismatch.
  429. * Try to find symbols near it so user can find it.
  430. **/
  431. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  432. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  433. {
  434. Elf_Sym *before;
  435. Elf_Sym *after;
  436. Elf_Ehdr *hdr = elf->hdr;
  437. Elf_Shdr *sechdrs = elf->sechdrs;
  438. const char *secstrings = (void *)hdr +
  439. sechdrs[hdr->e_shstrndx].sh_offset;
  440. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  441. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  442. if (before && after) {
  443. warn("%s - Section mismatch: reference to %s from %s "
  444. "between '%s' (at offset 0x%lx) and '%s'\n",
  445. modname, secname, fromsec,
  446. elf->strtab + before->st_name,
  447. (long)(r.r_offset - before->st_value),
  448. elf->strtab + after->st_name);
  449. } else if (before) {
  450. warn("%s - Section mismatch: reference to %s from %s "
  451. "after '%s' (at offset 0x%lx)\n",
  452. modname, secname, fromsec,
  453. elf->strtab + before->st_name,
  454. (long)(r.r_offset - before->st_value));
  455. } else if (after) {
  456. warn("%s - Section mismatch: reference to %s from %s "
  457. "before '%s' (at offset -0x%lx)\n",
  458. modname, secname, fromsec,
  459. elf->strtab + before->st_name,
  460. (long)(before->st_value - r.r_offset));
  461. } else {
  462. warn("%s - Section mismatch: reference to %s from %s "
  463. "(offset 0x%lx)\n",
  464. modname, secname, fromsec, (long)r.r_offset);
  465. }
  466. }
  467. /**
  468. * A module includes a number of sections that are discarded
  469. * either when loaded or when used as built-in.
  470. * For loaded modules all functions marked __init and all data
  471. * marked __initdata will be discarded when the module has been intialized.
  472. * Likewise for modules used built-in the sections marked __exit
  473. * are discarded because __exit marked function are supposed to be called
  474. * only when a moduel is unloaded which never happes for built-in modules.
  475. * The check_sec_ref() function traverses all relocation records
  476. * to find all references to a section that reference a section that will
  477. * be discarded and warns about it.
  478. **/
  479. static void check_sec_ref(struct module *mod, const char *modname,
  480. struct elf_info *elf,
  481. int section(const char*),
  482. int section_ref_ok(const char *))
  483. {
  484. int i;
  485. Elf_Sym *sym;
  486. Elf_Ehdr *hdr = elf->hdr;
  487. Elf_Shdr *sechdrs = elf->sechdrs;
  488. const char *secstrings = (void *)hdr +
  489. sechdrs[hdr->e_shstrndx].sh_offset;
  490. /* Walk through all sections */
  491. for (i = 0; i < hdr->e_shnum; i++) {
  492. const char *name = secstrings + sechdrs[i].sh_name +
  493. strlen(".rela");
  494. /* We want to process only relocation sections and not .init */
  495. if (section_ref_ok(name) || (sechdrs[i].sh_type != SHT_RELA))
  496. continue;
  497. Elf_Rela *rela;
  498. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  499. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  500. for (rela = start; rela < stop; rela++) {
  501. Elf_Rela r;
  502. const char *secname;
  503. r.r_offset = TO_NATIVE(rela->r_offset);
  504. r.r_info = TO_NATIVE(rela->r_info);
  505. sym = elf->symtab_start + ELF_R_SYM(r.r_info);
  506. secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  507. /* Skip special sections */
  508. if (sym->st_shndx >= SHN_LORESERVE)
  509. continue;
  510. if (section(secname))
  511. warn_sec_mismatch(modname, name, elf, sym, r);
  512. }
  513. }
  514. }
  515. /**
  516. * Functions used only during module init is marked __init and is stored in
  517. * a .init.text section. Likewise data is marked __initdata and stored in
  518. * a .init.data section.
  519. * If this section is one of these sections return 1
  520. * See include/linux/init.h for the details
  521. **/
  522. static int init_section(const char *name)
  523. {
  524. if (strcmp(name, ".init") == 0)
  525. return 1;
  526. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  527. return 1;
  528. return 0;
  529. }
  530. /**
  531. * Identify sections from which references to a .init section is OK.
  532. *
  533. * Unfortunately references to read only data that referenced .init
  534. * sections had to be excluded. Almost all of these are false
  535. * positives, they are created by gcc. The downside of excluding rodata
  536. * is that there really are some user references from rodata to
  537. * init code, e.g. drivers/video/vgacon.c:
  538. *
  539. * const struct consw vga_con = {
  540. * con_startup: vgacon_startup,
  541. *
  542. * where vgacon_startup is __init. If you want to wade through the false
  543. * positives, take out the check for rodata.
  544. **/
  545. static int init_section_ref_ok(const char *name)
  546. {
  547. const char **s;
  548. /* Absolute section names */
  549. const char *namelist1[] = {
  550. ".init",
  551. ".stab",
  552. ".rodata",
  553. ".text.lock",
  554. ".pci_fixup_header",
  555. ".pci_fixup_final",
  556. ".pdr",
  557. "__param",
  558. NULL
  559. };
  560. /* Start of section names */
  561. const char *namelist2[] = {
  562. ".init.",
  563. ".altinstructions",
  564. ".eh_frame",
  565. ".debug",
  566. NULL
  567. };
  568. for (s = namelist1; *s; s++)
  569. if (strcmp(*s, name) == 0)
  570. return 1;
  571. for (s = namelist2; *s; s++)
  572. if (strncmp(*s, name, strlen(*s)) == 0)
  573. return 1;
  574. return 0;
  575. }
  576. /*
  577. * Functions used only during module exit is marked __exit and is stored in
  578. * a .exit.text section. Likewise data is marked __exitdata and stored in
  579. * a .exit.data section.
  580. * If this section is one of these sections return 1
  581. * See include/linux/init.h for the details
  582. **/
  583. static int exit_section(const char *name)
  584. {
  585. if (strcmp(name, ".exit.text") == 0)
  586. return 1;
  587. if (strcmp(name, ".exit.data") == 0)
  588. return 1;
  589. return 0;
  590. }
  591. /*
  592. * Identify sections from which references to a .exit section is OK.
  593. *
  594. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  595. * For our future {in}sanity, add a comment that this is the ppc .opd
  596. * section, not the ia64 .opd section.
  597. * ia64 .opd should not point to discarded sections.
  598. **/
  599. static int exit_section_ref_ok(const char *name)
  600. {
  601. const char **s;
  602. /* Absolute section names */
  603. const char *namelist1[] = {
  604. ".exit.text",
  605. ".exit.data",
  606. ".init.text",
  607. ".opd", /* See comment [OPD] */
  608. ".altinstructions",
  609. ".pdr",
  610. ".exitcall.exit",
  611. ".eh_frame",
  612. ".stab",
  613. NULL
  614. };
  615. /* Start of section names */
  616. const char *namelist2[] = {
  617. ".debug",
  618. NULL
  619. };
  620. for (s = namelist1; *s; s++)
  621. if (strcmp(*s, name) == 0)
  622. return 1;
  623. for (s = namelist2; *s; s++)
  624. if (strncmp(*s, name, strlen(*s)) == 0)
  625. return 1;
  626. return 0;
  627. }
  628. static void read_symbols(char *modname)
  629. {
  630. const char *symname;
  631. char *version;
  632. struct module *mod;
  633. struct elf_info info = { };
  634. Elf_Sym *sym;
  635. parse_elf(&info, modname);
  636. mod = new_module(modname);
  637. /* When there's no vmlinux, don't print warnings about
  638. * unresolved symbols (since there'll be too many ;) */
  639. if (is_vmlinux(modname)) {
  640. have_vmlinux = 1;
  641. mod->skip = 1;
  642. }
  643. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  644. symname = info.strtab + sym->st_name;
  645. handle_modversions(mod, &info, sym, symname);
  646. handle_moddevtable(mod, &info, sym, symname);
  647. }
  648. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  649. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  650. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  651. if (version)
  652. maybe_frob_rcs_version(modname, version, info.modinfo,
  653. version - (char *)info.hdr);
  654. if (version || (all_versions && !is_vmlinux(modname)))
  655. get_src_version(modname, mod->srcversion,
  656. sizeof(mod->srcversion)-1);
  657. parse_elf_finish(&info);
  658. /* Our trick to get versioning for struct_module - it's
  659. * never passed as an argument to an exported function, so
  660. * the automatic versioning doesn't pick it up, but it's really
  661. * important anyhow */
  662. if (modversions)
  663. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  664. }
  665. #define SZ 500
  666. /* We first write the generated file into memory using the
  667. * following helper, then compare to the file on disk and
  668. * only update the later if anything changed */
  669. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  670. const char *fmt, ...)
  671. {
  672. char tmp[SZ];
  673. int len;
  674. va_list ap;
  675. va_start(ap, fmt);
  676. len = vsnprintf(tmp, SZ, fmt, ap);
  677. if (buf->size - buf->pos < len + 1) {
  678. buf->size += 128;
  679. buf->p = realloc(buf->p, buf->size);
  680. }
  681. strncpy(buf->p + buf->pos, tmp, len + 1);
  682. buf->pos += len;
  683. va_end(ap);
  684. }
  685. void buf_write(struct buffer *buf, const char *s, int len)
  686. {
  687. if (buf->size - buf->pos < len) {
  688. buf->size += len;
  689. buf->p = realloc(buf->p, buf->size);
  690. }
  691. strncpy(buf->p + buf->pos, s, len);
  692. buf->pos += len;
  693. }
  694. /**
  695. * Header for the generated file
  696. **/
  697. static void add_header(struct buffer *b, struct module *mod)
  698. {
  699. buf_printf(b, "#include <linux/module.h>\n");
  700. buf_printf(b, "#include <linux/vermagic.h>\n");
  701. buf_printf(b, "#include <linux/compiler.h>\n");
  702. buf_printf(b, "\n");
  703. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  704. buf_printf(b, "\n");
  705. buf_printf(b, "struct module __this_module\n");
  706. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  707. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  708. if (mod->has_init)
  709. buf_printf(b, " .init = init_module,\n");
  710. if (mod->has_cleanup)
  711. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  712. " .exit = cleanup_module,\n"
  713. "#endif\n");
  714. buf_printf(b, "};\n");
  715. }
  716. /**
  717. * Record CRCs for unresolved symbols
  718. **/
  719. static void add_versions(struct buffer *b, struct module *mod)
  720. {
  721. struct symbol *s, *exp;
  722. for (s = mod->unres; s; s = s->next) {
  723. exp = find_symbol(s->name);
  724. if (!exp || exp->module == mod) {
  725. if (have_vmlinux && !s->weak)
  726. warn("\"%s\" [%s.ko] undefined!\n",
  727. s->name, mod->name);
  728. continue;
  729. }
  730. s->module = exp->module;
  731. s->crc_valid = exp->crc_valid;
  732. s->crc = exp->crc;
  733. }
  734. if (!modversions)
  735. return;
  736. buf_printf(b, "\n");
  737. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  738. buf_printf(b, "__attribute_used__\n");
  739. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  740. for (s = mod->unres; s; s = s->next) {
  741. if (!s->module) {
  742. continue;
  743. }
  744. if (!s->crc_valid) {
  745. warn("\"%s\" [%s.ko] has no CRC!\n",
  746. s->name, mod->name);
  747. continue;
  748. }
  749. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  750. }
  751. buf_printf(b, "};\n");
  752. }
  753. static void add_depends(struct buffer *b, struct module *mod,
  754. struct module *modules)
  755. {
  756. struct symbol *s;
  757. struct module *m;
  758. int first = 1;
  759. for (m = modules; m; m = m->next) {
  760. m->seen = is_vmlinux(m->name);
  761. }
  762. buf_printf(b, "\n");
  763. buf_printf(b, "static const char __module_depends[]\n");
  764. buf_printf(b, "__attribute_used__\n");
  765. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  766. buf_printf(b, "\"depends=");
  767. for (s = mod->unres; s; s = s->next) {
  768. if (!s->module)
  769. continue;
  770. if (s->module->seen)
  771. continue;
  772. s->module->seen = 1;
  773. buf_printf(b, "%s%s", first ? "" : ",",
  774. strrchr(s->module->name, '/') + 1);
  775. first = 0;
  776. }
  777. buf_printf(b, "\";\n");
  778. }
  779. static void add_srcversion(struct buffer *b, struct module *mod)
  780. {
  781. if (mod->srcversion[0]) {
  782. buf_printf(b, "\n");
  783. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  784. mod->srcversion);
  785. }
  786. }
  787. static void write_if_changed(struct buffer *b, const char *fname)
  788. {
  789. char *tmp;
  790. FILE *file;
  791. struct stat st;
  792. file = fopen(fname, "r");
  793. if (!file)
  794. goto write;
  795. if (fstat(fileno(file), &st) < 0)
  796. goto close_write;
  797. if (st.st_size != b->pos)
  798. goto close_write;
  799. tmp = NOFAIL(malloc(b->pos));
  800. if (fread(tmp, 1, b->pos, file) != b->pos)
  801. goto free_write;
  802. if (memcmp(tmp, b->p, b->pos) != 0)
  803. goto free_write;
  804. free(tmp);
  805. fclose(file);
  806. return;
  807. free_write:
  808. free(tmp);
  809. close_write:
  810. fclose(file);
  811. write:
  812. file = fopen(fname, "w");
  813. if (!file) {
  814. perror(fname);
  815. exit(1);
  816. }
  817. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  818. perror(fname);
  819. exit(1);
  820. }
  821. fclose(file);
  822. }
  823. static void read_dump(const char *fname, unsigned int kernel)
  824. {
  825. unsigned long size, pos = 0;
  826. void *file = grab_file(fname, &size);
  827. char *line;
  828. if (!file)
  829. /* No symbol versions, silently ignore */
  830. return;
  831. while ((line = get_next_line(&pos, file, size))) {
  832. char *symname, *modname, *d;
  833. unsigned int crc;
  834. struct module *mod;
  835. struct symbol *s;
  836. if (!(symname = strchr(line, '\t')))
  837. goto fail;
  838. *symname++ = '\0';
  839. if (!(modname = strchr(symname, '\t')))
  840. goto fail;
  841. *modname++ = '\0';
  842. if (strchr(modname, '\t'))
  843. goto fail;
  844. crc = strtoul(line, &d, 16);
  845. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  846. goto fail;
  847. if (!(mod = find_module(modname))) {
  848. if (is_vmlinux(modname)) {
  849. have_vmlinux = 1;
  850. }
  851. mod = new_module(NOFAIL(strdup(modname)));
  852. mod->skip = 1;
  853. }
  854. s = sym_add_exported(symname, mod);
  855. s->kernel = kernel;
  856. s->preloaded = 1;
  857. sym_update_crc(symname, mod, crc);
  858. }
  859. return;
  860. fail:
  861. fatal("parse error in symbol dump file\n");
  862. }
  863. /* For normal builds always dump all symbols.
  864. * For external modules only dump symbols
  865. * that are not read from kernel Module.symvers.
  866. **/
  867. static int dump_sym(struct symbol *sym)
  868. {
  869. if (!external_module)
  870. return 1;
  871. if (sym->vmlinux || sym->kernel)
  872. return 0;
  873. return 1;
  874. }
  875. static void write_dump(const char *fname)
  876. {
  877. struct buffer buf = { };
  878. struct symbol *symbol;
  879. int n;
  880. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  881. symbol = symbolhash[n];
  882. while (symbol) {
  883. if (dump_sym(symbol))
  884. buf_printf(&buf, "0x%08x\t%s\t%s\n",
  885. symbol->crc, symbol->name,
  886. symbol->module->name);
  887. symbol = symbol->next;
  888. }
  889. }
  890. write_if_changed(&buf, fname);
  891. }
  892. int main(int argc, char **argv)
  893. {
  894. struct module *mod;
  895. struct buffer buf = { };
  896. char fname[SZ];
  897. char *kernel_read = NULL, *module_read = NULL;
  898. char *dump_write = NULL;
  899. int opt;
  900. while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
  901. switch(opt) {
  902. case 'i':
  903. kernel_read = optarg;
  904. break;
  905. case 'I':
  906. module_read = optarg;
  907. external_module = 1;
  908. break;
  909. case 'm':
  910. modversions = 1;
  911. break;
  912. case 'o':
  913. dump_write = optarg;
  914. break;
  915. case 'a':
  916. all_versions = 1;
  917. break;
  918. default:
  919. exit(1);
  920. }
  921. }
  922. if (kernel_read)
  923. read_dump(kernel_read, 1);
  924. if (module_read)
  925. read_dump(module_read, 0);
  926. while (optind < argc) {
  927. read_symbols(argv[optind++]);
  928. }
  929. for (mod = modules; mod; mod = mod->next) {
  930. if (mod->skip)
  931. continue;
  932. buf.pos = 0;
  933. add_header(&buf, mod);
  934. add_versions(&buf, mod);
  935. add_depends(&buf, mod, modules);
  936. add_moddevtable(&buf, mod);
  937. add_srcversion(&buf, mod);
  938. sprintf(fname, "%s.mod.c", mod->name);
  939. write_if_changed(&buf, fname);
  940. }
  941. if (dump_write)
  942. write_dump(dump_write);
  943. return 0;
  944. }