modpost.c 42 KB

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