modpost.c 43 KB

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