modpost.c 57 KB

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