modpost.c 37 KB

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