modpost.c 59 KB

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