modpost.c 55 KB

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