modpost.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006 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. #include <ctype.h>
  14. #include "modpost.h"
  15. #include "../../include/linux/license.h"
  16. /* Are we using CONFIG_MODVERSIONS? */
  17. int modversions = 0;
  18. /* Warn about undefined symbols? (do so if we have vmlinux) */
  19. int have_vmlinux = 0;
  20. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  21. static int all_versions = 0;
  22. /* If we are modposting external module set to 1 */
  23. static int external_module = 0;
  24. /* Only warn about unresolved symbols */
  25. static int warn_unresolved = 0;
  26. /* How a symbol is exported */
  27. enum export {
  28. export_plain, export_unused, export_gpl,
  29. export_unused_gpl, export_gpl_future, export_unknown
  30. };
  31. void fatal(const char *fmt, ...)
  32. {
  33. va_list arglist;
  34. fprintf(stderr, "FATAL: ");
  35. va_start(arglist, fmt);
  36. vfprintf(stderr, fmt, arglist);
  37. va_end(arglist);
  38. exit(1);
  39. }
  40. void warn(const char *fmt, ...)
  41. {
  42. va_list arglist;
  43. fprintf(stderr, "WARNING: ");
  44. va_start(arglist, fmt);
  45. vfprintf(stderr, fmt, arglist);
  46. va_end(arglist);
  47. }
  48. void merror(const char *fmt, ...)
  49. {
  50. va_list arglist;
  51. fprintf(stderr, "ERROR: ");
  52. va_start(arglist, fmt);
  53. vfprintf(stderr, fmt, arglist);
  54. va_end(arglist);
  55. }
  56. static int is_vmlinux(const char *modname)
  57. {
  58. const char *myname;
  59. if ((myname = strrchr(modname, '/')))
  60. myname++;
  61. else
  62. myname = modname;
  63. return (strcmp(myname, "vmlinux") == 0) ||
  64. (strcmp(myname, "vmlinux.o") == 0);
  65. }
  66. void *do_nofail(void *ptr, const char *expr)
  67. {
  68. if (!ptr) {
  69. fatal("modpost: Memory allocation failure: %s.\n", expr);
  70. }
  71. return ptr;
  72. }
  73. /* A list of all modules we processed */
  74. static struct module *modules;
  75. static struct module *find_module(char *modname)
  76. {
  77. struct module *mod;
  78. for (mod = modules; mod; mod = mod->next)
  79. if (strcmp(mod->name, modname) == 0)
  80. break;
  81. return mod;
  82. }
  83. static struct module *new_module(char *modname)
  84. {
  85. struct module *mod;
  86. char *p, *s;
  87. mod = NOFAIL(malloc(sizeof(*mod)));
  88. memset(mod, 0, sizeof(*mod));
  89. p = NOFAIL(strdup(modname));
  90. /* strip trailing .o */
  91. if ((s = strrchr(p, '.')) != NULL)
  92. if (strcmp(s, ".o") == 0)
  93. *s = '\0';
  94. /* add to list */
  95. mod->name = p;
  96. mod->gpl_compatible = -1;
  97. mod->next = modules;
  98. modules = mod;
  99. return mod;
  100. }
  101. /* A hash of all exported symbols,
  102. * struct symbol is also used for lists of unresolved symbols */
  103. #define SYMBOL_HASH_SIZE 1024
  104. struct symbol {
  105. struct symbol *next;
  106. struct module *module;
  107. unsigned int crc;
  108. int crc_valid;
  109. unsigned int weak:1;
  110. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  111. unsigned int kernel:1; /* 1 if symbol is from kernel
  112. * (only for external modules) **/
  113. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  114. enum export export; /* Type of export */
  115. char name[0];
  116. };
  117. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  118. /* This is based on the hash agorithm from gdbm, via tdb */
  119. static inline unsigned int tdb_hash(const char *name)
  120. {
  121. unsigned value; /* Used to compute the hash value. */
  122. unsigned i; /* Used to cycle through random values. */
  123. /* Set the initial value from the key size. */
  124. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  125. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  126. return (1103515243 * value + 12345);
  127. }
  128. /**
  129. * Allocate a new symbols for use in the hash of exported symbols or
  130. * the list of unresolved symbols per module
  131. **/
  132. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  133. struct symbol *next)
  134. {
  135. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  136. memset(s, 0, sizeof(*s));
  137. strcpy(s->name, name);
  138. s->weak = weak;
  139. s->next = next;
  140. return s;
  141. }
  142. /* For the hash of exported symbols */
  143. static struct symbol *new_symbol(const char *name, struct module *module,
  144. enum export export)
  145. {
  146. unsigned int hash;
  147. struct symbol *new;
  148. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  149. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  150. new->module = module;
  151. new->export = export;
  152. return new;
  153. }
  154. static struct symbol *find_symbol(const char *name)
  155. {
  156. struct symbol *s;
  157. /* For our purposes, .foo matches foo. PPC64 needs this. */
  158. if (name[0] == '.')
  159. name++;
  160. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  161. if (strcmp(s->name, name) == 0)
  162. return s;
  163. }
  164. return NULL;
  165. }
  166. static struct {
  167. const char *str;
  168. enum export export;
  169. } export_list[] = {
  170. { .str = "EXPORT_SYMBOL", .export = export_plain },
  171. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  172. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  173. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  174. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  175. { .str = "(unknown)", .export = export_unknown },
  176. };
  177. static const char *export_str(enum export ex)
  178. {
  179. return export_list[ex].str;
  180. }
  181. static enum export export_no(const char * s)
  182. {
  183. int i;
  184. if (!s)
  185. return export_unknown;
  186. for (i = 0; export_list[i].export != export_unknown; i++) {
  187. if (strcmp(export_list[i].str, s) == 0)
  188. return export_list[i].export;
  189. }
  190. return export_unknown;
  191. }
  192. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  193. {
  194. if (sec == elf->export_sec)
  195. return export_plain;
  196. else if (sec == elf->export_unused_sec)
  197. return export_unused;
  198. else if (sec == elf->export_gpl_sec)
  199. return export_gpl;
  200. else if (sec == elf->export_unused_gpl_sec)
  201. return export_unused_gpl;
  202. else if (sec == elf->export_gpl_future_sec)
  203. return export_gpl_future;
  204. else
  205. return export_unknown;
  206. }
  207. /**
  208. * Add an exported symbol - it may have already been added without a
  209. * CRC, in this case just update the CRC
  210. **/
  211. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  212. enum export export)
  213. {
  214. struct symbol *s = find_symbol(name);
  215. if (!s) {
  216. s = new_symbol(name, mod, export);
  217. } else {
  218. if (!s->preloaded) {
  219. warn("%s: '%s' exported twice. Previous export "
  220. "was in %s%s\n", mod->name, name,
  221. s->module->name,
  222. is_vmlinux(s->module->name) ?"":".ko");
  223. }
  224. }
  225. s->preloaded = 0;
  226. s->vmlinux = is_vmlinux(mod->name);
  227. s->kernel = 0;
  228. s->export = export;
  229. return s;
  230. }
  231. static void sym_update_crc(const char *name, struct module *mod,
  232. unsigned int crc, enum export export)
  233. {
  234. struct symbol *s = find_symbol(name);
  235. if (!s)
  236. s = new_symbol(name, mod, export);
  237. s->crc = crc;
  238. s->crc_valid = 1;
  239. }
  240. void *grab_file(const char *filename, unsigned long *size)
  241. {
  242. struct stat st;
  243. void *map;
  244. int fd;
  245. fd = open(filename, O_RDONLY);
  246. if (fd < 0 || fstat(fd, &st) != 0)
  247. return NULL;
  248. *size = st.st_size;
  249. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  250. close(fd);
  251. if (map == MAP_FAILED)
  252. return NULL;
  253. return map;
  254. }
  255. /**
  256. * Return a copy of the next line in a mmap'ed file.
  257. * spaces in the beginning of the line is trimmed away.
  258. * Return a pointer to a static buffer.
  259. **/
  260. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  261. {
  262. static char line[4096];
  263. int skip = 1;
  264. size_t len = 0;
  265. signed char *p = (signed char *)file + *pos;
  266. char *s = line;
  267. for (; *pos < size ; (*pos)++)
  268. {
  269. if (skip && isspace(*p)) {
  270. p++;
  271. continue;
  272. }
  273. skip = 0;
  274. if (*p != '\n' && (*pos < size)) {
  275. len++;
  276. *s++ = *p++;
  277. if (len > 4095)
  278. break; /* Too long, stop */
  279. } else {
  280. /* End of string */
  281. *s = '\0';
  282. return line;
  283. }
  284. }
  285. /* End of buffer */
  286. return NULL;
  287. }
  288. void release_file(void *file, unsigned long size)
  289. {
  290. munmap(file, size);
  291. }
  292. static int parse_elf(struct elf_info *info, const char *filename)
  293. {
  294. unsigned int i;
  295. Elf_Ehdr *hdr;
  296. Elf_Shdr *sechdrs;
  297. Elf_Sym *sym;
  298. hdr = grab_file(filename, &info->size);
  299. if (!hdr) {
  300. perror(filename);
  301. exit(1);
  302. }
  303. info->hdr = hdr;
  304. if (info->size < sizeof(*hdr)) {
  305. /* file too small, assume this is an empty .o file */
  306. return 0;
  307. }
  308. /* Is this a valid ELF file? */
  309. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  310. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  311. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  312. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  313. /* Not an ELF file - silently ignore it */
  314. return 0;
  315. }
  316. /* Fix endianness in ELF header */
  317. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  318. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  319. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  320. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  321. hdr->e_type = TO_NATIVE(hdr->e_type);
  322. sechdrs = (void *)hdr + hdr->e_shoff;
  323. info->sechdrs = sechdrs;
  324. /* Fix endianness in section headers */
  325. for (i = 0; i < hdr->e_shnum; i++) {
  326. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  327. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  328. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  329. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  330. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  331. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  332. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  333. }
  334. /* Find symbol table. */
  335. for (i = 1; i < hdr->e_shnum; i++) {
  336. const char *secstrings
  337. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  338. const char *secname;
  339. if (sechdrs[i].sh_offset > info->size) {
  340. fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
  341. return 0;
  342. }
  343. secname = secstrings + sechdrs[i].sh_name;
  344. if (strcmp(secname, ".modinfo") == 0) {
  345. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  346. info->modinfo_len = sechdrs[i].sh_size;
  347. } else if (strcmp(secname, "__ksymtab") == 0)
  348. info->export_sec = i;
  349. else if (strcmp(secname, "__ksymtab_unused") == 0)
  350. info->export_unused_sec = i;
  351. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  352. info->export_gpl_sec = i;
  353. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  354. info->export_unused_gpl_sec = i;
  355. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  356. info->export_gpl_future_sec = i;
  357. if (sechdrs[i].sh_type != SHT_SYMTAB)
  358. continue;
  359. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  360. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  361. + sechdrs[i].sh_size;
  362. info->strtab = (void *)hdr +
  363. sechdrs[sechdrs[i].sh_link].sh_offset;
  364. }
  365. if (!info->symtab_start) {
  366. fatal("%s has no symtab?\n", filename);
  367. }
  368. /* Fix endianness in symbols */
  369. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  370. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  371. sym->st_name = TO_NATIVE(sym->st_name);
  372. sym->st_value = TO_NATIVE(sym->st_value);
  373. sym->st_size = TO_NATIVE(sym->st_size);
  374. }
  375. return 1;
  376. }
  377. static void parse_elf_finish(struct elf_info *info)
  378. {
  379. release_file(info->hdr, info->size);
  380. }
  381. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  382. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  383. static void handle_modversions(struct module *mod, struct elf_info *info,
  384. Elf_Sym *sym, const char *symname)
  385. {
  386. unsigned int crc;
  387. enum export export = export_from_sec(info, sym->st_shndx);
  388. switch (sym->st_shndx) {
  389. case SHN_COMMON:
  390. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  391. break;
  392. case SHN_ABS:
  393. /* CRC'd symbol */
  394. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  395. crc = (unsigned int) sym->st_value;
  396. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  397. export);
  398. }
  399. break;
  400. case SHN_UNDEF:
  401. /* undefined symbol */
  402. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  403. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  404. break;
  405. /* ignore global offset table */
  406. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  407. break;
  408. /* ignore __this_module, it will be resolved shortly */
  409. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  410. break;
  411. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  412. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  413. /* add compatibility with older glibc */
  414. #ifndef STT_SPARC_REGISTER
  415. #define STT_SPARC_REGISTER STT_REGISTER
  416. #endif
  417. if (info->hdr->e_machine == EM_SPARC ||
  418. info->hdr->e_machine == EM_SPARCV9) {
  419. /* Ignore register directives. */
  420. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  421. break;
  422. if (symname[0] == '.') {
  423. char *munged = strdup(symname);
  424. munged[0] = '_';
  425. munged[1] = toupper(munged[1]);
  426. symname = munged;
  427. }
  428. }
  429. #endif
  430. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  431. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  432. mod->unres = alloc_symbol(symname +
  433. strlen(MODULE_SYMBOL_PREFIX),
  434. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  435. mod->unres);
  436. break;
  437. default:
  438. /* All exported symbols */
  439. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  440. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  441. export);
  442. }
  443. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  444. mod->has_init = 1;
  445. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  446. mod->has_cleanup = 1;
  447. break;
  448. }
  449. }
  450. /**
  451. * Parse tag=value strings from .modinfo section
  452. **/
  453. static char *next_string(char *string, unsigned long *secsize)
  454. {
  455. /* Skip non-zero chars */
  456. while (string[0]) {
  457. string++;
  458. if ((*secsize)-- <= 1)
  459. return NULL;
  460. }
  461. /* Skip any zero padding. */
  462. while (!string[0]) {
  463. string++;
  464. if ((*secsize)-- <= 1)
  465. return NULL;
  466. }
  467. return string;
  468. }
  469. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  470. const char *tag, char *info)
  471. {
  472. char *p;
  473. unsigned int taglen = strlen(tag);
  474. unsigned long size = modinfo_len;
  475. if (info) {
  476. size -= info - (char *)modinfo;
  477. modinfo = next_string(info, &size);
  478. }
  479. for (p = modinfo; p; p = next_string(p, &size)) {
  480. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  481. return p + taglen + 1;
  482. }
  483. return NULL;
  484. }
  485. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  486. const char *tag)
  487. {
  488. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  489. }
  490. /**
  491. * Test if string s ends in string sub
  492. * return 0 if match
  493. **/
  494. static int strrcmp(const char *s, const char *sub)
  495. {
  496. int slen, sublen;
  497. if (!s || !sub)
  498. return 1;
  499. slen = strlen(s);
  500. sublen = strlen(sub);
  501. if ((slen == 0) || (sublen == 0))
  502. return 1;
  503. if (sublen > slen)
  504. return 1;
  505. return memcmp(s + slen - sublen, sub, sublen);
  506. }
  507. /**
  508. * Whitelist to allow certain references to pass with no warning.
  509. *
  510. * Pattern 0:
  511. * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
  512. * The pattern is identified by:
  513. * fromsec = .text.init.refok | .data.init.refok
  514. *
  515. * Pattern 1:
  516. * If a module parameter is declared __initdata and permissions=0
  517. * then this is legal despite the warning generated.
  518. * We cannot see value of permissions here, so just ignore
  519. * this pattern.
  520. * The pattern is identified by:
  521. * tosec = .init.data
  522. * fromsec = .data*
  523. * atsym =__param*
  524. *
  525. * Pattern 2:
  526. * Many drivers utilise a *driver container with references to
  527. * add, remove, probe functions etc.
  528. * These functions may often be marked __init and we do not want to
  529. * warn here.
  530. * the pattern is identified by:
  531. * tosec = .init.text | .exit.text | .init.data
  532. * fromsec = .data | .data.rel | .data.rel.*
  533. * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer
  534. *
  535. * Pattern 3:
  536. * Whitelist all refereces from .text.head to .init.data
  537. * Whitelist all refereces from .text.head to .init.text
  538. *
  539. * Pattern 4:
  540. * Some symbols belong to init section but still it is ok to reference
  541. * these from non-init sections as these symbols don't have any memory
  542. * allocated for them and symbol address and value are same. So even
  543. * if init section is freed, its ok to reference those symbols.
  544. * For ex. symbols marking the init section boundaries.
  545. * This pattern is identified by
  546. * refsymname = __init_begin, _sinittext, _einittext
  547. *
  548. **/
  549. static int secref_whitelist(const char *modname, const char *tosec,
  550. const char *fromsec, const char *atsym,
  551. const char *refsymname)
  552. {
  553. int f1 = 1, f2 = 1;
  554. const char **s;
  555. const char *pat2sym[] = {
  556. "driver",
  557. "_template", /* scsi uses *_template a lot */
  558. "_timer", /* arm uses ops structures named _timer a lot */
  559. "_sht", /* scsi also used *_sht to some extent */
  560. "_ops",
  561. "_probe",
  562. "_probe_one",
  563. "_console",
  564. NULL
  565. };
  566. const char *pat3refsym[] = {
  567. "__init_begin",
  568. "_sinittext",
  569. "_einittext",
  570. NULL
  571. };
  572. /* Check for pattern 0 */
  573. if ((strcmp(fromsec, ".text.init.refok") == 0) ||
  574. (strcmp(fromsec, ".data.init.refok") == 0))
  575. return 1;
  576. /* Check for pattern 1 */
  577. if (strcmp(tosec, ".init.data") != 0)
  578. f1 = 0;
  579. if (strncmp(fromsec, ".data", strlen(".data")) != 0)
  580. f1 = 0;
  581. if (strncmp(atsym, "__param", strlen("__param")) != 0)
  582. f1 = 0;
  583. if (f1)
  584. return f1;
  585. /* Check for pattern 2 */
  586. if ((strcmp(tosec, ".init.text") != 0) &&
  587. (strcmp(tosec, ".exit.text") != 0) &&
  588. (strcmp(tosec, ".init.data") != 0))
  589. f2 = 0;
  590. if ((strcmp(fromsec, ".data") != 0) &&
  591. (strcmp(fromsec, ".data.rel") != 0) &&
  592. (strncmp(fromsec, ".data.rel.", strlen(".data.rel.")) != 0))
  593. f2 = 0;
  594. for (s = pat2sym; *s; s++)
  595. if (strrcmp(atsym, *s) == 0)
  596. f1 = 1;
  597. if (f1 && f2)
  598. return 1;
  599. /* Check for pattern 3 */
  600. if ((strcmp(fromsec, ".text.head") == 0) &&
  601. ((strcmp(tosec, ".init.data") == 0) ||
  602. (strcmp(tosec, ".init.text") == 0)))
  603. return 1;
  604. /* Check for pattern 4 */
  605. for (s = pat3refsym; *s; s++)
  606. if (strcmp(refsymname, *s) == 0)
  607. return 1;
  608. return 0;
  609. }
  610. /**
  611. * Find symbol based on relocation record info.
  612. * In some cases the symbol supplied is a valid symbol so
  613. * return refsym. If st_name != 0 we assume this is a valid symbol.
  614. * In other cases the symbol needs to be looked up in the symbol table
  615. * based on section and address.
  616. * **/
  617. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  618. Elf_Sym *relsym)
  619. {
  620. Elf_Sym *sym;
  621. if (relsym->st_name != 0)
  622. return relsym;
  623. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  624. if (sym->st_shndx != relsym->st_shndx)
  625. continue;
  626. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  627. continue;
  628. if (sym->st_value == addr)
  629. return sym;
  630. }
  631. return NULL;
  632. }
  633. static inline int is_arm_mapping_symbol(const char *str)
  634. {
  635. return str[0] == '$' && strchr("atd", str[1])
  636. && (str[2] == '\0' || str[2] == '.');
  637. }
  638. /*
  639. * If there's no name there, ignore it; likewise, ignore it if it's
  640. * one of the magic symbols emitted used by current ARM tools.
  641. *
  642. * Otherwise if find_symbols_between() returns those symbols, they'll
  643. * fail the whitelist tests and cause lots of false alarms ... fixable
  644. * only by merging __exit and __init sections into __text, bloating
  645. * the kernel (which is especially evil on embedded platforms).
  646. */
  647. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  648. {
  649. const char *name = elf->strtab + sym->st_name;
  650. if (!name || !strlen(name))
  651. return 0;
  652. return !is_arm_mapping_symbol(name);
  653. }
  654. /*
  655. * Find symbols before or equal addr and after addr - in the section sec.
  656. * If we find two symbols with equal offset prefer one with a valid name.
  657. * The ELF format may have a better way to detect what type of symbol
  658. * it is, but this works for now.
  659. **/
  660. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  661. const char *sec,
  662. Elf_Sym **before, Elf_Sym **after)
  663. {
  664. Elf_Sym *sym;
  665. Elf_Ehdr *hdr = elf->hdr;
  666. Elf_Addr beforediff = ~0;
  667. Elf_Addr afterdiff = ~0;
  668. const char *secstrings = (void *)hdr +
  669. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  670. *before = NULL;
  671. *after = NULL;
  672. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  673. const char *symsec;
  674. if (sym->st_shndx >= SHN_LORESERVE)
  675. continue;
  676. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  677. if (strcmp(symsec, sec) != 0)
  678. continue;
  679. if (!is_valid_name(elf, sym))
  680. continue;
  681. if (sym->st_value <= addr) {
  682. if ((addr - sym->st_value) < beforediff) {
  683. beforediff = addr - sym->st_value;
  684. *before = sym;
  685. }
  686. else if ((addr - sym->st_value) == beforediff) {
  687. *before = sym;
  688. }
  689. }
  690. else
  691. {
  692. if ((sym->st_value - addr) < afterdiff) {
  693. afterdiff = sym->st_value - addr;
  694. *after = sym;
  695. }
  696. else if ((sym->st_value - addr) == afterdiff) {
  697. *after = sym;
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * Print a warning about a section mismatch.
  704. * Try to find symbols near it so user can find it.
  705. * Check whitelist before warning - it may be a false positive.
  706. **/
  707. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  708. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  709. {
  710. const char *refsymname = "";
  711. Elf_Sym *before, *after;
  712. Elf_Sym *refsym;
  713. Elf_Ehdr *hdr = elf->hdr;
  714. Elf_Shdr *sechdrs = elf->sechdrs;
  715. const char *secstrings = (void *)hdr +
  716. sechdrs[hdr->e_shstrndx].sh_offset;
  717. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  718. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  719. refsym = find_elf_symbol(elf, r.r_addend, sym);
  720. if (refsym && strlen(elf->strtab + refsym->st_name))
  721. refsymname = elf->strtab + refsym->st_name;
  722. /* check whitelist - we may ignore it */
  723. if (before &&
  724. secref_whitelist(modname, secname, fromsec,
  725. elf->strtab + before->st_name, refsymname))
  726. return;
  727. if (before && after) {
  728. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  729. "(between '%s' and '%s')\n",
  730. modname, fromsec, (unsigned long long)r.r_offset,
  731. secname, refsymname,
  732. elf->strtab + before->st_name,
  733. elf->strtab + after->st_name);
  734. } else if (before) {
  735. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  736. "(after '%s')\n",
  737. modname, fromsec, (unsigned long long)r.r_offset,
  738. secname, refsymname,
  739. elf->strtab + before->st_name);
  740. } else if (after) {
  741. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  742. "before '%s' (at offset -0x%llx)\n",
  743. modname, fromsec, (unsigned long long)r.r_offset,
  744. secname, refsymname,
  745. elf->strtab + after->st_name);
  746. } else {
  747. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
  748. modname, fromsec, (unsigned long long)r.r_offset,
  749. secname, refsymname);
  750. }
  751. }
  752. static unsigned int *reloc_location(struct elf_info *elf,
  753. int rsection, Elf_Rela *r)
  754. {
  755. Elf_Shdr *sechdrs = elf->sechdrs;
  756. int section = sechdrs[rsection].sh_info;
  757. return (void *)elf->hdr + sechdrs[section].sh_offset +
  758. (r->r_offset - sechdrs[section].sh_addr);
  759. }
  760. static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  761. {
  762. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  763. unsigned int *location = reloc_location(elf, rsection, r);
  764. switch (r_typ) {
  765. case R_386_32:
  766. r->r_addend = TO_NATIVE(*location);
  767. break;
  768. case R_386_PC32:
  769. r->r_addend = TO_NATIVE(*location) + 4;
  770. /* For CONFIG_RELOCATABLE=y */
  771. if (elf->hdr->e_type == ET_EXEC)
  772. r->r_addend += r->r_offset;
  773. break;
  774. }
  775. return 0;
  776. }
  777. static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  778. {
  779. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  780. switch (r_typ) {
  781. case R_ARM_ABS32:
  782. /* From ARM ABI: (S + A) | T */
  783. r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info));
  784. break;
  785. case R_ARM_PC24:
  786. /* From ARM ABI: ((S + A) | T) - P */
  787. r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset +
  788. (r->r_offset - elf->sechdrs[rsection].sh_addr));
  789. break;
  790. default:
  791. return 1;
  792. }
  793. return 0;
  794. }
  795. static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  796. {
  797. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  798. unsigned int *location = reloc_location(elf, rsection, r);
  799. unsigned int inst;
  800. if (r_typ == R_MIPS_HI16)
  801. return 1; /* skip this */
  802. inst = TO_NATIVE(*location);
  803. switch (r_typ) {
  804. case R_MIPS_LO16:
  805. r->r_addend = inst & 0xffff;
  806. break;
  807. case R_MIPS_26:
  808. r->r_addend = (inst & 0x03ffffff) << 2;
  809. break;
  810. case R_MIPS_32:
  811. r->r_addend = inst;
  812. break;
  813. }
  814. return 0;
  815. }
  816. /**
  817. * A module includes a number of sections that are discarded
  818. * either when loaded or when used as built-in.
  819. * For loaded modules all functions marked __init and all data
  820. * marked __initdata will be discarded when the module has been intialized.
  821. * Likewise for modules used built-in the sections marked __exit
  822. * are discarded because __exit marked function are supposed to be called
  823. * only when a moduel is unloaded which never happes for built-in modules.
  824. * The check_sec_ref() function traverses all relocation records
  825. * to find all references to a section that reference a section that will
  826. * be discarded and warns about it.
  827. **/
  828. static void check_sec_ref(struct module *mod, const char *modname,
  829. struct elf_info *elf,
  830. int section(const char*),
  831. int section_ref_ok(const char *))
  832. {
  833. int i;
  834. Elf_Sym *sym;
  835. Elf_Ehdr *hdr = elf->hdr;
  836. Elf_Shdr *sechdrs = elf->sechdrs;
  837. const char *secstrings = (void *)hdr +
  838. sechdrs[hdr->e_shstrndx].sh_offset;
  839. /* Walk through all sections */
  840. for (i = 0; i < hdr->e_shnum; i++) {
  841. const char *name = secstrings + sechdrs[i].sh_name;
  842. const char *secname;
  843. Elf_Rela r;
  844. unsigned int r_sym;
  845. /* We want to process only relocation sections and not .init */
  846. if (sechdrs[i].sh_type == SHT_RELA) {
  847. Elf_Rela *rela;
  848. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  849. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  850. name += strlen(".rela");
  851. if (section_ref_ok(name))
  852. continue;
  853. for (rela = start; rela < stop; rela++) {
  854. r.r_offset = TO_NATIVE(rela->r_offset);
  855. #if KERNEL_ELFCLASS == ELFCLASS64
  856. if (hdr->e_machine == EM_MIPS) {
  857. unsigned int r_typ;
  858. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  859. r_sym = TO_NATIVE(r_sym);
  860. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  861. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  862. } else {
  863. r.r_info = TO_NATIVE(rela->r_info);
  864. r_sym = ELF_R_SYM(r.r_info);
  865. }
  866. #else
  867. r.r_info = TO_NATIVE(rela->r_info);
  868. r_sym = ELF_R_SYM(r.r_info);
  869. #endif
  870. r.r_addend = TO_NATIVE(rela->r_addend);
  871. sym = elf->symtab_start + r_sym;
  872. /* Skip special sections */
  873. if (sym->st_shndx >= SHN_LORESERVE)
  874. continue;
  875. secname = secstrings +
  876. sechdrs[sym->st_shndx].sh_name;
  877. if (section(secname))
  878. warn_sec_mismatch(modname, name,
  879. elf, sym, r);
  880. }
  881. } else if (sechdrs[i].sh_type == SHT_REL) {
  882. Elf_Rel *rel;
  883. Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
  884. Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
  885. name += strlen(".rel");
  886. if (section_ref_ok(name))
  887. continue;
  888. for (rel = start; rel < stop; rel++) {
  889. r.r_offset = TO_NATIVE(rel->r_offset);
  890. #if KERNEL_ELFCLASS == ELFCLASS64
  891. if (hdr->e_machine == EM_MIPS) {
  892. unsigned int r_typ;
  893. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  894. r_sym = TO_NATIVE(r_sym);
  895. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  896. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  897. } else {
  898. r.r_info = TO_NATIVE(rel->r_info);
  899. r_sym = ELF_R_SYM(r.r_info);
  900. }
  901. #else
  902. r.r_info = TO_NATIVE(rel->r_info);
  903. r_sym = ELF_R_SYM(r.r_info);
  904. #endif
  905. r.r_addend = 0;
  906. switch (hdr->e_machine) {
  907. case EM_386:
  908. if (addend_386_rel(elf, i, &r))
  909. continue;
  910. break;
  911. case EM_ARM:
  912. if(addend_arm_rel(elf, i, &r))
  913. continue;
  914. break;
  915. case EM_MIPS:
  916. if (addend_mips_rel(elf, i, &r))
  917. continue;
  918. break;
  919. }
  920. sym = elf->symtab_start + r_sym;
  921. /* Skip special sections */
  922. if (sym->st_shndx >= SHN_LORESERVE)
  923. continue;
  924. secname = secstrings +
  925. sechdrs[sym->st_shndx].sh_name;
  926. if (section(secname))
  927. warn_sec_mismatch(modname, name,
  928. elf, sym, r);
  929. }
  930. }
  931. }
  932. }
  933. /*
  934. * Identify sections from which references to either a
  935. * .init or a .exit section is OK.
  936. *
  937. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  938. * For our future {in}sanity, add a comment that this is the ppc .opd
  939. * section, not the ia64 .opd section.
  940. * ia64 .opd should not point to discarded sections.
  941. * [.rodata] like for .init.text we ignore .rodata references -same reason
  942. */
  943. static int initexit_section_ref_ok(const char *name)
  944. {
  945. const char **s;
  946. /* Absolute section names */
  947. const char *namelist1[] = {
  948. "__bug_table", /* used by powerpc for BUG() */
  949. "__ex_table",
  950. ".altinstructions",
  951. ".cranges", /* used by sh64 */
  952. ".fixup",
  953. ".machvec", /* ia64 + powerpc uses these */
  954. ".machine.desc",
  955. ".opd", /* See comment [OPD] */
  956. ".parainstructions",
  957. ".pdr",
  958. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  959. ".smp_locks",
  960. ".stab",
  961. ".m68k_fixup",
  962. NULL
  963. };
  964. /* Start of section names */
  965. const char *namelist2[] = {
  966. ".debug",
  967. ".eh_frame",
  968. ".note", /* ignore ELF notes - may contain anything */
  969. ".got", /* powerpc - global offset table */
  970. ".toc", /* powerpc - table of contents */
  971. NULL
  972. };
  973. /* part of section name */
  974. const char *namelist3 [] = {
  975. ".unwind", /* Sample: IA_64.unwind.exit.text */
  976. NULL
  977. };
  978. for (s = namelist1; *s; s++)
  979. if (strcmp(*s, name) == 0)
  980. return 1;
  981. for (s = namelist2; *s; s++)
  982. if (strncmp(*s, name, strlen(*s)) == 0)
  983. return 1;
  984. for (s = namelist3; *s; s++)
  985. if (strstr(name, *s) != NULL)
  986. return 1;
  987. return 0;
  988. }
  989. /**
  990. * Functions used only during module init is marked __init and is stored in
  991. * a .init.text section. Likewise data is marked __initdata and stored in
  992. * a .init.data section.
  993. * If this section is one of these sections return 1
  994. * See include/linux/init.h for the details
  995. **/
  996. static int init_section(const char *name)
  997. {
  998. if (strcmp(name, ".init") == 0)
  999. return 1;
  1000. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  1001. return 1;
  1002. return 0;
  1003. }
  1004. /*
  1005. * Identify sections from which references to a .init section is OK.
  1006. *
  1007. * Unfortunately references to read only data that referenced .init
  1008. * sections had to be excluded. Almost all of these are false
  1009. * positives, they are created by gcc. The downside of excluding rodata
  1010. * is that there really are some user references from rodata to
  1011. * init code, e.g. drivers/video/vgacon.c:
  1012. *
  1013. * const struct consw vga_con = {
  1014. * con_startup: vgacon_startup,
  1015. *
  1016. * where vgacon_startup is __init. If you want to wade through the false
  1017. * positives, take out the check for rodata.
  1018. */
  1019. static int init_section_ref_ok(const char *name)
  1020. {
  1021. const char **s;
  1022. /* Absolute section names */
  1023. const char *namelist1[] = {
  1024. "__dbe_table", /* MIPS generate these */
  1025. "__ftr_fixup", /* powerpc cpu feature fixup */
  1026. "__fw_ftr_fixup", /* powerpc firmware feature fixup */
  1027. "__param",
  1028. ".data.rel.ro", /* used by parisc64 */
  1029. ".init",
  1030. ".text.lock",
  1031. NULL
  1032. };
  1033. /* Start of section names */
  1034. const char *namelist2[] = {
  1035. ".init.",
  1036. ".pci_fixup",
  1037. ".rodata",
  1038. NULL
  1039. };
  1040. if (initexit_section_ref_ok(name))
  1041. return 1;
  1042. for (s = namelist1; *s; s++)
  1043. if (strcmp(*s, name) == 0)
  1044. return 1;
  1045. for (s = namelist2; *s; s++)
  1046. if (strncmp(*s, name, strlen(*s)) == 0)
  1047. return 1;
  1048. /* If section name ends with ".init" we allow references
  1049. * as is the case with .initcallN.init, .early_param.init, .taglist.init etc
  1050. */
  1051. if (strrcmp(name, ".init") == 0)
  1052. return 1;
  1053. return 0;
  1054. }
  1055. /*
  1056. * Functions used only during module exit is marked __exit and is stored in
  1057. * a .exit.text section. Likewise data is marked __exitdata and stored in
  1058. * a .exit.data section.
  1059. * If this section is one of these sections return 1
  1060. * See include/linux/init.h for the details
  1061. **/
  1062. static int exit_section(const char *name)
  1063. {
  1064. if (strcmp(name, ".exit.text") == 0)
  1065. return 1;
  1066. if (strcmp(name, ".exit.data") == 0)
  1067. return 1;
  1068. return 0;
  1069. }
  1070. /*
  1071. * Identify sections from which references to a .exit section is OK.
  1072. */
  1073. static int exit_section_ref_ok(const char *name)
  1074. {
  1075. const char **s;
  1076. /* Absolute section names */
  1077. const char *namelist1[] = {
  1078. ".exit.data",
  1079. ".exit.text",
  1080. ".exitcall.exit",
  1081. ".rodata",
  1082. NULL
  1083. };
  1084. if (initexit_section_ref_ok(name))
  1085. return 1;
  1086. for (s = namelist1; *s; s++)
  1087. if (strcmp(*s, name) == 0)
  1088. return 1;
  1089. return 0;
  1090. }
  1091. static void read_symbols(char *modname)
  1092. {
  1093. const char *symname;
  1094. char *version;
  1095. char *license;
  1096. struct module *mod;
  1097. struct elf_info info = { };
  1098. Elf_Sym *sym;
  1099. if (!parse_elf(&info, modname))
  1100. return;
  1101. mod = new_module(modname);
  1102. /* When there's no vmlinux, don't print warnings about
  1103. * unresolved symbols (since there'll be too many ;) */
  1104. if (is_vmlinux(modname)) {
  1105. have_vmlinux = 1;
  1106. mod->skip = 1;
  1107. }
  1108. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1109. while (license) {
  1110. if (license_is_gpl_compatible(license))
  1111. mod->gpl_compatible = 1;
  1112. else {
  1113. mod->gpl_compatible = 0;
  1114. break;
  1115. }
  1116. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1117. "license", license);
  1118. }
  1119. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1120. symname = info.strtab + sym->st_name;
  1121. handle_modversions(mod, &info, sym, symname);
  1122. handle_moddevtable(mod, &info, sym, symname);
  1123. }
  1124. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  1125. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  1126. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1127. if (version)
  1128. maybe_frob_rcs_version(modname, version, info.modinfo,
  1129. version - (char *)info.hdr);
  1130. if (version || (all_versions && !is_vmlinux(modname)))
  1131. get_src_version(modname, mod->srcversion,
  1132. sizeof(mod->srcversion)-1);
  1133. parse_elf_finish(&info);
  1134. /* Our trick to get versioning for struct_module - it's
  1135. * never passed as an argument to an exported function, so
  1136. * the automatic versioning doesn't pick it up, but it's really
  1137. * important anyhow */
  1138. if (modversions)
  1139. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1140. }
  1141. #define SZ 500
  1142. /* We first write the generated file into memory using the
  1143. * following helper, then compare to the file on disk and
  1144. * only update the later if anything changed */
  1145. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1146. const char *fmt, ...)
  1147. {
  1148. char tmp[SZ];
  1149. int len;
  1150. va_list ap;
  1151. va_start(ap, fmt);
  1152. len = vsnprintf(tmp, SZ, fmt, ap);
  1153. buf_write(buf, tmp, len);
  1154. va_end(ap);
  1155. }
  1156. void buf_write(struct buffer *buf, const char *s, int len)
  1157. {
  1158. if (buf->size - buf->pos < len) {
  1159. buf->size += len + SZ;
  1160. buf->p = realloc(buf->p, buf->size);
  1161. }
  1162. strncpy(buf->p + buf->pos, s, len);
  1163. buf->pos += len;
  1164. }
  1165. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1166. {
  1167. const char *e = is_vmlinux(m) ?"":".ko";
  1168. switch (exp) {
  1169. case export_gpl:
  1170. fatal("modpost: GPL-incompatible module %s%s "
  1171. "uses GPL-only symbol '%s'\n", m, e, s);
  1172. break;
  1173. case export_unused_gpl:
  1174. fatal("modpost: GPL-incompatible module %s%s "
  1175. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1176. break;
  1177. case export_gpl_future:
  1178. warn("modpost: GPL-incompatible module %s%s "
  1179. "uses future GPL-only symbol '%s'\n", m, e, s);
  1180. break;
  1181. case export_plain:
  1182. case export_unused:
  1183. case export_unknown:
  1184. /* ignore */
  1185. break;
  1186. }
  1187. }
  1188. static void check_for_unused(enum export exp, const char* m, const char* s)
  1189. {
  1190. const char *e = is_vmlinux(m) ?"":".ko";
  1191. switch (exp) {
  1192. case export_unused:
  1193. case export_unused_gpl:
  1194. warn("modpost: module %s%s "
  1195. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1196. break;
  1197. default:
  1198. /* ignore */
  1199. break;
  1200. }
  1201. }
  1202. static void check_exports(struct module *mod)
  1203. {
  1204. struct symbol *s, *exp;
  1205. for (s = mod->unres; s; s = s->next) {
  1206. const char *basename;
  1207. exp = find_symbol(s->name);
  1208. if (!exp || exp->module == mod)
  1209. continue;
  1210. basename = strrchr(mod->name, '/');
  1211. if (basename)
  1212. basename++;
  1213. else
  1214. basename = mod->name;
  1215. if (!mod->gpl_compatible)
  1216. check_for_gpl_usage(exp->export, basename, exp->name);
  1217. check_for_unused(exp->export, basename, exp->name);
  1218. }
  1219. }
  1220. /**
  1221. * Header for the generated file
  1222. **/
  1223. static void add_header(struct buffer *b, struct module *mod)
  1224. {
  1225. buf_printf(b, "#include <linux/module.h>\n");
  1226. buf_printf(b, "#include <linux/vermagic.h>\n");
  1227. buf_printf(b, "#include <linux/compiler.h>\n");
  1228. buf_printf(b, "\n");
  1229. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1230. buf_printf(b, "\n");
  1231. buf_printf(b, "struct module __this_module\n");
  1232. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1233. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1234. if (mod->has_init)
  1235. buf_printf(b, " .init = init_module,\n");
  1236. if (mod->has_cleanup)
  1237. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1238. " .exit = cleanup_module,\n"
  1239. "#endif\n");
  1240. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1241. buf_printf(b, "};\n");
  1242. }
  1243. /**
  1244. * Record CRCs for unresolved symbols
  1245. **/
  1246. static int add_versions(struct buffer *b, struct module *mod)
  1247. {
  1248. struct symbol *s, *exp;
  1249. int err = 0;
  1250. for (s = mod->unres; s; s = s->next) {
  1251. exp = find_symbol(s->name);
  1252. if (!exp || exp->module == mod) {
  1253. if (have_vmlinux && !s->weak) {
  1254. if (warn_unresolved) {
  1255. warn("\"%s\" [%s.ko] undefined!\n",
  1256. s->name, mod->name);
  1257. } else {
  1258. merror("\"%s\" [%s.ko] undefined!\n",
  1259. s->name, mod->name);
  1260. err = 1;
  1261. }
  1262. }
  1263. continue;
  1264. }
  1265. s->module = exp->module;
  1266. s->crc_valid = exp->crc_valid;
  1267. s->crc = exp->crc;
  1268. }
  1269. if (!modversions)
  1270. return err;
  1271. buf_printf(b, "\n");
  1272. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1273. buf_printf(b, "__attribute_used__\n");
  1274. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1275. for (s = mod->unres; s; s = s->next) {
  1276. if (!s->module) {
  1277. continue;
  1278. }
  1279. if (!s->crc_valid) {
  1280. warn("\"%s\" [%s.ko] has no CRC!\n",
  1281. s->name, mod->name);
  1282. continue;
  1283. }
  1284. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1285. }
  1286. buf_printf(b, "};\n");
  1287. return err;
  1288. }
  1289. static void add_depends(struct buffer *b, struct module *mod,
  1290. struct module *modules)
  1291. {
  1292. struct symbol *s;
  1293. struct module *m;
  1294. int first = 1;
  1295. for (m = modules; m; m = m->next) {
  1296. m->seen = is_vmlinux(m->name);
  1297. }
  1298. buf_printf(b, "\n");
  1299. buf_printf(b, "static const char __module_depends[]\n");
  1300. buf_printf(b, "__attribute_used__\n");
  1301. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1302. buf_printf(b, "\"depends=");
  1303. for (s = mod->unres; s; s = s->next) {
  1304. const char *p;
  1305. if (!s->module)
  1306. continue;
  1307. if (s->module->seen)
  1308. continue;
  1309. s->module->seen = 1;
  1310. if ((p = strrchr(s->module->name, '/')) != NULL)
  1311. p++;
  1312. else
  1313. p = s->module->name;
  1314. buf_printf(b, "%s%s", first ? "" : ",", p);
  1315. first = 0;
  1316. }
  1317. buf_printf(b, "\";\n");
  1318. }
  1319. static void add_srcversion(struct buffer *b, struct module *mod)
  1320. {
  1321. if (mod->srcversion[0]) {
  1322. buf_printf(b, "\n");
  1323. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1324. mod->srcversion);
  1325. }
  1326. }
  1327. static void write_if_changed(struct buffer *b, const char *fname)
  1328. {
  1329. char *tmp;
  1330. FILE *file;
  1331. struct stat st;
  1332. file = fopen(fname, "r");
  1333. if (!file)
  1334. goto write;
  1335. if (fstat(fileno(file), &st) < 0)
  1336. goto close_write;
  1337. if (st.st_size != b->pos)
  1338. goto close_write;
  1339. tmp = NOFAIL(malloc(b->pos));
  1340. if (fread(tmp, 1, b->pos, file) != b->pos)
  1341. goto free_write;
  1342. if (memcmp(tmp, b->p, b->pos) != 0)
  1343. goto free_write;
  1344. free(tmp);
  1345. fclose(file);
  1346. return;
  1347. free_write:
  1348. free(tmp);
  1349. close_write:
  1350. fclose(file);
  1351. write:
  1352. file = fopen(fname, "w");
  1353. if (!file) {
  1354. perror(fname);
  1355. exit(1);
  1356. }
  1357. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1358. perror(fname);
  1359. exit(1);
  1360. }
  1361. fclose(file);
  1362. }
  1363. /* parse Module.symvers file. line format:
  1364. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1365. **/
  1366. static void read_dump(const char *fname, unsigned int kernel)
  1367. {
  1368. unsigned long size, pos = 0;
  1369. void *file = grab_file(fname, &size);
  1370. char *line;
  1371. if (!file)
  1372. /* No symbol versions, silently ignore */
  1373. return;
  1374. while ((line = get_next_line(&pos, file, size))) {
  1375. char *symname, *modname, *d, *export, *end;
  1376. unsigned int crc;
  1377. struct module *mod;
  1378. struct symbol *s;
  1379. if (!(symname = strchr(line, '\t')))
  1380. goto fail;
  1381. *symname++ = '\0';
  1382. if (!(modname = strchr(symname, '\t')))
  1383. goto fail;
  1384. *modname++ = '\0';
  1385. if ((export = strchr(modname, '\t')) != NULL)
  1386. *export++ = '\0';
  1387. if (export && ((end = strchr(export, '\t')) != NULL))
  1388. *end = '\0';
  1389. crc = strtoul(line, &d, 16);
  1390. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1391. goto fail;
  1392. if (!(mod = find_module(modname))) {
  1393. if (is_vmlinux(modname)) {
  1394. have_vmlinux = 1;
  1395. }
  1396. mod = new_module(NOFAIL(strdup(modname)));
  1397. mod->skip = 1;
  1398. }
  1399. s = sym_add_exported(symname, mod, export_no(export));
  1400. s->kernel = kernel;
  1401. s->preloaded = 1;
  1402. sym_update_crc(symname, mod, crc, export_no(export));
  1403. }
  1404. return;
  1405. fail:
  1406. fatal("parse error in symbol dump file\n");
  1407. }
  1408. /* For normal builds always dump all symbols.
  1409. * For external modules only dump symbols
  1410. * that are not read from kernel Module.symvers.
  1411. **/
  1412. static int dump_sym(struct symbol *sym)
  1413. {
  1414. if (!external_module)
  1415. return 1;
  1416. if (sym->vmlinux || sym->kernel)
  1417. return 0;
  1418. return 1;
  1419. }
  1420. static void write_dump(const char *fname)
  1421. {
  1422. struct buffer buf = { };
  1423. struct symbol *symbol;
  1424. int n;
  1425. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1426. symbol = symbolhash[n];
  1427. while (symbol) {
  1428. if (dump_sym(symbol))
  1429. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1430. symbol->crc, symbol->name,
  1431. symbol->module->name,
  1432. export_str(symbol->export));
  1433. symbol = symbol->next;
  1434. }
  1435. }
  1436. write_if_changed(&buf, fname);
  1437. }
  1438. int main(int argc, char **argv)
  1439. {
  1440. struct module *mod;
  1441. struct buffer buf = { };
  1442. char fname[SZ];
  1443. char *kernel_read = NULL, *module_read = NULL;
  1444. char *dump_write = NULL;
  1445. int opt;
  1446. int err;
  1447. while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
  1448. switch(opt) {
  1449. case 'i':
  1450. kernel_read = optarg;
  1451. break;
  1452. case 'I':
  1453. module_read = optarg;
  1454. external_module = 1;
  1455. break;
  1456. case 'm':
  1457. modversions = 1;
  1458. break;
  1459. case 'o':
  1460. dump_write = optarg;
  1461. break;
  1462. case 'a':
  1463. all_versions = 1;
  1464. break;
  1465. case 'w':
  1466. warn_unresolved = 1;
  1467. break;
  1468. default:
  1469. exit(1);
  1470. }
  1471. }
  1472. if (kernel_read)
  1473. read_dump(kernel_read, 1);
  1474. if (module_read)
  1475. read_dump(module_read, 0);
  1476. while (optind < argc) {
  1477. read_symbols(argv[optind++]);
  1478. }
  1479. for (mod = modules; mod; mod = mod->next) {
  1480. if (mod->skip)
  1481. continue;
  1482. check_exports(mod);
  1483. }
  1484. err = 0;
  1485. for (mod = modules; mod; mod = mod->next) {
  1486. if (mod->skip)
  1487. continue;
  1488. buf.pos = 0;
  1489. add_header(&buf, mod);
  1490. err |= add_versions(&buf, mod);
  1491. add_depends(&buf, mod, modules);
  1492. add_moddevtable(&buf, mod);
  1493. add_srcversion(&buf, mod);
  1494. sprintf(fname, "%s.mod.c", mod->name);
  1495. write_if_changed(&buf, fname);
  1496. }
  1497. if (dump_write)
  1498. write_dump(dump_write);
  1499. return err;
  1500. }