modpost.c 44 KB

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