modpost.c 54 KB

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