modpost.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. #include "../../include/linux/license.h"
  16. /* Are we using CONFIG_MODVERSIONS? */
  17. int modversions = 0;
  18. /* Warn about undefined symbols? (do so if we have vmlinux) */
  19. int have_vmlinux = 0;
  20. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  21. static int all_versions = 0;
  22. /* If we are modposting external module set to 1 */
  23. static int external_module = 0;
  24. /* Only warn about unresolved symbols */
  25. static int warn_unresolved = 0;
  26. /* How a symbol is exported */
  27. enum export {
  28. export_plain, export_unused, export_gpl,
  29. export_unused_gpl, export_gpl_future, export_unknown
  30. };
  31. void fatal(const char *fmt, ...)
  32. {
  33. va_list arglist;
  34. fprintf(stderr, "FATAL: ");
  35. va_start(arglist, fmt);
  36. vfprintf(stderr, fmt, arglist);
  37. va_end(arglist);
  38. exit(1);
  39. }
  40. void warn(const char *fmt, ...)
  41. {
  42. va_list arglist;
  43. fprintf(stderr, "WARNING: ");
  44. va_start(arglist, fmt);
  45. vfprintf(stderr, fmt, arglist);
  46. va_end(arglist);
  47. }
  48. static int is_vmlinux(const char *modname)
  49. {
  50. const char *myname;
  51. if ((myname = strrchr(modname, '/')))
  52. myname++;
  53. else
  54. myname = modname;
  55. return strcmp(myname, "vmlinux") == 0;
  56. }
  57. void *do_nofail(void *ptr, const char *expr)
  58. {
  59. if (!ptr) {
  60. fatal("modpost: Memory allocation failure: %s.\n", expr);
  61. }
  62. return ptr;
  63. }
  64. /* A list of all modules we processed */
  65. static struct module *modules;
  66. static struct module *find_module(char *modname)
  67. {
  68. struct module *mod;
  69. for (mod = modules; mod; mod = mod->next)
  70. if (strcmp(mod->name, modname) == 0)
  71. break;
  72. return mod;
  73. }
  74. static struct module *new_module(char *modname)
  75. {
  76. struct module *mod;
  77. char *p, *s;
  78. mod = NOFAIL(malloc(sizeof(*mod)));
  79. memset(mod, 0, sizeof(*mod));
  80. p = NOFAIL(strdup(modname));
  81. /* strip trailing .o */
  82. if ((s = strrchr(p, '.')) != NULL)
  83. if (strcmp(s, ".o") == 0)
  84. *s = '\0';
  85. /* add to list */
  86. mod->name = p;
  87. mod->gpl_compatible = -1;
  88. mod->next = modules;
  89. modules = mod;
  90. return mod;
  91. }
  92. /* A hash of all exported symbols,
  93. * struct symbol is also used for lists of unresolved symbols */
  94. #define SYMBOL_HASH_SIZE 1024
  95. struct symbol {
  96. struct symbol *next;
  97. struct module *module;
  98. unsigned int crc;
  99. int crc_valid;
  100. unsigned int weak:1;
  101. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  102. unsigned int kernel:1; /* 1 if symbol is from kernel
  103. * (only for external modules) **/
  104. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  105. enum export export; /* Type of export */
  106. char name[0];
  107. };
  108. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  109. /* This is based on the hash agorithm from gdbm, via tdb */
  110. static inline unsigned int tdb_hash(const char *name)
  111. {
  112. unsigned value; /* Used to compute the hash value. */
  113. unsigned i; /* Used to cycle through random values. */
  114. /* Set the initial value from the key size. */
  115. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  116. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  117. return (1103515243 * value + 12345);
  118. }
  119. /**
  120. * Allocate a new symbols for use in the hash of exported symbols or
  121. * the list of unresolved symbols per module
  122. **/
  123. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  124. struct symbol *next)
  125. {
  126. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  127. memset(s, 0, sizeof(*s));
  128. strcpy(s->name, name);
  129. s->weak = weak;
  130. s->next = next;
  131. return s;
  132. }
  133. /* For the hash of exported symbols */
  134. static struct symbol *new_symbol(const char *name, struct module *module,
  135. enum export export)
  136. {
  137. unsigned int hash;
  138. struct symbol *new;
  139. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  140. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  141. new->module = module;
  142. new->export = export;
  143. return new;
  144. }
  145. static struct symbol *find_symbol(const char *name)
  146. {
  147. struct symbol *s;
  148. /* For our purposes, .foo matches foo. PPC64 needs this. */
  149. if (name[0] == '.')
  150. name++;
  151. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  152. if (strcmp(s->name, name) == 0)
  153. return s;
  154. }
  155. return NULL;
  156. }
  157. static struct {
  158. const char *str;
  159. enum export export;
  160. } export_list[] = {
  161. { .str = "EXPORT_SYMBOL", .export = export_plain },
  162. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  163. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  164. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  165. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  166. { .str = "(unknown)", .export = export_unknown },
  167. };
  168. static const char *export_str(enum export ex)
  169. {
  170. return export_list[ex].str;
  171. }
  172. static enum export export_no(const char * s)
  173. {
  174. int i;
  175. if (!s)
  176. return export_unknown;
  177. for (i = 0; export_list[i].export != export_unknown; i++) {
  178. if (strcmp(export_list[i].str, s) == 0)
  179. return export_list[i].export;
  180. }
  181. return export_unknown;
  182. }
  183. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  184. {
  185. if (sec == elf->export_sec)
  186. return export_plain;
  187. else if (sec == elf->export_unused_sec)
  188. return export_unused;
  189. else if (sec == elf->export_gpl_sec)
  190. return export_gpl;
  191. else if (sec == elf->export_unused_gpl_sec)
  192. return export_unused_gpl;
  193. else if (sec == elf->export_gpl_future_sec)
  194. return export_gpl_future;
  195. else
  196. return export_unknown;
  197. }
  198. /**
  199. * Add an exported symbol - it may have already been added without a
  200. * CRC, in this case just update the CRC
  201. **/
  202. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  203. enum export export)
  204. {
  205. struct symbol *s = find_symbol(name);
  206. if (!s) {
  207. s = new_symbol(name, mod, export);
  208. } else {
  209. if (!s->preloaded) {
  210. warn("%s: '%s' exported twice. Previous export "
  211. "was in %s%s\n", mod->name, name,
  212. s->module->name,
  213. is_vmlinux(s->module->name) ?"":".ko");
  214. }
  215. }
  216. s->preloaded = 0;
  217. s->vmlinux = is_vmlinux(mod->name);
  218. s->kernel = 0;
  219. s->export = export;
  220. return s;
  221. }
  222. static void sym_update_crc(const char *name, struct module *mod,
  223. unsigned int crc, enum export export)
  224. {
  225. struct symbol *s = find_symbol(name);
  226. if (!s)
  227. s = new_symbol(name, mod, export);
  228. s->crc = crc;
  229. s->crc_valid = 1;
  230. }
  231. void *grab_file(const char *filename, unsigned long *size)
  232. {
  233. struct stat st;
  234. void *map;
  235. int fd;
  236. fd = open(filename, O_RDONLY);
  237. if (fd < 0 || fstat(fd, &st) != 0)
  238. return NULL;
  239. *size = st.st_size;
  240. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  241. close(fd);
  242. if (map == MAP_FAILED)
  243. return NULL;
  244. return map;
  245. }
  246. /**
  247. * Return a copy of the next line in a mmap'ed file.
  248. * spaces in the beginning of the line is trimmed away.
  249. * Return a pointer to a static buffer.
  250. **/
  251. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  252. {
  253. static char line[4096];
  254. int skip = 1;
  255. size_t len = 0;
  256. signed char *p = (signed char *)file + *pos;
  257. char *s = line;
  258. for (; *pos < size ; (*pos)++)
  259. {
  260. if (skip && isspace(*p)) {
  261. p++;
  262. continue;
  263. }
  264. skip = 0;
  265. if (*p != '\n' && (*pos < size)) {
  266. len++;
  267. *s++ = *p++;
  268. if (len > 4095)
  269. break; /* Too long, stop */
  270. } else {
  271. /* End of string */
  272. *s = '\0';
  273. return line;
  274. }
  275. }
  276. /* End of buffer */
  277. return NULL;
  278. }
  279. void release_file(void *file, unsigned long size)
  280. {
  281. munmap(file, size);
  282. }
  283. static int parse_elf(struct elf_info *info, const char *filename)
  284. {
  285. unsigned int i;
  286. Elf_Ehdr *hdr;
  287. Elf_Shdr *sechdrs;
  288. Elf_Sym *sym;
  289. hdr = grab_file(filename, &info->size);
  290. if (!hdr) {
  291. perror(filename);
  292. exit(1);
  293. }
  294. info->hdr = hdr;
  295. if (info->size < sizeof(*hdr)) {
  296. /* file too small, assume this is an empty .o file */
  297. return 0;
  298. }
  299. /* Is this a valid ELF file? */
  300. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  301. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  302. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  303. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  304. /* Not an ELF file - silently ignore it */
  305. return 0;
  306. }
  307. /* Fix endianness in ELF header */
  308. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  309. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  310. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  311. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  312. sechdrs = (void *)hdr + hdr->e_shoff;
  313. info->sechdrs = sechdrs;
  314. /* Fix endianness in section headers */
  315. for (i = 0; i < hdr->e_shnum; i++) {
  316. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  317. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  318. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  319. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  320. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  321. }
  322. /* Find symbol table. */
  323. for (i = 1; i < hdr->e_shnum; i++) {
  324. const char *secstrings
  325. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  326. const char *secname;
  327. if (sechdrs[i].sh_offset > info->size) {
  328. fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
  329. return 0;
  330. }
  331. secname = secstrings + sechdrs[i].sh_name;
  332. if (strcmp(secname, ".modinfo") == 0) {
  333. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  334. info->modinfo_len = sechdrs[i].sh_size;
  335. } else if (strcmp(secname, "__ksymtab") == 0)
  336. info->export_sec = i;
  337. else if (strcmp(secname, "__ksymtab_unused") == 0)
  338. info->export_unused_sec = i;
  339. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  340. info->export_gpl_sec = i;
  341. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  342. info->export_unused_gpl_sec = i;
  343. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  344. info->export_gpl_future_sec = i;
  345. if (sechdrs[i].sh_type != SHT_SYMTAB)
  346. continue;
  347. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  348. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  349. + sechdrs[i].sh_size;
  350. info->strtab = (void *)hdr +
  351. sechdrs[sechdrs[i].sh_link].sh_offset;
  352. }
  353. if (!info->symtab_start) {
  354. fatal("%s has no symtab?\n", filename);
  355. }
  356. /* Fix endianness in symbols */
  357. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  358. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  359. sym->st_name = TO_NATIVE(sym->st_name);
  360. sym->st_value = TO_NATIVE(sym->st_value);
  361. sym->st_size = TO_NATIVE(sym->st_size);
  362. }
  363. return 1;
  364. }
  365. static void parse_elf_finish(struct elf_info *info)
  366. {
  367. release_file(info->hdr, info->size);
  368. }
  369. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  370. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  371. static void handle_modversions(struct module *mod, struct elf_info *info,
  372. Elf_Sym *sym, const char *symname)
  373. {
  374. unsigned int crc;
  375. enum export export = export_from_sec(info, sym->st_shndx);
  376. switch (sym->st_shndx) {
  377. case SHN_COMMON:
  378. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  379. break;
  380. case SHN_ABS:
  381. /* CRC'd symbol */
  382. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  383. crc = (unsigned int) sym->st_value;
  384. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  385. export);
  386. }
  387. break;
  388. case SHN_UNDEF:
  389. /* undefined symbol */
  390. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  391. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  392. break;
  393. /* ignore global offset table */
  394. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  395. break;
  396. /* ignore __this_module, it will be resolved shortly */
  397. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  398. break;
  399. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  400. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  401. /* add compatibility with older glibc */
  402. #ifndef STT_SPARC_REGISTER
  403. #define STT_SPARC_REGISTER STT_REGISTER
  404. #endif
  405. if (info->hdr->e_machine == EM_SPARC ||
  406. info->hdr->e_machine == EM_SPARCV9) {
  407. /* Ignore register directives. */
  408. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  409. break;
  410. if (symname[0] == '.') {
  411. char *munged = strdup(symname);
  412. munged[0] = '_';
  413. munged[1] = toupper(munged[1]);
  414. symname = munged;
  415. }
  416. }
  417. #endif
  418. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  419. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  420. mod->unres = alloc_symbol(symname +
  421. strlen(MODULE_SYMBOL_PREFIX),
  422. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  423. mod->unres);
  424. break;
  425. default:
  426. /* All exported symbols */
  427. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  428. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  429. export);
  430. }
  431. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  432. mod->has_init = 1;
  433. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  434. mod->has_cleanup = 1;
  435. break;
  436. }
  437. }
  438. /**
  439. * Parse tag=value strings from .modinfo section
  440. **/
  441. static char *next_string(char *string, unsigned long *secsize)
  442. {
  443. /* Skip non-zero chars */
  444. while (string[0]) {
  445. string++;
  446. if ((*secsize)-- <= 1)
  447. return NULL;
  448. }
  449. /* Skip any zero padding. */
  450. while (!string[0]) {
  451. string++;
  452. if ((*secsize)-- <= 1)
  453. return NULL;
  454. }
  455. return string;
  456. }
  457. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  458. const char *tag, char *info)
  459. {
  460. char *p;
  461. unsigned int taglen = strlen(tag);
  462. unsigned long size = modinfo_len;
  463. if (info) {
  464. size -= info - (char *)modinfo;
  465. modinfo = next_string(info, &size);
  466. }
  467. for (p = modinfo; p; p = next_string(p, &size)) {
  468. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  469. return p + taglen + 1;
  470. }
  471. return NULL;
  472. }
  473. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  474. const char *tag)
  475. {
  476. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  477. }
  478. /**
  479. * Test if string s ends in string sub
  480. * return 0 if match
  481. **/
  482. static int strrcmp(const char *s, const char *sub)
  483. {
  484. int slen, sublen;
  485. if (!s || !sub)
  486. return 1;
  487. slen = strlen(s);
  488. sublen = strlen(sub);
  489. if ((slen == 0) || (sublen == 0))
  490. return 1;
  491. if (sublen > slen)
  492. return 1;
  493. return memcmp(s + slen - sublen, sub, sublen);
  494. }
  495. /**
  496. * Whitelist to allow certain references to pass with no warning.
  497. * Pattern 1:
  498. * If a module parameter is declared __initdata and permissions=0
  499. * then this is legal despite the warning generated.
  500. * We cannot see value of permissions here, so just ignore
  501. * this pattern.
  502. * The pattern is identified by:
  503. * tosec = .init.data
  504. * fromsec = .data*
  505. * atsym =__param*
  506. *
  507. * Pattern 2:
  508. * Many drivers utilise a *driver container with references to
  509. * add, remove, probe functions etc.
  510. * These functions may often be marked __init and we do not want to
  511. * warn here.
  512. * the pattern is identified by:
  513. * tosec = .init.text | .exit.text | .init.data
  514. * fromsec = .data
  515. * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
  516. *
  517. * Pattern 3:
  518. * Some symbols belong to init section but still it is ok to reference
  519. * these from non-init sections as these symbols don't have any memory
  520. * allocated for them and symbol address and value are same. So even
  521. * if init section is freed, its ok to reference those symbols.
  522. * For ex. symbols marking the init section boundaries.
  523. * This pattern is identified by
  524. * refsymname = __init_begin, _sinittext, _einittext
  525. **/
  526. static int secref_whitelist(const char *modname, const char *tosec,
  527. const char *fromsec, const char *atsym,
  528. const char *refsymname)
  529. {
  530. int f1 = 1, f2 = 1;
  531. const char **s;
  532. const char *pat2sym[] = {
  533. "driver",
  534. "_template", /* scsi uses *_template a lot */
  535. "_sht", /* scsi also used *_sht to some extent */
  536. "_ops",
  537. "_probe",
  538. "_probe_one",
  539. "_console",
  540. NULL
  541. };
  542. const char *pat3refsym[] = {
  543. "__init_begin",
  544. "_sinittext",
  545. "_einittext",
  546. NULL
  547. };
  548. /* Check for pattern 1 */
  549. if (strcmp(tosec, ".init.data") != 0)
  550. f1 = 0;
  551. if (strncmp(fromsec, ".data", strlen(".data")) != 0)
  552. f1 = 0;
  553. if (strncmp(atsym, "__param", strlen("__param")) != 0)
  554. f1 = 0;
  555. if (f1)
  556. return f1;
  557. /* Check for pattern 2 */
  558. if ((strcmp(tosec, ".init.text") != 0) &&
  559. (strcmp(tosec, ".exit.text") != 0) &&
  560. (strcmp(tosec, ".init.data") != 0))
  561. f2 = 0;
  562. if (strcmp(fromsec, ".data") != 0)
  563. f2 = 0;
  564. for (s = pat2sym; *s; s++)
  565. if (strrcmp(atsym, *s) == 0)
  566. f1 = 1;
  567. if (f1 && f2)
  568. return 1;
  569. /* Whitelist all references from .pci_fixup section if vmlinux
  570. * Whitelist all refereces from .text.head to .init.data if vmlinux
  571. * Whitelist all refereces from .text.head to .init.text if vmlinux
  572. */
  573. if (is_vmlinux(modname)) {
  574. if ((strcmp(fromsec, ".pci_fixup") == 0) &&
  575. (strcmp(tosec, ".init.text") == 0))
  576. return 1;
  577. if ((strcmp(fromsec, ".text.head") == 0) &&
  578. ((strcmp(tosec, ".init.data") == 0) ||
  579. (strcmp(tosec, ".init.text") == 0)))
  580. return 1;
  581. /* Check for pattern 3 */
  582. for (s = pat3refsym; *s; s++)
  583. if (strcmp(refsymname, *s) == 0)
  584. return 1;
  585. }
  586. return 0;
  587. }
  588. /**
  589. * Find symbol based on relocation record info.
  590. * In some cases the symbol supplied is a valid symbol so
  591. * return refsym. If st_name != 0 we assume this is a valid symbol.
  592. * In other cases the symbol needs to be looked up in the symbol table
  593. * based on section and address.
  594. * **/
  595. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  596. Elf_Sym *relsym)
  597. {
  598. Elf_Sym *sym;
  599. if (relsym->st_name != 0)
  600. return relsym;
  601. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  602. if (sym->st_shndx != relsym->st_shndx)
  603. continue;
  604. if (sym->st_value == addr)
  605. return sym;
  606. }
  607. return NULL;
  608. }
  609. static inline int is_arm_mapping_symbol(const char *str)
  610. {
  611. return str[0] == '$' && strchr("atd", str[1])
  612. && (str[2] == '\0' || str[2] == '.');
  613. }
  614. /*
  615. * If there's no name there, ignore it; likewise, ignore it if it's
  616. * one of the magic symbols emitted used by current ARM tools.
  617. *
  618. * Otherwise if find_symbols_between() returns those symbols, they'll
  619. * fail the whitelist tests and cause lots of false alarms ... fixable
  620. * only by merging __exit and __init sections into __text, bloating
  621. * the kernel (which is especially evil on embedded platforms).
  622. */
  623. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  624. {
  625. const char *name = elf->strtab + sym->st_name;
  626. if (!name || !strlen(name))
  627. return 0;
  628. return !is_arm_mapping_symbol(name);
  629. }
  630. /*
  631. * Find symbols before or equal addr and after addr - in the section sec.
  632. * If we find two symbols with equal offset prefer one with a valid name.
  633. * The ELF format may have a better way to detect what type of symbol
  634. * it is, but this works for now.
  635. **/
  636. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  637. const char *sec,
  638. Elf_Sym **before, Elf_Sym **after)
  639. {
  640. Elf_Sym *sym;
  641. Elf_Ehdr *hdr = elf->hdr;
  642. Elf_Addr beforediff = ~0;
  643. Elf_Addr afterdiff = ~0;
  644. const char *secstrings = (void *)hdr +
  645. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  646. *before = NULL;
  647. *after = NULL;
  648. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  649. const char *symsec;
  650. if (sym->st_shndx >= SHN_LORESERVE)
  651. continue;
  652. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  653. if (strcmp(symsec, sec) != 0)
  654. continue;
  655. if (!is_valid_name(elf, sym))
  656. continue;
  657. if (sym->st_value <= addr) {
  658. if ((addr - sym->st_value) < beforediff) {
  659. beforediff = addr - sym->st_value;
  660. *before = sym;
  661. }
  662. else if ((addr - sym->st_value) == beforediff) {
  663. *before = sym;
  664. }
  665. }
  666. else
  667. {
  668. if ((sym->st_value - addr) < afterdiff) {
  669. afterdiff = sym->st_value - addr;
  670. *after = sym;
  671. }
  672. else if ((sym->st_value - addr) == afterdiff) {
  673. *after = sym;
  674. }
  675. }
  676. }
  677. }
  678. /**
  679. * Print a warning about a section mismatch.
  680. * Try to find symbols near it so user can find it.
  681. * Check whitelist before warning - it may be a false positive.
  682. **/
  683. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  684. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  685. {
  686. const char *refsymname = "";
  687. Elf_Sym *before, *after;
  688. Elf_Sym *refsym;
  689. Elf_Ehdr *hdr = elf->hdr;
  690. Elf_Shdr *sechdrs = elf->sechdrs;
  691. const char *secstrings = (void *)hdr +
  692. sechdrs[hdr->e_shstrndx].sh_offset;
  693. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  694. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  695. refsym = find_elf_symbol(elf, r.r_addend, sym);
  696. if (refsym && strlen(elf->strtab + refsym->st_name))
  697. refsymname = elf->strtab + refsym->st_name;
  698. /* check whitelist - we may ignore it */
  699. if (before &&
  700. secref_whitelist(modname, secname, fromsec,
  701. elf->strtab + before->st_name, refsymname))
  702. return;
  703. if (before && after) {
  704. warn("%s - Section mismatch: reference to %s:%s from %s "
  705. "between '%s' (at offset 0x%llx) and '%s'\n",
  706. modname, secname, refsymname, fromsec,
  707. elf->strtab + before->st_name,
  708. (long long)r.r_offset,
  709. elf->strtab + after->st_name);
  710. } else if (before) {
  711. warn("%s - Section mismatch: reference to %s:%s from %s "
  712. "after '%s' (at offset 0x%llx)\n",
  713. modname, secname, refsymname, fromsec,
  714. elf->strtab + before->st_name,
  715. (long long)r.r_offset);
  716. } else if (after) {
  717. warn("%s - Section mismatch: reference to %s:%s from %s "
  718. "before '%s' (at offset -0x%llx)\n",
  719. modname, secname, refsymname, fromsec,
  720. elf->strtab + after->st_name,
  721. (long long)r.r_offset);
  722. } else {
  723. warn("%s - Section mismatch: reference to %s:%s from %s "
  724. "(offset 0x%llx)\n",
  725. modname, secname, fromsec, refsymname,
  726. (long long)r.r_offset);
  727. }
  728. }
  729. /**
  730. * A module includes a number of sections that are discarded
  731. * either when loaded or when used as built-in.
  732. * For loaded modules all functions marked __init and all data
  733. * marked __initdata will be discarded when the module has been intialized.
  734. * Likewise for modules used built-in the sections marked __exit
  735. * are discarded because __exit marked function are supposed to be called
  736. * only when a moduel is unloaded which never happes for built-in modules.
  737. * The check_sec_ref() function traverses all relocation records
  738. * to find all references to a section that reference a section that will
  739. * be discarded and warns about it.
  740. **/
  741. static void check_sec_ref(struct module *mod, const char *modname,
  742. struct elf_info *elf,
  743. int section(const char*),
  744. int section_ref_ok(const char *))
  745. {
  746. int i;
  747. Elf_Sym *sym;
  748. Elf_Ehdr *hdr = elf->hdr;
  749. Elf_Shdr *sechdrs = elf->sechdrs;
  750. const char *secstrings = (void *)hdr +
  751. sechdrs[hdr->e_shstrndx].sh_offset;
  752. /* Walk through all sections */
  753. for (i = 0; i < hdr->e_shnum; i++) {
  754. const char *name = secstrings + sechdrs[i].sh_name;
  755. const char *secname;
  756. Elf_Rela r;
  757. unsigned int r_sym;
  758. /* We want to process only relocation sections and not .init */
  759. if (sechdrs[i].sh_type == SHT_RELA) {
  760. Elf_Rela *rela;
  761. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  762. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  763. name += strlen(".rela");
  764. if (section_ref_ok(name))
  765. continue;
  766. for (rela = start; rela < stop; rela++) {
  767. r.r_offset = TO_NATIVE(rela->r_offset);
  768. #if KERNEL_ELFCLASS == ELFCLASS64
  769. if (hdr->e_machine == EM_MIPS) {
  770. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  771. r_sym = TO_NATIVE(r_sym);
  772. } else {
  773. r.r_info = TO_NATIVE(rela->r_info);
  774. r_sym = ELF_R_SYM(r.r_info);
  775. }
  776. #else
  777. r.r_info = TO_NATIVE(rela->r_info);
  778. r_sym = ELF_R_SYM(r.r_info);
  779. #endif
  780. r.r_addend = TO_NATIVE(rela->r_addend);
  781. sym = elf->symtab_start + r_sym;
  782. /* Skip special sections */
  783. if (sym->st_shndx >= SHN_LORESERVE)
  784. continue;
  785. secname = secstrings +
  786. sechdrs[sym->st_shndx].sh_name;
  787. if (section(secname))
  788. warn_sec_mismatch(modname, name,
  789. elf, sym, r);
  790. }
  791. } else if (sechdrs[i].sh_type == SHT_REL) {
  792. Elf_Rel *rel;
  793. Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
  794. Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
  795. name += strlen(".rel");
  796. if (section_ref_ok(name))
  797. continue;
  798. for (rel = start; rel < stop; rel++) {
  799. r.r_offset = TO_NATIVE(rel->r_offset);
  800. #if KERNEL_ELFCLASS == ELFCLASS64
  801. if (hdr->e_machine == EM_MIPS) {
  802. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  803. r_sym = TO_NATIVE(r_sym);
  804. } else {
  805. r.r_info = TO_NATIVE(rel->r_info);
  806. r_sym = ELF_R_SYM(r.r_info);
  807. }
  808. #else
  809. r.r_info = TO_NATIVE(rel->r_info);
  810. r_sym = ELF_R_SYM(r.r_info);
  811. #endif
  812. r.r_addend = 0;
  813. sym = elf->symtab_start + r_sym;
  814. /* Skip special sections */
  815. if (sym->st_shndx >= SHN_LORESERVE)
  816. continue;
  817. secname = secstrings +
  818. sechdrs[sym->st_shndx].sh_name;
  819. if (section(secname))
  820. warn_sec_mismatch(modname, name,
  821. elf, sym, r);
  822. }
  823. }
  824. }
  825. }
  826. /**
  827. * Functions used only during module init is marked __init and is stored in
  828. * a .init.text section. Likewise data is marked __initdata and stored in
  829. * a .init.data section.
  830. * If this section is one of these sections return 1
  831. * See include/linux/init.h for the details
  832. **/
  833. static int init_section(const char *name)
  834. {
  835. if (strcmp(name, ".init") == 0)
  836. return 1;
  837. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  838. return 1;
  839. return 0;
  840. }
  841. /**
  842. * Identify sections from which references to a .init section is OK.
  843. *
  844. * Unfortunately references to read only data that referenced .init
  845. * sections had to be excluded. Almost all of these are false
  846. * positives, they are created by gcc. The downside of excluding rodata
  847. * is that there really are some user references from rodata to
  848. * init code, e.g. drivers/video/vgacon.c:
  849. *
  850. * const struct consw vga_con = {
  851. * con_startup: vgacon_startup,
  852. *
  853. * where vgacon_startup is __init. If you want to wade through the false
  854. * positives, take out the check for rodata.
  855. **/
  856. static int init_section_ref_ok(const char *name)
  857. {
  858. const char **s;
  859. /* Absolute section names */
  860. const char *namelist1[] = {
  861. ".init",
  862. ".opd", /* see comment [OPD] at exit_section_ref_ok() */
  863. ".toc1", /* used by ppc64 */
  864. ".stab",
  865. ".data.rel.ro", /* used by parisc64 */
  866. ".parainstructions",
  867. ".text.lock",
  868. "__bug_table", /* used by powerpc for BUG() */
  869. ".pci_fixup_header",
  870. ".pci_fixup_final",
  871. ".pdr",
  872. "__param",
  873. "__ex_table",
  874. ".fixup",
  875. ".smp_locks",
  876. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  877. "__ftr_fixup", /* powerpc cpu feature fixup */
  878. "__fw_ftr_fixup", /* powerpc firmware feature fixup */
  879. NULL
  880. };
  881. /* Start of section names */
  882. const char *namelist2[] = {
  883. ".init.",
  884. ".altinstructions",
  885. ".eh_frame",
  886. ".debug",
  887. ".parainstructions",
  888. ".rodata",
  889. NULL
  890. };
  891. /* part of section name */
  892. const char *namelist3 [] = {
  893. ".unwind", /* sample: IA_64.unwind.init.text */
  894. NULL
  895. };
  896. for (s = namelist1; *s; s++)
  897. if (strcmp(*s, name) == 0)
  898. return 1;
  899. for (s = namelist2; *s; s++)
  900. if (strncmp(*s, name, strlen(*s)) == 0)
  901. return 1;
  902. for (s = namelist3; *s; s++)
  903. if (strstr(name, *s) != NULL)
  904. return 1;
  905. if (strrcmp(name, ".init") == 0)
  906. return 1;
  907. return 0;
  908. }
  909. /*
  910. * Functions used only during module exit is marked __exit and is stored in
  911. * a .exit.text section. Likewise data is marked __exitdata and stored in
  912. * a .exit.data section.
  913. * If this section is one of these sections return 1
  914. * See include/linux/init.h for the details
  915. **/
  916. static int exit_section(const char *name)
  917. {
  918. if (strcmp(name, ".exit.text") == 0)
  919. return 1;
  920. if (strcmp(name, ".exit.data") == 0)
  921. return 1;
  922. return 0;
  923. }
  924. /*
  925. * Identify sections from which references to a .exit section is OK.
  926. *
  927. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  928. * For our future {in}sanity, add a comment that this is the ppc .opd
  929. * section, not the ia64 .opd section.
  930. * ia64 .opd should not point to discarded sections.
  931. * [.rodata] like for .init.text we ignore .rodata references -same reason
  932. **/
  933. static int exit_section_ref_ok(const char *name)
  934. {
  935. const char **s;
  936. /* Absolute section names */
  937. const char *namelist1[] = {
  938. ".exit.text",
  939. ".exit.data",
  940. ".init.text",
  941. ".rodata",
  942. ".opd", /* See comment [OPD] */
  943. ".toc1", /* used by ppc64 */
  944. ".altinstructions",
  945. ".pdr",
  946. "__bug_table", /* used by powerpc for BUG() */
  947. ".exitcall.exit",
  948. ".eh_frame",
  949. ".parainstructions",
  950. ".stab",
  951. "__ex_table",
  952. ".fixup",
  953. ".smp_locks",
  954. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  955. NULL
  956. };
  957. /* Start of section names */
  958. const char *namelist2[] = {
  959. ".debug",
  960. NULL
  961. };
  962. /* part of section name */
  963. const char *namelist3 [] = {
  964. ".unwind", /* Sample: IA_64.unwind.exit.text */
  965. NULL
  966. };
  967. for (s = namelist1; *s; s++)
  968. if (strcmp(*s, name) == 0)
  969. return 1;
  970. for (s = namelist2; *s; s++)
  971. if (strncmp(*s, name, strlen(*s)) == 0)
  972. return 1;
  973. for (s = namelist3; *s; s++)
  974. if (strstr(name, *s) != NULL)
  975. return 1;
  976. return 0;
  977. }
  978. static void read_symbols(char *modname)
  979. {
  980. const char *symname;
  981. char *version;
  982. char *license;
  983. struct module *mod;
  984. struct elf_info info = { };
  985. Elf_Sym *sym;
  986. if (!parse_elf(&info, modname))
  987. return;
  988. mod = new_module(modname);
  989. /* When there's no vmlinux, don't print warnings about
  990. * unresolved symbols (since there'll be too many ;) */
  991. if (is_vmlinux(modname)) {
  992. have_vmlinux = 1;
  993. mod->skip = 1;
  994. }
  995. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  996. while (license) {
  997. if (license_is_gpl_compatible(license))
  998. mod->gpl_compatible = 1;
  999. else {
  1000. mod->gpl_compatible = 0;
  1001. break;
  1002. }
  1003. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1004. "license", license);
  1005. }
  1006. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1007. symname = info.strtab + sym->st_name;
  1008. handle_modversions(mod, &info, sym, symname);
  1009. handle_moddevtable(mod, &info, sym, symname);
  1010. }
  1011. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  1012. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  1013. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1014. if (version)
  1015. maybe_frob_rcs_version(modname, version, info.modinfo,
  1016. version - (char *)info.hdr);
  1017. if (version || (all_versions && !is_vmlinux(modname)))
  1018. get_src_version(modname, mod->srcversion,
  1019. sizeof(mod->srcversion)-1);
  1020. parse_elf_finish(&info);
  1021. /* Our trick to get versioning for struct_module - it's
  1022. * never passed as an argument to an exported function, so
  1023. * the automatic versioning doesn't pick it up, but it's really
  1024. * important anyhow */
  1025. if (modversions)
  1026. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1027. }
  1028. #define SZ 500
  1029. /* We first write the generated file into memory using the
  1030. * following helper, then compare to the file on disk and
  1031. * only update the later if anything changed */
  1032. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1033. const char *fmt, ...)
  1034. {
  1035. char tmp[SZ];
  1036. int len;
  1037. va_list ap;
  1038. va_start(ap, fmt);
  1039. len = vsnprintf(tmp, SZ, fmt, ap);
  1040. buf_write(buf, tmp, len);
  1041. va_end(ap);
  1042. }
  1043. void buf_write(struct buffer *buf, const char *s, int len)
  1044. {
  1045. if (buf->size - buf->pos < len) {
  1046. buf->size += len + SZ;
  1047. buf->p = realloc(buf->p, buf->size);
  1048. }
  1049. strncpy(buf->p + buf->pos, s, len);
  1050. buf->pos += len;
  1051. }
  1052. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1053. {
  1054. const char *e = is_vmlinux(m) ?"":".ko";
  1055. switch (exp) {
  1056. case export_gpl:
  1057. fatal("modpost: GPL-incompatible module %s%s "
  1058. "uses GPL-only symbol '%s'\n", m, e, s);
  1059. break;
  1060. case export_unused_gpl:
  1061. fatal("modpost: GPL-incompatible module %s%s "
  1062. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1063. break;
  1064. case export_gpl_future:
  1065. warn("modpost: GPL-incompatible module %s%s "
  1066. "uses future GPL-only symbol '%s'\n", m, e, s);
  1067. break;
  1068. case export_plain:
  1069. case export_unused:
  1070. case export_unknown:
  1071. /* ignore */
  1072. break;
  1073. }
  1074. }
  1075. static void check_for_unused(enum export exp, const char* m, const char* s)
  1076. {
  1077. const char *e = is_vmlinux(m) ?"":".ko";
  1078. switch (exp) {
  1079. case export_unused:
  1080. case export_unused_gpl:
  1081. warn("modpost: module %s%s "
  1082. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1083. break;
  1084. default:
  1085. /* ignore */
  1086. break;
  1087. }
  1088. }
  1089. static void check_exports(struct module *mod)
  1090. {
  1091. struct symbol *s, *exp;
  1092. for (s = mod->unres; s; s = s->next) {
  1093. const char *basename;
  1094. exp = find_symbol(s->name);
  1095. if (!exp || exp->module == mod)
  1096. continue;
  1097. basename = strrchr(mod->name, '/');
  1098. if (basename)
  1099. basename++;
  1100. else
  1101. basename = mod->name;
  1102. if (!mod->gpl_compatible)
  1103. check_for_gpl_usage(exp->export, basename, exp->name);
  1104. check_for_unused(exp->export, basename, exp->name);
  1105. }
  1106. }
  1107. /**
  1108. * Header for the generated file
  1109. **/
  1110. static void add_header(struct buffer *b, struct module *mod)
  1111. {
  1112. buf_printf(b, "#include <linux/module.h>\n");
  1113. buf_printf(b, "#include <linux/vermagic.h>\n");
  1114. buf_printf(b, "#include <linux/compiler.h>\n");
  1115. buf_printf(b, "\n");
  1116. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1117. buf_printf(b, "\n");
  1118. buf_printf(b, "struct module __this_module\n");
  1119. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1120. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1121. if (mod->has_init)
  1122. buf_printf(b, " .init = init_module,\n");
  1123. if (mod->has_cleanup)
  1124. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1125. " .exit = cleanup_module,\n"
  1126. "#endif\n");
  1127. buf_printf(b, "};\n");
  1128. }
  1129. /**
  1130. * Record CRCs for unresolved symbols
  1131. **/
  1132. static int add_versions(struct buffer *b, struct module *mod)
  1133. {
  1134. struct symbol *s, *exp;
  1135. int err = 0;
  1136. for (s = mod->unres; s; s = s->next) {
  1137. exp = find_symbol(s->name);
  1138. if (!exp || exp->module == mod) {
  1139. if (have_vmlinux && !s->weak) {
  1140. warn("\"%s\" [%s.ko] undefined!\n",
  1141. s->name, mod->name);
  1142. err = warn_unresolved ? 0 : 1;
  1143. }
  1144. continue;
  1145. }
  1146. s->module = exp->module;
  1147. s->crc_valid = exp->crc_valid;
  1148. s->crc = exp->crc;
  1149. }
  1150. if (!modversions)
  1151. return err;
  1152. buf_printf(b, "\n");
  1153. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1154. buf_printf(b, "__attribute_used__\n");
  1155. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1156. for (s = mod->unres; s; s = s->next) {
  1157. if (!s->module) {
  1158. continue;
  1159. }
  1160. if (!s->crc_valid) {
  1161. warn("\"%s\" [%s.ko] has no CRC!\n",
  1162. s->name, mod->name);
  1163. continue;
  1164. }
  1165. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1166. }
  1167. buf_printf(b, "};\n");
  1168. return err;
  1169. }
  1170. static void add_depends(struct buffer *b, struct module *mod,
  1171. struct module *modules)
  1172. {
  1173. struct symbol *s;
  1174. struct module *m;
  1175. int first = 1;
  1176. for (m = modules; m; m = m->next) {
  1177. m->seen = is_vmlinux(m->name);
  1178. }
  1179. buf_printf(b, "\n");
  1180. buf_printf(b, "static const char __module_depends[]\n");
  1181. buf_printf(b, "__attribute_used__\n");
  1182. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1183. buf_printf(b, "\"depends=");
  1184. for (s = mod->unres; s; s = s->next) {
  1185. if (!s->module)
  1186. continue;
  1187. if (s->module->seen)
  1188. continue;
  1189. s->module->seen = 1;
  1190. buf_printf(b, "%s%s", first ? "" : ",",
  1191. strrchr(s->module->name, '/') + 1);
  1192. first = 0;
  1193. }
  1194. buf_printf(b, "\";\n");
  1195. }
  1196. static void add_srcversion(struct buffer *b, struct module *mod)
  1197. {
  1198. if (mod->srcversion[0]) {
  1199. buf_printf(b, "\n");
  1200. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1201. mod->srcversion);
  1202. }
  1203. }
  1204. static void write_if_changed(struct buffer *b, const char *fname)
  1205. {
  1206. char *tmp;
  1207. FILE *file;
  1208. struct stat st;
  1209. file = fopen(fname, "r");
  1210. if (!file)
  1211. goto write;
  1212. if (fstat(fileno(file), &st) < 0)
  1213. goto close_write;
  1214. if (st.st_size != b->pos)
  1215. goto close_write;
  1216. tmp = NOFAIL(malloc(b->pos));
  1217. if (fread(tmp, 1, b->pos, file) != b->pos)
  1218. goto free_write;
  1219. if (memcmp(tmp, b->p, b->pos) != 0)
  1220. goto free_write;
  1221. free(tmp);
  1222. fclose(file);
  1223. return;
  1224. free_write:
  1225. free(tmp);
  1226. close_write:
  1227. fclose(file);
  1228. write:
  1229. file = fopen(fname, "w");
  1230. if (!file) {
  1231. perror(fname);
  1232. exit(1);
  1233. }
  1234. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1235. perror(fname);
  1236. exit(1);
  1237. }
  1238. fclose(file);
  1239. }
  1240. /* parse Module.symvers file. line format:
  1241. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1242. **/
  1243. static void read_dump(const char *fname, unsigned int kernel)
  1244. {
  1245. unsigned long size, pos = 0;
  1246. void *file = grab_file(fname, &size);
  1247. char *line;
  1248. if (!file)
  1249. /* No symbol versions, silently ignore */
  1250. return;
  1251. while ((line = get_next_line(&pos, file, size))) {
  1252. char *symname, *modname, *d, *export, *end;
  1253. unsigned int crc;
  1254. struct module *mod;
  1255. struct symbol *s;
  1256. if (!(symname = strchr(line, '\t')))
  1257. goto fail;
  1258. *symname++ = '\0';
  1259. if (!(modname = strchr(symname, '\t')))
  1260. goto fail;
  1261. *modname++ = '\0';
  1262. if ((export = strchr(modname, '\t')) != NULL)
  1263. *export++ = '\0';
  1264. if (export && ((end = strchr(export, '\t')) != NULL))
  1265. *end = '\0';
  1266. crc = strtoul(line, &d, 16);
  1267. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1268. goto fail;
  1269. if (!(mod = find_module(modname))) {
  1270. if (is_vmlinux(modname)) {
  1271. have_vmlinux = 1;
  1272. }
  1273. mod = new_module(NOFAIL(strdup(modname)));
  1274. mod->skip = 1;
  1275. }
  1276. s = sym_add_exported(symname, mod, export_no(export));
  1277. s->kernel = kernel;
  1278. s->preloaded = 1;
  1279. sym_update_crc(symname, mod, crc, export_no(export));
  1280. }
  1281. return;
  1282. fail:
  1283. fatal("parse error in symbol dump file\n");
  1284. }
  1285. /* For normal builds always dump all symbols.
  1286. * For external modules only dump symbols
  1287. * that are not read from kernel Module.symvers.
  1288. **/
  1289. static int dump_sym(struct symbol *sym)
  1290. {
  1291. if (!external_module)
  1292. return 1;
  1293. if (sym->vmlinux || sym->kernel)
  1294. return 0;
  1295. return 1;
  1296. }
  1297. static void write_dump(const char *fname)
  1298. {
  1299. struct buffer buf = { };
  1300. struct symbol *symbol;
  1301. int n;
  1302. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1303. symbol = symbolhash[n];
  1304. while (symbol) {
  1305. if (dump_sym(symbol))
  1306. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1307. symbol->crc, symbol->name,
  1308. symbol->module->name,
  1309. export_str(symbol->export));
  1310. symbol = symbol->next;
  1311. }
  1312. }
  1313. write_if_changed(&buf, fname);
  1314. }
  1315. int main(int argc, char **argv)
  1316. {
  1317. struct module *mod;
  1318. struct buffer buf = { };
  1319. char fname[SZ];
  1320. char *kernel_read = NULL, *module_read = NULL;
  1321. char *dump_write = NULL;
  1322. int opt;
  1323. int err;
  1324. while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
  1325. switch(opt) {
  1326. case 'i':
  1327. kernel_read = optarg;
  1328. break;
  1329. case 'I':
  1330. module_read = optarg;
  1331. external_module = 1;
  1332. break;
  1333. case 'm':
  1334. modversions = 1;
  1335. break;
  1336. case 'o':
  1337. dump_write = optarg;
  1338. break;
  1339. case 'a':
  1340. all_versions = 1;
  1341. break;
  1342. case 'w':
  1343. warn_unresolved = 1;
  1344. break;
  1345. default:
  1346. exit(1);
  1347. }
  1348. }
  1349. if (kernel_read)
  1350. read_dump(kernel_read, 1);
  1351. if (module_read)
  1352. read_dump(module_read, 0);
  1353. while (optind < argc) {
  1354. read_symbols(argv[optind++]);
  1355. }
  1356. for (mod = modules; mod; mod = mod->next) {
  1357. if (mod->skip)
  1358. continue;
  1359. check_exports(mod);
  1360. }
  1361. err = 0;
  1362. for (mod = modules; mod; mod = mod->next) {
  1363. if (mod->skip)
  1364. continue;
  1365. buf.pos = 0;
  1366. add_header(&buf, mod);
  1367. err |= add_versions(&buf, mod);
  1368. add_depends(&buf, mod, modules);
  1369. add_moddevtable(&buf, mod);
  1370. add_srcversion(&buf, mod);
  1371. sprintf(fname, "%s.mod.c", mod->name);
  1372. write_if_changed(&buf, fname);
  1373. }
  1374. if (dump_write)
  1375. write_dump(dump_write);
  1376. return err;
  1377. }