modpost.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006-2008 Sam Ravnborg
  6. * Based in part on module-init-tools/depmod.c,file2alias
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * Usage: modpost vmlinux module1.o module2.o ...
  12. */
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include "modpost.h"
  18. #include "../../include/generated/autoconf.h"
  19. #include "../../include/linux/license.h"
  20. /* Some toolchains use a `_' prefix for all user symbols. */
  21. #ifdef CONFIG_SYMBOL_PREFIX
  22. #define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
  23. #else
  24. #define MODULE_SYMBOL_PREFIX ""
  25. #endif
  26. /* Are we using CONFIG_MODVERSIONS? */
  27. int modversions = 0;
  28. /* Warn about undefined symbols? (do so if we have vmlinux) */
  29. int have_vmlinux = 0;
  30. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  31. static int all_versions = 0;
  32. /* If we are modposting external module set to 1 */
  33. static int external_module = 0;
  34. /* Warn about section mismatch in vmlinux if set to 1 */
  35. static int vmlinux_section_warnings = 1;
  36. /* Only warn about unresolved symbols */
  37. static int warn_unresolved = 0;
  38. /* How a symbol is exported */
  39. static int sec_mismatch_count = 0;
  40. static int sec_mismatch_verbose = 1;
  41. enum export {
  42. export_plain, export_unused, export_gpl,
  43. export_unused_gpl, export_gpl_future, export_unknown
  44. };
  45. #define PRINTF __attribute__ ((format (printf, 1, 2)))
  46. PRINTF void fatal(const char *fmt, ...)
  47. {
  48. va_list arglist;
  49. fprintf(stderr, "FATAL: ");
  50. va_start(arglist, fmt);
  51. vfprintf(stderr, fmt, arglist);
  52. va_end(arglist);
  53. exit(1);
  54. }
  55. PRINTF void warn(const char *fmt, ...)
  56. {
  57. va_list arglist;
  58. fprintf(stderr, "WARNING: ");
  59. va_start(arglist, fmt);
  60. vfprintf(stderr, fmt, arglist);
  61. va_end(arglist);
  62. }
  63. PRINTF void merror(const char *fmt, ...)
  64. {
  65. va_list arglist;
  66. fprintf(stderr, "ERROR: ");
  67. va_start(arglist, fmt);
  68. vfprintf(stderr, fmt, arglist);
  69. va_end(arglist);
  70. }
  71. static int is_vmlinux(const char *modname)
  72. {
  73. const char *myname;
  74. myname = strrchr(modname, '/');
  75. if (myname)
  76. myname++;
  77. else
  78. myname = modname;
  79. return (strcmp(myname, "vmlinux") == 0) ||
  80. (strcmp(myname, "vmlinux.o") == 0);
  81. }
  82. void *do_nofail(void *ptr, const char *expr)
  83. {
  84. if (!ptr)
  85. fatal("modpost: Memory allocation failure: %s.\n", expr);
  86. return ptr;
  87. }
  88. /* A list of all modules we processed */
  89. static struct module *modules;
  90. static struct module *find_module(char *modname)
  91. {
  92. struct module *mod;
  93. for (mod = modules; mod; mod = mod->next)
  94. if (strcmp(mod->name, modname) == 0)
  95. break;
  96. return mod;
  97. }
  98. static struct module *new_module(char *modname)
  99. {
  100. struct module *mod;
  101. char *p, *s;
  102. mod = NOFAIL(malloc(sizeof(*mod)));
  103. memset(mod, 0, sizeof(*mod));
  104. p = NOFAIL(strdup(modname));
  105. /* strip trailing .o */
  106. s = strrchr(p, '.');
  107. if (s != NULL)
  108. if (strcmp(s, ".o") == 0) {
  109. *s = '\0';
  110. 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, MODULE_SYMBOL_PREFIX "__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 MODULE_SYMBOL_PREFIX "__crc_"
  509. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__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. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  559. strlen(MODULE_SYMBOL_PREFIX)) == 0) {
  560. mod->unres =
  561. alloc_symbol(symname +
  562. strlen(MODULE_SYMBOL_PREFIX),
  563. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  564. mod->unres);
  565. }
  566. break;
  567. default:
  568. /* All exported symbols */
  569. if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  570. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  571. export);
  572. }
  573. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  574. mod->has_init = 1;
  575. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  576. mod->has_cleanup = 1;
  577. break;
  578. }
  579. }
  580. /**
  581. * Parse tag=value strings from .modinfo section
  582. **/
  583. static char *next_string(char *string, unsigned long *secsize)
  584. {
  585. /* Skip non-zero chars */
  586. while (string[0]) {
  587. string++;
  588. if ((*secsize)-- <= 1)
  589. return NULL;
  590. }
  591. /* Skip any zero padding. */
  592. while (!string[0]) {
  593. string++;
  594. if ((*secsize)-- <= 1)
  595. return NULL;
  596. }
  597. return string;
  598. }
  599. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  600. const char *tag, char *info)
  601. {
  602. char *p;
  603. unsigned int taglen = strlen(tag);
  604. unsigned long size = modinfo_len;
  605. if (info) {
  606. size -= info - (char *)modinfo;
  607. modinfo = next_string(info, &size);
  608. }
  609. for (p = modinfo; p; p = next_string(p, &size)) {
  610. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  611. return p + taglen + 1;
  612. }
  613. return NULL;
  614. }
  615. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  616. const char *tag)
  617. {
  618. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  619. }
  620. /**
  621. * Test if string s ends in string sub
  622. * return 0 if match
  623. **/
  624. static int strrcmp(const char *s, const char *sub)
  625. {
  626. int slen, sublen;
  627. if (!s || !sub)
  628. return 1;
  629. slen = strlen(s);
  630. sublen = strlen(sub);
  631. if ((slen == 0) || (sublen == 0))
  632. return 1;
  633. if (sublen > slen)
  634. return 1;
  635. return memcmp(s + slen - sublen, sub, sublen);
  636. }
  637. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  638. {
  639. if (sym)
  640. return elf->strtab + sym->st_name;
  641. else
  642. return "(unknown)";
  643. }
  644. static const char *sec_name(struct elf_info *elf, int secindex)
  645. {
  646. Elf_Shdr *sechdrs = elf->sechdrs;
  647. return (void *)elf->hdr +
  648. elf->sechdrs[elf->secindex_strings].sh_offset +
  649. sechdrs[secindex].sh_name;
  650. }
  651. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  652. {
  653. return (void *)elf->hdr +
  654. elf->sechdrs[elf->secindex_strings].sh_offset +
  655. sechdr->sh_name;
  656. }
  657. /* if sym is empty or point to a string
  658. * like ".[0-9]+" then return 1.
  659. * This is the optional prefix added by ld to some sections
  660. */
  661. static int number_prefix(const char *sym)
  662. {
  663. if (*sym++ == '\0')
  664. return 1;
  665. if (*sym != '.')
  666. return 0;
  667. do {
  668. char c = *sym++;
  669. if (c < '0' || c > '9')
  670. return 0;
  671. } while (*sym);
  672. return 1;
  673. }
  674. /* The pattern is an array of simple patterns.
  675. * "foo" will match an exact string equal to "foo"
  676. * "*foo" will match a string that ends with "foo"
  677. * "foo*" will match a string that begins with "foo"
  678. * "foo$" will match a string equal to "foo" or "foo.1"
  679. * where the '1' can be any number including several digits.
  680. * The $ syntax is for sections where ld append a dot number
  681. * to make section name unique.
  682. */
  683. static int match(const char *sym, const char * const pat[])
  684. {
  685. const char *p;
  686. while (*pat) {
  687. p = *pat++;
  688. const char *endp = p + strlen(p) - 1;
  689. /* "*foo" */
  690. if (*p == '*') {
  691. if (strrcmp(sym, p + 1) == 0)
  692. return 1;
  693. }
  694. /* "foo*" */
  695. else if (*endp == '*') {
  696. if (strncmp(sym, p, strlen(p) - 1) == 0)
  697. return 1;
  698. }
  699. /* "foo$" */
  700. else if (*endp == '$') {
  701. if (strncmp(sym, p, strlen(p) - 1) == 0) {
  702. if (number_prefix(sym + strlen(p) - 1))
  703. return 1;
  704. }
  705. }
  706. /* no wildcards */
  707. else {
  708. if (strcmp(p, sym) == 0)
  709. return 1;
  710. }
  711. }
  712. /* no match */
  713. return 0;
  714. }
  715. /* sections that we do not want to do full section mismatch check on */
  716. static const char *section_white_list[] =
  717. {
  718. ".comment*",
  719. ".debug*",
  720. ".zdebug*", /* Compressed debug sections. */
  721. ".GCC-command-line", /* mn10300 */
  722. ".GCC.command.line", /* record-gcc-switches, non mn10300 */
  723. ".mdebug*", /* alpha, score, mips etc. */
  724. ".pdr", /* alpha, score, mips etc. */
  725. ".stab*",
  726. ".note*",
  727. ".got*",
  728. ".toc*",
  729. ".xt.prop", /* xtensa */
  730. ".xt.lit", /* xtensa */
  731. NULL
  732. };
  733. /*
  734. * This is used to find sections missing the SHF_ALLOC flag.
  735. * The cause of this is often a section specified in assembler
  736. * without "ax" / "aw".
  737. */
  738. static void check_section(const char *modname, struct elf_info *elf,
  739. Elf_Shdr *sechdr)
  740. {
  741. const char *sec = sech_name(elf, sechdr);
  742. if (sechdr->sh_type == SHT_PROGBITS &&
  743. !(sechdr->sh_flags & SHF_ALLOC) &&
  744. !match(sec, section_white_list)) {
  745. warn("%s (%s): unexpected non-allocatable section.\n"
  746. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  747. "Note that for example <linux/init.h> contains\n"
  748. "section definitions for use in .S files.\n\n",
  749. modname, sec);
  750. }
  751. }
  752. #define ALL_INIT_DATA_SECTIONS \
  753. ".init.setup$", ".init.rodata$", \
  754. ".cpuinit.rodata$", ".meminit.rodata$", \
  755. ".init.data$", ".cpuinit.data$", ".meminit.data$"
  756. #define ALL_EXIT_DATA_SECTIONS \
  757. ".exit.data$", ".cpuexit.data$", ".memexit.data$"
  758. #define ALL_INIT_TEXT_SECTIONS \
  759. ".init.text$", ".cpuinit.text$", ".meminit.text$"
  760. #define ALL_EXIT_TEXT_SECTIONS \
  761. ".exit.text$", ".cpuexit.text$", ".memexit.text$"
  762. #define ALL_PCI_INIT_SECTIONS \
  763. ".pci_fixup_early$", ".pci_fixup_header$", ".pci_fixup_final$", \
  764. ".pci_fixup_enable$", ".pci_fixup_resume$", \
  765. ".pci_fixup_resume_early$", ".pci_fixup_suspend$"
  766. #define ALL_XXXINIT_SECTIONS CPU_INIT_SECTIONS, MEM_INIT_SECTIONS
  767. #define ALL_XXXEXIT_SECTIONS CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS
  768. #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
  769. #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
  770. #define DATA_SECTIONS ".data$", ".data.rel$"
  771. #define TEXT_SECTIONS ".text$"
  772. #define INIT_SECTIONS ".init.*"
  773. #define CPU_INIT_SECTIONS ".cpuinit.*"
  774. #define MEM_INIT_SECTIONS ".meminit.*"
  775. #define EXIT_SECTIONS ".exit.*"
  776. #define CPU_EXIT_SECTIONS ".cpuexit.*"
  777. #define MEM_EXIT_SECTIONS ".memexit.*"
  778. /* init data sections */
  779. static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
  780. /* all init sections */
  781. static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
  782. /* All init and exit sections (code + data) */
  783. static const char *init_exit_sections[] =
  784. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  785. /* data section */
  786. static const char *data_sections[] = { DATA_SECTIONS, NULL };
  787. /* symbols in .data that may refer to init/exit sections */
  788. #define DEFAULT_SYMBOL_WHITE_LIST \
  789. "*driver", \
  790. "*_template", /* scsi uses *_template a lot */ \
  791. "*_timer", /* arm uses ops structures named _timer a lot */ \
  792. "*_sht", /* scsi also used *_sht to some extent */ \
  793. "*_ops", \
  794. "*_probe", \
  795. "*_probe_one", \
  796. "*_console"
  797. static const char *head_sections[] = { ".head.text*", NULL };
  798. static const char *linker_symbols[] =
  799. { "__init_begin", "_sinittext", "_einittext", NULL };
  800. enum mismatch {
  801. TEXT_TO_ANY_INIT,
  802. DATA_TO_ANY_INIT,
  803. TEXT_TO_ANY_EXIT,
  804. DATA_TO_ANY_EXIT,
  805. XXXINIT_TO_SOME_INIT,
  806. XXXEXIT_TO_SOME_EXIT,
  807. ANY_INIT_TO_ANY_EXIT,
  808. ANY_EXIT_TO_ANY_INIT,
  809. EXPORT_TO_INIT_EXIT,
  810. };
  811. struct sectioncheck {
  812. const char *fromsec[20];
  813. const char *tosec[20];
  814. enum mismatch mismatch;
  815. const char *symbol_white_list[20];
  816. };
  817. const struct sectioncheck sectioncheck[] = {
  818. /* Do not reference init/exit code/data from
  819. * normal code and data
  820. */
  821. {
  822. .fromsec = { TEXT_SECTIONS, NULL },
  823. .tosec = { ALL_INIT_SECTIONS, NULL },
  824. .mismatch = TEXT_TO_ANY_INIT,
  825. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  826. },
  827. {
  828. .fromsec = { DATA_SECTIONS, NULL },
  829. .tosec = { ALL_XXXINIT_SECTIONS, NULL },
  830. .mismatch = DATA_TO_ANY_INIT,
  831. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  832. },
  833. {
  834. .fromsec = { DATA_SECTIONS, NULL },
  835. .tosec = { INIT_SECTIONS, NULL },
  836. .mismatch = DATA_TO_ANY_INIT,
  837. .symbol_white_list = {
  838. "*_template", "*_timer", "*_sht", "*_ops",
  839. "*_probe", "*_probe_one", "*_console", NULL
  840. },
  841. },
  842. {
  843. .fromsec = { TEXT_SECTIONS, NULL },
  844. .tosec = { ALL_EXIT_SECTIONS, NULL },
  845. .mismatch = TEXT_TO_ANY_EXIT,
  846. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  847. },
  848. {
  849. .fromsec = { DATA_SECTIONS, NULL },
  850. .tosec = { ALL_EXIT_SECTIONS, NULL },
  851. .mismatch = DATA_TO_ANY_EXIT,
  852. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  853. },
  854. /* Do not reference init code/data from cpuinit/meminit code/data */
  855. {
  856. .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
  857. .tosec = { INIT_SECTIONS, NULL },
  858. .mismatch = XXXINIT_TO_SOME_INIT,
  859. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  860. },
  861. /* Do not reference cpuinit code/data from meminit code/data */
  862. {
  863. .fromsec = { MEM_INIT_SECTIONS, NULL },
  864. .tosec = { CPU_INIT_SECTIONS, NULL },
  865. .mismatch = XXXINIT_TO_SOME_INIT,
  866. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  867. },
  868. /* Do not reference meminit code/data from cpuinit code/data */
  869. {
  870. .fromsec = { CPU_INIT_SECTIONS, NULL },
  871. .tosec = { MEM_INIT_SECTIONS, NULL },
  872. .mismatch = XXXINIT_TO_SOME_INIT,
  873. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  874. },
  875. /* Do not reference exit code/data from cpuexit/memexit code/data */
  876. {
  877. .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
  878. .tosec = { EXIT_SECTIONS, NULL },
  879. .mismatch = XXXEXIT_TO_SOME_EXIT,
  880. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  881. },
  882. /* Do not reference cpuexit code/data from memexit code/data */
  883. {
  884. .fromsec = { MEM_EXIT_SECTIONS, NULL },
  885. .tosec = { CPU_EXIT_SECTIONS, NULL },
  886. .mismatch = XXXEXIT_TO_SOME_EXIT,
  887. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  888. },
  889. /* Do not reference memexit code/data from cpuexit code/data */
  890. {
  891. .fromsec = { CPU_EXIT_SECTIONS, NULL },
  892. .tosec = { MEM_EXIT_SECTIONS, NULL },
  893. .mismatch = XXXEXIT_TO_SOME_EXIT,
  894. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  895. },
  896. /* Do not use exit code/data from init code */
  897. {
  898. .fromsec = { ALL_INIT_SECTIONS, NULL },
  899. .tosec = { ALL_EXIT_SECTIONS, NULL },
  900. .mismatch = ANY_INIT_TO_ANY_EXIT,
  901. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  902. },
  903. /* Do not use init code/data from exit code */
  904. {
  905. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  906. .tosec = { ALL_INIT_SECTIONS, NULL },
  907. .mismatch = ANY_EXIT_TO_ANY_INIT,
  908. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  909. },
  910. {
  911. .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
  912. .tosec = { INIT_SECTIONS, NULL },
  913. .mismatch = ANY_INIT_TO_ANY_EXIT,
  914. .symbol_white_list = { NULL },
  915. },
  916. /* Do not export init/exit functions or data */
  917. {
  918. .fromsec = { "__ksymtab*", NULL },
  919. .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
  920. .mismatch = EXPORT_TO_INIT_EXIT,
  921. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  922. }
  923. };
  924. static const struct sectioncheck *section_mismatch(
  925. const char *fromsec, const char *tosec)
  926. {
  927. int i;
  928. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  929. const struct sectioncheck *check = &sectioncheck[0];
  930. for (i = 0; i < elems; i++) {
  931. if (match(fromsec, check->fromsec) &&
  932. match(tosec, check->tosec))
  933. return check;
  934. check++;
  935. }
  936. return NULL;
  937. }
  938. /**
  939. * Whitelist to allow certain references to pass with no warning.
  940. *
  941. * Pattern 1:
  942. * If a module parameter is declared __initdata and permissions=0
  943. * then this is legal despite the warning generated.
  944. * We cannot see value of permissions here, so just ignore
  945. * this pattern.
  946. * The pattern is identified by:
  947. * tosec = .init.data
  948. * fromsec = .data*
  949. * atsym =__param*
  950. *
  951. * Pattern 1a:
  952. * module_param_call() ops can refer to __init set function if permissions=0
  953. * The pattern is identified by:
  954. * tosec = .init.text
  955. * fromsec = .data*
  956. * atsym = __param_ops_*
  957. *
  958. * Pattern 2:
  959. * Many drivers utilise a *driver container with references to
  960. * add, remove, probe functions etc.
  961. * These functions may often be marked __cpuinit and we do not want to
  962. * warn here.
  963. * the pattern is identified by:
  964. * tosec = init or exit section
  965. * fromsec = data section
  966. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  967. * *probe_one, *_console, *_timer
  968. *
  969. * Pattern 3:
  970. * Whitelist all references from .head.text to any init section
  971. *
  972. * Pattern 4:
  973. * Some symbols belong to init section but still it is ok to reference
  974. * these from non-init sections as these symbols don't have any memory
  975. * allocated for them and symbol address and value are same. So even
  976. * if init section is freed, its ok to reference those symbols.
  977. * For ex. symbols marking the init section boundaries.
  978. * This pattern is identified by
  979. * refsymname = __init_begin, _sinittext, _einittext
  980. *
  981. **/
  982. static int secref_whitelist(const struct sectioncheck *mismatch,
  983. const char *fromsec, const char *fromsym,
  984. const char *tosec, const char *tosym)
  985. {
  986. /* Check for pattern 1 */
  987. if (match(tosec, init_data_sections) &&
  988. match(fromsec, data_sections) &&
  989. (strncmp(fromsym, "__param", strlen("__param")) == 0))
  990. return 0;
  991. /* Check for pattern 1a */
  992. if (strcmp(tosec, ".init.text") == 0 &&
  993. match(fromsec, data_sections) &&
  994. (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
  995. return 0;
  996. /* Check for pattern 2 */
  997. if (match(tosec, init_exit_sections) &&
  998. match(fromsec, data_sections) &&
  999. match(fromsym, mismatch->symbol_white_list))
  1000. return 0;
  1001. /* Check for pattern 3 */
  1002. if (match(fromsec, head_sections) &&
  1003. match(tosec, init_sections))
  1004. return 0;
  1005. /* Check for pattern 4 */
  1006. if (match(tosym, linker_symbols))
  1007. return 0;
  1008. return 1;
  1009. }
  1010. /**
  1011. * Find symbol based on relocation record info.
  1012. * In some cases the symbol supplied is a valid symbol so
  1013. * return refsym. If st_name != 0 we assume this is a valid symbol.
  1014. * In other cases the symbol needs to be looked up in the symbol table
  1015. * based on section and address.
  1016. * **/
  1017. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  1018. Elf_Sym *relsym)
  1019. {
  1020. Elf_Sym *sym;
  1021. Elf_Sym *near = NULL;
  1022. Elf64_Sword distance = 20;
  1023. Elf64_Sword d;
  1024. unsigned int relsym_secindex;
  1025. if (relsym->st_name != 0)
  1026. return relsym;
  1027. relsym_secindex = get_secindex(elf, relsym);
  1028. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  1029. if (get_secindex(elf, sym) != relsym_secindex)
  1030. continue;
  1031. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  1032. continue;
  1033. if (sym->st_value == addr)
  1034. return sym;
  1035. /* Find a symbol nearby - addr are maybe negative */
  1036. d = sym->st_value - addr;
  1037. if (d < 0)
  1038. d = addr - sym->st_value;
  1039. if (d < distance) {
  1040. distance = d;
  1041. near = sym;
  1042. }
  1043. }
  1044. /* We need a close match */
  1045. if (distance < 20)
  1046. return near;
  1047. else
  1048. return NULL;
  1049. }
  1050. static inline int is_arm_mapping_symbol(const char *str)
  1051. {
  1052. return str[0] == '$' && strchr("atd", str[1])
  1053. && (str[2] == '\0' || str[2] == '.');
  1054. }
  1055. /*
  1056. * If there's no name there, ignore it; likewise, ignore it if it's
  1057. * one of the magic symbols emitted used by current ARM tools.
  1058. *
  1059. * Otherwise if find_symbols_between() returns those symbols, they'll
  1060. * fail the whitelist tests and cause lots of false alarms ... fixable
  1061. * only by merging __exit and __init sections into __text, bloating
  1062. * the kernel (which is especially evil on embedded platforms).
  1063. */
  1064. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  1065. {
  1066. const char *name = elf->strtab + sym->st_name;
  1067. if (!name || !strlen(name))
  1068. return 0;
  1069. return !is_arm_mapping_symbol(name);
  1070. }
  1071. /*
  1072. * Find symbols before or equal addr and after addr - in the section sec.
  1073. * If we find two symbols with equal offset prefer one with a valid name.
  1074. * The ELF format may have a better way to detect what type of symbol
  1075. * it is, but this works for now.
  1076. **/
  1077. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  1078. const char *sec)
  1079. {
  1080. Elf_Sym *sym;
  1081. Elf_Sym *near = NULL;
  1082. Elf_Addr distance = ~0;
  1083. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  1084. const char *symsec;
  1085. if (is_shndx_special(sym->st_shndx))
  1086. continue;
  1087. symsec = sec_name(elf, get_secindex(elf, sym));
  1088. if (strcmp(symsec, sec) != 0)
  1089. continue;
  1090. if (!is_valid_name(elf, sym))
  1091. continue;
  1092. if (sym->st_value <= addr) {
  1093. if ((addr - sym->st_value) < distance) {
  1094. distance = addr - sym->st_value;
  1095. near = sym;
  1096. } else if ((addr - sym->st_value) == distance) {
  1097. near = sym;
  1098. }
  1099. }
  1100. }
  1101. return near;
  1102. }
  1103. /*
  1104. * Convert a section name to the function/data attribute
  1105. * .init.text => __init
  1106. * .cpuinit.data => __cpudata
  1107. * .memexitconst => __memconst
  1108. * etc.
  1109. *
  1110. * The memory of returned value has been allocated on a heap. The user of this
  1111. * method should free it after usage.
  1112. */
  1113. static char *sec2annotation(const char *s)
  1114. {
  1115. if (match(s, init_exit_sections)) {
  1116. char *p = malloc(20);
  1117. char *r = p;
  1118. *p++ = '_';
  1119. *p++ = '_';
  1120. if (*s == '.')
  1121. s++;
  1122. while (*s && *s != '.')
  1123. *p++ = *s++;
  1124. *p = '\0';
  1125. if (*s == '.')
  1126. s++;
  1127. if (strstr(s, "rodata") != NULL)
  1128. strcat(p, "const ");
  1129. else if (strstr(s, "data") != NULL)
  1130. strcat(p, "data ");
  1131. else
  1132. strcat(p, " ");
  1133. return r;
  1134. } else {
  1135. return strdup("");
  1136. }
  1137. }
  1138. static int is_function(Elf_Sym *sym)
  1139. {
  1140. if (sym)
  1141. return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
  1142. else
  1143. return -1;
  1144. }
  1145. static void print_section_list(const char * const list[20])
  1146. {
  1147. const char *const *s = list;
  1148. while (*s) {
  1149. fprintf(stderr, "%s", *s);
  1150. s++;
  1151. if (*s)
  1152. fprintf(stderr, ", ");
  1153. }
  1154. fprintf(stderr, "\n");
  1155. }
  1156. /*
  1157. * Print a warning about a section mismatch.
  1158. * Try to find symbols near it so user can find it.
  1159. * Check whitelist before warning - it may be a false positive.
  1160. */
  1161. static void report_sec_mismatch(const char *modname,
  1162. const struct sectioncheck *mismatch,
  1163. const char *fromsec,
  1164. unsigned long long fromaddr,
  1165. const char *fromsym,
  1166. int from_is_func,
  1167. const char *tosec, const char *tosym,
  1168. int to_is_func)
  1169. {
  1170. const char *from, *from_p;
  1171. const char *to, *to_p;
  1172. char *prl_from;
  1173. char *prl_to;
  1174. switch (from_is_func) {
  1175. case 0: from = "variable"; from_p = ""; break;
  1176. case 1: from = "function"; from_p = "()"; break;
  1177. default: from = "(unknown reference)"; from_p = ""; break;
  1178. }
  1179. switch (to_is_func) {
  1180. case 0: to = "variable"; to_p = ""; break;
  1181. case 1: to = "function"; to_p = "()"; break;
  1182. default: to = "(unknown reference)"; to_p = ""; break;
  1183. }
  1184. sec_mismatch_count++;
  1185. if (!sec_mismatch_verbose)
  1186. return;
  1187. warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
  1188. "to the %s %s:%s%s\n",
  1189. modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
  1190. tosym, to_p);
  1191. switch (mismatch->mismatch) {
  1192. case TEXT_TO_ANY_INIT:
  1193. prl_from = sec2annotation(fromsec);
  1194. prl_to = sec2annotation(tosec);
  1195. fprintf(stderr,
  1196. "The function %s%s() references\n"
  1197. "the %s %s%s%s.\n"
  1198. "This is often because %s lacks a %s\n"
  1199. "annotation or the annotation of %s is wrong.\n",
  1200. prl_from, fromsym,
  1201. to, prl_to, tosym, to_p,
  1202. fromsym, prl_to, tosym);
  1203. free(prl_from);
  1204. free(prl_to);
  1205. break;
  1206. case DATA_TO_ANY_INIT: {
  1207. prl_to = sec2annotation(tosec);
  1208. fprintf(stderr,
  1209. "The variable %s references\n"
  1210. "the %s %s%s%s\n"
  1211. "If the reference is valid then annotate the\n"
  1212. "variable with __init* or __refdata (see linux/init.h) "
  1213. "or name the variable:\n",
  1214. fromsym, to, prl_to, tosym, to_p);
  1215. print_section_list(mismatch->symbol_white_list);
  1216. free(prl_to);
  1217. break;
  1218. }
  1219. case TEXT_TO_ANY_EXIT:
  1220. prl_to = sec2annotation(tosec);
  1221. fprintf(stderr,
  1222. "The function %s() references a %s in an exit section.\n"
  1223. "Often the %s %s%s has valid usage outside the exit section\n"
  1224. "and the fix is to remove the %sannotation of %s.\n",
  1225. fromsym, to, to, tosym, to_p, prl_to, tosym);
  1226. free(prl_to);
  1227. break;
  1228. case DATA_TO_ANY_EXIT: {
  1229. prl_to = sec2annotation(tosec);
  1230. fprintf(stderr,
  1231. "The variable %s references\n"
  1232. "the %s %s%s%s\n"
  1233. "If the reference is valid then annotate the\n"
  1234. "variable with __exit* (see linux/init.h) or "
  1235. "name the variable:\n",
  1236. fromsym, to, prl_to, tosym, to_p);
  1237. print_section_list(mismatch->symbol_white_list);
  1238. free(prl_to);
  1239. break;
  1240. }
  1241. case XXXINIT_TO_SOME_INIT:
  1242. case XXXEXIT_TO_SOME_EXIT:
  1243. prl_from = sec2annotation(fromsec);
  1244. prl_to = sec2annotation(tosec);
  1245. fprintf(stderr,
  1246. "The %s %s%s%s references\n"
  1247. "a %s %s%s%s.\n"
  1248. "If %s is only used by %s then\n"
  1249. "annotate %s with a matching annotation.\n",
  1250. from, prl_from, fromsym, from_p,
  1251. to, prl_to, tosym, to_p,
  1252. tosym, fromsym, tosym);
  1253. free(prl_from);
  1254. free(prl_to);
  1255. break;
  1256. case ANY_INIT_TO_ANY_EXIT:
  1257. prl_from = sec2annotation(fromsec);
  1258. prl_to = sec2annotation(tosec);
  1259. fprintf(stderr,
  1260. "The %s %s%s%s references\n"
  1261. "a %s %s%s%s.\n"
  1262. "This is often seen when error handling "
  1263. "in the init function\n"
  1264. "uses functionality in the exit path.\n"
  1265. "The fix is often to remove the %sannotation of\n"
  1266. "%s%s so it may be used outside an exit section.\n",
  1267. from, prl_from, fromsym, from_p,
  1268. to, prl_to, tosym, to_p,
  1269. prl_to, tosym, to_p);
  1270. free(prl_from);
  1271. free(prl_to);
  1272. break;
  1273. case ANY_EXIT_TO_ANY_INIT:
  1274. prl_from = sec2annotation(fromsec);
  1275. prl_to = sec2annotation(tosec);
  1276. fprintf(stderr,
  1277. "The %s %s%s%s references\n"
  1278. "a %s %s%s%s.\n"
  1279. "This is often seen when error handling "
  1280. "in the exit function\n"
  1281. "uses functionality in the init path.\n"
  1282. "The fix is often to remove the %sannotation of\n"
  1283. "%s%s so it may be used outside an init section.\n",
  1284. from, prl_from, fromsym, from_p,
  1285. to, prl_to, tosym, to_p,
  1286. prl_to, tosym, to_p);
  1287. free(prl_from);
  1288. free(prl_to);
  1289. break;
  1290. case EXPORT_TO_INIT_EXIT:
  1291. prl_to = sec2annotation(tosec);
  1292. fprintf(stderr,
  1293. "The symbol %s is exported and annotated %s\n"
  1294. "Fix this by removing the %sannotation of %s "
  1295. "or drop the export.\n",
  1296. tosym, prl_to, prl_to, tosym);
  1297. free(prl_to);
  1298. break;
  1299. }
  1300. fprintf(stderr, "\n");
  1301. }
  1302. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1303. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1304. {
  1305. const char *tosec;
  1306. const struct sectioncheck *mismatch;
  1307. tosec = sec_name(elf, get_secindex(elf, sym));
  1308. mismatch = section_mismatch(fromsec, tosec);
  1309. if (mismatch) {
  1310. Elf_Sym *to;
  1311. Elf_Sym *from;
  1312. const char *tosym;
  1313. const char *fromsym;
  1314. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1315. fromsym = sym_name(elf, from);
  1316. to = find_elf_symbol(elf, r->r_addend, sym);
  1317. tosym = sym_name(elf, to);
  1318. /* check whitelist - we may ignore it */
  1319. if (secref_whitelist(mismatch,
  1320. fromsec, fromsym, tosec, tosym)) {
  1321. report_sec_mismatch(modname, mismatch,
  1322. fromsec, r->r_offset, fromsym,
  1323. is_function(from), tosec, tosym,
  1324. is_function(to));
  1325. }
  1326. }
  1327. }
  1328. static unsigned int *reloc_location(struct elf_info *elf,
  1329. Elf_Shdr *sechdr, Elf_Rela *r)
  1330. {
  1331. Elf_Shdr *sechdrs = elf->sechdrs;
  1332. int section = sechdr->sh_info;
  1333. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1334. r->r_offset;
  1335. }
  1336. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1337. {
  1338. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1339. unsigned int *location = reloc_location(elf, sechdr, r);
  1340. switch (r_typ) {
  1341. case R_386_32:
  1342. r->r_addend = TO_NATIVE(*location);
  1343. break;
  1344. case R_386_PC32:
  1345. r->r_addend = TO_NATIVE(*location) + 4;
  1346. /* For CONFIG_RELOCATABLE=y */
  1347. if (elf->hdr->e_type == ET_EXEC)
  1348. r->r_addend += r->r_offset;
  1349. break;
  1350. }
  1351. return 0;
  1352. }
  1353. #ifndef R_ARM_CALL
  1354. #define R_ARM_CALL 28
  1355. #endif
  1356. #ifndef R_ARM_JUMP24
  1357. #define R_ARM_JUMP24 29
  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. /* From ARM ABI: ((S + A) | T) - P */
  1372. r->r_addend = (int)(long)(elf->hdr +
  1373. sechdr->sh_offset +
  1374. (r->r_offset - sechdr->sh_addr));
  1375. break;
  1376. default:
  1377. return 1;
  1378. }
  1379. return 0;
  1380. }
  1381. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1382. {
  1383. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1384. unsigned int *location = reloc_location(elf, sechdr, r);
  1385. unsigned int inst;
  1386. if (r_typ == R_MIPS_HI16)
  1387. return 1; /* skip this */
  1388. inst = TO_NATIVE(*location);
  1389. switch (r_typ) {
  1390. case R_MIPS_LO16:
  1391. r->r_addend = inst & 0xffff;
  1392. break;
  1393. case R_MIPS_26:
  1394. r->r_addend = (inst & 0x03ffffff) << 2;
  1395. break;
  1396. case R_MIPS_32:
  1397. r->r_addend = inst;
  1398. break;
  1399. }
  1400. return 0;
  1401. }
  1402. static void section_rela(const char *modname, struct elf_info *elf,
  1403. Elf_Shdr *sechdr)
  1404. {
  1405. Elf_Sym *sym;
  1406. Elf_Rela *rela;
  1407. Elf_Rela r;
  1408. unsigned int r_sym;
  1409. const char *fromsec;
  1410. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1411. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1412. fromsec = sech_name(elf, sechdr);
  1413. fromsec += strlen(".rela");
  1414. /* if from section (name) is know good then skip it */
  1415. if (match(fromsec, section_white_list))
  1416. return;
  1417. for (rela = start; rela < stop; rela++) {
  1418. r.r_offset = TO_NATIVE(rela->r_offset);
  1419. #if KERNEL_ELFCLASS == ELFCLASS64
  1420. if (elf->hdr->e_machine == EM_MIPS) {
  1421. unsigned int r_typ;
  1422. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1423. r_sym = TO_NATIVE(r_sym);
  1424. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1425. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1426. } else {
  1427. r.r_info = TO_NATIVE(rela->r_info);
  1428. r_sym = ELF_R_SYM(r.r_info);
  1429. }
  1430. #else
  1431. r.r_info = TO_NATIVE(rela->r_info);
  1432. r_sym = ELF_R_SYM(r.r_info);
  1433. #endif
  1434. r.r_addend = TO_NATIVE(rela->r_addend);
  1435. sym = elf->symtab_start + r_sym;
  1436. /* Skip special sections */
  1437. if (is_shndx_special(sym->st_shndx))
  1438. continue;
  1439. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1440. }
  1441. }
  1442. static void section_rel(const char *modname, struct elf_info *elf,
  1443. Elf_Shdr *sechdr)
  1444. {
  1445. Elf_Sym *sym;
  1446. Elf_Rel *rel;
  1447. Elf_Rela r;
  1448. unsigned int r_sym;
  1449. const char *fromsec;
  1450. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1451. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1452. fromsec = sech_name(elf, sechdr);
  1453. fromsec += strlen(".rel");
  1454. /* if from section (name) is know good then skip it */
  1455. if (match(fromsec, section_white_list))
  1456. return;
  1457. for (rel = start; rel < stop; rel++) {
  1458. r.r_offset = TO_NATIVE(rel->r_offset);
  1459. #if KERNEL_ELFCLASS == ELFCLASS64
  1460. if (elf->hdr->e_machine == EM_MIPS) {
  1461. unsigned int r_typ;
  1462. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1463. r_sym = TO_NATIVE(r_sym);
  1464. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1465. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1466. } else {
  1467. r.r_info = TO_NATIVE(rel->r_info);
  1468. r_sym = ELF_R_SYM(r.r_info);
  1469. }
  1470. #else
  1471. r.r_info = TO_NATIVE(rel->r_info);
  1472. r_sym = ELF_R_SYM(r.r_info);
  1473. #endif
  1474. r.r_addend = 0;
  1475. switch (elf->hdr->e_machine) {
  1476. case EM_386:
  1477. if (addend_386_rel(elf, sechdr, &r))
  1478. continue;
  1479. break;
  1480. case EM_ARM:
  1481. if (addend_arm_rel(elf, sechdr, &r))
  1482. continue;
  1483. break;
  1484. case EM_MIPS:
  1485. if (addend_mips_rel(elf, sechdr, &r))
  1486. continue;
  1487. break;
  1488. }
  1489. sym = elf->symtab_start + r_sym;
  1490. /* Skip special sections */
  1491. if (is_shndx_special(sym->st_shndx))
  1492. continue;
  1493. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1494. }
  1495. }
  1496. /**
  1497. * A module includes a number of sections that are discarded
  1498. * either when loaded or when used as built-in.
  1499. * For loaded modules all functions marked __init and all data
  1500. * marked __initdata will be discarded when the module has been initialized.
  1501. * Likewise for modules used built-in the sections marked __exit
  1502. * are discarded because __exit marked function are supposed to be called
  1503. * only when a module is unloaded which never happens for built-in modules.
  1504. * The check_sec_ref() function traverses all relocation records
  1505. * to find all references to a section that reference a section that will
  1506. * be discarded and warns about it.
  1507. **/
  1508. static void check_sec_ref(struct module *mod, const char *modname,
  1509. struct elf_info *elf)
  1510. {
  1511. int i;
  1512. Elf_Shdr *sechdrs = elf->sechdrs;
  1513. /* Walk through all sections */
  1514. for (i = 0; i < elf->num_sections; i++) {
  1515. check_section(modname, elf, &elf->sechdrs[i]);
  1516. /* We want to process only relocation sections and not .init */
  1517. if (sechdrs[i].sh_type == SHT_RELA)
  1518. section_rela(modname, elf, &elf->sechdrs[i]);
  1519. else if (sechdrs[i].sh_type == SHT_REL)
  1520. section_rel(modname, elf, &elf->sechdrs[i]);
  1521. }
  1522. }
  1523. static void read_symbols(char *modname)
  1524. {
  1525. const char *symname;
  1526. char *version;
  1527. char *license;
  1528. struct module *mod;
  1529. struct elf_info info = { };
  1530. Elf_Sym *sym;
  1531. if (!parse_elf(&info, modname))
  1532. return;
  1533. mod = new_module(modname);
  1534. /* When there's no vmlinux, don't print warnings about
  1535. * unresolved symbols (since there'll be too many ;) */
  1536. if (is_vmlinux(modname)) {
  1537. have_vmlinux = 1;
  1538. mod->skip = 1;
  1539. }
  1540. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1541. if (info.modinfo && !license && !is_vmlinux(modname))
  1542. warn("modpost: missing MODULE_LICENSE() in %s\n"
  1543. "see include/linux/module.h for "
  1544. "more information\n", modname);
  1545. while (license) {
  1546. if (license_is_gpl_compatible(license))
  1547. mod->gpl_compatible = 1;
  1548. else {
  1549. mod->gpl_compatible = 0;
  1550. break;
  1551. }
  1552. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1553. "license", license);
  1554. }
  1555. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1556. symname = info.strtab + sym->st_name;
  1557. handle_modversions(mod, &info, sym, symname);
  1558. handle_moddevtable(mod, &info, sym, symname);
  1559. }
  1560. if (!is_vmlinux(modname) ||
  1561. (is_vmlinux(modname) && vmlinux_section_warnings))
  1562. check_sec_ref(mod, modname, &info);
  1563. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1564. if (version)
  1565. maybe_frob_rcs_version(modname, version, info.modinfo,
  1566. version - (char *)info.hdr);
  1567. if (version || (all_versions && !is_vmlinux(modname)))
  1568. get_src_version(modname, mod->srcversion,
  1569. sizeof(mod->srcversion)-1);
  1570. parse_elf_finish(&info);
  1571. /* Our trick to get versioning for module struct etc. - it's
  1572. * never passed as an argument to an exported function, so
  1573. * the automatic versioning doesn't pick it up, but it's really
  1574. * important anyhow */
  1575. if (modversions)
  1576. mod->unres = alloc_symbol("module_layout", 0, mod->unres);
  1577. }
  1578. #define SZ 500
  1579. /* We first write the generated file into memory using the
  1580. * following helper, then compare to the file on disk and
  1581. * only update the later if anything changed */
  1582. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1583. const char *fmt, ...)
  1584. {
  1585. char tmp[SZ];
  1586. int len;
  1587. va_list ap;
  1588. va_start(ap, fmt);
  1589. len = vsnprintf(tmp, SZ, fmt, ap);
  1590. buf_write(buf, tmp, len);
  1591. va_end(ap);
  1592. }
  1593. void buf_write(struct buffer *buf, const char *s, int len)
  1594. {
  1595. if (buf->size - buf->pos < len) {
  1596. buf->size += len + SZ;
  1597. buf->p = realloc(buf->p, buf->size);
  1598. }
  1599. strncpy(buf->p + buf->pos, s, len);
  1600. buf->pos += len;
  1601. }
  1602. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1603. {
  1604. const char *e = is_vmlinux(m) ?"":".ko";
  1605. switch (exp) {
  1606. case export_gpl:
  1607. fatal("modpost: GPL-incompatible module %s%s "
  1608. "uses GPL-only symbol '%s'\n", m, e, s);
  1609. break;
  1610. case export_unused_gpl:
  1611. fatal("modpost: GPL-incompatible module %s%s "
  1612. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1613. break;
  1614. case export_gpl_future:
  1615. warn("modpost: GPL-incompatible module %s%s "
  1616. "uses future GPL-only symbol '%s'\n", m, e, s);
  1617. break;
  1618. case export_plain:
  1619. case export_unused:
  1620. case export_unknown:
  1621. /* ignore */
  1622. break;
  1623. }
  1624. }
  1625. static void check_for_unused(enum export exp, const char *m, const char *s)
  1626. {
  1627. const char *e = is_vmlinux(m) ?"":".ko";
  1628. switch (exp) {
  1629. case export_unused:
  1630. case export_unused_gpl:
  1631. warn("modpost: module %s%s "
  1632. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1633. break;
  1634. default:
  1635. /* ignore */
  1636. break;
  1637. }
  1638. }
  1639. static void check_exports(struct module *mod)
  1640. {
  1641. struct symbol *s, *exp;
  1642. for (s = mod->unres; s; s = s->next) {
  1643. const char *basename;
  1644. exp = find_symbol(s->name);
  1645. if (!exp || exp->module == mod)
  1646. continue;
  1647. basename = strrchr(mod->name, '/');
  1648. if (basename)
  1649. basename++;
  1650. else
  1651. basename = mod->name;
  1652. if (!mod->gpl_compatible)
  1653. check_for_gpl_usage(exp->export, basename, exp->name);
  1654. check_for_unused(exp->export, basename, exp->name);
  1655. }
  1656. }
  1657. /**
  1658. * Header for the generated file
  1659. **/
  1660. static void add_header(struct buffer *b, struct module *mod)
  1661. {
  1662. buf_printf(b, "#include <linux/module.h>\n");
  1663. buf_printf(b, "#include <linux/vermagic.h>\n");
  1664. buf_printf(b, "#include <linux/compiler.h>\n");
  1665. buf_printf(b, "\n");
  1666. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1667. buf_printf(b, "\n");
  1668. buf_printf(b, "struct module __this_module\n");
  1669. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1670. buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
  1671. if (mod->has_init)
  1672. buf_printf(b, "\t.init = init_module,\n");
  1673. if (mod->has_cleanup)
  1674. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1675. "\t.exit = cleanup_module,\n"
  1676. "#endif\n");
  1677. buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
  1678. buf_printf(b, "};\n");
  1679. }
  1680. static void add_intree_flag(struct buffer *b, int is_intree)
  1681. {
  1682. if (is_intree)
  1683. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  1684. }
  1685. static void add_staging_flag(struct buffer *b, const char *name)
  1686. {
  1687. static const char *staging_dir = "drivers/staging";
  1688. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  1689. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  1690. }
  1691. /**
  1692. * Record CRCs for unresolved symbols
  1693. **/
  1694. static int add_versions(struct buffer *b, struct module *mod)
  1695. {
  1696. struct symbol *s, *exp;
  1697. int err = 0;
  1698. for (s = mod->unres; s; s = s->next) {
  1699. exp = find_symbol(s->name);
  1700. if (!exp || exp->module == mod) {
  1701. if (have_vmlinux && !s->weak) {
  1702. if (warn_unresolved) {
  1703. warn("\"%s\" [%s.ko] undefined!\n",
  1704. s->name, mod->name);
  1705. } else {
  1706. merror("\"%s\" [%s.ko] undefined!\n",
  1707. s->name, mod->name);
  1708. err = 1;
  1709. }
  1710. }
  1711. continue;
  1712. }
  1713. s->module = exp->module;
  1714. s->crc_valid = exp->crc_valid;
  1715. s->crc = exp->crc;
  1716. }
  1717. if (!modversions)
  1718. return err;
  1719. buf_printf(b, "\n");
  1720. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1721. buf_printf(b, "__used\n");
  1722. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1723. for (s = mod->unres; s; s = s->next) {
  1724. if (!s->module)
  1725. continue;
  1726. if (!s->crc_valid) {
  1727. warn("\"%s\" [%s.ko] has no CRC!\n",
  1728. s->name, mod->name);
  1729. continue;
  1730. }
  1731. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1732. }
  1733. buf_printf(b, "};\n");
  1734. return err;
  1735. }
  1736. static void add_depends(struct buffer *b, struct module *mod,
  1737. struct module *modules)
  1738. {
  1739. struct symbol *s;
  1740. struct module *m;
  1741. int first = 1;
  1742. for (m = modules; m; m = m->next)
  1743. m->seen = is_vmlinux(m->name);
  1744. buf_printf(b, "\n");
  1745. buf_printf(b, "static const char __module_depends[]\n");
  1746. buf_printf(b, "__used\n");
  1747. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1748. buf_printf(b, "\"depends=");
  1749. for (s = mod->unres; s; s = s->next) {
  1750. const char *p;
  1751. if (!s->module)
  1752. continue;
  1753. if (s->module->seen)
  1754. continue;
  1755. s->module->seen = 1;
  1756. p = strrchr(s->module->name, '/');
  1757. if (p)
  1758. p++;
  1759. else
  1760. p = s->module->name;
  1761. buf_printf(b, "%s%s", first ? "" : ",", p);
  1762. first = 0;
  1763. }
  1764. buf_printf(b, "\";\n");
  1765. }
  1766. static void add_srcversion(struct buffer *b, struct module *mod)
  1767. {
  1768. if (mod->srcversion[0]) {
  1769. buf_printf(b, "\n");
  1770. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1771. mod->srcversion);
  1772. }
  1773. }
  1774. static void write_if_changed(struct buffer *b, const char *fname)
  1775. {
  1776. char *tmp;
  1777. FILE *file;
  1778. struct stat st;
  1779. file = fopen(fname, "r");
  1780. if (!file)
  1781. goto write;
  1782. if (fstat(fileno(file), &st) < 0)
  1783. goto close_write;
  1784. if (st.st_size != b->pos)
  1785. goto close_write;
  1786. tmp = NOFAIL(malloc(b->pos));
  1787. if (fread(tmp, 1, b->pos, file) != b->pos)
  1788. goto free_write;
  1789. if (memcmp(tmp, b->p, b->pos) != 0)
  1790. goto free_write;
  1791. free(tmp);
  1792. fclose(file);
  1793. return;
  1794. free_write:
  1795. free(tmp);
  1796. close_write:
  1797. fclose(file);
  1798. write:
  1799. file = fopen(fname, "w");
  1800. if (!file) {
  1801. perror(fname);
  1802. exit(1);
  1803. }
  1804. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1805. perror(fname);
  1806. exit(1);
  1807. }
  1808. fclose(file);
  1809. }
  1810. /* parse Module.symvers file. line format:
  1811. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1812. **/
  1813. static void read_dump(const char *fname, unsigned int kernel)
  1814. {
  1815. unsigned long size, pos = 0;
  1816. void *file = grab_file(fname, &size);
  1817. char *line;
  1818. if (!file)
  1819. /* No symbol versions, silently ignore */
  1820. return;
  1821. while ((line = get_next_line(&pos, file, size))) {
  1822. char *symname, *modname, *d, *export, *end;
  1823. unsigned int crc;
  1824. struct module *mod;
  1825. struct symbol *s;
  1826. if (!(symname = strchr(line, '\t')))
  1827. goto fail;
  1828. *symname++ = '\0';
  1829. if (!(modname = strchr(symname, '\t')))
  1830. goto fail;
  1831. *modname++ = '\0';
  1832. if ((export = strchr(modname, '\t')) != NULL)
  1833. *export++ = '\0';
  1834. if (export && ((end = strchr(export, '\t')) != NULL))
  1835. *end = '\0';
  1836. crc = strtoul(line, &d, 16);
  1837. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1838. goto fail;
  1839. mod = find_module(modname);
  1840. if (!mod) {
  1841. if (is_vmlinux(modname))
  1842. have_vmlinux = 1;
  1843. mod = new_module(modname);
  1844. mod->skip = 1;
  1845. }
  1846. s = sym_add_exported(symname, mod, export_no(export));
  1847. s->kernel = kernel;
  1848. s->preloaded = 1;
  1849. sym_update_crc(symname, mod, crc, export_no(export));
  1850. }
  1851. return;
  1852. fail:
  1853. fatal("parse error in symbol dump file\n");
  1854. }
  1855. /* For normal builds always dump all symbols.
  1856. * For external modules only dump symbols
  1857. * that are not read from kernel Module.symvers.
  1858. **/
  1859. static int dump_sym(struct symbol *sym)
  1860. {
  1861. if (!external_module)
  1862. return 1;
  1863. if (sym->vmlinux || sym->kernel)
  1864. return 0;
  1865. return 1;
  1866. }
  1867. static void write_dump(const char *fname)
  1868. {
  1869. struct buffer buf = { };
  1870. struct symbol *symbol;
  1871. int n;
  1872. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1873. symbol = symbolhash[n];
  1874. while (symbol) {
  1875. if (dump_sym(symbol))
  1876. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1877. symbol->crc, symbol->name,
  1878. symbol->module->name,
  1879. export_str(symbol->export));
  1880. symbol = symbol->next;
  1881. }
  1882. }
  1883. write_if_changed(&buf, fname);
  1884. }
  1885. struct ext_sym_list {
  1886. struct ext_sym_list *next;
  1887. const char *file;
  1888. };
  1889. int main(int argc, char **argv)
  1890. {
  1891. struct module *mod;
  1892. struct buffer buf = { };
  1893. char *kernel_read = NULL, *module_read = NULL;
  1894. char *dump_write = NULL;
  1895. int opt;
  1896. int err;
  1897. struct ext_sym_list *extsym_iter;
  1898. struct ext_sym_list *extsym_start = NULL;
  1899. while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
  1900. switch (opt) {
  1901. case 'i':
  1902. kernel_read = optarg;
  1903. break;
  1904. case 'I':
  1905. module_read = optarg;
  1906. external_module = 1;
  1907. break;
  1908. case 'c':
  1909. cross_build = 1;
  1910. break;
  1911. case 'e':
  1912. external_module = 1;
  1913. extsym_iter =
  1914. NOFAIL(malloc(sizeof(*extsym_iter)));
  1915. extsym_iter->next = extsym_start;
  1916. extsym_iter->file = optarg;
  1917. extsym_start = extsym_iter;
  1918. break;
  1919. case 'm':
  1920. modversions = 1;
  1921. break;
  1922. case 'o':
  1923. dump_write = optarg;
  1924. break;
  1925. case 'a':
  1926. all_versions = 1;
  1927. break;
  1928. case 's':
  1929. vmlinux_section_warnings = 0;
  1930. break;
  1931. case 'S':
  1932. sec_mismatch_verbose = 0;
  1933. break;
  1934. case 'w':
  1935. warn_unresolved = 1;
  1936. break;
  1937. default:
  1938. exit(1);
  1939. }
  1940. }
  1941. if (kernel_read)
  1942. read_dump(kernel_read, 1);
  1943. if (module_read)
  1944. read_dump(module_read, 0);
  1945. while (extsym_start) {
  1946. read_dump(extsym_start->file, 0);
  1947. extsym_iter = extsym_start->next;
  1948. free(extsym_start);
  1949. extsym_start = extsym_iter;
  1950. }
  1951. while (optind < argc)
  1952. read_symbols(argv[optind++]);
  1953. for (mod = modules; mod; mod = mod->next) {
  1954. if (mod->skip)
  1955. continue;
  1956. check_exports(mod);
  1957. }
  1958. err = 0;
  1959. for (mod = modules; mod; mod = mod->next) {
  1960. char fname[strlen(mod->name) + 10];
  1961. if (mod->skip)
  1962. continue;
  1963. buf.pos = 0;
  1964. add_header(&buf, mod);
  1965. add_intree_flag(&buf, !external_module);
  1966. add_staging_flag(&buf, mod->name);
  1967. err |= add_versions(&buf, mod);
  1968. add_depends(&buf, mod, modules);
  1969. add_moddevtable(&buf, mod);
  1970. add_srcversion(&buf, mod);
  1971. sprintf(fname, "%s.mod.c", mod->name);
  1972. write_if_changed(&buf, fname);
  1973. }
  1974. if (dump_write)
  1975. write_dump(dump_write);
  1976. if (sec_mismatch_count && !sec_mismatch_verbose)
  1977. warn("modpost: Found %d section mismatch(es).\n"
  1978. "To see full details build your kernel with:\n"
  1979. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1980. sec_mismatch_count);
  1981. return err;
  1982. }