modpost.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. * Functions used only during module init is marked __init and is stored in
  511. * a .init.text section. Likewise data is marked __initdata and stored in
  512. * a .init.data section.
  513. * If this section is one of these sections return 1
  514. * See include/linux/init.h for the details
  515. */
  516. static int init_section(const char *name)
  517. {
  518. if (strcmp(name, ".init") == 0)
  519. return 1;
  520. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  521. return 1;
  522. return 0;
  523. }
  524. /*
  525. * Functions used only during module exit is marked __exit and is stored in
  526. * a .exit.text section. Likewise data is marked __exitdata and stored in
  527. * a .exit.data section.
  528. * If this section is one of these sections return 1
  529. * See include/linux/init.h for the details
  530. **/
  531. static int exit_section(const char *name)
  532. {
  533. if (strcmp(name, ".exit.text") == 0)
  534. return 1;
  535. if (strcmp(name, ".exit.data") == 0)
  536. return 1;
  537. return 0;
  538. }
  539. /*
  540. * Data sections are named like this:
  541. * .data | .data.rel | .data.rel.*
  542. * Return 1 if the specified section is a data section
  543. */
  544. static int data_section(const char *name)
  545. {
  546. if ((strcmp(name, ".data") == 0) ||
  547. (strcmp(name, ".data.rel") == 0) ||
  548. (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0))
  549. return 1;
  550. else
  551. return 0;
  552. }
  553. /**
  554. * Whitelist to allow certain references to pass with no warning.
  555. *
  556. * Pattern 0:
  557. * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
  558. * The pattern is identified by:
  559. * fromsec = .text.init.refok* | .data.init.refok*
  560. *
  561. * Pattern 1:
  562. * If a module parameter is declared __initdata and permissions=0
  563. * then this is legal despite the warning generated.
  564. * We cannot see value of permissions here, so just ignore
  565. * this pattern.
  566. * The pattern is identified by:
  567. * tosec = .init.data
  568. * fromsec = .data*
  569. * atsym =__param*
  570. *
  571. * Pattern 2:
  572. * Many drivers utilise a *driver container with references to
  573. * add, remove, probe functions etc.
  574. * These functions may often be marked __init and we do not want to
  575. * warn here.
  576. * the pattern is identified by:
  577. * tosec = init or exit section
  578. * fromsec = data section
  579. * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer
  580. *
  581. * Pattern 3:
  582. * Whitelist all refereces from .text.head to .init.data
  583. * Whitelist all refereces from .text.head to .init.text
  584. *
  585. * Pattern 4:
  586. * Some symbols belong to init section but still it is ok to reference
  587. * these from non-init sections as these symbols don't have any memory
  588. * allocated for them and symbol address and value are same. So even
  589. * if init section is freed, its ok to reference those symbols.
  590. * For ex. symbols marking the init section boundaries.
  591. * This pattern is identified by
  592. * refsymname = __init_begin, _sinittext, _einittext
  593. *
  594. * Pattern 5:
  595. * Xtensa uses literal sections for constants that are accessed PC-relative.
  596. * Literal sections may safely reference their text sections.
  597. * (Note that the name for the literal section omits any trailing '.text')
  598. * tosec = <section>[.text]
  599. * fromsec = <section>.literal
  600. **/
  601. static int secref_whitelist(const char *modname, const char *tosec,
  602. const char *fromsec, const char *atsym,
  603. const char *refsymname)
  604. {
  605. int len;
  606. const char **s;
  607. const char *pat2sym[] = {
  608. "driver",
  609. "_template", /* scsi uses *_template a lot */
  610. "_timer", /* arm uses ops structures named _timer a lot */
  611. "_sht", /* scsi also used *_sht to some extent */
  612. "_ops",
  613. "_probe",
  614. "_probe_one",
  615. "_console",
  616. NULL
  617. };
  618. const char *pat3refsym[] = {
  619. "__init_begin",
  620. "_sinittext",
  621. "_einittext",
  622. NULL
  623. };
  624. /* Check for pattern 0 */
  625. if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) ||
  626. (strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0))
  627. return 1;
  628. /* Check for pattern 1 */
  629. if ((strcmp(tosec, ".init.data") == 0) &&
  630. (strncmp(fromsec, ".data", strlen(".data")) == 0) &&
  631. (strncmp(atsym, "__param", strlen("__param")) == 0))
  632. return 1;
  633. /* Check for pattern 2 */
  634. if ((init_section(tosec) || exit_section(tosec)) && data_section(fromsec))
  635. for (s = pat2sym; *s; s++)
  636. if (strrcmp(atsym, *s) == 0)
  637. return 1;
  638. /* Check for pattern 3 */
  639. if ((strcmp(fromsec, ".text.head") == 0) &&
  640. ((strcmp(tosec, ".init.data") == 0) ||
  641. (strcmp(tosec, ".init.text") == 0)))
  642. return 1;
  643. /* Check for pattern 4 */
  644. for (s = pat3refsym; *s; s++)
  645. if (strcmp(refsymname, *s) == 0)
  646. return 1;
  647. /* Check for pattern 5 */
  648. if (strrcmp(tosec, ".text") == 0)
  649. len = strlen(tosec) - strlen(".text");
  650. else
  651. len = strlen(tosec);
  652. if ((strncmp(tosec, fromsec, len) == 0) && (strlen(fromsec) > len) &&
  653. (strcmp(fromsec + len, ".literal") == 0))
  654. return 1;
  655. return 0;
  656. }
  657. /**
  658. * Find symbol based on relocation record info.
  659. * In some cases the symbol supplied is a valid symbol so
  660. * return refsym. If st_name != 0 we assume this is a valid symbol.
  661. * In other cases the symbol needs to be looked up in the symbol table
  662. * based on section and address.
  663. * **/
  664. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  665. Elf_Sym *relsym)
  666. {
  667. Elf_Sym *sym;
  668. if (relsym->st_name != 0)
  669. return relsym;
  670. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  671. if (sym->st_shndx != relsym->st_shndx)
  672. continue;
  673. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  674. continue;
  675. if (sym->st_value == addr)
  676. return sym;
  677. }
  678. return NULL;
  679. }
  680. static inline int is_arm_mapping_symbol(const char *str)
  681. {
  682. return str[0] == '$' && strchr("atd", str[1])
  683. && (str[2] == '\0' || str[2] == '.');
  684. }
  685. /*
  686. * If there's no name there, ignore it; likewise, ignore it if it's
  687. * one of the magic symbols emitted used by current ARM tools.
  688. *
  689. * Otherwise if find_symbols_between() returns those symbols, they'll
  690. * fail the whitelist tests and cause lots of false alarms ... fixable
  691. * only by merging __exit and __init sections into __text, bloating
  692. * the kernel (which is especially evil on embedded platforms).
  693. */
  694. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  695. {
  696. const char *name = elf->strtab + sym->st_name;
  697. if (!name || !strlen(name))
  698. return 0;
  699. return !is_arm_mapping_symbol(name);
  700. }
  701. /*
  702. * Find symbols before or equal addr and after addr - in the section sec.
  703. * If we find two symbols with equal offset prefer one with a valid name.
  704. * The ELF format may have a better way to detect what type of symbol
  705. * it is, but this works for now.
  706. **/
  707. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  708. const char *sec,
  709. Elf_Sym **before, Elf_Sym **after)
  710. {
  711. Elf_Sym *sym;
  712. Elf_Ehdr *hdr = elf->hdr;
  713. Elf_Addr beforediff = ~0;
  714. Elf_Addr afterdiff = ~0;
  715. const char *secstrings = (void *)hdr +
  716. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  717. *before = NULL;
  718. *after = NULL;
  719. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  720. const char *symsec;
  721. if (sym->st_shndx >= SHN_LORESERVE)
  722. continue;
  723. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  724. if (strcmp(symsec, sec) != 0)
  725. continue;
  726. if (!is_valid_name(elf, sym))
  727. continue;
  728. if (sym->st_value <= addr) {
  729. if ((addr - sym->st_value) < beforediff) {
  730. beforediff = addr - sym->st_value;
  731. *before = sym;
  732. }
  733. else if ((addr - sym->st_value) == beforediff) {
  734. *before = sym;
  735. }
  736. }
  737. else
  738. {
  739. if ((sym->st_value - addr) < afterdiff) {
  740. afterdiff = sym->st_value - addr;
  741. *after = sym;
  742. }
  743. else if ((sym->st_value - addr) == afterdiff) {
  744. *after = sym;
  745. }
  746. }
  747. }
  748. }
  749. /**
  750. * Print a warning about a section mismatch.
  751. * Try to find symbols near it so user can find it.
  752. * Check whitelist before warning - it may be a false positive.
  753. **/
  754. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  755. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  756. {
  757. const char *refsymname = "";
  758. Elf_Sym *before, *after;
  759. Elf_Sym *refsym;
  760. Elf_Ehdr *hdr = elf->hdr;
  761. Elf_Shdr *sechdrs = elf->sechdrs;
  762. const char *secstrings = (void *)hdr +
  763. sechdrs[hdr->e_shstrndx].sh_offset;
  764. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  765. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  766. refsym = find_elf_symbol(elf, r.r_addend, sym);
  767. if (refsym && strlen(elf->strtab + refsym->st_name))
  768. refsymname = elf->strtab + refsym->st_name;
  769. /* check whitelist - we may ignore it */
  770. if (secref_whitelist(modname, secname, fromsec,
  771. before ? elf->strtab + before->st_name : "",
  772. refsymname))
  773. return;
  774. if (before && after) {
  775. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  776. "(between '%s' and '%s')\n",
  777. modname, fromsec, (unsigned long long)r.r_offset,
  778. secname, refsymname,
  779. elf->strtab + before->st_name,
  780. elf->strtab + after->st_name);
  781. } else if (before) {
  782. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  783. "(after '%s')\n",
  784. modname, fromsec, (unsigned long long)r.r_offset,
  785. secname, refsymname,
  786. elf->strtab + before->st_name);
  787. } else if (after) {
  788. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  789. "before '%s' (at offset -0x%llx)\n",
  790. modname, fromsec, (unsigned long long)r.r_offset,
  791. secname, refsymname,
  792. elf->strtab + after->st_name);
  793. } else {
  794. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
  795. modname, fromsec, (unsigned long long)r.r_offset,
  796. secname, refsymname);
  797. }
  798. }
  799. static unsigned int *reloc_location(struct elf_info *elf,
  800. int rsection, Elf_Rela *r)
  801. {
  802. Elf_Shdr *sechdrs = elf->sechdrs;
  803. int section = sechdrs[rsection].sh_info;
  804. return (void *)elf->hdr + sechdrs[section].sh_offset +
  805. (r->r_offset - sechdrs[section].sh_addr);
  806. }
  807. static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  808. {
  809. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  810. unsigned int *location = reloc_location(elf, rsection, r);
  811. switch (r_typ) {
  812. case R_386_32:
  813. r->r_addend = TO_NATIVE(*location);
  814. break;
  815. case R_386_PC32:
  816. r->r_addend = TO_NATIVE(*location) + 4;
  817. /* For CONFIG_RELOCATABLE=y */
  818. if (elf->hdr->e_type == ET_EXEC)
  819. r->r_addend += r->r_offset;
  820. break;
  821. }
  822. return 0;
  823. }
  824. static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  825. {
  826. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  827. switch (r_typ) {
  828. case R_ARM_ABS32:
  829. /* From ARM ABI: (S + A) | T */
  830. r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info));
  831. break;
  832. case R_ARM_PC24:
  833. /* From ARM ABI: ((S + A) | T) - P */
  834. r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset +
  835. (r->r_offset - elf->sechdrs[rsection].sh_addr));
  836. break;
  837. default:
  838. return 1;
  839. }
  840. return 0;
  841. }
  842. static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  843. {
  844. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  845. unsigned int *location = reloc_location(elf, rsection, r);
  846. unsigned int inst;
  847. if (r_typ == R_MIPS_HI16)
  848. return 1; /* skip this */
  849. inst = TO_NATIVE(*location);
  850. switch (r_typ) {
  851. case R_MIPS_LO16:
  852. r->r_addend = inst & 0xffff;
  853. break;
  854. case R_MIPS_26:
  855. r->r_addend = (inst & 0x03ffffff) << 2;
  856. break;
  857. case R_MIPS_32:
  858. r->r_addend = inst;
  859. break;
  860. }
  861. return 0;
  862. }
  863. /**
  864. * A module includes a number of sections that are discarded
  865. * either when loaded or when used as built-in.
  866. * For loaded modules all functions marked __init and all data
  867. * marked __initdata will be discarded when the module has been intialized.
  868. * Likewise for modules used built-in the sections marked __exit
  869. * are discarded because __exit marked function are supposed to be called
  870. * only when a moduel is unloaded which never happes for built-in modules.
  871. * The check_sec_ref() function traverses all relocation records
  872. * to find all references to a section that reference a section that will
  873. * be discarded and warns about it.
  874. **/
  875. static void check_sec_ref(struct module *mod, const char *modname,
  876. struct elf_info *elf,
  877. int section(const char*),
  878. int section_ref_ok(const char *))
  879. {
  880. int i;
  881. Elf_Sym *sym;
  882. Elf_Ehdr *hdr = elf->hdr;
  883. Elf_Shdr *sechdrs = elf->sechdrs;
  884. const char *secstrings = (void *)hdr +
  885. sechdrs[hdr->e_shstrndx].sh_offset;
  886. /* Walk through all sections */
  887. for (i = 0; i < hdr->e_shnum; i++) {
  888. const char *name = secstrings + sechdrs[i].sh_name;
  889. const char *secname;
  890. Elf_Rela r;
  891. unsigned int r_sym;
  892. /* We want to process only relocation sections and not .init */
  893. if (sechdrs[i].sh_type == SHT_RELA) {
  894. Elf_Rela *rela;
  895. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  896. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  897. name += strlen(".rela");
  898. if (section_ref_ok(name))
  899. continue;
  900. for (rela = start; rela < stop; rela++) {
  901. r.r_offset = TO_NATIVE(rela->r_offset);
  902. #if KERNEL_ELFCLASS == ELFCLASS64
  903. if (hdr->e_machine == EM_MIPS) {
  904. unsigned int r_typ;
  905. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  906. r_sym = TO_NATIVE(r_sym);
  907. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  908. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  909. } else {
  910. r.r_info = TO_NATIVE(rela->r_info);
  911. r_sym = ELF_R_SYM(r.r_info);
  912. }
  913. #else
  914. r.r_info = TO_NATIVE(rela->r_info);
  915. r_sym = ELF_R_SYM(r.r_info);
  916. #endif
  917. r.r_addend = TO_NATIVE(rela->r_addend);
  918. sym = elf->symtab_start + r_sym;
  919. /* Skip special sections */
  920. if (sym->st_shndx >= SHN_LORESERVE)
  921. continue;
  922. secname = secstrings +
  923. sechdrs[sym->st_shndx].sh_name;
  924. if (section(secname))
  925. warn_sec_mismatch(modname, name,
  926. elf, sym, r);
  927. }
  928. } else if (sechdrs[i].sh_type == SHT_REL) {
  929. Elf_Rel *rel;
  930. Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
  931. Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
  932. name += strlen(".rel");
  933. if (section_ref_ok(name))
  934. continue;
  935. for (rel = start; rel < stop; rel++) {
  936. r.r_offset = TO_NATIVE(rel->r_offset);
  937. #if KERNEL_ELFCLASS == ELFCLASS64
  938. if (hdr->e_machine == EM_MIPS) {
  939. unsigned int r_typ;
  940. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  941. r_sym = TO_NATIVE(r_sym);
  942. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  943. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  944. } else {
  945. r.r_info = TO_NATIVE(rel->r_info);
  946. r_sym = ELF_R_SYM(r.r_info);
  947. }
  948. #else
  949. r.r_info = TO_NATIVE(rel->r_info);
  950. r_sym = ELF_R_SYM(r.r_info);
  951. #endif
  952. r.r_addend = 0;
  953. switch (hdr->e_machine) {
  954. case EM_386:
  955. if (addend_386_rel(elf, i, &r))
  956. continue;
  957. break;
  958. case EM_ARM:
  959. if(addend_arm_rel(elf, i, &r))
  960. continue;
  961. break;
  962. case EM_MIPS:
  963. if (addend_mips_rel(elf, i, &r))
  964. continue;
  965. break;
  966. }
  967. sym = elf->symtab_start + r_sym;
  968. /* Skip special sections */
  969. if (sym->st_shndx >= SHN_LORESERVE)
  970. continue;
  971. secname = secstrings +
  972. sechdrs[sym->st_shndx].sh_name;
  973. if (section(secname))
  974. warn_sec_mismatch(modname, name,
  975. elf, sym, r);
  976. }
  977. }
  978. }
  979. }
  980. /*
  981. * Identify sections from which references to either a
  982. * .init or a .exit section is OK.
  983. *
  984. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  985. * For our future {in}sanity, add a comment that this is the ppc .opd
  986. * section, not the ia64 .opd section.
  987. * ia64 .opd should not point to discarded sections.
  988. * [.rodata] like for .init.text we ignore .rodata references -same reason
  989. */
  990. static int initexit_section_ref_ok(const char *name)
  991. {
  992. const char **s;
  993. /* Absolute section names */
  994. const char *namelist1[] = {
  995. "__bug_table", /* used by powerpc for BUG() */
  996. "__ex_table",
  997. ".altinstructions",
  998. ".cranges", /* used by sh64 */
  999. ".fixup",
  1000. ".machvec", /* ia64 + powerpc uses these */
  1001. ".machine.desc",
  1002. ".opd", /* See comment [OPD] */
  1003. ".parainstructions",
  1004. ".pdr",
  1005. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  1006. ".smp_locks",
  1007. ".stab",
  1008. ".m68k_fixup",
  1009. ".xt.prop", /* xtensa informational section */
  1010. ".xt.lit", /* xtensa informational section */
  1011. NULL
  1012. };
  1013. /* Start of section names */
  1014. const char *namelist2[] = {
  1015. ".debug",
  1016. ".eh_frame",
  1017. ".note", /* ignore ELF notes - may contain anything */
  1018. ".got", /* powerpc - global offset table */
  1019. ".toc", /* powerpc - table of contents */
  1020. NULL
  1021. };
  1022. /* part of section name */
  1023. const char *namelist3 [] = {
  1024. ".unwind", /* Sample: IA_64.unwind.exit.text */
  1025. NULL
  1026. };
  1027. for (s = namelist1; *s; s++)
  1028. if (strcmp(*s, name) == 0)
  1029. return 1;
  1030. for (s = namelist2; *s; s++)
  1031. if (strncmp(*s, name, strlen(*s)) == 0)
  1032. return 1;
  1033. for (s = namelist3; *s; s++)
  1034. if (strstr(name, *s) != NULL)
  1035. return 1;
  1036. return 0;
  1037. }
  1038. /*
  1039. * Identify sections from which references to a .init section is OK.
  1040. *
  1041. * Unfortunately references to read only data that referenced .init
  1042. * sections had to be excluded. Almost all of these are false
  1043. * positives, they are created by gcc. The downside of excluding rodata
  1044. * is that there really are some user references from rodata to
  1045. * init code, e.g. drivers/video/vgacon.c:
  1046. *
  1047. * const struct consw vga_con = {
  1048. * con_startup: vgacon_startup,
  1049. *
  1050. * where vgacon_startup is __init. If you want to wade through the false
  1051. * positives, take out the check for rodata.
  1052. */
  1053. static int init_section_ref_ok(const char *name)
  1054. {
  1055. const char **s;
  1056. /* Absolute section names */
  1057. const char *namelist1[] = {
  1058. "__dbe_table", /* MIPS generate these */
  1059. "__ftr_fixup", /* powerpc cpu feature fixup */
  1060. "__fw_ftr_fixup", /* powerpc firmware feature fixup */
  1061. "__param",
  1062. ".data.rel.ro", /* used by parisc64 */
  1063. ".init",
  1064. ".text.lock",
  1065. NULL
  1066. };
  1067. /* Start of section names */
  1068. const char *namelist2[] = {
  1069. ".init.",
  1070. ".pci_fixup",
  1071. ".rodata",
  1072. NULL
  1073. };
  1074. if (initexit_section_ref_ok(name))
  1075. return 1;
  1076. for (s = namelist1; *s; s++)
  1077. if (strcmp(*s, name) == 0)
  1078. return 1;
  1079. for (s = namelist2; *s; s++)
  1080. if (strncmp(*s, name, strlen(*s)) == 0)
  1081. return 1;
  1082. /* If section name ends with ".init" we allow references
  1083. * as is the case with .initcallN.init, .early_param.init, .taglist.init etc
  1084. */
  1085. if (strrcmp(name, ".init") == 0)
  1086. return 1;
  1087. return 0;
  1088. }
  1089. /*
  1090. * Identify sections from which references to a .exit section is OK.
  1091. */
  1092. static int exit_section_ref_ok(const char *name)
  1093. {
  1094. const char **s;
  1095. /* Absolute section names */
  1096. const char *namelist1[] = {
  1097. ".exit.data",
  1098. ".exit.text",
  1099. ".exitcall.exit",
  1100. ".rodata",
  1101. NULL
  1102. };
  1103. if (initexit_section_ref_ok(name))
  1104. return 1;
  1105. for (s = namelist1; *s; s++)
  1106. if (strcmp(*s, name) == 0)
  1107. return 1;
  1108. return 0;
  1109. }
  1110. static void read_symbols(char *modname)
  1111. {
  1112. const char *symname;
  1113. char *version;
  1114. char *license;
  1115. struct module *mod;
  1116. struct elf_info info = { };
  1117. Elf_Sym *sym;
  1118. if (!parse_elf(&info, modname))
  1119. return;
  1120. mod = new_module(modname);
  1121. /* When there's no vmlinux, don't print warnings about
  1122. * unresolved symbols (since there'll be too many ;) */
  1123. if (is_vmlinux(modname)) {
  1124. have_vmlinux = 1;
  1125. mod->skip = 1;
  1126. }
  1127. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1128. while (license) {
  1129. if (license_is_gpl_compatible(license))
  1130. mod->gpl_compatible = 1;
  1131. else {
  1132. mod->gpl_compatible = 0;
  1133. break;
  1134. }
  1135. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1136. "license", license);
  1137. }
  1138. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1139. symname = info.strtab + sym->st_name;
  1140. handle_modversions(mod, &info, sym, symname);
  1141. handle_moddevtable(mod, &info, sym, symname);
  1142. }
  1143. if (is_vmlinux(modname) && vmlinux_section_warnings) {
  1144. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  1145. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  1146. }
  1147. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1148. if (version)
  1149. maybe_frob_rcs_version(modname, version, info.modinfo,
  1150. version - (char *)info.hdr);
  1151. if (version || (all_versions && !is_vmlinux(modname)))
  1152. get_src_version(modname, mod->srcversion,
  1153. sizeof(mod->srcversion)-1);
  1154. parse_elf_finish(&info);
  1155. /* Our trick to get versioning for struct_module - it's
  1156. * never passed as an argument to an exported function, so
  1157. * the automatic versioning doesn't pick it up, but it's really
  1158. * important anyhow */
  1159. if (modversions)
  1160. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1161. }
  1162. #define SZ 500
  1163. /* We first write the generated file into memory using the
  1164. * following helper, then compare to the file on disk and
  1165. * only update the later if anything changed */
  1166. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1167. const char *fmt, ...)
  1168. {
  1169. char tmp[SZ];
  1170. int len;
  1171. va_list ap;
  1172. va_start(ap, fmt);
  1173. len = vsnprintf(tmp, SZ, fmt, ap);
  1174. buf_write(buf, tmp, len);
  1175. va_end(ap);
  1176. }
  1177. void buf_write(struct buffer *buf, const char *s, int len)
  1178. {
  1179. if (buf->size - buf->pos < len) {
  1180. buf->size += len + SZ;
  1181. buf->p = realloc(buf->p, buf->size);
  1182. }
  1183. strncpy(buf->p + buf->pos, s, len);
  1184. buf->pos += len;
  1185. }
  1186. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1187. {
  1188. const char *e = is_vmlinux(m) ?"":".ko";
  1189. switch (exp) {
  1190. case export_gpl:
  1191. fatal("modpost: GPL-incompatible module %s%s "
  1192. "uses GPL-only symbol '%s'\n", m, e, s);
  1193. break;
  1194. case export_unused_gpl:
  1195. fatal("modpost: GPL-incompatible module %s%s "
  1196. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1197. break;
  1198. case export_gpl_future:
  1199. warn("modpost: GPL-incompatible module %s%s "
  1200. "uses future GPL-only symbol '%s'\n", m, e, s);
  1201. break;
  1202. case export_plain:
  1203. case export_unused:
  1204. case export_unknown:
  1205. /* ignore */
  1206. break;
  1207. }
  1208. }
  1209. static void check_for_unused(enum export exp, const char* m, const char* s)
  1210. {
  1211. const char *e = is_vmlinux(m) ?"":".ko";
  1212. switch (exp) {
  1213. case export_unused:
  1214. case export_unused_gpl:
  1215. warn("modpost: module %s%s "
  1216. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1217. break;
  1218. default:
  1219. /* ignore */
  1220. break;
  1221. }
  1222. }
  1223. static void check_exports(struct module *mod)
  1224. {
  1225. struct symbol *s, *exp;
  1226. for (s = mod->unres; s; s = s->next) {
  1227. const char *basename;
  1228. exp = find_symbol(s->name);
  1229. if (!exp || exp->module == mod)
  1230. continue;
  1231. basename = strrchr(mod->name, '/');
  1232. if (basename)
  1233. basename++;
  1234. else
  1235. basename = mod->name;
  1236. if (!mod->gpl_compatible)
  1237. check_for_gpl_usage(exp->export, basename, exp->name);
  1238. check_for_unused(exp->export, basename, exp->name);
  1239. }
  1240. }
  1241. /**
  1242. * Header for the generated file
  1243. **/
  1244. static void add_header(struct buffer *b, struct module *mod)
  1245. {
  1246. buf_printf(b, "#include <linux/module.h>\n");
  1247. buf_printf(b, "#include <linux/vermagic.h>\n");
  1248. buf_printf(b, "#include <linux/compiler.h>\n");
  1249. buf_printf(b, "\n");
  1250. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1251. buf_printf(b, "\n");
  1252. buf_printf(b, "struct module __this_module\n");
  1253. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1254. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1255. if (mod->has_init)
  1256. buf_printf(b, " .init = init_module,\n");
  1257. if (mod->has_cleanup)
  1258. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1259. " .exit = cleanup_module,\n"
  1260. "#endif\n");
  1261. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1262. buf_printf(b, "};\n");
  1263. }
  1264. /**
  1265. * Record CRCs for unresolved symbols
  1266. **/
  1267. static int add_versions(struct buffer *b, struct module *mod)
  1268. {
  1269. struct symbol *s, *exp;
  1270. int err = 0;
  1271. for (s = mod->unres; s; s = s->next) {
  1272. exp = find_symbol(s->name);
  1273. if (!exp || exp->module == mod) {
  1274. if (have_vmlinux && !s->weak) {
  1275. if (warn_unresolved) {
  1276. warn("\"%s\" [%s.ko] undefined!\n",
  1277. s->name, mod->name);
  1278. } else {
  1279. merror("\"%s\" [%s.ko] undefined!\n",
  1280. s->name, mod->name);
  1281. err = 1;
  1282. }
  1283. }
  1284. continue;
  1285. }
  1286. s->module = exp->module;
  1287. s->crc_valid = exp->crc_valid;
  1288. s->crc = exp->crc;
  1289. }
  1290. if (!modversions)
  1291. return err;
  1292. buf_printf(b, "\n");
  1293. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1294. buf_printf(b, "__attribute_used__\n");
  1295. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1296. for (s = mod->unres; s; s = s->next) {
  1297. if (!s->module) {
  1298. continue;
  1299. }
  1300. if (!s->crc_valid) {
  1301. warn("\"%s\" [%s.ko] has no CRC!\n",
  1302. s->name, mod->name);
  1303. continue;
  1304. }
  1305. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1306. }
  1307. buf_printf(b, "};\n");
  1308. return err;
  1309. }
  1310. static void add_depends(struct buffer *b, struct module *mod,
  1311. struct module *modules)
  1312. {
  1313. struct symbol *s;
  1314. struct module *m;
  1315. int first = 1;
  1316. for (m = modules; m; m = m->next) {
  1317. m->seen = is_vmlinux(m->name);
  1318. }
  1319. buf_printf(b, "\n");
  1320. buf_printf(b, "static const char __module_depends[]\n");
  1321. buf_printf(b, "__attribute_used__\n");
  1322. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1323. buf_printf(b, "\"depends=");
  1324. for (s = mod->unres; s; s = s->next) {
  1325. const char *p;
  1326. if (!s->module)
  1327. continue;
  1328. if (s->module->seen)
  1329. continue;
  1330. s->module->seen = 1;
  1331. if ((p = strrchr(s->module->name, '/')) != NULL)
  1332. p++;
  1333. else
  1334. p = s->module->name;
  1335. buf_printf(b, "%s%s", first ? "" : ",", p);
  1336. first = 0;
  1337. }
  1338. buf_printf(b, "\";\n");
  1339. }
  1340. static void add_srcversion(struct buffer *b, struct module *mod)
  1341. {
  1342. if (mod->srcversion[0]) {
  1343. buf_printf(b, "\n");
  1344. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1345. mod->srcversion);
  1346. }
  1347. }
  1348. static void write_if_changed(struct buffer *b, const char *fname)
  1349. {
  1350. char *tmp;
  1351. FILE *file;
  1352. struct stat st;
  1353. file = fopen(fname, "r");
  1354. if (!file)
  1355. goto write;
  1356. if (fstat(fileno(file), &st) < 0)
  1357. goto close_write;
  1358. if (st.st_size != b->pos)
  1359. goto close_write;
  1360. tmp = NOFAIL(malloc(b->pos));
  1361. if (fread(tmp, 1, b->pos, file) != b->pos)
  1362. goto free_write;
  1363. if (memcmp(tmp, b->p, b->pos) != 0)
  1364. goto free_write;
  1365. free(tmp);
  1366. fclose(file);
  1367. return;
  1368. free_write:
  1369. free(tmp);
  1370. close_write:
  1371. fclose(file);
  1372. write:
  1373. file = fopen(fname, "w");
  1374. if (!file) {
  1375. perror(fname);
  1376. exit(1);
  1377. }
  1378. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1379. perror(fname);
  1380. exit(1);
  1381. }
  1382. fclose(file);
  1383. }
  1384. /* parse Module.symvers file. line format:
  1385. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1386. **/
  1387. static void read_dump(const char *fname, unsigned int kernel)
  1388. {
  1389. unsigned long size, pos = 0;
  1390. void *file = grab_file(fname, &size);
  1391. char *line;
  1392. if (!file)
  1393. /* No symbol versions, silently ignore */
  1394. return;
  1395. while ((line = get_next_line(&pos, file, size))) {
  1396. char *symname, *modname, *d, *export, *end;
  1397. unsigned int crc;
  1398. struct module *mod;
  1399. struct symbol *s;
  1400. if (!(symname = strchr(line, '\t')))
  1401. goto fail;
  1402. *symname++ = '\0';
  1403. if (!(modname = strchr(symname, '\t')))
  1404. goto fail;
  1405. *modname++ = '\0';
  1406. if ((export = strchr(modname, '\t')) != NULL)
  1407. *export++ = '\0';
  1408. if (export && ((end = strchr(export, '\t')) != NULL))
  1409. *end = '\0';
  1410. crc = strtoul(line, &d, 16);
  1411. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1412. goto fail;
  1413. if (!(mod = find_module(modname))) {
  1414. if (is_vmlinux(modname)) {
  1415. have_vmlinux = 1;
  1416. }
  1417. mod = new_module(NOFAIL(strdup(modname)));
  1418. mod->skip = 1;
  1419. }
  1420. s = sym_add_exported(symname, mod, export_no(export));
  1421. s->kernel = kernel;
  1422. s->preloaded = 1;
  1423. sym_update_crc(symname, mod, crc, export_no(export));
  1424. }
  1425. return;
  1426. fail:
  1427. fatal("parse error in symbol dump file\n");
  1428. }
  1429. /* For normal builds always dump all symbols.
  1430. * For external modules only dump symbols
  1431. * that are not read from kernel Module.symvers.
  1432. **/
  1433. static int dump_sym(struct symbol *sym)
  1434. {
  1435. if (!external_module)
  1436. return 1;
  1437. if (sym->vmlinux || sym->kernel)
  1438. return 0;
  1439. return 1;
  1440. }
  1441. static void write_dump(const char *fname)
  1442. {
  1443. struct buffer buf = { };
  1444. struct symbol *symbol;
  1445. int n;
  1446. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1447. symbol = symbolhash[n];
  1448. while (symbol) {
  1449. if (dump_sym(symbol))
  1450. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1451. symbol->crc, symbol->name,
  1452. symbol->module->name,
  1453. export_str(symbol->export));
  1454. symbol = symbol->next;
  1455. }
  1456. }
  1457. write_if_changed(&buf, fname);
  1458. }
  1459. int main(int argc, char **argv)
  1460. {
  1461. struct module *mod;
  1462. struct buffer buf = { };
  1463. char fname[SZ];
  1464. char *kernel_read = NULL, *module_read = NULL;
  1465. char *dump_write = NULL;
  1466. int opt;
  1467. int err;
  1468. while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
  1469. switch(opt) {
  1470. case 'i':
  1471. kernel_read = optarg;
  1472. break;
  1473. case 'I':
  1474. module_read = optarg;
  1475. external_module = 1;
  1476. break;
  1477. case 'm':
  1478. modversions = 1;
  1479. break;
  1480. case 'o':
  1481. dump_write = optarg;
  1482. break;
  1483. case 'a':
  1484. all_versions = 1;
  1485. break;
  1486. case 's':
  1487. vmlinux_section_warnings = 0;
  1488. break;
  1489. case 'w':
  1490. warn_unresolved = 1;
  1491. break;
  1492. default:
  1493. exit(1);
  1494. }
  1495. }
  1496. if (kernel_read)
  1497. read_dump(kernel_read, 1);
  1498. if (module_read)
  1499. read_dump(module_read, 0);
  1500. while (optind < argc) {
  1501. read_symbols(argv[optind++]);
  1502. }
  1503. for (mod = modules; mod; mod = mod->next) {
  1504. if (mod->skip)
  1505. continue;
  1506. check_exports(mod);
  1507. }
  1508. err = 0;
  1509. for (mod = modules; mod; mod = mod->next) {
  1510. if (mod->skip)
  1511. continue;
  1512. buf.pos = 0;
  1513. add_header(&buf, mod);
  1514. err |= add_versions(&buf, mod);
  1515. add_depends(&buf, mod, modules);
  1516. add_moddevtable(&buf, mod);
  1517. add_srcversion(&buf, mod);
  1518. sprintf(fname, "%s.mod.c", mod->name);
  1519. write_if_changed(&buf, fname);
  1520. }
  1521. if (dump_write)
  1522. write_dump(dump_write);
  1523. return err;
  1524. }