modpost.c 43 KB

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