modpost.c 36 KB

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