modpost.c 40 KB

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