modpost.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  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. ".mdebug*", /* alpha, score, mips etc. */
  723. ".pdr", /* alpha, score, mips etc. */
  724. ".stab*",
  725. ".note*",
  726. ".got*",
  727. ".toc*",
  728. ".xt.prop", /* xtensa */
  729. ".xt.lit", /* xtensa */
  730. NULL
  731. };
  732. /*
  733. * This is used to find sections missing the SHF_ALLOC flag.
  734. * The cause of this is often a section specified in assembler
  735. * without "ax" / "aw".
  736. */
  737. static void check_section(const char *modname, struct elf_info *elf,
  738. Elf_Shdr *sechdr)
  739. {
  740. const char *sec = sech_name(elf, sechdr);
  741. if (sechdr->sh_type == SHT_PROGBITS &&
  742. !(sechdr->sh_flags & SHF_ALLOC) &&
  743. !match(sec, section_white_list)) {
  744. warn("%s (%s): unexpected non-allocatable section.\n"
  745. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  746. "Note that for example <linux/init.h> contains\n"
  747. "section definitions for use in .S files.\n\n",
  748. modname, sec);
  749. }
  750. }
  751. #define ALL_INIT_DATA_SECTIONS \
  752. ".init.setup$", ".init.rodata$", \
  753. ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$", \
  754. ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
  755. #define ALL_EXIT_DATA_SECTIONS \
  756. ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
  757. #define ALL_INIT_TEXT_SECTIONS \
  758. ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
  759. #define ALL_EXIT_TEXT_SECTIONS \
  760. ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
  761. #define ALL_PCI_INIT_SECTIONS \
  762. ".pci_fixup_early$", ".pci_fixup_header$", ".pci_fixup_final$", \
  763. ".pci_fixup_enable$", ".pci_fixup_resume$", \
  764. ".pci_fixup_resume_early$", ".pci_fixup_suspend$"
  765. #define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
  766. MEM_INIT_SECTIONS
  767. #define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
  768. MEM_EXIT_SECTIONS
  769. #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
  770. #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
  771. #define DATA_SECTIONS ".data$", ".data.rel$"
  772. #define TEXT_SECTIONS ".text$"
  773. #define INIT_SECTIONS ".init.*"
  774. #define DEV_INIT_SECTIONS ".devinit.*"
  775. #define CPU_INIT_SECTIONS ".cpuinit.*"
  776. #define MEM_INIT_SECTIONS ".meminit.*"
  777. #define EXIT_SECTIONS ".exit.*"
  778. #define DEV_EXIT_SECTIONS ".devexit.*"
  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 devinit/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 devexit/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 __devinit 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, \"%s\" },\n", s->crc, s->name);
  1735. }
  1736. buf_printf(b, "};\n");
  1737. return err;
  1738. }
  1739. static void add_depends(struct buffer *b, struct module *mod,
  1740. struct module *modules)
  1741. {
  1742. struct symbol *s;
  1743. struct module *m;
  1744. int first = 1;
  1745. for (m = modules; m; m = m->next)
  1746. m->seen = is_vmlinux(m->name);
  1747. buf_printf(b, "\n");
  1748. buf_printf(b, "static const char __module_depends[]\n");
  1749. buf_printf(b, "__used\n");
  1750. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1751. buf_printf(b, "\"depends=");
  1752. for (s = mod->unres; s; s = s->next) {
  1753. const char *p;
  1754. if (!s->module)
  1755. continue;
  1756. if (s->module->seen)
  1757. continue;
  1758. s->module->seen = 1;
  1759. p = strrchr(s->module->name, '/');
  1760. if (p)
  1761. p++;
  1762. else
  1763. p = s->module->name;
  1764. buf_printf(b, "%s%s", first ? "" : ",", p);
  1765. first = 0;
  1766. }
  1767. buf_printf(b, "\";\n");
  1768. }
  1769. static void add_srcversion(struct buffer *b, struct module *mod)
  1770. {
  1771. if (mod->srcversion[0]) {
  1772. buf_printf(b, "\n");
  1773. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1774. mod->srcversion);
  1775. }
  1776. }
  1777. static void write_if_changed(struct buffer *b, const char *fname)
  1778. {
  1779. char *tmp;
  1780. FILE *file;
  1781. struct stat st;
  1782. file = fopen(fname, "r");
  1783. if (!file)
  1784. goto write;
  1785. if (fstat(fileno(file), &st) < 0)
  1786. goto close_write;
  1787. if (st.st_size != b->pos)
  1788. goto close_write;
  1789. tmp = NOFAIL(malloc(b->pos));
  1790. if (fread(tmp, 1, b->pos, file) != b->pos)
  1791. goto free_write;
  1792. if (memcmp(tmp, b->p, b->pos) != 0)
  1793. goto free_write;
  1794. free(tmp);
  1795. fclose(file);
  1796. return;
  1797. free_write:
  1798. free(tmp);
  1799. close_write:
  1800. fclose(file);
  1801. write:
  1802. file = fopen(fname, "w");
  1803. if (!file) {
  1804. perror(fname);
  1805. exit(1);
  1806. }
  1807. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1808. perror(fname);
  1809. exit(1);
  1810. }
  1811. fclose(file);
  1812. }
  1813. /* parse Module.symvers file. line format:
  1814. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1815. **/
  1816. static void read_dump(const char *fname, unsigned int kernel)
  1817. {
  1818. unsigned long size, pos = 0;
  1819. void *file = grab_file(fname, &size);
  1820. char *line;
  1821. if (!file)
  1822. /* No symbol versions, silently ignore */
  1823. return;
  1824. while ((line = get_next_line(&pos, file, size))) {
  1825. char *symname, *modname, *d, *export, *end;
  1826. unsigned int crc;
  1827. struct module *mod;
  1828. struct symbol *s;
  1829. if (!(symname = strchr(line, '\t')))
  1830. goto fail;
  1831. *symname++ = '\0';
  1832. if (!(modname = strchr(symname, '\t')))
  1833. goto fail;
  1834. *modname++ = '\0';
  1835. if ((export = strchr(modname, '\t')) != NULL)
  1836. *export++ = '\0';
  1837. if (export && ((end = strchr(export, '\t')) != NULL))
  1838. *end = '\0';
  1839. crc = strtoul(line, &d, 16);
  1840. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1841. goto fail;
  1842. mod = find_module(modname);
  1843. if (!mod) {
  1844. if (is_vmlinux(modname))
  1845. have_vmlinux = 1;
  1846. mod = new_module(modname);
  1847. mod->skip = 1;
  1848. }
  1849. s = sym_add_exported(symname, mod, export_no(export));
  1850. s->kernel = kernel;
  1851. s->preloaded = 1;
  1852. sym_update_crc(symname, mod, crc, export_no(export));
  1853. }
  1854. return;
  1855. fail:
  1856. fatal("parse error in symbol dump file\n");
  1857. }
  1858. /* For normal builds always dump all symbols.
  1859. * For external modules only dump symbols
  1860. * that are not read from kernel Module.symvers.
  1861. **/
  1862. static int dump_sym(struct symbol *sym)
  1863. {
  1864. if (!external_module)
  1865. return 1;
  1866. if (sym->vmlinux || sym->kernel)
  1867. return 0;
  1868. return 1;
  1869. }
  1870. static void write_dump(const char *fname)
  1871. {
  1872. struct buffer buf = { };
  1873. struct symbol *symbol;
  1874. int n;
  1875. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1876. symbol = symbolhash[n];
  1877. while (symbol) {
  1878. if (dump_sym(symbol))
  1879. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1880. symbol->crc, symbol->name,
  1881. symbol->module->name,
  1882. export_str(symbol->export));
  1883. symbol = symbol->next;
  1884. }
  1885. }
  1886. write_if_changed(&buf, fname);
  1887. }
  1888. struct ext_sym_list {
  1889. struct ext_sym_list *next;
  1890. const char *file;
  1891. };
  1892. int main(int argc, char **argv)
  1893. {
  1894. struct module *mod;
  1895. struct buffer buf = { };
  1896. char *kernel_read = NULL, *module_read = NULL;
  1897. char *dump_write = NULL;
  1898. int opt;
  1899. int err;
  1900. struct ext_sym_list *extsym_iter;
  1901. struct ext_sym_list *extsym_start = NULL;
  1902. while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
  1903. switch (opt) {
  1904. case 'i':
  1905. kernel_read = optarg;
  1906. break;
  1907. case 'I':
  1908. module_read = optarg;
  1909. external_module = 1;
  1910. break;
  1911. case 'c':
  1912. cross_build = 1;
  1913. break;
  1914. case 'e':
  1915. external_module = 1;
  1916. extsym_iter =
  1917. NOFAIL(malloc(sizeof(*extsym_iter)));
  1918. extsym_iter->next = extsym_start;
  1919. extsym_iter->file = optarg;
  1920. extsym_start = extsym_iter;
  1921. break;
  1922. case 'm':
  1923. modversions = 1;
  1924. break;
  1925. case 'o':
  1926. dump_write = optarg;
  1927. break;
  1928. case 'a':
  1929. all_versions = 1;
  1930. break;
  1931. case 's':
  1932. vmlinux_section_warnings = 0;
  1933. break;
  1934. case 'S':
  1935. sec_mismatch_verbose = 0;
  1936. break;
  1937. case 'w':
  1938. warn_unresolved = 1;
  1939. break;
  1940. default:
  1941. exit(1);
  1942. }
  1943. }
  1944. if (kernel_read)
  1945. read_dump(kernel_read, 1);
  1946. if (module_read)
  1947. read_dump(module_read, 0);
  1948. while (extsym_start) {
  1949. read_dump(extsym_start->file, 0);
  1950. extsym_iter = extsym_start->next;
  1951. free(extsym_start);
  1952. extsym_start = extsym_iter;
  1953. }
  1954. while (optind < argc)
  1955. read_symbols(argv[optind++]);
  1956. for (mod = modules; mod; mod = mod->next) {
  1957. if (mod->skip)
  1958. continue;
  1959. check_exports(mod);
  1960. }
  1961. err = 0;
  1962. for (mod = modules; mod; mod = mod->next) {
  1963. char fname[strlen(mod->name) + 10];
  1964. if (mod->skip)
  1965. continue;
  1966. buf.pos = 0;
  1967. add_header(&buf, mod);
  1968. add_intree_flag(&buf, !external_module);
  1969. add_staging_flag(&buf, mod->name);
  1970. err |= add_versions(&buf, mod);
  1971. add_depends(&buf, mod, modules);
  1972. add_moddevtable(&buf, mod);
  1973. add_srcversion(&buf, mod);
  1974. sprintf(fname, "%s.mod.c", mod->name);
  1975. write_if_changed(&buf, fname);
  1976. }
  1977. if (dump_write)
  1978. write_dump(dump_write);
  1979. if (sec_mismatch_count && !sec_mismatch_verbose)
  1980. warn("modpost: Found %d section mismatch(es).\n"
  1981. "To see full details build your kernel with:\n"
  1982. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1983. sec_mismatch_count);
  1984. return err;
  1985. }