modpost.c 55 KB

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