modpost.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include "modpost.h"
  17. #include "../../include/generated/autoconf.h"
  18. #include "../../include/linux/license.h"
  19. /* Some toolchains use a `_' prefix for all user symbols. */
  20. #ifdef CONFIG_SYMBOL_PREFIX
  21. #define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
  22. #else
  23. #define MODULE_SYMBOL_PREFIX ""
  24. #endif
  25. /* Are we using CONFIG_MODVERSIONS? */
  26. int modversions = 0;
  27. /* Warn about undefined symbols? (do so if we have vmlinux) */
  28. int have_vmlinux = 0;
  29. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  30. static int all_versions = 0;
  31. /* If we are modposting external module set to 1 */
  32. static int external_module = 0;
  33. /* Warn about section mismatch in vmlinux if set to 1 */
  34. static int vmlinux_section_warnings = 1;
  35. /* Only warn about unresolved symbols */
  36. static int warn_unresolved = 0;
  37. /* How a symbol is exported */
  38. static int sec_mismatch_count = 0;
  39. static int sec_mismatch_verbose = 1;
  40. enum export {
  41. export_plain, export_unused, export_gpl,
  42. export_unused_gpl, export_gpl_future, export_unknown
  43. };
  44. #define PRINTF __attribute__ ((format (printf, 1, 2)))
  45. PRINTF void fatal(const char *fmt, ...)
  46. {
  47. va_list arglist;
  48. fprintf(stderr, "FATAL: ");
  49. va_start(arglist, fmt);
  50. vfprintf(stderr, fmt, arglist);
  51. va_end(arglist);
  52. exit(1);
  53. }
  54. PRINTF void warn(const char *fmt, ...)
  55. {
  56. va_list arglist;
  57. fprintf(stderr, "WARNING: ");
  58. va_start(arglist, fmt);
  59. vfprintf(stderr, fmt, arglist);
  60. va_end(arglist);
  61. }
  62. PRINTF void merror(const char *fmt, ...)
  63. {
  64. va_list arglist;
  65. fprintf(stderr, "ERROR: ");
  66. va_start(arglist, fmt);
  67. vfprintf(stderr, fmt, arglist);
  68. va_end(arglist);
  69. }
  70. static int is_vmlinux(const char *modname)
  71. {
  72. const char *myname;
  73. myname = strrchr(modname, '/');
  74. if (myname)
  75. myname++;
  76. else
  77. myname = modname;
  78. return (strcmp(myname, "vmlinux") == 0) ||
  79. (strcmp(myname, "vmlinux.o") == 0);
  80. }
  81. void *do_nofail(void *ptr, const char *expr)
  82. {
  83. if (!ptr)
  84. fatal("modpost: Memory allocation failure: %s.\n", expr);
  85. return ptr;
  86. }
  87. /* A list of all modules we processed */
  88. static struct module *modules;
  89. static struct module *find_module(char *modname)
  90. {
  91. struct module *mod;
  92. for (mod = modules; mod; mod = mod->next)
  93. if (strcmp(mod->name, modname) == 0)
  94. break;
  95. return mod;
  96. }
  97. static struct module *new_module(char *modname)
  98. {
  99. struct module *mod;
  100. char *p, *s;
  101. mod = NOFAIL(malloc(sizeof(*mod)));
  102. memset(mod, 0, sizeof(*mod));
  103. p = NOFAIL(strdup(modname));
  104. /* strip trailing .o */
  105. s = strrchr(p, '.');
  106. if (s != NULL)
  107. if (strcmp(s, ".o") == 0)
  108. *s = '\0';
  109. /* add to list */
  110. mod->name = p;
  111. mod->gpl_compatible = -1;
  112. mod->next = modules;
  113. modules = mod;
  114. return mod;
  115. }
  116. /* A hash of all exported symbols,
  117. * struct symbol is also used for lists of unresolved symbols */
  118. #define SYMBOL_HASH_SIZE 1024
  119. struct symbol {
  120. struct symbol *next;
  121. struct module *module;
  122. unsigned int crc;
  123. int crc_valid;
  124. unsigned int weak:1;
  125. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  126. unsigned int kernel:1; /* 1 if symbol is from kernel
  127. * (only for external modules) **/
  128. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  129. enum export export; /* Type of export */
  130. char name[0];
  131. };
  132. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  133. /* This is based on the hash agorithm from gdbm, via tdb */
  134. static inline unsigned int tdb_hash(const char *name)
  135. {
  136. unsigned value; /* Used to compute the hash value. */
  137. unsigned i; /* Used to cycle through random values. */
  138. /* Set the initial value from the key size. */
  139. for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
  140. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  141. return (1103515243 * value + 12345);
  142. }
  143. /**
  144. * Allocate a new symbols for use in the hash of exported symbols or
  145. * the list of unresolved symbols per module
  146. **/
  147. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  148. struct symbol *next)
  149. {
  150. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  151. memset(s, 0, sizeof(*s));
  152. strcpy(s->name, name);
  153. s->weak = weak;
  154. s->next = next;
  155. return s;
  156. }
  157. /* For the hash of exported symbols */
  158. static struct symbol *new_symbol(const char *name, struct module *module,
  159. enum export export)
  160. {
  161. unsigned int hash;
  162. struct symbol *new;
  163. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  164. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  165. new->module = module;
  166. new->export = export;
  167. return new;
  168. }
  169. static struct symbol *find_symbol(const char *name)
  170. {
  171. struct symbol *s;
  172. /* For our purposes, .foo matches foo. PPC64 needs this. */
  173. if (name[0] == '.')
  174. name++;
  175. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
  176. if (strcmp(s->name, name) == 0)
  177. return s;
  178. }
  179. return NULL;
  180. }
  181. static struct {
  182. const char *str;
  183. enum export export;
  184. } export_list[] = {
  185. { .str = "EXPORT_SYMBOL", .export = export_plain },
  186. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  187. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  188. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  189. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  190. { .str = "(unknown)", .export = export_unknown },
  191. };
  192. static const char *export_str(enum export ex)
  193. {
  194. return export_list[ex].str;
  195. }
  196. static enum export export_no(const char *s)
  197. {
  198. int i;
  199. if (!s)
  200. return export_unknown;
  201. for (i = 0; export_list[i].export != export_unknown; i++) {
  202. if (strcmp(export_list[i].str, s) == 0)
  203. return export_list[i].export;
  204. }
  205. return export_unknown;
  206. }
  207. static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
  208. {
  209. if (sec == elf->export_sec)
  210. return export_plain;
  211. else if (sec == elf->export_unused_sec)
  212. return export_unused;
  213. else if (sec == elf->export_gpl_sec)
  214. return export_gpl;
  215. else if (sec == elf->export_unused_gpl_sec)
  216. return export_unused_gpl;
  217. else if (sec == elf->export_gpl_future_sec)
  218. return export_gpl_future;
  219. else
  220. return export_unknown;
  221. }
  222. /**
  223. * Add an exported symbol - it may have already been added without a
  224. * CRC, in this case just update the CRC
  225. **/
  226. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  227. enum export export)
  228. {
  229. struct symbol *s = find_symbol(name);
  230. if (!s) {
  231. s = new_symbol(name, mod, export);
  232. } else {
  233. if (!s->preloaded) {
  234. warn("%s: '%s' exported twice. Previous export "
  235. "was in %s%s\n", mod->name, name,
  236. s->module->name,
  237. is_vmlinux(s->module->name) ?"":".ko");
  238. } else {
  239. /* In case Modules.symvers was out of date */
  240. s->module = mod;
  241. }
  242. }
  243. s->preloaded = 0;
  244. s->vmlinux = is_vmlinux(mod->name);
  245. s->kernel = 0;
  246. s->export = export;
  247. return s;
  248. }
  249. static void sym_update_crc(const char *name, struct module *mod,
  250. unsigned int crc, enum export export)
  251. {
  252. struct symbol *s = find_symbol(name);
  253. if (!s)
  254. s = new_symbol(name, mod, export);
  255. s->crc = crc;
  256. s->crc_valid = 1;
  257. }
  258. void *grab_file(const char *filename, unsigned long *size)
  259. {
  260. struct stat st;
  261. void *map;
  262. int fd;
  263. fd = open(filename, O_RDONLY);
  264. if (fd < 0 || fstat(fd, &st) != 0)
  265. return NULL;
  266. *size = st.st_size;
  267. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  268. close(fd);
  269. if (map == MAP_FAILED)
  270. return NULL;
  271. return map;
  272. }
  273. /**
  274. * Return a copy of the next line in a mmap'ed file.
  275. * spaces in the beginning of the line is trimmed away.
  276. * Return a pointer to a static buffer.
  277. **/
  278. char *get_next_line(unsigned long *pos, void *file, unsigned long size)
  279. {
  280. static char line[4096];
  281. int skip = 1;
  282. size_t len = 0;
  283. signed char *p = (signed char *)file + *pos;
  284. char *s = line;
  285. for (; *pos < size ; (*pos)++) {
  286. if (skip && isspace(*p)) {
  287. p++;
  288. continue;
  289. }
  290. skip = 0;
  291. if (*p != '\n' && (*pos < size)) {
  292. len++;
  293. *s++ = *p++;
  294. if (len > 4095)
  295. break; /* Too long, stop */
  296. } else {
  297. /* End of string */
  298. *s = '\0';
  299. return line;
  300. }
  301. }
  302. /* End of buffer */
  303. return NULL;
  304. }
  305. void release_file(void *file, unsigned long size)
  306. {
  307. munmap(file, size);
  308. }
  309. static int parse_elf(struct elf_info *info, const char *filename)
  310. {
  311. unsigned int i;
  312. Elf_Ehdr *hdr;
  313. Elf_Shdr *sechdrs;
  314. Elf_Sym *sym;
  315. const char *secstrings;
  316. unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
  317. hdr = grab_file(filename, &info->size);
  318. if (!hdr) {
  319. perror(filename);
  320. exit(1);
  321. }
  322. info->hdr = hdr;
  323. if (info->size < sizeof(*hdr)) {
  324. /* file too small, assume this is an empty .o file */
  325. return 0;
  326. }
  327. /* Is this a valid ELF file? */
  328. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  329. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  330. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  331. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  332. /* Not an ELF file - silently ignore it */
  333. return 0;
  334. }
  335. /* Fix endianness in ELF header */
  336. hdr->e_type = TO_NATIVE(hdr->e_type);
  337. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  338. hdr->e_version = TO_NATIVE(hdr->e_version);
  339. hdr->e_entry = TO_NATIVE(hdr->e_entry);
  340. hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
  341. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  342. hdr->e_flags = TO_NATIVE(hdr->e_flags);
  343. hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
  344. hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
  345. hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
  346. hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
  347. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  348. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  349. sechdrs = (void *)hdr + hdr->e_shoff;
  350. info->sechdrs = sechdrs;
  351. /* Check if file offset is correct */
  352. if (hdr->e_shoff > info->size) {
  353. fatal("section header offset=%lu in file '%s' is bigger than "
  354. "filesize=%lu\n", (unsigned long)hdr->e_shoff,
  355. filename, info->size);
  356. return 0;
  357. }
  358. if (hdr->e_shnum == 0) {
  359. /*
  360. * There are more than 64k sections,
  361. * read count from .sh_size.
  362. * note: it doesn't need shndx2secindex()
  363. */
  364. info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
  365. }
  366. else {
  367. info->num_sections = hdr->e_shnum;
  368. }
  369. if (hdr->e_shstrndx == SHN_XINDEX) {
  370. info->secindex_strings =
  371. shndx2secindex(TO_NATIVE(sechdrs[0].sh_link));
  372. }
  373. else {
  374. info->secindex_strings = hdr->e_shstrndx;
  375. }
  376. /* Fix endianness in section headers */
  377. for (i = 0; i < info->num_sections; i++) {
  378. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  379. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  380. sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
  381. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  382. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  383. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  384. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  385. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  386. sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
  387. sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
  388. }
  389. /* Find symbol table. */
  390. secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
  391. for (i = 1; i < info->num_sections; i++) {
  392. const char *secname;
  393. int nobits = sechdrs[i].sh_type == SHT_NOBITS;
  394. if (!nobits && sechdrs[i].sh_offset > info->size) {
  395. fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
  396. "sizeof(*hrd)=%zu\n", filename,
  397. (unsigned long)sechdrs[i].sh_offset,
  398. sizeof(*hdr));
  399. return 0;
  400. }
  401. secname = secstrings + sechdrs[i].sh_name;
  402. if (strcmp(secname, ".modinfo") == 0) {
  403. if (nobits)
  404. fatal("%s has NOBITS .modinfo\n", filename);
  405. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  406. info->modinfo_len = sechdrs[i].sh_size;
  407. } else if (strcmp(secname, "__ksymtab") == 0)
  408. info->export_sec = i;
  409. else if (strcmp(secname, "__ksymtab_unused") == 0)
  410. info->export_unused_sec = i;
  411. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  412. info->export_gpl_sec = i;
  413. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  414. info->export_unused_gpl_sec = i;
  415. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  416. info->export_gpl_future_sec = i;
  417. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  418. unsigned int sh_link_idx;
  419. symtab_idx = i;
  420. info->symtab_start = (void *)hdr +
  421. sechdrs[i].sh_offset;
  422. info->symtab_stop = (void *)hdr +
  423. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  424. sh_link_idx = shndx2secindex(sechdrs[i].sh_link);
  425. info->strtab = (void *)hdr +
  426. sechdrs[sh_link_idx].sh_offset;
  427. }
  428. /* 32bit section no. table? ("more than 64k sections") */
  429. if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
  430. symtab_shndx_idx = i;
  431. info->symtab_shndx_start = (void *)hdr +
  432. sechdrs[i].sh_offset;
  433. info->symtab_shndx_stop = (void *)hdr +
  434. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  435. }
  436. }
  437. if (!info->symtab_start)
  438. fatal("%s has no symtab?\n", filename);
  439. /* Fix endianness in symbols */
  440. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  441. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  442. sym->st_name = TO_NATIVE(sym->st_name);
  443. sym->st_value = TO_NATIVE(sym->st_value);
  444. sym->st_size = TO_NATIVE(sym->st_size);
  445. }
  446. if (symtab_shndx_idx != ~0U) {
  447. Elf32_Word *p;
  448. if (symtab_idx !=
  449. shndx2secindex(sechdrs[symtab_shndx_idx].sh_link))
  450. fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
  451. filename,
  452. shndx2secindex(sechdrs[symtab_shndx_idx].sh_link),
  453. symtab_idx);
  454. /* Fix endianness */
  455. for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
  456. p++)
  457. *p = TO_NATIVE(*p);
  458. }
  459. return 1;
  460. }
  461. static void parse_elf_finish(struct elf_info *info)
  462. {
  463. release_file(info->hdr, info->size);
  464. }
  465. static int ignore_undef_symbol(struct elf_info *info, const char *symname)
  466. {
  467. /* ignore __this_module, it will be resolved shortly */
  468. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  469. return 1;
  470. /* ignore global offset table */
  471. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  472. return 1;
  473. if (info->hdr->e_machine == EM_PPC)
  474. /* Special register function linked on all modules during final link of .ko */
  475. if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
  476. strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
  477. strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
  478. strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
  479. return 1;
  480. if (info->hdr->e_machine == EM_PPC64)
  481. /* Special register function linked on all modules during final link of .ko */
  482. if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
  483. strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0)
  484. return 1;
  485. /* Do not ignore this symbol */
  486. return 0;
  487. }
  488. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  489. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  490. static void handle_modversions(struct module *mod, struct elf_info *info,
  491. Elf_Sym *sym, const char *symname)
  492. {
  493. unsigned int crc;
  494. enum export export = export_from_sec(info, get_secindex(info, sym));
  495. switch (sym->st_shndx) {
  496. case SHN_COMMON:
  497. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  498. break;
  499. case SHN_ABS:
  500. /* CRC'd symbol */
  501. if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  502. crc = (unsigned int) sym->st_value;
  503. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  504. export);
  505. }
  506. break;
  507. case SHN_UNDEF:
  508. /* undefined symbol */
  509. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  510. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  511. break;
  512. if (ignore_undef_symbol(info, symname))
  513. break;
  514. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  515. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  516. /* add compatibility with older glibc */
  517. #ifndef STT_SPARC_REGISTER
  518. #define STT_SPARC_REGISTER STT_REGISTER
  519. #endif
  520. if (info->hdr->e_machine == EM_SPARC ||
  521. info->hdr->e_machine == EM_SPARCV9) {
  522. /* Ignore register directives. */
  523. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  524. break;
  525. if (symname[0] == '.') {
  526. char *munged = strdup(symname);
  527. munged[0] = '_';
  528. munged[1] = toupper(munged[1]);
  529. symname = munged;
  530. }
  531. }
  532. #endif
  533. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  534. strlen(MODULE_SYMBOL_PREFIX)) == 0) {
  535. mod->unres =
  536. alloc_symbol(symname +
  537. strlen(MODULE_SYMBOL_PREFIX),
  538. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  539. mod->unres);
  540. }
  541. break;
  542. default:
  543. /* All exported symbols */
  544. if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  545. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  546. export);
  547. }
  548. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  549. mod->has_init = 1;
  550. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  551. mod->has_cleanup = 1;
  552. break;
  553. }
  554. }
  555. /**
  556. * Parse tag=value strings from .modinfo section
  557. **/
  558. static char *next_string(char *string, unsigned long *secsize)
  559. {
  560. /* Skip non-zero chars */
  561. while (string[0]) {
  562. string++;
  563. if ((*secsize)-- <= 1)
  564. return NULL;
  565. }
  566. /* Skip any zero padding. */
  567. while (!string[0]) {
  568. string++;
  569. if ((*secsize)-- <= 1)
  570. return NULL;
  571. }
  572. return string;
  573. }
  574. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  575. const char *tag, char *info)
  576. {
  577. char *p;
  578. unsigned int taglen = strlen(tag);
  579. unsigned long size = modinfo_len;
  580. if (info) {
  581. size -= info - (char *)modinfo;
  582. modinfo = next_string(info, &size);
  583. }
  584. for (p = modinfo; p; p = next_string(p, &size)) {
  585. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  586. return p + taglen + 1;
  587. }
  588. return NULL;
  589. }
  590. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  591. const char *tag)
  592. {
  593. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  594. }
  595. /**
  596. * Test if string s ends in string sub
  597. * return 0 if match
  598. **/
  599. static int strrcmp(const char *s, const char *sub)
  600. {
  601. int slen, sublen;
  602. if (!s || !sub)
  603. return 1;
  604. slen = strlen(s);
  605. sublen = strlen(sub);
  606. if ((slen == 0) || (sublen == 0))
  607. return 1;
  608. if (sublen > slen)
  609. return 1;
  610. return memcmp(s + slen - sublen, sub, sublen);
  611. }
  612. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  613. {
  614. if (sym)
  615. return elf->strtab + sym->st_name;
  616. else
  617. return "(unknown)";
  618. }
  619. static const char *sec_name(struct elf_info *elf, int secindex)
  620. {
  621. Elf_Shdr *sechdrs = elf->sechdrs;
  622. return (void *)elf->hdr +
  623. elf->sechdrs[elf->secindex_strings].sh_offset +
  624. sechdrs[secindex].sh_name;
  625. }
  626. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  627. {
  628. return (void *)elf->hdr +
  629. elf->sechdrs[elf->secindex_strings].sh_offset +
  630. sechdr->sh_name;
  631. }
  632. /* if sym is empty or point to a string
  633. * like ".[0-9]+" then return 1.
  634. * This is the optional prefix added by ld to some sections
  635. */
  636. static int number_prefix(const char *sym)
  637. {
  638. if (*sym++ == '\0')
  639. return 1;
  640. if (*sym != '.')
  641. return 0;
  642. do {
  643. char c = *sym++;
  644. if (c < '0' || c > '9')
  645. return 0;
  646. } while (*sym);
  647. return 1;
  648. }
  649. /* The pattern is an array of simple patterns.
  650. * "foo" will match an exact string equal to "foo"
  651. * "*foo" will match a string that ends with "foo"
  652. * "foo*" will match a string that begins with "foo"
  653. * "foo$" will match a string equal to "foo" or "foo.1"
  654. * where the '1' can be any number including several digits.
  655. * The $ syntax is for sections where ld append a dot number
  656. * to make section name unique.
  657. */
  658. static int match(const char *sym, const char * const pat[])
  659. {
  660. const char *p;
  661. while (*pat) {
  662. p = *pat++;
  663. const char *endp = p + strlen(p) - 1;
  664. /* "*foo" */
  665. if (*p == '*') {
  666. if (strrcmp(sym, p + 1) == 0)
  667. return 1;
  668. }
  669. /* "foo*" */
  670. else if (*endp == '*') {
  671. if (strncmp(sym, p, strlen(p) - 1) == 0)
  672. return 1;
  673. }
  674. /* "foo$" */
  675. else if (*endp == '$') {
  676. if (strncmp(sym, p, strlen(p) - 1) == 0) {
  677. if (number_prefix(sym + strlen(p) - 1))
  678. return 1;
  679. }
  680. }
  681. /* no wildcards */
  682. else {
  683. if (strcmp(p, sym) == 0)
  684. return 1;
  685. }
  686. }
  687. /* no match */
  688. return 0;
  689. }
  690. /* sections that we do not want to do full section mismatch check on */
  691. static const char *section_white_list[] =
  692. {
  693. ".comment*",
  694. ".debug*",
  695. ".mdebug*", /* alpha, score, mips etc. */
  696. ".pdr", /* alpha, score, mips etc. */
  697. ".stab*",
  698. ".note*",
  699. ".got*",
  700. ".toc*",
  701. NULL
  702. };
  703. /*
  704. * This is used to find sections missing the SHF_ALLOC flag.
  705. * The cause of this is often a section specified in assembler
  706. * without "ax" / "aw".
  707. */
  708. static void check_section(const char *modname, struct elf_info *elf,
  709. Elf_Shdr *sechdr)
  710. {
  711. const char *sec = sech_name(elf, sechdr);
  712. if (sechdr->sh_type == SHT_PROGBITS &&
  713. !(sechdr->sh_flags & SHF_ALLOC) &&
  714. !match(sec, section_white_list)) {
  715. warn("%s (%s): unexpected non-allocatable section.\n"
  716. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  717. "Note that for example <linux/init.h> contains\n"
  718. "section definitions for use in .S files.\n\n",
  719. modname, sec);
  720. }
  721. }
  722. #define ALL_INIT_DATA_SECTIONS \
  723. ".init.setup$", ".init.rodata$", \
  724. ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$" \
  725. ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
  726. #define ALL_EXIT_DATA_SECTIONS \
  727. ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
  728. #define ALL_INIT_TEXT_SECTIONS \
  729. ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
  730. #define ALL_EXIT_TEXT_SECTIONS \
  731. ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
  732. #define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
  733. MEM_INIT_SECTIONS
  734. #define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
  735. MEM_EXIT_SECTIONS
  736. #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
  737. #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
  738. #define DATA_SECTIONS ".data$", ".data.rel$"
  739. #define TEXT_SECTIONS ".text$"
  740. #define INIT_SECTIONS ".init.*"
  741. #define DEV_INIT_SECTIONS ".devinit.*"
  742. #define CPU_INIT_SECTIONS ".cpuinit.*"
  743. #define MEM_INIT_SECTIONS ".meminit.*"
  744. #define EXIT_SECTIONS ".exit.*"
  745. #define DEV_EXIT_SECTIONS ".devexit.*"
  746. #define CPU_EXIT_SECTIONS ".cpuexit.*"
  747. #define MEM_EXIT_SECTIONS ".memexit.*"
  748. /* init data sections */
  749. static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
  750. /* all init sections */
  751. static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
  752. /* All init and exit sections (code + data) */
  753. static const char *init_exit_sections[] =
  754. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  755. /* data section */
  756. static const char *data_sections[] = { DATA_SECTIONS, NULL };
  757. /* symbols in .data that may refer to init/exit sections */
  758. #define DEFAULT_SYMBOL_WHITE_LIST \
  759. "*driver", \
  760. "*_template", /* scsi uses *_template a lot */ \
  761. "*_timer", /* arm uses ops structures named _timer a lot */ \
  762. "*_sht", /* scsi also used *_sht to some extent */ \
  763. "*_ops", \
  764. "*_probe", \
  765. "*_probe_one", \
  766. "*_console"
  767. static const char *head_sections[] = { ".head.text*", NULL };
  768. static const char *linker_symbols[] =
  769. { "__init_begin", "_sinittext", "_einittext", NULL };
  770. enum mismatch {
  771. TEXT_TO_ANY_INIT,
  772. DATA_TO_ANY_INIT,
  773. TEXT_TO_ANY_EXIT,
  774. DATA_TO_ANY_EXIT,
  775. XXXINIT_TO_SOME_INIT,
  776. XXXEXIT_TO_SOME_EXIT,
  777. ANY_INIT_TO_ANY_EXIT,
  778. ANY_EXIT_TO_ANY_INIT,
  779. EXPORT_TO_INIT_EXIT,
  780. };
  781. struct sectioncheck {
  782. const char *fromsec[20];
  783. const char *tosec[20];
  784. enum mismatch mismatch;
  785. const char *symbol_white_list[20];
  786. };
  787. const struct sectioncheck sectioncheck[] = {
  788. /* Do not reference init/exit code/data from
  789. * normal code and data
  790. */
  791. {
  792. .fromsec = { TEXT_SECTIONS, NULL },
  793. .tosec = { ALL_INIT_SECTIONS, NULL },
  794. .mismatch = TEXT_TO_ANY_INIT,
  795. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  796. },
  797. {
  798. .fromsec = { DATA_SECTIONS, NULL },
  799. .tosec = { ALL_XXXINIT_SECTIONS, NULL },
  800. .mismatch = DATA_TO_ANY_INIT,
  801. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  802. },
  803. {
  804. .fromsec = { DATA_SECTIONS, NULL },
  805. .tosec = { INIT_SECTIONS, NULL },
  806. .mismatch = DATA_TO_ANY_INIT,
  807. .symbol_white_list = {
  808. "*_template", "*_timer", "*_sht", "*_ops",
  809. "*_probe", "*_probe_one", "*_console", NULL
  810. },
  811. },
  812. {
  813. .fromsec = { TEXT_SECTIONS, NULL },
  814. .tosec = { ALL_EXIT_SECTIONS, NULL },
  815. .mismatch = TEXT_TO_ANY_EXIT,
  816. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  817. },
  818. {
  819. .fromsec = { DATA_SECTIONS, NULL },
  820. .tosec = { ALL_EXIT_SECTIONS, NULL },
  821. .mismatch = DATA_TO_ANY_EXIT,
  822. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  823. },
  824. /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
  825. {
  826. .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
  827. .tosec = { INIT_SECTIONS, NULL },
  828. .mismatch = XXXINIT_TO_SOME_INIT,
  829. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  830. },
  831. /* Do not reference cpuinit code/data from meminit code/data */
  832. {
  833. .fromsec = { MEM_INIT_SECTIONS, NULL },
  834. .tosec = { CPU_INIT_SECTIONS, NULL },
  835. .mismatch = XXXINIT_TO_SOME_INIT,
  836. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  837. },
  838. /* Do not reference meminit code/data from cpuinit code/data */
  839. {
  840. .fromsec = { CPU_INIT_SECTIONS, NULL },
  841. .tosec = { MEM_INIT_SECTIONS, NULL },
  842. .mismatch = XXXINIT_TO_SOME_INIT,
  843. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  844. },
  845. /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
  846. {
  847. .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
  848. .tosec = { EXIT_SECTIONS, NULL },
  849. .mismatch = XXXEXIT_TO_SOME_EXIT,
  850. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  851. },
  852. /* Do not reference cpuexit code/data from memexit code/data */
  853. {
  854. .fromsec = { MEM_EXIT_SECTIONS, NULL },
  855. .tosec = { CPU_EXIT_SECTIONS, NULL },
  856. .mismatch = XXXEXIT_TO_SOME_EXIT,
  857. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  858. },
  859. /* Do not reference memexit code/data from cpuexit code/data */
  860. {
  861. .fromsec = { CPU_EXIT_SECTIONS, NULL },
  862. .tosec = { MEM_EXIT_SECTIONS, NULL },
  863. .mismatch = XXXEXIT_TO_SOME_EXIT,
  864. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  865. },
  866. /* Do not use exit code/data from init code */
  867. {
  868. .fromsec = { ALL_INIT_SECTIONS, NULL },
  869. .tosec = { ALL_EXIT_SECTIONS, NULL },
  870. .mismatch = ANY_INIT_TO_ANY_EXIT,
  871. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  872. },
  873. /* Do not use init code/data from exit code */
  874. {
  875. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  876. .tosec = { ALL_INIT_SECTIONS, NULL },
  877. .mismatch = ANY_EXIT_TO_ANY_INIT,
  878. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  879. },
  880. /* Do not export init/exit functions or data */
  881. {
  882. .fromsec = { "__ksymtab*", NULL },
  883. .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
  884. .mismatch = EXPORT_TO_INIT_EXIT,
  885. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  886. }
  887. };
  888. static const struct sectioncheck *section_mismatch(
  889. const char *fromsec, const char *tosec)
  890. {
  891. int i;
  892. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  893. const struct sectioncheck *check = &sectioncheck[0];
  894. for (i = 0; i < elems; i++) {
  895. if (match(fromsec, check->fromsec) &&
  896. match(tosec, check->tosec))
  897. return check;
  898. check++;
  899. }
  900. return NULL;
  901. }
  902. /**
  903. * Whitelist to allow certain references to pass with no warning.
  904. *
  905. * Pattern 1:
  906. * If a module parameter is declared __initdata and permissions=0
  907. * then this is legal despite the warning generated.
  908. * We cannot see value of permissions here, so just ignore
  909. * this pattern.
  910. * The pattern is identified by:
  911. * tosec = .init.data
  912. * fromsec = .data*
  913. * atsym =__param*
  914. *
  915. * Pattern 1a:
  916. * module_param_call() ops can refer to __init set function if permissions=0
  917. * The pattern is identified by:
  918. * tosec = .init.text
  919. * fromsec = .data*
  920. * atsym = __param_ops_*
  921. *
  922. * Pattern 2:
  923. * Many drivers utilise a *driver container with references to
  924. * add, remove, probe functions etc.
  925. * These functions may often be marked __devinit and we do not want to
  926. * warn here.
  927. * the pattern is identified by:
  928. * tosec = init or exit section
  929. * fromsec = data section
  930. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  931. * *probe_one, *_console, *_timer
  932. *
  933. * Pattern 3:
  934. * Whitelist all references from .head.text to any init section
  935. *
  936. * Pattern 4:
  937. * Some symbols belong to init section but still it is ok to reference
  938. * these from non-init sections as these symbols don't have any memory
  939. * allocated for them and symbol address and value are same. So even
  940. * if init section is freed, its ok to reference those symbols.
  941. * For ex. symbols marking the init section boundaries.
  942. * This pattern is identified by
  943. * refsymname = __init_begin, _sinittext, _einittext
  944. *
  945. **/
  946. static int secref_whitelist(const struct sectioncheck *mismatch,
  947. const char *fromsec, const char *fromsym,
  948. const char *tosec, const char *tosym)
  949. {
  950. /* Check for pattern 1 */
  951. if (match(tosec, init_data_sections) &&
  952. match(fromsec, data_sections) &&
  953. (strncmp(fromsym, "__param", strlen("__param")) == 0))
  954. return 0;
  955. /* Check for pattern 1a */
  956. if (strcmp(tosec, ".init.text") == 0 &&
  957. match(fromsec, data_sections) &&
  958. (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
  959. return 0;
  960. /* Check for pattern 2 */
  961. if (match(tosec, init_exit_sections) &&
  962. match(fromsec, data_sections) &&
  963. match(fromsym, mismatch->symbol_white_list))
  964. return 0;
  965. /* Check for pattern 3 */
  966. if (match(fromsec, head_sections) &&
  967. match(tosec, init_sections))
  968. return 0;
  969. /* Check for pattern 4 */
  970. if (match(tosym, linker_symbols))
  971. return 0;
  972. return 1;
  973. }
  974. /**
  975. * Find symbol based on relocation record info.
  976. * In some cases the symbol supplied is a valid symbol so
  977. * return refsym. If st_name != 0 we assume this is a valid symbol.
  978. * In other cases the symbol needs to be looked up in the symbol table
  979. * based on section and address.
  980. * **/
  981. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  982. Elf_Sym *relsym)
  983. {
  984. Elf_Sym *sym;
  985. Elf_Sym *near = NULL;
  986. Elf64_Sword distance = 20;
  987. Elf64_Sword d;
  988. unsigned int relsym_secindex;
  989. if (relsym->st_name != 0)
  990. return relsym;
  991. relsym_secindex = get_secindex(elf, relsym);
  992. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  993. if (get_secindex(elf, sym) != relsym_secindex)
  994. continue;
  995. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  996. continue;
  997. if (sym->st_value == addr)
  998. return sym;
  999. /* Find a symbol nearby - addr are maybe negative */
  1000. d = sym->st_value - addr;
  1001. if (d < 0)
  1002. d = addr - sym->st_value;
  1003. if (d < distance) {
  1004. distance = d;
  1005. near = sym;
  1006. }
  1007. }
  1008. /* We need a close match */
  1009. if (distance < 20)
  1010. return near;
  1011. else
  1012. return NULL;
  1013. }
  1014. static inline int is_arm_mapping_symbol(const char *str)
  1015. {
  1016. return str[0] == '$' && strchr("atd", str[1])
  1017. && (str[2] == '\0' || str[2] == '.');
  1018. }
  1019. /*
  1020. * If there's no name there, ignore it; likewise, ignore it if it's
  1021. * one of the magic symbols emitted used by current ARM tools.
  1022. *
  1023. * Otherwise if find_symbols_between() returns those symbols, they'll
  1024. * fail the whitelist tests and cause lots of false alarms ... fixable
  1025. * only by merging __exit and __init sections into __text, bloating
  1026. * the kernel (which is especially evil on embedded platforms).
  1027. */
  1028. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  1029. {
  1030. const char *name = elf->strtab + sym->st_name;
  1031. if (!name || !strlen(name))
  1032. return 0;
  1033. return !is_arm_mapping_symbol(name);
  1034. }
  1035. /*
  1036. * Find symbols before or equal addr and after addr - in the section sec.
  1037. * If we find two symbols with equal offset prefer one with a valid name.
  1038. * The ELF format may have a better way to detect what type of symbol
  1039. * it is, but this works for now.
  1040. **/
  1041. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  1042. const char *sec)
  1043. {
  1044. Elf_Sym *sym;
  1045. Elf_Sym *near = NULL;
  1046. Elf_Addr distance = ~0;
  1047. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  1048. const char *symsec;
  1049. if (is_shndx_special(sym->st_shndx))
  1050. continue;
  1051. symsec = sec_name(elf, get_secindex(elf, sym));
  1052. if (strcmp(symsec, sec) != 0)
  1053. continue;
  1054. if (!is_valid_name(elf, sym))
  1055. continue;
  1056. if (sym->st_value <= addr) {
  1057. if ((addr - sym->st_value) < distance) {
  1058. distance = addr - sym->st_value;
  1059. near = sym;
  1060. } else if ((addr - sym->st_value) == distance) {
  1061. near = sym;
  1062. }
  1063. }
  1064. }
  1065. return near;
  1066. }
  1067. /*
  1068. * Convert a section name to the function/data attribute
  1069. * .init.text => __init
  1070. * .cpuinit.data => __cpudata
  1071. * .memexitconst => __memconst
  1072. * etc.
  1073. */
  1074. static char *sec2annotation(const char *s)
  1075. {
  1076. if (match(s, init_exit_sections)) {
  1077. char *p = malloc(20);
  1078. char *r = p;
  1079. *p++ = '_';
  1080. *p++ = '_';
  1081. if (*s == '.')
  1082. s++;
  1083. while (*s && *s != '.')
  1084. *p++ = *s++;
  1085. *p = '\0';
  1086. if (*s == '.')
  1087. s++;
  1088. if (strstr(s, "rodata") != NULL)
  1089. strcat(p, "const ");
  1090. else if (strstr(s, "data") != NULL)
  1091. strcat(p, "data ");
  1092. else
  1093. strcat(p, " ");
  1094. return r; /* we leak her but we do not care */
  1095. } else {
  1096. return "";
  1097. }
  1098. }
  1099. static int is_function(Elf_Sym *sym)
  1100. {
  1101. if (sym)
  1102. return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
  1103. else
  1104. return -1;
  1105. }
  1106. /*
  1107. * Print a warning about a section mismatch.
  1108. * Try to find symbols near it so user can find it.
  1109. * Check whitelist before warning - it may be a false positive.
  1110. */
  1111. static void report_sec_mismatch(const char *modname,
  1112. const struct sectioncheck *mismatch,
  1113. const char *fromsec,
  1114. unsigned long long fromaddr,
  1115. const char *fromsym,
  1116. int from_is_func,
  1117. const char *tosec, const char *tosym,
  1118. int to_is_func)
  1119. {
  1120. const char *from, *from_p;
  1121. const char *to, *to_p;
  1122. char *prl_from;
  1123. char *prl_to;
  1124. switch (from_is_func) {
  1125. case 0: from = "variable"; from_p = ""; break;
  1126. case 1: from = "function"; from_p = "()"; break;
  1127. default: from = "(unknown reference)"; from_p = ""; break;
  1128. }
  1129. switch (to_is_func) {
  1130. case 0: to = "variable"; to_p = ""; break;
  1131. case 1: to = "function"; to_p = "()"; break;
  1132. default: to = "(unknown reference)"; to_p = ""; break;
  1133. }
  1134. sec_mismatch_count++;
  1135. if (!sec_mismatch_verbose)
  1136. return;
  1137. warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
  1138. "to the %s %s:%s%s\n",
  1139. modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
  1140. tosym, to_p);
  1141. switch (mismatch->mismatch) {
  1142. case TEXT_TO_ANY_INIT:
  1143. prl_from = sec2annotation(fromsec);
  1144. prl_to = sec2annotation(tosec);
  1145. fprintf(stderr,
  1146. "The function %s%s() references\n"
  1147. "the %s %s%s%s.\n"
  1148. "This is often because %s lacks a %s\n"
  1149. "annotation or the annotation of %s is wrong.\n",
  1150. prl_from, fromsym,
  1151. to, prl_to, tosym, to_p,
  1152. fromsym, prl_to, tosym);
  1153. free(prl_from);
  1154. free(prl_to);
  1155. break;
  1156. case DATA_TO_ANY_INIT: {
  1157. prl_to = sec2annotation(tosec);
  1158. const char *const *s = mismatch->symbol_white_list;
  1159. fprintf(stderr,
  1160. "The variable %s references\n"
  1161. "the %s %s%s%s\n"
  1162. "If the reference is valid then annotate the\n"
  1163. "variable with __init* or __refdata (see linux/init.h) "
  1164. "or name the variable:\n",
  1165. fromsym, to, prl_to, tosym, to_p);
  1166. while (*s)
  1167. fprintf(stderr, "%s, ", *s++);
  1168. fprintf(stderr, "\n");
  1169. free(prl_to);
  1170. break;
  1171. }
  1172. case TEXT_TO_ANY_EXIT:
  1173. prl_to = sec2annotation(tosec);
  1174. fprintf(stderr,
  1175. "The function %s() references a %s in an exit section.\n"
  1176. "Often the %s %s%s has valid usage outside the exit section\n"
  1177. "and the fix is to remove the %sannotation of %s.\n",
  1178. fromsym, to, to, tosym, to_p, prl_to, tosym);
  1179. free(prl_to);
  1180. break;
  1181. case DATA_TO_ANY_EXIT: {
  1182. prl_to = sec2annotation(tosec);
  1183. const char *const *s = mismatch->symbol_white_list;
  1184. fprintf(stderr,
  1185. "The variable %s references\n"
  1186. "the %s %s%s%s\n"
  1187. "If the reference is valid then annotate the\n"
  1188. "variable with __exit* (see linux/init.h) or "
  1189. "name the variable:\n",
  1190. fromsym, to, prl_to, tosym, to_p);
  1191. while (*s)
  1192. fprintf(stderr, "%s, ", *s++);
  1193. fprintf(stderr, "\n");
  1194. free(prl_to);
  1195. break;
  1196. }
  1197. case XXXINIT_TO_SOME_INIT:
  1198. case XXXEXIT_TO_SOME_EXIT:
  1199. prl_from = sec2annotation(fromsec);
  1200. prl_to = sec2annotation(tosec);
  1201. fprintf(stderr,
  1202. "The %s %s%s%s references\n"
  1203. "a %s %s%s%s.\n"
  1204. "If %s is only used by %s then\n"
  1205. "annotate %s with a matching annotation.\n",
  1206. from, prl_from, fromsym, from_p,
  1207. to, prl_to, tosym, to_p,
  1208. tosym, fromsym, tosym);
  1209. free(prl_from);
  1210. free(prl_to);
  1211. break;
  1212. case ANY_INIT_TO_ANY_EXIT:
  1213. prl_from = sec2annotation(fromsec);
  1214. prl_to = sec2annotation(tosec);
  1215. fprintf(stderr,
  1216. "The %s %s%s%s references\n"
  1217. "a %s %s%s%s.\n"
  1218. "This is often seen when error handling "
  1219. "in the init function\n"
  1220. "uses functionality in the exit path.\n"
  1221. "The fix is often to remove the %sannotation of\n"
  1222. "%s%s so it may be used outside an exit section.\n",
  1223. from, prl_from, fromsym, from_p,
  1224. to, prl_to, tosym, to_p,
  1225. sec2annotation(tosec), tosym, to_p);
  1226. free(prl_from);
  1227. free(prl_to);
  1228. break;
  1229. case ANY_EXIT_TO_ANY_INIT:
  1230. prl_from = sec2annotation(fromsec);
  1231. prl_to = sec2annotation(tosec);
  1232. fprintf(stderr,
  1233. "The %s %s%s%s references\n"
  1234. "a %s %s%s%s.\n"
  1235. "This is often seen when error handling "
  1236. "in the exit function\n"
  1237. "uses functionality in the init path.\n"
  1238. "The fix is often to remove the %sannotation of\n"
  1239. "%s%s so it may be used outside an init section.\n",
  1240. from, prl_from, fromsym, from_p,
  1241. to, prl_to, tosym, to_p,
  1242. prl_to, tosym, to_p);
  1243. free(prl_from);
  1244. free(prl_to);
  1245. break;
  1246. case EXPORT_TO_INIT_EXIT:
  1247. prl_to = sec2annotation(tosec);
  1248. fprintf(stderr,
  1249. "The symbol %s is exported and annotated %s\n"
  1250. "Fix this by removing the %sannotation of %s "
  1251. "or drop the export.\n",
  1252. tosym, prl_to, prl_to, tosym);
  1253. free(prl_to);
  1254. break;
  1255. }
  1256. fprintf(stderr, "\n");
  1257. }
  1258. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1259. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1260. {
  1261. const char *tosec;
  1262. const struct sectioncheck *mismatch;
  1263. tosec = sec_name(elf, get_secindex(elf, sym));
  1264. mismatch = section_mismatch(fromsec, tosec);
  1265. if (mismatch) {
  1266. Elf_Sym *to;
  1267. Elf_Sym *from;
  1268. const char *tosym;
  1269. const char *fromsym;
  1270. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1271. fromsym = sym_name(elf, from);
  1272. to = find_elf_symbol(elf, r->r_addend, sym);
  1273. tosym = sym_name(elf, to);
  1274. /* check whitelist - we may ignore it */
  1275. if (secref_whitelist(mismatch,
  1276. fromsec, fromsym, tosec, tosym)) {
  1277. report_sec_mismatch(modname, mismatch,
  1278. fromsec, r->r_offset, fromsym,
  1279. is_function(from), tosec, tosym,
  1280. is_function(to));
  1281. }
  1282. }
  1283. }
  1284. static unsigned int *reloc_location(struct elf_info *elf,
  1285. Elf_Shdr *sechdr, Elf_Rela *r)
  1286. {
  1287. Elf_Shdr *sechdrs = elf->sechdrs;
  1288. int section = shndx2secindex(sechdr->sh_info);
  1289. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1290. r->r_offset - sechdrs[section].sh_addr;
  1291. }
  1292. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1293. {
  1294. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1295. unsigned int *location = reloc_location(elf, sechdr, r);
  1296. switch (r_typ) {
  1297. case R_386_32:
  1298. r->r_addend = TO_NATIVE(*location);
  1299. break;
  1300. case R_386_PC32:
  1301. r->r_addend = TO_NATIVE(*location) + 4;
  1302. /* For CONFIG_RELOCATABLE=y */
  1303. if (elf->hdr->e_type == ET_EXEC)
  1304. r->r_addend += r->r_offset;
  1305. break;
  1306. }
  1307. return 0;
  1308. }
  1309. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1310. {
  1311. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1312. switch (r_typ) {
  1313. case R_ARM_ABS32:
  1314. /* From ARM ABI: (S + A) | T */
  1315. r->r_addend = (int)(long)
  1316. (elf->symtab_start + ELF_R_SYM(r->r_info));
  1317. break;
  1318. case R_ARM_PC24:
  1319. /* From ARM ABI: ((S + A) | T) - P */
  1320. r->r_addend = (int)(long)(elf->hdr +
  1321. sechdr->sh_offset +
  1322. (r->r_offset - sechdr->sh_addr));
  1323. break;
  1324. default:
  1325. return 1;
  1326. }
  1327. return 0;
  1328. }
  1329. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1330. {
  1331. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1332. unsigned int *location = reloc_location(elf, sechdr, r);
  1333. unsigned int inst;
  1334. if (r_typ == R_MIPS_HI16)
  1335. return 1; /* skip this */
  1336. inst = TO_NATIVE(*location);
  1337. switch (r_typ) {
  1338. case R_MIPS_LO16:
  1339. r->r_addend = inst & 0xffff;
  1340. break;
  1341. case R_MIPS_26:
  1342. r->r_addend = (inst & 0x03ffffff) << 2;
  1343. break;
  1344. case R_MIPS_32:
  1345. r->r_addend = inst;
  1346. break;
  1347. }
  1348. return 0;
  1349. }
  1350. static void section_rela(const char *modname, struct elf_info *elf,
  1351. Elf_Shdr *sechdr)
  1352. {
  1353. Elf_Sym *sym;
  1354. Elf_Rela *rela;
  1355. Elf_Rela r;
  1356. unsigned int r_sym;
  1357. const char *fromsec;
  1358. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1359. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1360. fromsec = sech_name(elf, sechdr);
  1361. fromsec += strlen(".rela");
  1362. /* if from section (name) is know good then skip it */
  1363. if (match(fromsec, section_white_list))
  1364. return;
  1365. for (rela = start; rela < stop; rela++) {
  1366. r.r_offset = TO_NATIVE(rela->r_offset);
  1367. #if KERNEL_ELFCLASS == ELFCLASS64
  1368. if (elf->hdr->e_machine == EM_MIPS) {
  1369. unsigned int r_typ;
  1370. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1371. r_sym = TO_NATIVE(r_sym);
  1372. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1373. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1374. } else {
  1375. r.r_info = TO_NATIVE(rela->r_info);
  1376. r_sym = ELF_R_SYM(r.r_info);
  1377. }
  1378. #else
  1379. r.r_info = TO_NATIVE(rela->r_info);
  1380. r_sym = ELF_R_SYM(r.r_info);
  1381. #endif
  1382. r.r_addend = TO_NATIVE(rela->r_addend);
  1383. sym = elf->symtab_start + r_sym;
  1384. /* Skip special sections */
  1385. if (is_shndx_special(sym->st_shndx))
  1386. continue;
  1387. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1388. }
  1389. }
  1390. static void section_rel(const char *modname, struct elf_info *elf,
  1391. Elf_Shdr *sechdr)
  1392. {
  1393. Elf_Sym *sym;
  1394. Elf_Rel *rel;
  1395. Elf_Rela r;
  1396. unsigned int r_sym;
  1397. const char *fromsec;
  1398. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1399. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1400. fromsec = sech_name(elf, sechdr);
  1401. fromsec += strlen(".rel");
  1402. /* if from section (name) is know good then skip it */
  1403. if (match(fromsec, section_white_list))
  1404. return;
  1405. for (rel = start; rel < stop; rel++) {
  1406. r.r_offset = TO_NATIVE(rel->r_offset);
  1407. #if KERNEL_ELFCLASS == ELFCLASS64
  1408. if (elf->hdr->e_machine == EM_MIPS) {
  1409. unsigned int r_typ;
  1410. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1411. r_sym = TO_NATIVE(r_sym);
  1412. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1413. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1414. } else {
  1415. r.r_info = TO_NATIVE(rel->r_info);
  1416. r_sym = ELF_R_SYM(r.r_info);
  1417. }
  1418. #else
  1419. r.r_info = TO_NATIVE(rel->r_info);
  1420. r_sym = ELF_R_SYM(r.r_info);
  1421. #endif
  1422. r.r_addend = 0;
  1423. switch (elf->hdr->e_machine) {
  1424. case EM_386:
  1425. if (addend_386_rel(elf, sechdr, &r))
  1426. continue;
  1427. break;
  1428. case EM_ARM:
  1429. if (addend_arm_rel(elf, sechdr, &r))
  1430. continue;
  1431. break;
  1432. case EM_MIPS:
  1433. if (addend_mips_rel(elf, sechdr, &r))
  1434. continue;
  1435. break;
  1436. }
  1437. sym = elf->symtab_start + r_sym;
  1438. /* Skip special sections */
  1439. if (is_shndx_special(sym->st_shndx))
  1440. continue;
  1441. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1442. }
  1443. }
  1444. /**
  1445. * A module includes a number of sections that are discarded
  1446. * either when loaded or when used as built-in.
  1447. * For loaded modules all functions marked __init and all data
  1448. * marked __initdata will be discarded when the module has been intialized.
  1449. * Likewise for modules used built-in the sections marked __exit
  1450. * are discarded because __exit marked function are supposed to be called
  1451. * only when a module is unloaded which never happens for built-in modules.
  1452. * The check_sec_ref() function traverses all relocation records
  1453. * to find all references to a section that reference a section that will
  1454. * be discarded and warns about it.
  1455. **/
  1456. static void check_sec_ref(struct module *mod, const char *modname,
  1457. struct elf_info *elf)
  1458. {
  1459. int i;
  1460. Elf_Shdr *sechdrs = elf->sechdrs;
  1461. /* Walk through all sections */
  1462. for (i = 0; i < elf->num_sections; i++) {
  1463. check_section(modname, elf, &elf->sechdrs[i]);
  1464. /* We want to process only relocation sections and not .init */
  1465. if (sechdrs[i].sh_type == SHT_RELA)
  1466. section_rela(modname, elf, &elf->sechdrs[i]);
  1467. else if (sechdrs[i].sh_type == SHT_REL)
  1468. section_rel(modname, elf, &elf->sechdrs[i]);
  1469. }
  1470. }
  1471. static void read_symbols(char *modname)
  1472. {
  1473. const char *symname;
  1474. char *version;
  1475. char *license;
  1476. struct module *mod;
  1477. struct elf_info info = { };
  1478. Elf_Sym *sym;
  1479. if (!parse_elf(&info, modname))
  1480. return;
  1481. mod = new_module(modname);
  1482. /* When there's no vmlinux, don't print warnings about
  1483. * unresolved symbols (since there'll be too many ;) */
  1484. if (is_vmlinux(modname)) {
  1485. have_vmlinux = 1;
  1486. mod->skip = 1;
  1487. }
  1488. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1489. if (info.modinfo && !license && !is_vmlinux(modname))
  1490. warn("modpost: missing MODULE_LICENSE() in %s\n"
  1491. "see include/linux/module.h for "
  1492. "more information\n", modname);
  1493. while (license) {
  1494. if (license_is_gpl_compatible(license))
  1495. mod->gpl_compatible = 1;
  1496. else {
  1497. mod->gpl_compatible = 0;
  1498. break;
  1499. }
  1500. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1501. "license", license);
  1502. }
  1503. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1504. symname = info.strtab + sym->st_name;
  1505. handle_modversions(mod, &info, sym, symname);
  1506. handle_moddevtable(mod, &info, sym, symname);
  1507. }
  1508. if (!is_vmlinux(modname) ||
  1509. (is_vmlinux(modname) && vmlinux_section_warnings))
  1510. check_sec_ref(mod, modname, &info);
  1511. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1512. if (version)
  1513. maybe_frob_rcs_version(modname, version, info.modinfo,
  1514. version - (char *)info.hdr);
  1515. if (version || (all_versions && !is_vmlinux(modname)))
  1516. get_src_version(modname, mod->srcversion,
  1517. sizeof(mod->srcversion)-1);
  1518. parse_elf_finish(&info);
  1519. /* Our trick to get versioning for module struct etc. - it's
  1520. * never passed as an argument to an exported function, so
  1521. * the automatic versioning doesn't pick it up, but it's really
  1522. * important anyhow */
  1523. if (modversions)
  1524. mod->unres = alloc_symbol("module_layout", 0, mod->unres);
  1525. }
  1526. #define SZ 500
  1527. /* We first write the generated file into memory using the
  1528. * following helper, then compare to the file on disk and
  1529. * only update the later if anything changed */
  1530. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1531. const char *fmt, ...)
  1532. {
  1533. char tmp[SZ];
  1534. int len;
  1535. va_list ap;
  1536. va_start(ap, fmt);
  1537. len = vsnprintf(tmp, SZ, fmt, ap);
  1538. buf_write(buf, tmp, len);
  1539. va_end(ap);
  1540. }
  1541. void buf_write(struct buffer *buf, const char *s, int len)
  1542. {
  1543. if (buf->size - buf->pos < len) {
  1544. buf->size += len + SZ;
  1545. buf->p = realloc(buf->p, buf->size);
  1546. }
  1547. strncpy(buf->p + buf->pos, s, len);
  1548. buf->pos += len;
  1549. }
  1550. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1551. {
  1552. const char *e = is_vmlinux(m) ?"":".ko";
  1553. switch (exp) {
  1554. case export_gpl:
  1555. fatal("modpost: GPL-incompatible module %s%s "
  1556. "uses GPL-only symbol '%s'\n", m, e, s);
  1557. break;
  1558. case export_unused_gpl:
  1559. fatal("modpost: GPL-incompatible module %s%s "
  1560. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1561. break;
  1562. case export_gpl_future:
  1563. warn("modpost: GPL-incompatible module %s%s "
  1564. "uses future GPL-only symbol '%s'\n", m, e, s);
  1565. break;
  1566. case export_plain:
  1567. case export_unused:
  1568. case export_unknown:
  1569. /* ignore */
  1570. break;
  1571. }
  1572. }
  1573. static void check_for_unused(enum export exp, const char *m, const char *s)
  1574. {
  1575. const char *e = is_vmlinux(m) ?"":".ko";
  1576. switch (exp) {
  1577. case export_unused:
  1578. case export_unused_gpl:
  1579. warn("modpost: module %s%s "
  1580. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1581. break;
  1582. default:
  1583. /* ignore */
  1584. break;
  1585. }
  1586. }
  1587. static void check_exports(struct module *mod)
  1588. {
  1589. struct symbol *s, *exp;
  1590. for (s = mod->unres; s; s = s->next) {
  1591. const char *basename;
  1592. exp = find_symbol(s->name);
  1593. if (!exp || exp->module == mod)
  1594. continue;
  1595. basename = strrchr(mod->name, '/');
  1596. if (basename)
  1597. basename++;
  1598. else
  1599. basename = mod->name;
  1600. if (!mod->gpl_compatible)
  1601. check_for_gpl_usage(exp->export, basename, exp->name);
  1602. check_for_unused(exp->export, basename, exp->name);
  1603. }
  1604. }
  1605. /**
  1606. * Header for the generated file
  1607. **/
  1608. static void add_header(struct buffer *b, struct module *mod)
  1609. {
  1610. buf_printf(b, "#include <linux/module.h>\n");
  1611. buf_printf(b, "#include <linux/vermagic.h>\n");
  1612. buf_printf(b, "#include <linux/compiler.h>\n");
  1613. buf_printf(b, "\n");
  1614. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1615. buf_printf(b, "\n");
  1616. buf_printf(b, "struct module __this_module\n");
  1617. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1618. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1619. if (mod->has_init)
  1620. buf_printf(b, " .init = init_module,\n");
  1621. if (mod->has_cleanup)
  1622. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1623. " .exit = cleanup_module,\n"
  1624. "#endif\n");
  1625. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1626. buf_printf(b, "};\n");
  1627. }
  1628. static void add_staging_flag(struct buffer *b, const char *name)
  1629. {
  1630. static const char *staging_dir = "drivers/staging";
  1631. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  1632. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  1633. }
  1634. /**
  1635. * Record CRCs for unresolved symbols
  1636. **/
  1637. static int add_versions(struct buffer *b, struct module *mod)
  1638. {
  1639. struct symbol *s, *exp;
  1640. int err = 0;
  1641. for (s = mod->unres; s; s = s->next) {
  1642. exp = find_symbol(s->name);
  1643. if (!exp || exp->module == mod) {
  1644. if (have_vmlinux && !s->weak) {
  1645. if (warn_unresolved) {
  1646. warn("\"%s\" [%s.ko] undefined!\n",
  1647. s->name, mod->name);
  1648. } else {
  1649. merror("\"%s\" [%s.ko] undefined!\n",
  1650. s->name, mod->name);
  1651. err = 1;
  1652. }
  1653. }
  1654. continue;
  1655. }
  1656. s->module = exp->module;
  1657. s->crc_valid = exp->crc_valid;
  1658. s->crc = exp->crc;
  1659. }
  1660. if (!modversions)
  1661. return err;
  1662. buf_printf(b, "\n");
  1663. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1664. buf_printf(b, "__used\n");
  1665. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1666. for (s = mod->unres; s; s = s->next) {
  1667. if (!s->module)
  1668. continue;
  1669. if (!s->crc_valid) {
  1670. warn("\"%s\" [%s.ko] has no CRC!\n",
  1671. s->name, mod->name);
  1672. continue;
  1673. }
  1674. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1675. }
  1676. buf_printf(b, "};\n");
  1677. return err;
  1678. }
  1679. static void add_depends(struct buffer *b, struct module *mod,
  1680. struct module *modules)
  1681. {
  1682. struct symbol *s;
  1683. struct module *m;
  1684. int first = 1;
  1685. for (m = modules; m; m = m->next)
  1686. m->seen = is_vmlinux(m->name);
  1687. buf_printf(b, "\n");
  1688. buf_printf(b, "static const char __module_depends[]\n");
  1689. buf_printf(b, "__used\n");
  1690. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1691. buf_printf(b, "\"depends=");
  1692. for (s = mod->unres; s; s = s->next) {
  1693. const char *p;
  1694. if (!s->module)
  1695. continue;
  1696. if (s->module->seen)
  1697. continue;
  1698. s->module->seen = 1;
  1699. p = strrchr(s->module->name, '/');
  1700. if (p)
  1701. p++;
  1702. else
  1703. p = s->module->name;
  1704. buf_printf(b, "%s%s", first ? "" : ",", p);
  1705. first = 0;
  1706. }
  1707. buf_printf(b, "\";\n");
  1708. }
  1709. static void add_srcversion(struct buffer *b, struct module *mod)
  1710. {
  1711. if (mod->srcversion[0]) {
  1712. buf_printf(b, "\n");
  1713. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1714. mod->srcversion);
  1715. }
  1716. }
  1717. static void write_if_changed(struct buffer *b, const char *fname)
  1718. {
  1719. char *tmp;
  1720. FILE *file;
  1721. struct stat st;
  1722. file = fopen(fname, "r");
  1723. if (!file)
  1724. goto write;
  1725. if (fstat(fileno(file), &st) < 0)
  1726. goto close_write;
  1727. if (st.st_size != b->pos)
  1728. goto close_write;
  1729. tmp = NOFAIL(malloc(b->pos));
  1730. if (fread(tmp, 1, b->pos, file) != b->pos)
  1731. goto free_write;
  1732. if (memcmp(tmp, b->p, b->pos) != 0)
  1733. goto free_write;
  1734. free(tmp);
  1735. fclose(file);
  1736. return;
  1737. free_write:
  1738. free(tmp);
  1739. close_write:
  1740. fclose(file);
  1741. write:
  1742. file = fopen(fname, "w");
  1743. if (!file) {
  1744. perror(fname);
  1745. exit(1);
  1746. }
  1747. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1748. perror(fname);
  1749. exit(1);
  1750. }
  1751. fclose(file);
  1752. }
  1753. /* parse Module.symvers file. line format:
  1754. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1755. **/
  1756. static void read_dump(const char *fname, unsigned int kernel)
  1757. {
  1758. unsigned long size, pos = 0;
  1759. void *file = grab_file(fname, &size);
  1760. char *line;
  1761. if (!file)
  1762. /* No symbol versions, silently ignore */
  1763. return;
  1764. while ((line = get_next_line(&pos, file, size))) {
  1765. char *symname, *modname, *d, *export, *end;
  1766. unsigned int crc;
  1767. struct module *mod;
  1768. struct symbol *s;
  1769. if (!(symname = strchr(line, '\t')))
  1770. goto fail;
  1771. *symname++ = '\0';
  1772. if (!(modname = strchr(symname, '\t')))
  1773. goto fail;
  1774. *modname++ = '\0';
  1775. if ((export = strchr(modname, '\t')) != NULL)
  1776. *export++ = '\0';
  1777. if (export && ((end = strchr(export, '\t')) != NULL))
  1778. *end = '\0';
  1779. crc = strtoul(line, &d, 16);
  1780. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1781. goto fail;
  1782. mod = find_module(modname);
  1783. if (!mod) {
  1784. if (is_vmlinux(modname))
  1785. have_vmlinux = 1;
  1786. mod = new_module(modname);
  1787. mod->skip = 1;
  1788. }
  1789. s = sym_add_exported(symname, mod, export_no(export));
  1790. s->kernel = kernel;
  1791. s->preloaded = 1;
  1792. sym_update_crc(symname, mod, crc, export_no(export));
  1793. }
  1794. return;
  1795. fail:
  1796. fatal("parse error in symbol dump file\n");
  1797. }
  1798. /* For normal builds always dump all symbols.
  1799. * For external modules only dump symbols
  1800. * that are not read from kernel Module.symvers.
  1801. **/
  1802. static int dump_sym(struct symbol *sym)
  1803. {
  1804. if (!external_module)
  1805. return 1;
  1806. if (sym->vmlinux || sym->kernel)
  1807. return 0;
  1808. return 1;
  1809. }
  1810. static void write_dump(const char *fname)
  1811. {
  1812. struct buffer buf = { };
  1813. struct symbol *symbol;
  1814. int n;
  1815. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1816. symbol = symbolhash[n];
  1817. while (symbol) {
  1818. if (dump_sym(symbol))
  1819. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1820. symbol->crc, symbol->name,
  1821. symbol->module->name,
  1822. export_str(symbol->export));
  1823. symbol = symbol->next;
  1824. }
  1825. }
  1826. write_if_changed(&buf, fname);
  1827. }
  1828. struct ext_sym_list {
  1829. struct ext_sym_list *next;
  1830. const char *file;
  1831. };
  1832. int main(int argc, char **argv)
  1833. {
  1834. struct module *mod;
  1835. struct buffer buf = { };
  1836. char *kernel_read = NULL, *module_read = NULL;
  1837. char *dump_write = NULL;
  1838. int opt;
  1839. int err;
  1840. struct ext_sym_list *extsym_iter;
  1841. struct ext_sym_list *extsym_start = NULL;
  1842. while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
  1843. switch (opt) {
  1844. case 'i':
  1845. kernel_read = optarg;
  1846. break;
  1847. case 'I':
  1848. module_read = optarg;
  1849. external_module = 1;
  1850. break;
  1851. case 'c':
  1852. cross_build = 1;
  1853. break;
  1854. case 'e':
  1855. external_module = 1;
  1856. extsym_iter =
  1857. NOFAIL(malloc(sizeof(*extsym_iter)));
  1858. extsym_iter->next = extsym_start;
  1859. extsym_iter->file = optarg;
  1860. extsym_start = extsym_iter;
  1861. break;
  1862. case 'm':
  1863. modversions = 1;
  1864. break;
  1865. case 'o':
  1866. dump_write = optarg;
  1867. break;
  1868. case 'a':
  1869. all_versions = 1;
  1870. break;
  1871. case 's':
  1872. vmlinux_section_warnings = 0;
  1873. break;
  1874. case 'S':
  1875. sec_mismatch_verbose = 0;
  1876. break;
  1877. case 'w':
  1878. warn_unresolved = 1;
  1879. break;
  1880. default:
  1881. exit(1);
  1882. }
  1883. }
  1884. if (kernel_read)
  1885. read_dump(kernel_read, 1);
  1886. if (module_read)
  1887. read_dump(module_read, 0);
  1888. while (extsym_start) {
  1889. read_dump(extsym_start->file, 0);
  1890. extsym_iter = extsym_start->next;
  1891. free(extsym_start);
  1892. extsym_start = extsym_iter;
  1893. }
  1894. while (optind < argc)
  1895. read_symbols(argv[optind++]);
  1896. for (mod = modules; mod; mod = mod->next) {
  1897. if (mod->skip)
  1898. continue;
  1899. check_exports(mod);
  1900. }
  1901. err = 0;
  1902. for (mod = modules; mod; mod = mod->next) {
  1903. char fname[strlen(mod->name) + 10];
  1904. if (mod->skip)
  1905. continue;
  1906. buf.pos = 0;
  1907. add_header(&buf, mod);
  1908. add_staging_flag(&buf, mod->name);
  1909. err |= add_versions(&buf, mod);
  1910. add_depends(&buf, mod, modules);
  1911. add_moddevtable(&buf, mod);
  1912. add_srcversion(&buf, mod);
  1913. sprintf(fname, "%s.mod.c", mod->name);
  1914. write_if_changed(&buf, fname);
  1915. }
  1916. if (dump_write)
  1917. write_dump(dump_write);
  1918. if (sec_mismatch_count && !sec_mismatch_verbose)
  1919. warn("modpost: Found %d section mismatch(es).\n"
  1920. "To see full details build your kernel with:\n"
  1921. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1922. sec_mismatch_count);
  1923. return err;
  1924. }