modpost.c 43 KB

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