modpost.c 57 KB

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