modpost.c 43 KB

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