modpost.c 57 KB

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