modpost.c 53 KB

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