modpost.c 40 KB

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