modpost.c 56 KB

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