modpost.c 43 KB

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