modpost.c 42 KB

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