modpost.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  525. {
  526. if (sym)
  527. return elf->strtab + sym->st_name;
  528. else
  529. return "";
  530. }
  531. static const char *sec_name(struct elf_info *elf, int shndx)
  532. {
  533. Elf_Shdr *sechdrs = elf->sechdrs;
  534. return (void *)elf->hdr +
  535. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  536. sechdrs[shndx].sh_name;
  537. }
  538. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  539. {
  540. return (void *)elf->hdr +
  541. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  542. sechdr->sh_name;
  543. }
  544. /* if sym is empty or point to a string
  545. * like ".[0-9]+" then return 1.
  546. * This is the optional prefix added by ld to some sections
  547. */
  548. static int number_prefix(const char *sym)
  549. {
  550. if (*sym++ == '\0')
  551. return 1;
  552. if (*sym != '.')
  553. return 0;
  554. do {
  555. char c = *sym++;
  556. if (c < '0' || c > '9')
  557. return 0;
  558. } while (*sym);
  559. return 1;
  560. }
  561. /* The pattern is an array of simple patterns.
  562. * "foo" will match an exact string equal to "foo"
  563. * "*foo" will match a string that ends with "foo"
  564. * "foo*" will match a string that begins with "foo"
  565. * "foo$" will match a string equal to "foo" or "foo.1"
  566. * where the '1' can be any number including several digits.
  567. * The $ syntax is for sections where ld append a dot number
  568. * to make section name unique.
  569. */
  570. int match(const char *sym, const char * const pat[])
  571. {
  572. const char *p;
  573. while (*pat) {
  574. p = *pat++;
  575. const char *endp = p + strlen(p) - 1;
  576. /* "*foo" */
  577. if (*p == '*') {
  578. if (strrcmp(sym, p + 1) == 0)
  579. return 1;
  580. }
  581. /* "foo*" */
  582. else if (*endp == '*') {
  583. if (strncmp(sym, p, strlen(p) - 1) == 0)
  584. return 1;
  585. }
  586. /* "foo$" */
  587. else if (*endp == '$') {
  588. if (strncmp(sym, p, strlen(p) - 1) == 0) {
  589. if (number_prefix(sym + strlen(p) - 1))
  590. return 1;
  591. }
  592. }
  593. /* no wildcards */
  594. else {
  595. if (strcmp(p, sym) == 0)
  596. return 1;
  597. }
  598. }
  599. /* no match */
  600. return 0;
  601. }
  602. /* sections that we do not want to do full section mismatch check on */
  603. static const char *section_white_list[] =
  604. { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
  605. #define ALL_INIT_DATA_SECTIONS \
  606. ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
  607. #define ALL_EXIT_DATA_SECTIONS \
  608. ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
  609. #define ALL_INIT_TEXT_SECTIONS \
  610. ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
  611. #define ALL_EXIT_TEXT_SECTIONS \
  612. ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
  613. #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
  614. #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
  615. #define DATA_SECTIONS ".data$", ".data.rel$"
  616. #define TEXT_SECTIONS ".text$"
  617. #define INIT_SECTIONS ".init.data$", ".init.text$"
  618. #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
  619. #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
  620. #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
  621. #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
  622. #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
  623. #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
  624. #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
  625. /* init data sections */
  626. static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
  627. /* all init sections */
  628. static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
  629. /* All init and exit sections (code + data) */
  630. static const char *init_exit_sections[] =
  631. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  632. /* data section */
  633. static const char *data_sections[] = { DATA_SECTIONS, NULL };
  634. /* sections that may refer to an init/exit section with no warning */
  635. static const char *initref_sections[] =
  636. {
  637. ".text.init.refok*",
  638. ".exit.text.refok*",
  639. ".data.init.refok*",
  640. NULL
  641. };
  642. /* symbols in .data that may refer to init/exit sections */
  643. static const char *symbol_white_list[] =
  644. {
  645. "*driver",
  646. "*_template", /* scsi uses *_template a lot */
  647. "*_timer", /* arm uses ops structures named _timer a lot */
  648. "*_sht", /* scsi also used *_sht to some extent */
  649. "*_ops",
  650. "*_probe",
  651. "*_probe_one",
  652. "*_console",
  653. NULL
  654. };
  655. static const char *head_sections[] = { ".head.text*", NULL };
  656. static const char *linker_symbols[] =
  657. { "__init_begin", "_sinittext", "_einittext", NULL };
  658. struct sectioncheck {
  659. const char *fromsec[20];
  660. const char *tosec[20];
  661. };
  662. const struct sectioncheck sectioncheck[] = {
  663. /* Do not reference init/exit code/data from
  664. * normal code and data
  665. */
  666. {
  667. .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
  668. .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
  669. },
  670. /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
  671. {
  672. .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
  673. .tosec = { INIT_SECTIONS, NULL }
  674. },
  675. /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
  676. {
  677. .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
  678. .tosec = { EXIT_SECTIONS, NULL }
  679. },
  680. /* Do not use exit code/data from init code */
  681. {
  682. .fromsec = { ALL_INIT_SECTIONS, NULL },
  683. .tosec = { ALL_EXIT_SECTIONS, NULL },
  684. },
  685. /* Do not use init code/data from exit code */
  686. {
  687. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  688. .tosec = { ALL_INIT_SECTIONS, NULL }
  689. },
  690. /* Do not export init/exit functions or data */
  691. {
  692. .fromsec = { "__ksymtab*", NULL },
  693. .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
  694. }
  695. };
  696. static int section_mismatch(const char *fromsec, const char *tosec)
  697. {
  698. int i;
  699. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  700. const struct sectioncheck *check = &sectioncheck[0];
  701. for (i = 0; i < elems; i++) {
  702. if (match(fromsec, check->fromsec) &&
  703. match(tosec, check->tosec))
  704. return 1;
  705. check++;
  706. }
  707. return 0;
  708. }
  709. /**
  710. * Whitelist to allow certain references to pass with no warning.
  711. *
  712. * Pattern 0:
  713. * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
  714. * The pattern is identified by:
  715. * fromsec = .text.init.refok* | .data.init.refok*
  716. *
  717. * Pattern 1:
  718. * If a module parameter is declared __initdata and permissions=0
  719. * then this is legal despite the warning generated.
  720. * We cannot see value of permissions here, so just ignore
  721. * this pattern.
  722. * The pattern is identified by:
  723. * tosec = .init.data
  724. * fromsec = .data*
  725. * atsym =__param*
  726. *
  727. * Pattern 2:
  728. * Many drivers utilise a *driver container with references to
  729. * add, remove, probe functions etc.
  730. * These functions may often be marked __init and we do not want to
  731. * warn here.
  732. * the pattern is identified by:
  733. * tosec = init or exit section
  734. * fromsec = data section
  735. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  736. * *probe_one, *_console, *_timer
  737. *
  738. * Pattern 3:
  739. * Whitelist all refereces from .text.head to .init.data
  740. * Whitelist all refereces from .text.head to .init.text
  741. *
  742. * Pattern 4:
  743. * Some symbols belong to init section but still it is ok to reference
  744. * these from non-init sections as these symbols don't have any memory
  745. * allocated for them and symbol address and value are same. So even
  746. * if init section is freed, its ok to reference those symbols.
  747. * For ex. symbols marking the init section boundaries.
  748. * This pattern is identified by
  749. * refsymname = __init_begin, _sinittext, _einittext
  750. *
  751. **/
  752. static int secref_whitelist(const char *fromsec, const char *fromsym,
  753. const char *tosec, const char *tosym)
  754. {
  755. /* Check for pattern 0 */
  756. if (match(fromsec, initref_sections))
  757. return 0;
  758. /* Check for pattern 1 */
  759. if (match(tosec, init_data_sections) &&
  760. match(fromsec, data_sections) &&
  761. (strncmp(fromsym, "__param", strlen("__param")) == 0))
  762. return 0;
  763. /* Check for pattern 2 */
  764. if (match(tosec, init_exit_sections) &&
  765. match(fromsec, data_sections) &&
  766. match(fromsym, symbol_white_list))
  767. return 0;
  768. /* Check for pattern 3 */
  769. if (match(fromsec, head_sections) &&
  770. match(tosec, init_sections))
  771. return 0;
  772. /* Check for pattern 4 */
  773. if (match(tosym, linker_symbols))
  774. return 0;
  775. return 1;
  776. }
  777. /**
  778. * Find symbol based on relocation record info.
  779. * In some cases the symbol supplied is a valid symbol so
  780. * return refsym. If st_name != 0 we assume this is a valid symbol.
  781. * In other cases the symbol needs to be looked up in the symbol table
  782. * based on section and address.
  783. * **/
  784. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  785. Elf_Sym *relsym)
  786. {
  787. Elf_Sym *sym;
  788. Elf_Sym *near = NULL;
  789. Elf64_Sword distance = 20;
  790. Elf64_Sword d;
  791. if (relsym->st_name != 0)
  792. return relsym;
  793. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  794. if (sym->st_shndx != relsym->st_shndx)
  795. continue;
  796. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  797. continue;
  798. if (sym->st_value == addr)
  799. return sym;
  800. /* Find a symbol nearby - addr are maybe negative */
  801. d = sym->st_value - addr;
  802. if (d < 0)
  803. d = addr - sym->st_value;
  804. if (d < distance) {
  805. distance = d;
  806. near = sym;
  807. }
  808. }
  809. /* We need a close match */
  810. if (distance < 20)
  811. return near;
  812. else
  813. return NULL;
  814. }
  815. static inline int is_arm_mapping_symbol(const char *str)
  816. {
  817. return str[0] == '$' && strchr("atd", str[1])
  818. && (str[2] == '\0' || str[2] == '.');
  819. }
  820. /*
  821. * If there's no name there, ignore it; likewise, ignore it if it's
  822. * one of the magic symbols emitted used by current ARM tools.
  823. *
  824. * Otherwise if find_symbols_between() returns those symbols, they'll
  825. * fail the whitelist tests and cause lots of false alarms ... fixable
  826. * only by merging __exit and __init sections into __text, bloating
  827. * the kernel (which is especially evil on embedded platforms).
  828. */
  829. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  830. {
  831. const char *name = elf->strtab + sym->st_name;
  832. if (!name || !strlen(name))
  833. return 0;
  834. return !is_arm_mapping_symbol(name);
  835. }
  836. /*
  837. * Find symbols before or equal addr and after addr - in the section sec.
  838. * If we find two symbols with equal offset prefer one with a valid name.
  839. * The ELF format may have a better way to detect what type of symbol
  840. * it is, but this works for now.
  841. **/
  842. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  843. const char *sec)
  844. {
  845. Elf_Sym *sym;
  846. Elf_Sym *near = NULL;
  847. Elf_Addr distance = ~0;
  848. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  849. const char *symsec;
  850. if (sym->st_shndx >= SHN_LORESERVE)
  851. continue;
  852. symsec = sec_name(elf, sym->st_shndx);
  853. if (strcmp(symsec, sec) != 0)
  854. continue;
  855. if (!is_valid_name(elf, sym))
  856. continue;
  857. if (sym->st_value <= addr) {
  858. if ((addr - sym->st_value) < distance) {
  859. distance = addr - sym->st_value;
  860. near = sym;
  861. } else if ((addr - sym->st_value) == distance) {
  862. near = sym;
  863. }
  864. }
  865. }
  866. return near;
  867. }
  868. /*
  869. * Print a warning about a section mismatch.
  870. * Try to find symbols near it so user can find it.
  871. * Check whitelist before warning - it may be a false positive.
  872. */
  873. static void report_sec_mismatch(const char *modname,
  874. const char *fromsec,
  875. unsigned long long fromaddr,
  876. const char *fromsym,
  877. const char *tosec, const char *tosym)
  878. {
  879. if (strlen(tosym)) {
  880. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  881. "in '%s'\n",
  882. modname, fromsec, fromaddr,
  883. tosec, tosym, fromsym);
  884. } else {
  885. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
  886. modname, fromsec, fromaddr,
  887. tosec, tosym);
  888. }
  889. }
  890. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  891. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  892. {
  893. const char *tosec;
  894. tosec = sec_name(elf, sym->st_shndx);
  895. if (section_mismatch(fromsec, tosec)) {
  896. const char *fromsym;
  897. const char *tosym;
  898. fromsym = sym_name(elf,
  899. find_elf_symbol2(elf, r->r_offset, fromsec));
  900. tosym = sym_name(elf,
  901. find_elf_symbol(elf, r->r_addend, sym));
  902. /* check whitelist - we may ignore it */
  903. if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
  904. report_sec_mismatch(modname, fromsec, r->r_offset,
  905. fromsym, tosec, tosym);
  906. }
  907. }
  908. }
  909. static unsigned int *reloc_location(struct elf_info *elf,
  910. Elf_Shdr *sechdr, Elf_Rela *r)
  911. {
  912. Elf_Shdr *sechdrs = elf->sechdrs;
  913. int section = sechdr->sh_info;
  914. return (void *)elf->hdr + sechdrs[section].sh_offset +
  915. (r->r_offset - sechdrs[section].sh_addr);
  916. }
  917. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  918. {
  919. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  920. unsigned int *location = reloc_location(elf, sechdr, r);
  921. switch (r_typ) {
  922. case R_386_32:
  923. r->r_addend = TO_NATIVE(*location);
  924. break;
  925. case R_386_PC32:
  926. r->r_addend = TO_NATIVE(*location) + 4;
  927. /* For CONFIG_RELOCATABLE=y */
  928. if (elf->hdr->e_type == ET_EXEC)
  929. r->r_addend += r->r_offset;
  930. break;
  931. }
  932. return 0;
  933. }
  934. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  935. {
  936. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  937. switch (r_typ) {
  938. case R_ARM_ABS32:
  939. /* From ARM ABI: (S + A) | T */
  940. r->r_addend = (int)(long)
  941. (elf->symtab_start + ELF_R_SYM(r->r_info));
  942. break;
  943. case R_ARM_PC24:
  944. /* From ARM ABI: ((S + A) | T) - P */
  945. r->r_addend = (int)(long)(elf->hdr +
  946. sechdr->sh_offset +
  947. (r->r_offset - sechdr->sh_addr));
  948. break;
  949. default:
  950. return 1;
  951. }
  952. return 0;
  953. }
  954. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  955. {
  956. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  957. unsigned int *location = reloc_location(elf, sechdr, r);
  958. unsigned int inst;
  959. if (r_typ == R_MIPS_HI16)
  960. return 1; /* skip this */
  961. inst = TO_NATIVE(*location);
  962. switch (r_typ) {
  963. case R_MIPS_LO16:
  964. r->r_addend = inst & 0xffff;
  965. break;
  966. case R_MIPS_26:
  967. r->r_addend = (inst & 0x03ffffff) << 2;
  968. break;
  969. case R_MIPS_32:
  970. r->r_addend = inst;
  971. break;
  972. }
  973. return 0;
  974. }
  975. static void section_rela(const char *modname, struct elf_info *elf,
  976. Elf_Shdr *sechdr)
  977. {
  978. Elf_Sym *sym;
  979. Elf_Rela *rela;
  980. Elf_Rela r;
  981. unsigned int r_sym;
  982. const char *fromsec;
  983. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  984. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  985. fromsec = sech_name(elf, sechdr);
  986. fromsec += strlen(".rela");
  987. /* if from section (name) is know good then skip it */
  988. if (match(fromsec, section_white_list))
  989. return;
  990. for (rela = start; rela < stop; rela++) {
  991. r.r_offset = TO_NATIVE(rela->r_offset);
  992. #if KERNEL_ELFCLASS == ELFCLASS64
  993. if (elf->hdr->e_machine == EM_MIPS) {
  994. unsigned int r_typ;
  995. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  996. r_sym = TO_NATIVE(r_sym);
  997. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  998. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  999. } else {
  1000. r.r_info = TO_NATIVE(rela->r_info);
  1001. r_sym = ELF_R_SYM(r.r_info);
  1002. }
  1003. #else
  1004. r.r_info = TO_NATIVE(rela->r_info);
  1005. r_sym = ELF_R_SYM(r.r_info);
  1006. #endif
  1007. r.r_addend = TO_NATIVE(rela->r_addend);
  1008. sym = elf->symtab_start + r_sym;
  1009. /* Skip special sections */
  1010. if (sym->st_shndx >= SHN_LORESERVE)
  1011. continue;
  1012. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1013. }
  1014. }
  1015. static void section_rel(const char *modname, struct elf_info *elf,
  1016. Elf_Shdr *sechdr)
  1017. {
  1018. Elf_Sym *sym;
  1019. Elf_Rel *rel;
  1020. Elf_Rela r;
  1021. unsigned int r_sym;
  1022. const char *fromsec;
  1023. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1024. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1025. fromsec = sech_name(elf, sechdr);
  1026. fromsec += strlen(".rel");
  1027. /* if from section (name) is know good then skip it */
  1028. if (match(fromsec, section_white_list))
  1029. return;
  1030. for (rel = start; rel < stop; rel++) {
  1031. r.r_offset = TO_NATIVE(rel->r_offset);
  1032. #if KERNEL_ELFCLASS == ELFCLASS64
  1033. if (elf->hdr->e_machine == EM_MIPS) {
  1034. unsigned int r_typ;
  1035. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1036. r_sym = TO_NATIVE(r_sym);
  1037. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1038. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1039. } else {
  1040. r.r_info = TO_NATIVE(rel->r_info);
  1041. r_sym = ELF_R_SYM(r.r_info);
  1042. }
  1043. #else
  1044. r.r_info = TO_NATIVE(rel->r_info);
  1045. r_sym = ELF_R_SYM(r.r_info);
  1046. #endif
  1047. r.r_addend = 0;
  1048. switch (elf->hdr->e_machine) {
  1049. case EM_386:
  1050. if (addend_386_rel(elf, sechdr, &r))
  1051. continue;
  1052. break;
  1053. case EM_ARM:
  1054. if (addend_arm_rel(elf, sechdr, &r))
  1055. continue;
  1056. break;
  1057. case EM_MIPS:
  1058. if (addend_mips_rel(elf, sechdr, &r))
  1059. continue;
  1060. break;
  1061. }
  1062. sym = elf->symtab_start + r_sym;
  1063. /* Skip special sections */
  1064. if (sym->st_shndx >= SHN_LORESERVE)
  1065. continue;
  1066. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1067. }
  1068. }
  1069. /**
  1070. * A module includes a number of sections that are discarded
  1071. * either when loaded or when used as built-in.
  1072. * For loaded modules all functions marked __init and all data
  1073. * marked __initdata will be discarded when the module has been intialized.
  1074. * Likewise for modules used built-in the sections marked __exit
  1075. * are discarded because __exit marked function are supposed to be called
  1076. * only when a moduel is unloaded which never happes for built-in modules.
  1077. * The check_sec_ref() function traverses all relocation records
  1078. * to find all references to a section that reference a section that will
  1079. * be discarded and warns about it.
  1080. **/
  1081. static void check_sec_ref(struct module *mod, const char *modname,
  1082. struct elf_info *elf)
  1083. {
  1084. int i;
  1085. Elf_Shdr *sechdrs = elf->sechdrs;
  1086. /* Walk through all sections */
  1087. for (i = 0; i < elf->hdr->e_shnum; i++) {
  1088. /* We want to process only relocation sections and not .init */
  1089. if (sechdrs[i].sh_type == SHT_RELA)
  1090. section_rela(modname, elf, &elf->sechdrs[i]);
  1091. else if (sechdrs[i].sh_type == SHT_REL)
  1092. section_rel(modname, elf, &elf->sechdrs[i]);
  1093. }
  1094. }
  1095. static void read_symbols(char *modname)
  1096. {
  1097. const char *symname;
  1098. char *version;
  1099. char *license;
  1100. struct module *mod;
  1101. struct elf_info info = { };
  1102. Elf_Sym *sym;
  1103. if (!parse_elf(&info, modname))
  1104. return;
  1105. mod = new_module(modname);
  1106. /* When there's no vmlinux, don't print warnings about
  1107. * unresolved symbols (since there'll be too many ;) */
  1108. if (is_vmlinux(modname)) {
  1109. have_vmlinux = 1;
  1110. mod->skip = 1;
  1111. }
  1112. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1113. while (license) {
  1114. if (license_is_gpl_compatible(license))
  1115. mod->gpl_compatible = 1;
  1116. else {
  1117. mod->gpl_compatible = 0;
  1118. break;
  1119. }
  1120. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1121. "license", license);
  1122. }
  1123. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1124. symname = info.strtab + sym->st_name;
  1125. handle_modversions(mod, &info, sym, symname);
  1126. handle_moddevtable(mod, &info, sym, symname);
  1127. }
  1128. if (!is_vmlinux(modname) ||
  1129. (is_vmlinux(modname) && vmlinux_section_warnings))
  1130. check_sec_ref(mod, modname, &info);
  1131. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1132. if (version)
  1133. maybe_frob_rcs_version(modname, version, info.modinfo,
  1134. version - (char *)info.hdr);
  1135. if (version || (all_versions && !is_vmlinux(modname)))
  1136. get_src_version(modname, mod->srcversion,
  1137. sizeof(mod->srcversion)-1);
  1138. parse_elf_finish(&info);
  1139. /* Our trick to get versioning for struct_module - it's
  1140. * never passed as an argument to an exported function, so
  1141. * the automatic versioning doesn't pick it up, but it's really
  1142. * important anyhow */
  1143. if (modversions)
  1144. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1145. }
  1146. #define SZ 500
  1147. /* We first write the generated file into memory using the
  1148. * following helper, then compare to the file on disk and
  1149. * only update the later if anything changed */
  1150. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1151. const char *fmt, ...)
  1152. {
  1153. char tmp[SZ];
  1154. int len;
  1155. va_list ap;
  1156. va_start(ap, fmt);
  1157. len = vsnprintf(tmp, SZ, fmt, ap);
  1158. buf_write(buf, tmp, len);
  1159. va_end(ap);
  1160. }
  1161. void buf_write(struct buffer *buf, const char *s, int len)
  1162. {
  1163. if (buf->size - buf->pos < len) {
  1164. buf->size += len + SZ;
  1165. buf->p = realloc(buf->p, buf->size);
  1166. }
  1167. strncpy(buf->p + buf->pos, s, len);
  1168. buf->pos += len;
  1169. }
  1170. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1171. {
  1172. const char *e = is_vmlinux(m) ?"":".ko";
  1173. switch (exp) {
  1174. case export_gpl:
  1175. fatal("modpost: GPL-incompatible module %s%s "
  1176. "uses GPL-only symbol '%s'\n", m, e, s);
  1177. break;
  1178. case export_unused_gpl:
  1179. fatal("modpost: GPL-incompatible module %s%s "
  1180. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1181. break;
  1182. case export_gpl_future:
  1183. warn("modpost: GPL-incompatible module %s%s "
  1184. "uses future GPL-only symbol '%s'\n", m, e, s);
  1185. break;
  1186. case export_plain:
  1187. case export_unused:
  1188. case export_unknown:
  1189. /* ignore */
  1190. break;
  1191. }
  1192. }
  1193. static void check_for_unused(enum export exp, const char *m, const char *s)
  1194. {
  1195. const char *e = is_vmlinux(m) ?"":".ko";
  1196. switch (exp) {
  1197. case export_unused:
  1198. case export_unused_gpl:
  1199. warn("modpost: module %s%s "
  1200. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1201. break;
  1202. default:
  1203. /* ignore */
  1204. break;
  1205. }
  1206. }
  1207. static void check_exports(struct module *mod)
  1208. {
  1209. struct symbol *s, *exp;
  1210. for (s = mod->unres; s; s = s->next) {
  1211. const char *basename;
  1212. exp = find_symbol(s->name);
  1213. if (!exp || exp->module == mod)
  1214. continue;
  1215. basename = strrchr(mod->name, '/');
  1216. if (basename)
  1217. basename++;
  1218. else
  1219. basename = mod->name;
  1220. if (!mod->gpl_compatible)
  1221. check_for_gpl_usage(exp->export, basename, exp->name);
  1222. check_for_unused(exp->export, basename, exp->name);
  1223. }
  1224. }
  1225. /**
  1226. * Header for the generated file
  1227. **/
  1228. static void add_header(struct buffer *b, struct module *mod)
  1229. {
  1230. buf_printf(b, "#include <linux/module.h>\n");
  1231. buf_printf(b, "#include <linux/vermagic.h>\n");
  1232. buf_printf(b, "#include <linux/compiler.h>\n");
  1233. buf_printf(b, "\n");
  1234. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1235. buf_printf(b, "\n");
  1236. buf_printf(b, "struct module __this_module\n");
  1237. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1238. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1239. if (mod->has_init)
  1240. buf_printf(b, " .init = init_module,\n");
  1241. if (mod->has_cleanup)
  1242. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1243. " .exit = cleanup_module,\n"
  1244. "#endif\n");
  1245. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1246. buf_printf(b, "};\n");
  1247. }
  1248. /**
  1249. * Record CRCs for unresolved symbols
  1250. **/
  1251. static int add_versions(struct buffer *b, struct module *mod)
  1252. {
  1253. struct symbol *s, *exp;
  1254. int err = 0;
  1255. for (s = mod->unres; s; s = s->next) {
  1256. exp = find_symbol(s->name);
  1257. if (!exp || exp->module == mod) {
  1258. if (have_vmlinux && !s->weak) {
  1259. if (warn_unresolved) {
  1260. warn("\"%s\" [%s.ko] undefined!\n",
  1261. s->name, mod->name);
  1262. } else {
  1263. merror("\"%s\" [%s.ko] undefined!\n",
  1264. s->name, mod->name);
  1265. err = 1;
  1266. }
  1267. }
  1268. continue;
  1269. }
  1270. s->module = exp->module;
  1271. s->crc_valid = exp->crc_valid;
  1272. s->crc = exp->crc;
  1273. }
  1274. if (!modversions)
  1275. return err;
  1276. buf_printf(b, "\n");
  1277. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1278. buf_printf(b, "__used\n");
  1279. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1280. for (s = mod->unres; s; s = s->next) {
  1281. if (!s->module)
  1282. continue;
  1283. if (!s->crc_valid) {
  1284. warn("\"%s\" [%s.ko] has no CRC!\n",
  1285. s->name, mod->name);
  1286. continue;
  1287. }
  1288. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1289. }
  1290. buf_printf(b, "};\n");
  1291. return err;
  1292. }
  1293. static void add_depends(struct buffer *b, struct module *mod,
  1294. struct module *modules)
  1295. {
  1296. struct symbol *s;
  1297. struct module *m;
  1298. int first = 1;
  1299. for (m = modules; m; m = m->next)
  1300. m->seen = is_vmlinux(m->name);
  1301. buf_printf(b, "\n");
  1302. buf_printf(b, "static const char __module_depends[]\n");
  1303. buf_printf(b, "__used\n");
  1304. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1305. buf_printf(b, "\"depends=");
  1306. for (s = mod->unres; s; s = s->next) {
  1307. const char *p;
  1308. if (!s->module)
  1309. continue;
  1310. if (s->module->seen)
  1311. continue;
  1312. s->module->seen = 1;
  1313. p = strrchr(s->module->name, '/');
  1314. if (p)
  1315. p++;
  1316. else
  1317. p = s->module->name;
  1318. buf_printf(b, "%s%s", first ? "" : ",", p);
  1319. first = 0;
  1320. }
  1321. buf_printf(b, "\";\n");
  1322. }
  1323. static void add_srcversion(struct buffer *b, struct module *mod)
  1324. {
  1325. if (mod->srcversion[0]) {
  1326. buf_printf(b, "\n");
  1327. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1328. mod->srcversion);
  1329. }
  1330. }
  1331. static void write_if_changed(struct buffer *b, const char *fname)
  1332. {
  1333. char *tmp;
  1334. FILE *file;
  1335. struct stat st;
  1336. file = fopen(fname, "r");
  1337. if (!file)
  1338. goto write;
  1339. if (fstat(fileno(file), &st) < 0)
  1340. goto close_write;
  1341. if (st.st_size != b->pos)
  1342. goto close_write;
  1343. tmp = NOFAIL(malloc(b->pos));
  1344. if (fread(tmp, 1, b->pos, file) != b->pos)
  1345. goto free_write;
  1346. if (memcmp(tmp, b->p, b->pos) != 0)
  1347. goto free_write;
  1348. free(tmp);
  1349. fclose(file);
  1350. return;
  1351. free_write:
  1352. free(tmp);
  1353. close_write:
  1354. fclose(file);
  1355. write:
  1356. file = fopen(fname, "w");
  1357. if (!file) {
  1358. perror(fname);
  1359. exit(1);
  1360. }
  1361. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1362. perror(fname);
  1363. exit(1);
  1364. }
  1365. fclose(file);
  1366. }
  1367. /* parse Module.symvers file. line format:
  1368. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1369. **/
  1370. static void read_dump(const char *fname, unsigned int kernel)
  1371. {
  1372. unsigned long size, pos = 0;
  1373. void *file = grab_file(fname, &size);
  1374. char *line;
  1375. if (!file)
  1376. /* No symbol versions, silently ignore */
  1377. return;
  1378. while ((line = get_next_line(&pos, file, size))) {
  1379. char *symname, *modname, *d, *export, *end;
  1380. unsigned int crc;
  1381. struct module *mod;
  1382. struct symbol *s;
  1383. if (!(symname = strchr(line, '\t')))
  1384. goto fail;
  1385. *symname++ = '\0';
  1386. if (!(modname = strchr(symname, '\t')))
  1387. goto fail;
  1388. *modname++ = '\0';
  1389. if ((export = strchr(modname, '\t')) != NULL)
  1390. *export++ = '\0';
  1391. if (export && ((end = strchr(export, '\t')) != NULL))
  1392. *end = '\0';
  1393. crc = strtoul(line, &d, 16);
  1394. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1395. goto fail;
  1396. mod = find_module(modname);
  1397. if (!mod) {
  1398. if (is_vmlinux(modname))
  1399. have_vmlinux = 1;
  1400. mod = new_module(NOFAIL(strdup(modname)));
  1401. mod->skip = 1;
  1402. }
  1403. s = sym_add_exported(symname, mod, export_no(export));
  1404. s->kernel = kernel;
  1405. s->preloaded = 1;
  1406. sym_update_crc(symname, mod, crc, export_no(export));
  1407. }
  1408. return;
  1409. fail:
  1410. fatal("parse error in symbol dump file\n");
  1411. }
  1412. /* For normal builds always dump all symbols.
  1413. * For external modules only dump symbols
  1414. * that are not read from kernel Module.symvers.
  1415. **/
  1416. static int dump_sym(struct symbol *sym)
  1417. {
  1418. if (!external_module)
  1419. return 1;
  1420. if (sym->vmlinux || sym->kernel)
  1421. return 0;
  1422. return 1;
  1423. }
  1424. static void write_dump(const char *fname)
  1425. {
  1426. struct buffer buf = { };
  1427. struct symbol *symbol;
  1428. int n;
  1429. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1430. symbol = symbolhash[n];
  1431. while (symbol) {
  1432. if (dump_sym(symbol))
  1433. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1434. symbol->crc, symbol->name,
  1435. symbol->module->name,
  1436. export_str(symbol->export));
  1437. symbol = symbol->next;
  1438. }
  1439. }
  1440. write_if_changed(&buf, fname);
  1441. }
  1442. int main(int argc, char **argv)
  1443. {
  1444. struct module *mod;
  1445. struct buffer buf = { };
  1446. char *kernel_read = NULL, *module_read = NULL;
  1447. char *dump_write = NULL;
  1448. int opt;
  1449. int err;
  1450. while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
  1451. switch (opt) {
  1452. case 'i':
  1453. kernel_read = optarg;
  1454. break;
  1455. case 'I':
  1456. module_read = optarg;
  1457. external_module = 1;
  1458. break;
  1459. case 'm':
  1460. modversions = 1;
  1461. break;
  1462. case 'o':
  1463. dump_write = optarg;
  1464. break;
  1465. case 'a':
  1466. all_versions = 1;
  1467. break;
  1468. case 's':
  1469. vmlinux_section_warnings = 0;
  1470. break;
  1471. case 'w':
  1472. warn_unresolved = 1;
  1473. break;
  1474. default:
  1475. exit(1);
  1476. }
  1477. }
  1478. if (kernel_read)
  1479. read_dump(kernel_read, 1);
  1480. if (module_read)
  1481. read_dump(module_read, 0);
  1482. while (optind < argc)
  1483. read_symbols(argv[optind++]);
  1484. for (mod = modules; mod; mod = mod->next) {
  1485. if (mod->skip)
  1486. continue;
  1487. check_exports(mod);
  1488. }
  1489. err = 0;
  1490. for (mod = modules; mod; mod = mod->next) {
  1491. char fname[strlen(mod->name) + 10];
  1492. if (mod->skip)
  1493. continue;
  1494. buf.pos = 0;
  1495. add_header(&buf, mod);
  1496. err |= add_versions(&buf, mod);
  1497. add_depends(&buf, mod, modules);
  1498. add_moddevtable(&buf, mod);
  1499. add_srcversion(&buf, mod);
  1500. sprintf(fname, "%s.mod.c", mod->name);
  1501. write_if_changed(&buf, fname);
  1502. }
  1503. if (dump_write)
  1504. write_dump(dump_write);
  1505. return err;
  1506. }