modpost.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. *
  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. /* Are we using CONFIG_MODVERSIONS? */
  16. int modversions = 0;
  17. /* Warn about undefined symbols? (do so if we have vmlinux) */
  18. int have_vmlinux = 0;
  19. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  20. static int all_versions = 0;
  21. /* If we are modposting external module set to 1 */
  22. static int external_module = 0;
  23. void fatal(const char *fmt, ...)
  24. {
  25. va_list arglist;
  26. fprintf(stderr, "FATAL: ");
  27. va_start(arglist, fmt);
  28. vfprintf(stderr, fmt, arglist);
  29. va_end(arglist);
  30. exit(1);
  31. }
  32. void warn(const char *fmt, ...)
  33. {
  34. va_list arglist;
  35. fprintf(stderr, "WARNING: ");
  36. va_start(arglist, fmt);
  37. vfprintf(stderr, fmt, arglist);
  38. va_end(arglist);
  39. }
  40. static int is_vmlinux(const char *modname)
  41. {
  42. const char *myname;
  43. if ((myname = strrchr(modname, '/')))
  44. myname++;
  45. else
  46. myname = modname;
  47. return strcmp(myname, "vmlinux") == 0;
  48. }
  49. void *do_nofail(void *ptr, const char *expr)
  50. {
  51. if (!ptr) {
  52. fatal("modpost: Memory allocation failure: %s.\n", expr);
  53. }
  54. return ptr;
  55. }
  56. /* A list of all modules we processed */
  57. static struct module *modules;
  58. static struct module *find_module(char *modname)
  59. {
  60. struct module *mod;
  61. for (mod = modules; mod; mod = mod->next)
  62. if (strcmp(mod->name, modname) == 0)
  63. break;
  64. return mod;
  65. }
  66. static struct module *new_module(char *modname)
  67. {
  68. struct module *mod;
  69. char *p, *s;
  70. mod = NOFAIL(malloc(sizeof(*mod)));
  71. memset(mod, 0, sizeof(*mod));
  72. p = NOFAIL(strdup(modname));
  73. /* strip trailing .o */
  74. if ((s = strrchr(p, '.')) != NULL)
  75. if (strcmp(s, ".o") == 0)
  76. *s = '\0';
  77. /* add to list */
  78. mod->name = p;
  79. mod->next = modules;
  80. modules = mod;
  81. return mod;
  82. }
  83. /* A hash of all exported symbols,
  84. * struct symbol is also used for lists of unresolved symbols */
  85. #define SYMBOL_HASH_SIZE 1024
  86. struct symbol {
  87. struct symbol *next;
  88. struct module *module;
  89. unsigned int crc;
  90. int crc_valid;
  91. unsigned int weak:1;
  92. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  93. unsigned int kernel:1; /* 1 if symbol is from kernel
  94. * (only for external modules) **/
  95. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  96. char name[0];
  97. };
  98. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  99. /* This is based on the hash agorithm from gdbm, via tdb */
  100. static inline unsigned int tdb_hash(const char *name)
  101. {
  102. unsigned value; /* Used to compute the hash value. */
  103. unsigned i; /* Used to cycle through random values. */
  104. /* Set the initial value from the key size. */
  105. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  106. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  107. return (1103515243 * value + 12345);
  108. }
  109. /**
  110. * Allocate a new symbols for use in the hash of exported symbols or
  111. * the list of unresolved symbols per module
  112. **/
  113. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  114. struct symbol *next)
  115. {
  116. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  117. memset(s, 0, sizeof(*s));
  118. strcpy(s->name, name);
  119. s->weak = weak;
  120. s->next = next;
  121. return s;
  122. }
  123. /* For the hash of exported symbols */
  124. static struct symbol *new_symbol(const char *name, struct module *module)
  125. {
  126. unsigned int hash;
  127. struct symbol *new;
  128. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  129. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  130. new->module = module;
  131. return new;
  132. }
  133. static struct symbol *find_symbol(const char *name)
  134. {
  135. struct symbol *s;
  136. /* For our purposes, .foo matches foo. PPC64 needs this. */
  137. if (name[0] == '.')
  138. name++;
  139. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  140. if (strcmp(s->name, name) == 0)
  141. return s;
  142. }
  143. return NULL;
  144. }
  145. /**
  146. * Add an exported symbol - it may have already been added without a
  147. * CRC, in this case just update the CRC
  148. **/
  149. static struct symbol *sym_add_exported(const char *name, struct module *mod)
  150. {
  151. struct symbol *s = find_symbol(name);
  152. if (!s) {
  153. s = new_symbol(name, mod);
  154. } else {
  155. if (!s->preloaded) {
  156. warn("%s: duplicate symbol '%s' previous definition "
  157. "was in %s%s\n", mod->name, name,
  158. s->module->name,
  159. is_vmlinux(s->module->name) ?"":".ko");
  160. }
  161. }
  162. s->preloaded = 0;
  163. s->vmlinux = is_vmlinux(mod->name);
  164. s->kernel = 0;
  165. return s;
  166. }
  167. static void sym_update_crc(const char *name, struct module *mod,
  168. unsigned int crc)
  169. {
  170. struct symbol *s = find_symbol(name);
  171. if (!s)
  172. s = new_symbol(name, mod);
  173. s->crc = crc;
  174. s->crc_valid = 1;
  175. }
  176. void *grab_file(const char *filename, unsigned long *size)
  177. {
  178. struct stat st;
  179. void *map;
  180. int fd;
  181. fd = open(filename, O_RDONLY);
  182. if (fd < 0 || fstat(fd, &st) != 0)
  183. return NULL;
  184. *size = st.st_size;
  185. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  186. close(fd);
  187. if (map == MAP_FAILED)
  188. return NULL;
  189. return map;
  190. }
  191. /**
  192. * Return a copy of the next line in a mmap'ed file.
  193. * spaces in the beginning of the line is trimmed away.
  194. * Return a pointer to a static buffer.
  195. **/
  196. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  197. {
  198. static char line[4096];
  199. int skip = 1;
  200. size_t len = 0;
  201. signed char *p = (signed char *)file + *pos;
  202. char *s = line;
  203. for (; *pos < size ; (*pos)++)
  204. {
  205. if (skip && isspace(*p)) {
  206. p++;
  207. continue;
  208. }
  209. skip = 0;
  210. if (*p != '\n' && (*pos < size)) {
  211. len++;
  212. *s++ = *p++;
  213. if (len > 4095)
  214. break; /* Too long, stop */
  215. } else {
  216. /* End of string */
  217. *s = '\0';
  218. return line;
  219. }
  220. }
  221. /* End of buffer */
  222. return NULL;
  223. }
  224. void release_file(void *file, unsigned long size)
  225. {
  226. munmap(file, size);
  227. }
  228. static void parse_elf(struct elf_info *info, const char *filename)
  229. {
  230. unsigned int i;
  231. Elf_Ehdr *hdr = info->hdr;
  232. Elf_Shdr *sechdrs;
  233. Elf_Sym *sym;
  234. hdr = grab_file(filename, &info->size);
  235. if (!hdr) {
  236. perror(filename);
  237. abort();
  238. }
  239. info->hdr = hdr;
  240. if (info->size < sizeof(*hdr))
  241. goto truncated;
  242. /* Fix endianness in ELF header */
  243. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  244. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  245. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  246. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  247. sechdrs = (void *)hdr + hdr->e_shoff;
  248. info->sechdrs = sechdrs;
  249. /* Fix endianness in section headers */
  250. for (i = 0; i < hdr->e_shnum; i++) {
  251. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  252. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  253. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  254. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  255. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  256. }
  257. /* Find symbol table. */
  258. for (i = 1; i < hdr->e_shnum; i++) {
  259. const char *secstrings
  260. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  261. if (sechdrs[i].sh_offset > info->size)
  262. goto truncated;
  263. if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
  264. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  265. info->modinfo_len = sechdrs[i].sh_size;
  266. }
  267. if (sechdrs[i].sh_type != SHT_SYMTAB)
  268. continue;
  269. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  270. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  271. + sechdrs[i].sh_size;
  272. info->strtab = (void *)hdr +
  273. sechdrs[sechdrs[i].sh_link].sh_offset;
  274. }
  275. if (!info->symtab_start) {
  276. fatal("%s has no symtab?\n", filename);
  277. }
  278. /* Fix endianness in symbols */
  279. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  280. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  281. sym->st_name = TO_NATIVE(sym->st_name);
  282. sym->st_value = TO_NATIVE(sym->st_value);
  283. sym->st_size = TO_NATIVE(sym->st_size);
  284. }
  285. return;
  286. truncated:
  287. fatal("%s is truncated.\n", filename);
  288. }
  289. static void parse_elf_finish(struct elf_info *info)
  290. {
  291. release_file(info->hdr, info->size);
  292. }
  293. #define CRC_PFX "__crc_"
  294. #define KSYMTAB_PFX "__ksymtab_"
  295. static void handle_modversions(struct module *mod, struct elf_info *info,
  296. Elf_Sym *sym, const char *symname)
  297. {
  298. unsigned int crc;
  299. switch (sym->st_shndx) {
  300. case SHN_COMMON:
  301. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  302. break;
  303. case SHN_ABS:
  304. /* CRC'd symbol */
  305. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  306. crc = (unsigned int) sym->st_value;
  307. sym_update_crc(symname + strlen(CRC_PFX), mod, crc);
  308. }
  309. break;
  310. case SHN_UNDEF:
  311. /* undefined symbol */
  312. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  313. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  314. break;
  315. /* ignore global offset table */
  316. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  317. break;
  318. /* ignore __this_module, it will be resolved shortly */
  319. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  320. break;
  321. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  322. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  323. /* add compatibility with older glibc */
  324. #ifndef STT_SPARC_REGISTER
  325. #define STT_SPARC_REGISTER STT_REGISTER
  326. #endif
  327. if (info->hdr->e_machine == EM_SPARC ||
  328. info->hdr->e_machine == EM_SPARCV9) {
  329. /* Ignore register directives. */
  330. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  331. break;
  332. if (symname[0] == '.') {
  333. char *munged = strdup(symname);
  334. munged[0] = '_';
  335. munged[1] = toupper(munged[1]);
  336. symname = munged;
  337. }
  338. }
  339. #endif
  340. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  341. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  342. mod->unres = alloc_symbol(symname +
  343. strlen(MODULE_SYMBOL_PREFIX),
  344. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  345. mod->unres);
  346. break;
  347. default:
  348. /* All exported symbols */
  349. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  350. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
  351. }
  352. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  353. mod->has_init = 1;
  354. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  355. mod->has_cleanup = 1;
  356. break;
  357. }
  358. }
  359. /**
  360. * Parse tag=value strings from .modinfo section
  361. **/
  362. static char *next_string(char *string, unsigned long *secsize)
  363. {
  364. /* Skip non-zero chars */
  365. while (string[0]) {
  366. string++;
  367. if ((*secsize)-- <= 1)
  368. return NULL;
  369. }
  370. /* Skip any zero padding. */
  371. while (!string[0]) {
  372. string++;
  373. if ((*secsize)-- <= 1)
  374. return NULL;
  375. }
  376. return string;
  377. }
  378. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  379. const char *tag)
  380. {
  381. char *p;
  382. unsigned int taglen = strlen(tag);
  383. unsigned long size = modinfo_len;
  384. for (p = modinfo; p; p = next_string(p, &size)) {
  385. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  386. return p + taglen + 1;
  387. }
  388. return NULL;
  389. }
  390. /**
  391. * Find symbol based on relocation record info.
  392. * In some cases the symbol supplied is a valid symbol so
  393. * return refsym. If st_name != 0 we assume this is a valid symbol.
  394. * In other cases the symbol needs to be looked up in the symbol table
  395. * based on section and address.
  396. * **/
  397. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  398. Elf_Sym *relsym)
  399. {
  400. Elf_Sym *sym;
  401. if (relsym->st_name != 0)
  402. return relsym;
  403. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  404. if (sym->st_shndx != relsym->st_shndx)
  405. continue;
  406. if (sym->st_value == addr)
  407. return sym;
  408. }
  409. return NULL;
  410. }
  411. /*
  412. * Find symbols before or equal addr and after addr - in the section sec
  413. **/
  414. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  415. const char *sec,
  416. Elf_Sym **before, Elf_Sym **after)
  417. {
  418. Elf_Sym *sym;
  419. Elf_Ehdr *hdr = elf->hdr;
  420. Elf_Addr beforediff = ~0;
  421. Elf_Addr afterdiff = ~0;
  422. const char *secstrings = (void *)hdr +
  423. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  424. *before = NULL;
  425. *after = NULL;
  426. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  427. const char *symsec;
  428. if (sym->st_shndx >= SHN_LORESERVE)
  429. continue;
  430. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  431. if (strcmp(symsec, sec) != 0)
  432. continue;
  433. if (sym->st_value <= addr) {
  434. if ((addr - sym->st_value) < beforediff) {
  435. beforediff = addr - sym->st_value;
  436. *before = sym;
  437. }
  438. }
  439. else
  440. {
  441. if ((sym->st_value - addr) < afterdiff) {
  442. afterdiff = sym->st_value - addr;
  443. *after = sym;
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * Print a warning about a section mismatch.
  450. * Try to find symbols near it so user can find it.
  451. **/
  452. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  453. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  454. {
  455. const char *refsymname = "";
  456. Elf_Sym *before, *after;
  457. Elf_Sym *refsym;
  458. Elf_Ehdr *hdr = elf->hdr;
  459. Elf_Shdr *sechdrs = elf->sechdrs;
  460. const char *secstrings = (void *)hdr +
  461. sechdrs[hdr->e_shstrndx].sh_offset;
  462. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  463. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  464. refsym = find_elf_symbol(elf, r.r_addend, sym);
  465. if (refsym && strlen(elf->strtab + refsym->st_name))
  466. refsymname = elf->strtab + refsym->st_name;
  467. if (before && after) {
  468. warn("%s - Section mismatch: reference to %s:%s from %s "
  469. "between '%s' (at offset 0x%llx) and '%s'\n",
  470. modname, secname, refsymname, fromsec,
  471. elf->strtab + before->st_name,
  472. (long long)r.r_offset,
  473. elf->strtab + after->st_name);
  474. } else if (before) {
  475. warn("%s - Section mismatch: reference to %s:%s from %s "
  476. "after '%s' (at offset 0x%llx)\n",
  477. modname, secname, refsymname, fromsec,
  478. elf->strtab + before->st_name,
  479. (long long)r.r_offset);
  480. } else if (after) {
  481. warn("%s - Section mismatch: reference to %s:%s from %s "
  482. "before '%s' (at offset -0x%llx)\n",
  483. modname, secname, refsymname, fromsec,
  484. elf->strtab + before->st_name,
  485. (long long)r.r_offset);
  486. } else {
  487. warn("%s - Section mismatch: reference to %s:%s from %s "
  488. "(offset 0x%llx)\n",
  489. modname, secname, fromsec, refsymname,
  490. (long long)r.r_offset);
  491. }
  492. }
  493. /**
  494. * A module includes a number of sections that are discarded
  495. * either when loaded or when used as built-in.
  496. * For loaded modules all functions marked __init and all data
  497. * marked __initdata will be discarded when the module has been intialized.
  498. * Likewise for modules used built-in the sections marked __exit
  499. * are discarded because __exit marked function are supposed to be called
  500. * only when a moduel is unloaded which never happes for built-in modules.
  501. * The check_sec_ref() function traverses all relocation records
  502. * to find all references to a section that reference a section that will
  503. * be discarded and warns about it.
  504. **/
  505. static void check_sec_ref(struct module *mod, const char *modname,
  506. struct elf_info *elf,
  507. int section(const char*),
  508. int section_ref_ok(const char *))
  509. {
  510. int i;
  511. Elf_Sym *sym;
  512. Elf_Ehdr *hdr = elf->hdr;
  513. Elf_Shdr *sechdrs = elf->sechdrs;
  514. const char *secstrings = (void *)hdr +
  515. sechdrs[hdr->e_shstrndx].sh_offset;
  516. /* Walk through all sections */
  517. for (i = 0; i < hdr->e_shnum; i++) {
  518. Elf_Rela *rela;
  519. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  520. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  521. const char *name = secstrings + sechdrs[i].sh_name +
  522. strlen(".rela");
  523. /* We want to process only relocation sections and not .init */
  524. if (section_ref_ok(name) || (sechdrs[i].sh_type != SHT_RELA))
  525. continue;
  526. for (rela = start; rela < stop; rela++) {
  527. Elf_Rela r;
  528. const char *secname;
  529. r.r_offset = TO_NATIVE(rela->r_offset);
  530. r.r_info = TO_NATIVE(rela->r_info);
  531. r.r_addend = TO_NATIVE(rela->r_addend);
  532. sym = elf->symtab_start + ELF_R_SYM(r.r_info);
  533. /* Skip special sections */
  534. if (sym->st_shndx >= SHN_LORESERVE)
  535. continue;
  536. secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  537. if (section(secname))
  538. warn_sec_mismatch(modname, name, elf, sym, r);
  539. }
  540. }
  541. }
  542. /**
  543. * Functions used only during module init is marked __init and is stored in
  544. * a .init.text section. Likewise data is marked __initdata and stored in
  545. * a .init.data section.
  546. * If this section is one of these sections return 1
  547. * See include/linux/init.h for the details
  548. **/
  549. static int init_section(const char *name)
  550. {
  551. if (strcmp(name, ".init") == 0)
  552. return 1;
  553. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  554. return 1;
  555. return 0;
  556. }
  557. /**
  558. * Identify sections from which references to a .init section is OK.
  559. *
  560. * Unfortunately references to read only data that referenced .init
  561. * sections had to be excluded. Almost all of these are false
  562. * positives, they are created by gcc. The downside of excluding rodata
  563. * is that there really are some user references from rodata to
  564. * init code, e.g. drivers/video/vgacon.c:
  565. *
  566. * const struct consw vga_con = {
  567. * con_startup: vgacon_startup,
  568. *
  569. * where vgacon_startup is __init. If you want to wade through the false
  570. * positives, take out the check for rodata.
  571. **/
  572. static int init_section_ref_ok(const char *name)
  573. {
  574. const char **s;
  575. /* Absolute section names */
  576. const char *namelist1[] = {
  577. ".init",
  578. ".stab",
  579. ".rodata",
  580. ".text.lock",
  581. ".pci_fixup_header",
  582. ".pci_fixup_final",
  583. ".pdr",
  584. "__param",
  585. NULL
  586. };
  587. /* Start of section names */
  588. const char *namelist2[] = {
  589. ".init.",
  590. ".altinstructions",
  591. ".eh_frame",
  592. ".debug",
  593. NULL
  594. };
  595. /* part of section name */
  596. const char *namelist3 [] = {
  597. ".unwind", /* sample: IA_64.unwind.init.text */
  598. NULL
  599. };
  600. for (s = namelist1; *s; s++)
  601. if (strcmp(*s, name) == 0)
  602. return 1;
  603. for (s = namelist2; *s; s++)
  604. if (strncmp(*s, name, strlen(*s)) == 0)
  605. return 1;
  606. for (s = namelist3; *s; s++)
  607. if (strstr(*s, name) != NULL)
  608. return 1;
  609. return 0;
  610. }
  611. /*
  612. * Functions used only during module exit is marked __exit and is stored in
  613. * a .exit.text section. Likewise data is marked __exitdata and stored in
  614. * a .exit.data section.
  615. * If this section is one of these sections return 1
  616. * See include/linux/init.h for the details
  617. **/
  618. static int exit_section(const char *name)
  619. {
  620. if (strcmp(name, ".exit.text") == 0)
  621. return 1;
  622. if (strcmp(name, ".exit.data") == 0)
  623. return 1;
  624. return 0;
  625. }
  626. /*
  627. * Identify sections from which references to a .exit section is OK.
  628. *
  629. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  630. * For our future {in}sanity, add a comment that this is the ppc .opd
  631. * section, not the ia64 .opd section.
  632. * ia64 .opd should not point to discarded sections.
  633. **/
  634. static int exit_section_ref_ok(const char *name)
  635. {
  636. const char **s;
  637. /* Absolute section names */
  638. const char *namelist1[] = {
  639. ".exit.text",
  640. ".exit.data",
  641. ".init.text",
  642. ".opd", /* See comment [OPD] */
  643. ".altinstructions",
  644. ".pdr",
  645. ".exitcall.exit",
  646. ".eh_frame",
  647. ".stab",
  648. NULL
  649. };
  650. /* Start of section names */
  651. const char *namelist2[] = {
  652. ".debug",
  653. NULL
  654. };
  655. /* part of section name */
  656. const char *namelist3 [] = {
  657. ".unwind", /* Sample: IA_64.unwind.exit.text */
  658. NULL
  659. };
  660. for (s = namelist1; *s; s++)
  661. if (strcmp(*s, name) == 0)
  662. return 1;
  663. for (s = namelist2; *s; s++)
  664. if (strncmp(*s, name, strlen(*s)) == 0)
  665. return 1;
  666. for (s = namelist3; *s; s++)
  667. if (strstr(*s, name) != NULL)
  668. return 1;
  669. return 0;
  670. }
  671. static void read_symbols(char *modname)
  672. {
  673. const char *symname;
  674. char *version;
  675. struct module *mod;
  676. struct elf_info info = { };
  677. Elf_Sym *sym;
  678. parse_elf(&info, modname);
  679. mod = new_module(modname);
  680. /* When there's no vmlinux, don't print warnings about
  681. * unresolved symbols (since there'll be too many ;) */
  682. if (is_vmlinux(modname)) {
  683. have_vmlinux = 1;
  684. mod->skip = 1;
  685. }
  686. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  687. symname = info.strtab + sym->st_name;
  688. handle_modversions(mod, &info, sym, symname);
  689. handle_moddevtable(mod, &info, sym, symname);
  690. }
  691. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  692. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  693. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  694. if (version)
  695. maybe_frob_rcs_version(modname, version, info.modinfo,
  696. version - (char *)info.hdr);
  697. if (version || (all_versions && !is_vmlinux(modname)))
  698. get_src_version(modname, mod->srcversion,
  699. sizeof(mod->srcversion)-1);
  700. parse_elf_finish(&info);
  701. /* Our trick to get versioning for struct_module - it's
  702. * never passed as an argument to an exported function, so
  703. * the automatic versioning doesn't pick it up, but it's really
  704. * important anyhow */
  705. if (modversions)
  706. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  707. }
  708. #define SZ 500
  709. /* We first write the generated file into memory using the
  710. * following helper, then compare to the file on disk and
  711. * only update the later if anything changed */
  712. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  713. const char *fmt, ...)
  714. {
  715. char tmp[SZ];
  716. int len;
  717. va_list ap;
  718. va_start(ap, fmt);
  719. len = vsnprintf(tmp, SZ, fmt, ap);
  720. if (buf->size - buf->pos < len + 1) {
  721. buf->size += 128;
  722. buf->p = realloc(buf->p, buf->size);
  723. }
  724. strncpy(buf->p + buf->pos, tmp, len + 1);
  725. buf->pos += len;
  726. va_end(ap);
  727. }
  728. void buf_write(struct buffer *buf, const char *s, int len)
  729. {
  730. if (buf->size - buf->pos < len) {
  731. buf->size += len;
  732. buf->p = realloc(buf->p, buf->size);
  733. }
  734. strncpy(buf->p + buf->pos, s, len);
  735. buf->pos += len;
  736. }
  737. /**
  738. * Header for the generated file
  739. **/
  740. static void add_header(struct buffer *b, struct module *mod)
  741. {
  742. buf_printf(b, "#include <linux/module.h>\n");
  743. buf_printf(b, "#include <linux/vermagic.h>\n");
  744. buf_printf(b, "#include <linux/compiler.h>\n");
  745. buf_printf(b, "\n");
  746. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  747. buf_printf(b, "\n");
  748. buf_printf(b, "struct module __this_module\n");
  749. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  750. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  751. if (mod->has_init)
  752. buf_printf(b, " .init = init_module,\n");
  753. if (mod->has_cleanup)
  754. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  755. " .exit = cleanup_module,\n"
  756. "#endif\n");
  757. buf_printf(b, "};\n");
  758. }
  759. /**
  760. * Record CRCs for unresolved symbols
  761. **/
  762. static void add_versions(struct buffer *b, struct module *mod)
  763. {
  764. struct symbol *s, *exp;
  765. for (s = mod->unres; s; s = s->next) {
  766. exp = find_symbol(s->name);
  767. if (!exp || exp->module == mod) {
  768. if (have_vmlinux && !s->weak)
  769. warn("\"%s\" [%s.ko] undefined!\n",
  770. s->name, mod->name);
  771. continue;
  772. }
  773. s->module = exp->module;
  774. s->crc_valid = exp->crc_valid;
  775. s->crc = exp->crc;
  776. }
  777. if (!modversions)
  778. return;
  779. buf_printf(b, "\n");
  780. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  781. buf_printf(b, "__attribute_used__\n");
  782. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  783. for (s = mod->unres; s; s = s->next) {
  784. if (!s->module) {
  785. continue;
  786. }
  787. if (!s->crc_valid) {
  788. warn("\"%s\" [%s.ko] has no CRC!\n",
  789. s->name, mod->name);
  790. continue;
  791. }
  792. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  793. }
  794. buf_printf(b, "};\n");
  795. }
  796. static void add_depends(struct buffer *b, struct module *mod,
  797. struct module *modules)
  798. {
  799. struct symbol *s;
  800. struct module *m;
  801. int first = 1;
  802. for (m = modules; m; m = m->next) {
  803. m->seen = is_vmlinux(m->name);
  804. }
  805. buf_printf(b, "\n");
  806. buf_printf(b, "static const char __module_depends[]\n");
  807. buf_printf(b, "__attribute_used__\n");
  808. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  809. buf_printf(b, "\"depends=");
  810. for (s = mod->unres; s; s = s->next) {
  811. if (!s->module)
  812. continue;
  813. if (s->module->seen)
  814. continue;
  815. s->module->seen = 1;
  816. buf_printf(b, "%s%s", first ? "" : ",",
  817. strrchr(s->module->name, '/') + 1);
  818. first = 0;
  819. }
  820. buf_printf(b, "\";\n");
  821. }
  822. static void add_srcversion(struct buffer *b, struct module *mod)
  823. {
  824. if (mod->srcversion[0]) {
  825. buf_printf(b, "\n");
  826. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  827. mod->srcversion);
  828. }
  829. }
  830. static void write_if_changed(struct buffer *b, const char *fname)
  831. {
  832. char *tmp;
  833. FILE *file;
  834. struct stat st;
  835. file = fopen(fname, "r");
  836. if (!file)
  837. goto write;
  838. if (fstat(fileno(file), &st) < 0)
  839. goto close_write;
  840. if (st.st_size != b->pos)
  841. goto close_write;
  842. tmp = NOFAIL(malloc(b->pos));
  843. if (fread(tmp, 1, b->pos, file) != b->pos)
  844. goto free_write;
  845. if (memcmp(tmp, b->p, b->pos) != 0)
  846. goto free_write;
  847. free(tmp);
  848. fclose(file);
  849. return;
  850. free_write:
  851. free(tmp);
  852. close_write:
  853. fclose(file);
  854. write:
  855. file = fopen(fname, "w");
  856. if (!file) {
  857. perror(fname);
  858. exit(1);
  859. }
  860. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  861. perror(fname);
  862. exit(1);
  863. }
  864. fclose(file);
  865. }
  866. static void read_dump(const char *fname, unsigned int kernel)
  867. {
  868. unsigned long size, pos = 0;
  869. void *file = grab_file(fname, &size);
  870. char *line;
  871. if (!file)
  872. /* No symbol versions, silently ignore */
  873. return;
  874. while ((line = get_next_line(&pos, file, size))) {
  875. char *symname, *modname, *d;
  876. unsigned int crc;
  877. struct module *mod;
  878. struct symbol *s;
  879. if (!(symname = strchr(line, '\t')))
  880. goto fail;
  881. *symname++ = '\0';
  882. if (!(modname = strchr(symname, '\t')))
  883. goto fail;
  884. *modname++ = '\0';
  885. if (strchr(modname, '\t'))
  886. goto fail;
  887. crc = strtoul(line, &d, 16);
  888. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  889. goto fail;
  890. if (!(mod = find_module(modname))) {
  891. if (is_vmlinux(modname)) {
  892. have_vmlinux = 1;
  893. }
  894. mod = new_module(NOFAIL(strdup(modname)));
  895. mod->skip = 1;
  896. }
  897. s = sym_add_exported(symname, mod);
  898. s->kernel = kernel;
  899. s->preloaded = 1;
  900. sym_update_crc(symname, mod, crc);
  901. }
  902. return;
  903. fail:
  904. fatal("parse error in symbol dump file\n");
  905. }
  906. /* For normal builds always dump all symbols.
  907. * For external modules only dump symbols
  908. * that are not read from kernel Module.symvers.
  909. **/
  910. static int dump_sym(struct symbol *sym)
  911. {
  912. if (!external_module)
  913. return 1;
  914. if (sym->vmlinux || sym->kernel)
  915. return 0;
  916. return 1;
  917. }
  918. static void write_dump(const char *fname)
  919. {
  920. struct buffer buf = { };
  921. struct symbol *symbol;
  922. int n;
  923. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  924. symbol = symbolhash[n];
  925. while (symbol) {
  926. if (dump_sym(symbol))
  927. buf_printf(&buf, "0x%08x\t%s\t%s\n",
  928. symbol->crc, symbol->name,
  929. symbol->module->name);
  930. symbol = symbol->next;
  931. }
  932. }
  933. write_if_changed(&buf, fname);
  934. }
  935. int main(int argc, char **argv)
  936. {
  937. struct module *mod;
  938. struct buffer buf = { };
  939. char fname[SZ];
  940. char *kernel_read = NULL, *module_read = NULL;
  941. char *dump_write = NULL;
  942. int opt;
  943. while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
  944. switch(opt) {
  945. case 'i':
  946. kernel_read = optarg;
  947. break;
  948. case 'I':
  949. module_read = optarg;
  950. external_module = 1;
  951. break;
  952. case 'm':
  953. modversions = 1;
  954. break;
  955. case 'o':
  956. dump_write = optarg;
  957. break;
  958. case 'a':
  959. all_versions = 1;
  960. break;
  961. default:
  962. exit(1);
  963. }
  964. }
  965. if (kernel_read)
  966. read_dump(kernel_read, 1);
  967. if (module_read)
  968. read_dump(module_read, 0);
  969. while (optind < argc) {
  970. read_symbols(argv[optind++]);
  971. }
  972. for (mod = modules; mod; mod = mod->next) {
  973. if (mod->skip)
  974. continue;
  975. buf.pos = 0;
  976. add_header(&buf, mod);
  977. add_versions(&buf, mod);
  978. add_depends(&buf, mod, modules);
  979. add_moddevtable(&buf, mod);
  980. add_srcversion(&buf, mod);
  981. sprintf(fname, "%s.mod.c", mod->name);
  982. write_if_changed(&buf, fname);
  983. }
  984. if (dump_write)
  985. write_dump(dump_write);
  986. return 0;
  987. }