modpost.c 57 KB

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