modpost.c 43 KB

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