modpost.c 54 KB

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