modpost.c 56 KB

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