modpost.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006-2008 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. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include "modpost.h"
  17. #include "../../include/generated/autoconf.h"
  18. #include "../../include/linux/license.h"
  19. /* Some toolchains use a `_' prefix for all user symbols. */
  20. #ifdef CONFIG_SYMBOL_PREFIX
  21. #define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
  22. #else
  23. #define MODULE_SYMBOL_PREFIX ""
  24. #endif
  25. /* Are we using CONFIG_MODVERSIONS? */
  26. int modversions = 0;
  27. /* Warn about undefined symbols? (do so if we have vmlinux) */
  28. int have_vmlinux = 0;
  29. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  30. static int all_versions = 0;
  31. /* If we are modposting external module set to 1 */
  32. static int external_module = 0;
  33. /* Warn about section mismatch in vmlinux if set to 1 */
  34. static int vmlinux_section_warnings = 1;
  35. /* Only warn about unresolved symbols */
  36. static int warn_unresolved = 0;
  37. /* How a symbol is exported */
  38. static int sec_mismatch_count = 0;
  39. static int sec_mismatch_verbose = 1;
  40. enum export {
  41. export_plain, export_unused, export_gpl,
  42. export_unused_gpl, export_gpl_future, export_unknown
  43. };
  44. #define PRINTF __attribute__ ((format (printf, 1, 2)))
  45. PRINTF void fatal(const char *fmt, ...)
  46. {
  47. va_list arglist;
  48. fprintf(stderr, "FATAL: ");
  49. va_start(arglist, fmt);
  50. vfprintf(stderr, fmt, arglist);
  51. va_end(arglist);
  52. exit(1);
  53. }
  54. PRINTF void warn(const char *fmt, ...)
  55. {
  56. va_list arglist;
  57. fprintf(stderr, "WARNING: ");
  58. va_start(arglist, fmt);
  59. vfprintf(stderr, fmt, arglist);
  60. va_end(arglist);
  61. }
  62. PRINTF void merror(const char *fmt, ...)
  63. {
  64. va_list arglist;
  65. fprintf(stderr, "ERROR: ");
  66. va_start(arglist, fmt);
  67. vfprintf(stderr, fmt, arglist);
  68. va_end(arglist);
  69. }
  70. static int is_vmlinux(const char *modname)
  71. {
  72. const char *myname;
  73. myname = strrchr(modname, '/');
  74. if (myname)
  75. myname++;
  76. else
  77. myname = modname;
  78. return (strcmp(myname, "vmlinux") == 0) ||
  79. (strcmp(myname, "vmlinux.o") == 0);
  80. }
  81. void *do_nofail(void *ptr, const char *expr)
  82. {
  83. if (!ptr)
  84. fatal("modpost: Memory allocation failure: %s.\n", expr);
  85. return ptr;
  86. }
  87. /* A list of all modules we processed */
  88. static struct module *modules;
  89. static struct module *find_module(char *modname)
  90. {
  91. struct module *mod;
  92. for (mod = modules; mod; mod = mod->next)
  93. if (strcmp(mod->name, modname) == 0)
  94. break;
  95. return mod;
  96. }
  97. static struct module *new_module(char *modname)
  98. {
  99. struct module *mod;
  100. char *p, *s;
  101. mod = NOFAIL(malloc(sizeof(*mod)));
  102. memset(mod, 0, sizeof(*mod));
  103. p = NOFAIL(strdup(modname));
  104. /* strip trailing .o */
  105. s = strrchr(p, '.');
  106. if (s != NULL)
  107. if (strcmp(s, ".o") == 0)
  108. *s = '\0';
  109. /* add to list */
  110. mod->name = p;
  111. mod->gpl_compatible = -1;
  112. mod->next = modules;
  113. modules = mod;
  114. return mod;
  115. }
  116. /* A hash of all exported symbols,
  117. * struct symbol is also used for lists of unresolved symbols */
  118. #define SYMBOL_HASH_SIZE 1024
  119. struct symbol {
  120. struct symbol *next;
  121. struct module *module;
  122. unsigned int crc;
  123. int crc_valid;
  124. unsigned int weak:1;
  125. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  126. unsigned int kernel:1; /* 1 if symbol is from kernel
  127. * (only for external modules) **/
  128. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  129. enum export export; /* Type of export */
  130. char name[0];
  131. };
  132. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  133. /* This is based on the hash agorithm from gdbm, via tdb */
  134. static inline unsigned int tdb_hash(const char *name)
  135. {
  136. unsigned value; /* Used to compute the hash value. */
  137. unsigned i; /* Used to cycle through random values. */
  138. /* Set the initial value from the key size. */
  139. for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
  140. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  141. return (1103515243 * value + 12345);
  142. }
  143. /**
  144. * Allocate a new symbols for use in the hash of exported symbols or
  145. * the list of unresolved symbols per module
  146. **/
  147. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  148. struct symbol *next)
  149. {
  150. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  151. memset(s, 0, sizeof(*s));
  152. strcpy(s->name, name);
  153. s->weak = weak;
  154. s->next = next;
  155. return s;
  156. }
  157. /* For the hash of exported symbols */
  158. static struct symbol *new_symbol(const char *name, struct module *module,
  159. enum export export)
  160. {
  161. unsigned int hash;
  162. struct symbol *new;
  163. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  164. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  165. new->module = module;
  166. new->export = export;
  167. return new;
  168. }
  169. static struct symbol *find_symbol(const char *name)
  170. {
  171. struct symbol *s;
  172. /* For our purposes, .foo matches foo. PPC64 needs this. */
  173. if (name[0] == '.')
  174. name++;
  175. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
  176. if (strcmp(s->name, name) == 0)
  177. return s;
  178. }
  179. return NULL;
  180. }
  181. static struct {
  182. const char *str;
  183. enum export export;
  184. } export_list[] = {
  185. { .str = "EXPORT_SYMBOL", .export = export_plain },
  186. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  187. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  188. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  189. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  190. { .str = "(unknown)", .export = export_unknown },
  191. };
  192. static const char *export_str(enum export ex)
  193. {
  194. return export_list[ex].str;
  195. }
  196. static enum export export_no(const char *s)
  197. {
  198. int i;
  199. if (!s)
  200. return export_unknown;
  201. for (i = 0; export_list[i].export != export_unknown; i++) {
  202. if (strcmp(export_list[i].str, s) == 0)
  203. return export_list[i].export;
  204. }
  205. return export_unknown;
  206. }
  207. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  208. {
  209. if (sec == elf->export_sec)
  210. return export_plain;
  211. else if (sec == elf->export_unused_sec)
  212. return export_unused;
  213. else if (sec == elf->export_gpl_sec)
  214. return export_gpl;
  215. else if (sec == elf->export_unused_gpl_sec)
  216. return export_unused_gpl;
  217. else if (sec == elf->export_gpl_future_sec)
  218. return export_gpl_future;
  219. else
  220. return export_unknown;
  221. }
  222. /**
  223. * Add an exported symbol - it may have already been added without a
  224. * CRC, in this case just update the CRC
  225. **/
  226. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  227. enum export export)
  228. {
  229. struct symbol *s = find_symbol(name);
  230. if (!s) {
  231. s = new_symbol(name, mod, export);
  232. } else {
  233. if (!s->preloaded) {
  234. warn("%s: '%s' exported twice. Previous export "
  235. "was in %s%s\n", mod->name, name,
  236. s->module->name,
  237. is_vmlinux(s->module->name) ?"":".ko");
  238. } else {
  239. /* In case Modules.symvers was out of date */
  240. s->module = mod;
  241. }
  242. }
  243. s->preloaded = 0;
  244. s->vmlinux = is_vmlinux(mod->name);
  245. s->kernel = 0;
  246. s->export = export;
  247. return s;
  248. }
  249. static void sym_update_crc(const char *name, struct module *mod,
  250. unsigned int crc, enum export export)
  251. {
  252. struct symbol *s = find_symbol(name);
  253. if (!s)
  254. s = new_symbol(name, mod, export);
  255. s->crc = crc;
  256. s->crc_valid = 1;
  257. }
  258. void *grab_file(const char *filename, unsigned long *size)
  259. {
  260. struct stat st;
  261. void *map;
  262. int fd;
  263. fd = open(filename, O_RDONLY);
  264. if (fd < 0 || fstat(fd, &st) != 0)
  265. return NULL;
  266. *size = st.st_size;
  267. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  268. close(fd);
  269. if (map == MAP_FAILED)
  270. return NULL;
  271. return map;
  272. }
  273. /**
  274. * Return a copy of the next line in a mmap'ed file.
  275. * spaces in the beginning of the line is trimmed away.
  276. * Return a pointer to a static buffer.
  277. **/
  278. char *get_next_line(unsigned long *pos, void *file, unsigned long size)
  279. {
  280. static char line[4096];
  281. int skip = 1;
  282. size_t len = 0;
  283. signed char *p = (signed char *)file + *pos;
  284. char *s = line;
  285. for (; *pos < size ; (*pos)++) {
  286. if (skip && isspace(*p)) {
  287. p++;
  288. continue;
  289. }
  290. skip = 0;
  291. if (*p != '\n' && (*pos < size)) {
  292. len++;
  293. *s++ = *p++;
  294. if (len > 4095)
  295. break; /* Too long, stop */
  296. } else {
  297. /* End of string */
  298. *s = '\0';
  299. return line;
  300. }
  301. }
  302. /* End of buffer */
  303. return NULL;
  304. }
  305. void release_file(void *file, unsigned long size)
  306. {
  307. munmap(file, size);
  308. }
  309. static int parse_elf(struct elf_info *info, const char *filename)
  310. {
  311. unsigned int i;
  312. Elf_Ehdr *hdr;
  313. Elf_Shdr *sechdrs;
  314. Elf_Sym *sym;
  315. hdr = grab_file(filename, &info->size);
  316. if (!hdr) {
  317. perror(filename);
  318. exit(1);
  319. }
  320. info->hdr = hdr;
  321. if (info->size < sizeof(*hdr)) {
  322. /* file too small, assume this is an empty .o file */
  323. return 0;
  324. }
  325. /* Is this a valid ELF file? */
  326. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  327. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  328. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  329. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  330. /* Not an ELF file - silently ignore it */
  331. return 0;
  332. }
  333. /* Fix endianness in ELF header */
  334. hdr->e_type = TO_NATIVE(hdr->e_type);
  335. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  336. hdr->e_version = TO_NATIVE(hdr->e_version);
  337. hdr->e_entry = TO_NATIVE(hdr->e_entry);
  338. hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
  339. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  340. hdr->e_flags = TO_NATIVE(hdr->e_flags);
  341. hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
  342. hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
  343. hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
  344. hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
  345. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  346. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  347. sechdrs = (void *)hdr + hdr->e_shoff;
  348. info->sechdrs = sechdrs;
  349. /* Check if file offset is correct */
  350. if (hdr->e_shoff > info->size) {
  351. fatal("section header offset=%lu in file '%s' is bigger than "
  352. "filesize=%lu\n", (unsigned long)hdr->e_shoff,
  353. filename, info->size);
  354. return 0;
  355. }
  356. /* Fix endianness in section headers */
  357. for (i = 0; i < hdr->e_shnum; i++) {
  358. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  359. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  360. sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
  361. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  362. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  363. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  364. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  365. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  366. sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
  367. sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
  368. }
  369. /* Find symbol table. */
  370. for (i = 1; i < hdr->e_shnum; i++) {
  371. const char *secstrings
  372. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  373. const char *secname;
  374. int nobits = sechdrs[i].sh_type == SHT_NOBITS;
  375. if (!nobits && sechdrs[i].sh_offset > info->size) {
  376. fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
  377. "sizeof(*hrd)=%zu\n", filename,
  378. (unsigned long)sechdrs[i].sh_offset,
  379. sizeof(*hdr));
  380. return 0;
  381. }
  382. secname = secstrings + sechdrs[i].sh_name;
  383. if (strcmp(secname, ".modinfo") == 0) {
  384. if (nobits)
  385. fatal("%s has NOBITS .modinfo\n", filename);
  386. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  387. info->modinfo_len = sechdrs[i].sh_size;
  388. } else if (strcmp(secname, "__ksymtab") == 0)
  389. info->export_sec = i;
  390. else if (strcmp(secname, "__ksymtab_unused") == 0)
  391. info->export_unused_sec = i;
  392. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  393. info->export_gpl_sec = i;
  394. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  395. info->export_unused_gpl_sec = i;
  396. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  397. info->export_gpl_future_sec = i;
  398. if (sechdrs[i].sh_type != SHT_SYMTAB)
  399. continue;
  400. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  401. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  402. + sechdrs[i].sh_size;
  403. info->strtab = (void *)hdr +
  404. sechdrs[sechdrs[i].sh_link].sh_offset;
  405. }
  406. if (!info->symtab_start)
  407. fatal("%s has no symtab?\n", filename);
  408. /* Fix endianness in symbols */
  409. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  410. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  411. sym->st_name = TO_NATIVE(sym->st_name);
  412. sym->st_value = TO_NATIVE(sym->st_value);
  413. sym->st_size = TO_NATIVE(sym->st_size);
  414. }
  415. return 1;
  416. }
  417. static void parse_elf_finish(struct elf_info *info)
  418. {
  419. release_file(info->hdr, info->size);
  420. }
  421. static int ignore_undef_symbol(struct elf_info *info, const char *symname)
  422. {
  423. /* ignore __this_module, it will be resolved shortly */
  424. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  425. return 1;
  426. /* ignore global offset table */
  427. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  428. return 1;
  429. if (info->hdr->e_machine == EM_PPC)
  430. /* Special register function linked on all modules during final link of .ko */
  431. if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
  432. strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
  433. strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
  434. strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
  435. return 1;
  436. if (info->hdr->e_machine == EM_PPC64)
  437. /* Special register function linked on all modules during final link of .ko */
  438. if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
  439. strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0)
  440. return 1;
  441. /* Do not ignore this symbol */
  442. return 0;
  443. }
  444. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  445. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  446. static void handle_modversions(struct module *mod, struct elf_info *info,
  447. Elf_Sym *sym, const char *symname)
  448. {
  449. unsigned int crc;
  450. enum export export = export_from_sec(info, sym->st_shndx);
  451. switch (sym->st_shndx) {
  452. case SHN_COMMON:
  453. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  454. break;
  455. case SHN_ABS:
  456. /* CRC'd symbol */
  457. if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  458. crc = (unsigned int) sym->st_value;
  459. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  460. export);
  461. }
  462. break;
  463. case SHN_UNDEF:
  464. /* undefined symbol */
  465. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  466. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  467. break;
  468. if (ignore_undef_symbol(info, symname))
  469. break;
  470. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  471. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  472. /* add compatibility with older glibc */
  473. #ifndef STT_SPARC_REGISTER
  474. #define STT_SPARC_REGISTER STT_REGISTER
  475. #endif
  476. if (info->hdr->e_machine == EM_SPARC ||
  477. info->hdr->e_machine == EM_SPARCV9) {
  478. /* Ignore register directives. */
  479. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  480. break;
  481. if (symname[0] == '.') {
  482. char *munged = strdup(symname);
  483. munged[0] = '_';
  484. munged[1] = toupper(munged[1]);
  485. symname = munged;
  486. }
  487. }
  488. #endif
  489. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  490. strlen(MODULE_SYMBOL_PREFIX)) == 0) {
  491. mod->unres =
  492. alloc_symbol(symname +
  493. strlen(MODULE_SYMBOL_PREFIX),
  494. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  495. mod->unres);
  496. }
  497. break;
  498. default:
  499. /* All exported symbols */
  500. if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  501. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  502. export);
  503. }
  504. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  505. mod->has_init = 1;
  506. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  507. mod->has_cleanup = 1;
  508. break;
  509. }
  510. }
  511. /**
  512. * Parse tag=value strings from .modinfo section
  513. **/
  514. static char *next_string(char *string, unsigned long *secsize)
  515. {
  516. /* Skip non-zero chars */
  517. while (string[0]) {
  518. string++;
  519. if ((*secsize)-- <= 1)
  520. return NULL;
  521. }
  522. /* Skip any zero padding. */
  523. while (!string[0]) {
  524. string++;
  525. if ((*secsize)-- <= 1)
  526. return NULL;
  527. }
  528. return string;
  529. }
  530. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  531. const char *tag, char *info)
  532. {
  533. char *p;
  534. unsigned int taglen = strlen(tag);
  535. unsigned long size = modinfo_len;
  536. if (info) {
  537. size -= info - (char *)modinfo;
  538. modinfo = next_string(info, &size);
  539. }
  540. for (p = modinfo; p; p = next_string(p, &size)) {
  541. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  542. return p + taglen + 1;
  543. }
  544. return NULL;
  545. }
  546. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  547. const char *tag)
  548. {
  549. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  550. }
  551. /**
  552. * Test if string s ends in string sub
  553. * return 0 if match
  554. **/
  555. static int strrcmp(const char *s, const char *sub)
  556. {
  557. int slen, sublen;
  558. if (!s || !sub)
  559. return 1;
  560. slen = strlen(s);
  561. sublen = strlen(sub);
  562. if ((slen == 0) || (sublen == 0))
  563. return 1;
  564. if (sublen > slen)
  565. return 1;
  566. return memcmp(s + slen - sublen, sub, sublen);
  567. }
  568. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  569. {
  570. if (sym)
  571. return elf->strtab + sym->st_name;
  572. else
  573. return "(unknown)";
  574. }
  575. static const char *sec_name(struct elf_info *elf, int shndx)
  576. {
  577. Elf_Shdr *sechdrs = elf->sechdrs;
  578. return (void *)elf->hdr +
  579. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  580. sechdrs[shndx].sh_name;
  581. }
  582. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  583. {
  584. return (void *)elf->hdr +
  585. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  586. sechdr->sh_name;
  587. }
  588. /* if sym is empty or point to a string
  589. * like ".[0-9]+" then return 1.
  590. * This is the optional prefix added by ld to some sections
  591. */
  592. static int number_prefix(const char *sym)
  593. {
  594. if (*sym++ == '\0')
  595. return 1;
  596. if (*sym != '.')
  597. return 0;
  598. do {
  599. char c = *sym++;
  600. if (c < '0' || c > '9')
  601. return 0;
  602. } while (*sym);
  603. return 1;
  604. }
  605. /* The pattern is an array of simple patterns.
  606. * "foo" will match an exact string equal to "foo"
  607. * "*foo" will match a string that ends with "foo"
  608. * "foo*" will match a string that begins with "foo"
  609. * "foo$" will match a string equal to "foo" or "foo.1"
  610. * where the '1' can be any number including several digits.
  611. * The $ syntax is for sections where ld append a dot number
  612. * to make section name unique.
  613. */
  614. static int match(const char *sym, const char * const pat[])
  615. {
  616. const char *p;
  617. while (*pat) {
  618. p = *pat++;
  619. const char *endp = p + strlen(p) - 1;
  620. /* "*foo" */
  621. if (*p == '*') {
  622. if (strrcmp(sym, p + 1) == 0)
  623. return 1;
  624. }
  625. /* "foo*" */
  626. else if (*endp == '*') {
  627. if (strncmp(sym, p, strlen(p) - 1) == 0)
  628. return 1;
  629. }
  630. /* "foo$" */
  631. else if (*endp == '$') {
  632. if (strncmp(sym, p, strlen(p) - 1) == 0) {
  633. if (number_prefix(sym + strlen(p) - 1))
  634. return 1;
  635. }
  636. }
  637. /* no wildcards */
  638. else {
  639. if (strcmp(p, sym) == 0)
  640. return 1;
  641. }
  642. }
  643. /* no match */
  644. return 0;
  645. }
  646. /* sections that we do not want to do full section mismatch check on */
  647. static const char *section_white_list[] =
  648. {
  649. ".comment*",
  650. ".debug*",
  651. ".mdebug*", /* alpha, score, mips etc. */
  652. ".pdr", /* alpha, score, mips etc. */
  653. ".stab*",
  654. ".note*",
  655. ".got*",
  656. ".toc*",
  657. NULL
  658. };
  659. /*
  660. * This is used to find sections missing the SHF_ALLOC flag.
  661. * The cause of this is often a section specified in assembler
  662. * without "ax" / "aw".
  663. */
  664. static void check_section(const char *modname, struct elf_info *elf,
  665. Elf_Shdr *sechdr)
  666. {
  667. const char *sec = sech_name(elf, sechdr);
  668. if (sechdr->sh_type == SHT_PROGBITS &&
  669. !(sechdr->sh_flags & SHF_ALLOC) &&
  670. !match(sec, section_white_list)) {
  671. warn("%s (%s): unexpected non-allocatable section.\n"
  672. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  673. "Note that for example <linux/init.h> contains\n"
  674. "section definitions for use in .S files.\n\n",
  675. modname, sec);
  676. }
  677. }
  678. #define ALL_INIT_DATA_SECTIONS \
  679. ".init.setup$", ".init.rodata$", \
  680. ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$" \
  681. ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
  682. #define ALL_EXIT_DATA_SECTIONS \
  683. ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
  684. #define ALL_INIT_TEXT_SECTIONS \
  685. ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
  686. #define ALL_EXIT_TEXT_SECTIONS \
  687. ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
  688. #define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
  689. MEM_INIT_SECTIONS
  690. #define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
  691. MEM_EXIT_SECTIONS
  692. #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
  693. #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
  694. #define DATA_SECTIONS ".data$", ".data.rel$"
  695. #define TEXT_SECTIONS ".text$"
  696. #define INIT_SECTIONS ".init.*"
  697. #define DEV_INIT_SECTIONS ".devinit.*"
  698. #define CPU_INIT_SECTIONS ".cpuinit.*"
  699. #define MEM_INIT_SECTIONS ".meminit.*"
  700. #define EXIT_SECTIONS ".exit.*"
  701. #define DEV_EXIT_SECTIONS ".devexit.*"
  702. #define CPU_EXIT_SECTIONS ".cpuexit.*"
  703. #define MEM_EXIT_SECTIONS ".memexit.*"
  704. /* init data sections */
  705. static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
  706. /* all init sections */
  707. static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
  708. /* All init and exit sections (code + data) */
  709. static const char *init_exit_sections[] =
  710. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  711. /* data section */
  712. static const char *data_sections[] = { DATA_SECTIONS, NULL };
  713. /* symbols in .data that may refer to init/exit sections */
  714. #define DEFAULT_SYMBOL_WHITE_LIST \
  715. "*driver", \
  716. "*_template", /* scsi uses *_template a lot */ \
  717. "*_timer", /* arm uses ops structures named _timer a lot */ \
  718. "*_sht", /* scsi also used *_sht to some extent */ \
  719. "*_ops", \
  720. "*_probe", \
  721. "*_probe_one", \
  722. "*_console"
  723. static const char *head_sections[] = { ".head.text*", NULL };
  724. static const char *linker_symbols[] =
  725. { "__init_begin", "_sinittext", "_einittext", NULL };
  726. enum mismatch {
  727. TEXT_TO_ANY_INIT,
  728. DATA_TO_ANY_INIT,
  729. TEXT_TO_ANY_EXIT,
  730. DATA_TO_ANY_EXIT,
  731. XXXINIT_TO_SOME_INIT,
  732. XXXEXIT_TO_SOME_EXIT,
  733. ANY_INIT_TO_ANY_EXIT,
  734. ANY_EXIT_TO_ANY_INIT,
  735. EXPORT_TO_INIT_EXIT,
  736. };
  737. struct sectioncheck {
  738. const char *fromsec[20];
  739. const char *tosec[20];
  740. enum mismatch mismatch;
  741. const char *symbol_white_list[20];
  742. };
  743. const struct sectioncheck sectioncheck[] = {
  744. /* Do not reference init/exit code/data from
  745. * normal code and data
  746. */
  747. {
  748. .fromsec = { TEXT_SECTIONS, NULL },
  749. .tosec = { ALL_INIT_SECTIONS, NULL },
  750. .mismatch = TEXT_TO_ANY_INIT,
  751. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  752. },
  753. {
  754. .fromsec = { DATA_SECTIONS, NULL },
  755. .tosec = { ALL_XXXINIT_SECTIONS, NULL },
  756. .mismatch = DATA_TO_ANY_INIT,
  757. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  758. },
  759. {
  760. .fromsec = { DATA_SECTIONS, NULL },
  761. .tosec = { INIT_SECTIONS, NULL },
  762. .mismatch = DATA_TO_ANY_INIT,
  763. .symbol_white_list = {
  764. "*_template", "*_timer", "*_sht", "*_ops",
  765. "*_probe", "*_probe_one", "*_console", NULL
  766. },
  767. },
  768. {
  769. .fromsec = { TEXT_SECTIONS, NULL },
  770. .tosec = { ALL_EXIT_SECTIONS, NULL },
  771. .mismatch = TEXT_TO_ANY_EXIT,
  772. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  773. },
  774. {
  775. .fromsec = { DATA_SECTIONS, NULL },
  776. .tosec = { ALL_EXIT_SECTIONS, NULL },
  777. .mismatch = DATA_TO_ANY_EXIT,
  778. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  779. },
  780. /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
  781. {
  782. .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
  783. .tosec = { INIT_SECTIONS, NULL },
  784. .mismatch = XXXINIT_TO_SOME_INIT,
  785. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  786. },
  787. /* Do not reference cpuinit code/data from meminit code/data */
  788. {
  789. .fromsec = { MEM_INIT_SECTIONS, NULL },
  790. .tosec = { CPU_INIT_SECTIONS, NULL },
  791. .mismatch = XXXINIT_TO_SOME_INIT,
  792. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  793. },
  794. /* Do not reference meminit code/data from cpuinit code/data */
  795. {
  796. .fromsec = { CPU_INIT_SECTIONS, NULL },
  797. .tosec = { MEM_INIT_SECTIONS, NULL },
  798. .mismatch = XXXINIT_TO_SOME_INIT,
  799. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  800. },
  801. /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
  802. {
  803. .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
  804. .tosec = { EXIT_SECTIONS, NULL },
  805. .mismatch = XXXEXIT_TO_SOME_EXIT,
  806. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  807. },
  808. /* Do not reference cpuexit code/data from memexit code/data */
  809. {
  810. .fromsec = { MEM_EXIT_SECTIONS, NULL },
  811. .tosec = { CPU_EXIT_SECTIONS, NULL },
  812. .mismatch = XXXEXIT_TO_SOME_EXIT,
  813. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  814. },
  815. /* Do not reference memexit code/data from cpuexit code/data */
  816. {
  817. .fromsec = { CPU_EXIT_SECTIONS, NULL },
  818. .tosec = { MEM_EXIT_SECTIONS, NULL },
  819. .mismatch = XXXEXIT_TO_SOME_EXIT,
  820. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  821. },
  822. /* Do not use exit code/data from init code */
  823. {
  824. .fromsec = { ALL_INIT_SECTIONS, NULL },
  825. .tosec = { ALL_EXIT_SECTIONS, NULL },
  826. .mismatch = ANY_INIT_TO_ANY_EXIT,
  827. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  828. },
  829. /* Do not use init code/data from exit code */
  830. {
  831. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  832. .tosec = { ALL_INIT_SECTIONS, NULL },
  833. .mismatch = ANY_EXIT_TO_ANY_INIT,
  834. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  835. },
  836. /* Do not export init/exit functions or data */
  837. {
  838. .fromsec = { "__ksymtab*", NULL },
  839. .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
  840. .mismatch = EXPORT_TO_INIT_EXIT,
  841. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  842. }
  843. };
  844. static const struct sectioncheck *section_mismatch(
  845. const char *fromsec, const char *tosec)
  846. {
  847. int i;
  848. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  849. const struct sectioncheck *check = &sectioncheck[0];
  850. for (i = 0; i < elems; i++) {
  851. if (match(fromsec, check->fromsec) &&
  852. match(tosec, check->tosec))
  853. return check;
  854. check++;
  855. }
  856. return NULL;
  857. }
  858. /**
  859. * Whitelist to allow certain references to pass with no warning.
  860. *
  861. * Pattern 1:
  862. * If a module parameter is declared __initdata and permissions=0
  863. * then this is legal despite the warning generated.
  864. * We cannot see value of permissions here, so just ignore
  865. * this pattern.
  866. * The pattern is identified by:
  867. * tosec = .init.data
  868. * fromsec = .data*
  869. * atsym =__param*
  870. *
  871. * Pattern 2:
  872. * Many drivers utilise a *driver container with references to
  873. * add, remove, probe functions etc.
  874. * These functions may often be marked __devinit and we do not want to
  875. * warn here.
  876. * the pattern is identified by:
  877. * tosec = init or exit section
  878. * fromsec = data section
  879. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  880. * *probe_one, *_console, *_timer
  881. *
  882. * Pattern 3:
  883. * Whitelist all references from .head.text to any init section
  884. *
  885. * Pattern 4:
  886. * Some symbols belong to init section but still it is ok to reference
  887. * these from non-init sections as these symbols don't have any memory
  888. * allocated for them and symbol address and value are same. So even
  889. * if init section is freed, its ok to reference those symbols.
  890. * For ex. symbols marking the init section boundaries.
  891. * This pattern is identified by
  892. * refsymname = __init_begin, _sinittext, _einittext
  893. *
  894. **/
  895. static int secref_whitelist(const struct sectioncheck *mismatch,
  896. const char *fromsec, const char *fromsym,
  897. const char *tosec, const char *tosym)
  898. {
  899. /* Check for pattern 1 */
  900. if (match(tosec, init_data_sections) &&
  901. match(fromsec, data_sections) &&
  902. (strncmp(fromsym, "__param", strlen("__param")) == 0))
  903. return 0;
  904. /* Check for pattern 2 */
  905. if (match(tosec, init_exit_sections) &&
  906. match(fromsec, data_sections) &&
  907. match(fromsym, mismatch->symbol_white_list))
  908. return 0;
  909. /* Check for pattern 3 */
  910. if (match(fromsec, head_sections) &&
  911. match(tosec, init_sections))
  912. return 0;
  913. /* Check for pattern 4 */
  914. if (match(tosym, linker_symbols))
  915. return 0;
  916. return 1;
  917. }
  918. /**
  919. * Find symbol based on relocation record info.
  920. * In some cases the symbol supplied is a valid symbol so
  921. * return refsym. If st_name != 0 we assume this is a valid symbol.
  922. * In other cases the symbol needs to be looked up in the symbol table
  923. * based on section and address.
  924. * **/
  925. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  926. Elf_Sym *relsym)
  927. {
  928. Elf_Sym *sym;
  929. Elf_Sym *near = NULL;
  930. Elf64_Sword distance = 20;
  931. Elf64_Sword d;
  932. if (relsym->st_name != 0)
  933. return relsym;
  934. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  935. if (sym->st_shndx != relsym->st_shndx)
  936. continue;
  937. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  938. continue;
  939. if (sym->st_value == addr)
  940. return sym;
  941. /* Find a symbol nearby - addr are maybe negative */
  942. d = sym->st_value - addr;
  943. if (d < 0)
  944. d = addr - sym->st_value;
  945. if (d < distance) {
  946. distance = d;
  947. near = sym;
  948. }
  949. }
  950. /* We need a close match */
  951. if (distance < 20)
  952. return near;
  953. else
  954. return NULL;
  955. }
  956. static inline int is_arm_mapping_symbol(const char *str)
  957. {
  958. return str[0] == '$' && strchr("atd", str[1])
  959. && (str[2] == '\0' || str[2] == '.');
  960. }
  961. /*
  962. * If there's no name there, ignore it; likewise, ignore it if it's
  963. * one of the magic symbols emitted used by current ARM tools.
  964. *
  965. * Otherwise if find_symbols_between() returns those symbols, they'll
  966. * fail the whitelist tests and cause lots of false alarms ... fixable
  967. * only by merging __exit and __init sections into __text, bloating
  968. * the kernel (which is especially evil on embedded platforms).
  969. */
  970. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  971. {
  972. const char *name = elf->strtab + sym->st_name;
  973. if (!name || !strlen(name))
  974. return 0;
  975. return !is_arm_mapping_symbol(name);
  976. }
  977. /*
  978. * Find symbols before or equal addr and after addr - in the section sec.
  979. * If we find two symbols with equal offset prefer one with a valid name.
  980. * The ELF format may have a better way to detect what type of symbol
  981. * it is, but this works for now.
  982. **/
  983. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  984. const char *sec)
  985. {
  986. Elf_Sym *sym;
  987. Elf_Sym *near = NULL;
  988. Elf_Addr distance = ~0;
  989. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  990. const char *symsec;
  991. if (sym->st_shndx >= SHN_LORESERVE)
  992. continue;
  993. symsec = sec_name(elf, sym->st_shndx);
  994. if (strcmp(symsec, sec) != 0)
  995. continue;
  996. if (!is_valid_name(elf, sym))
  997. continue;
  998. if (sym->st_value <= addr) {
  999. if ((addr - sym->st_value) < distance) {
  1000. distance = addr - sym->st_value;
  1001. near = sym;
  1002. } else if ((addr - sym->st_value) == distance) {
  1003. near = sym;
  1004. }
  1005. }
  1006. }
  1007. return near;
  1008. }
  1009. /*
  1010. * Convert a section name to the function/data attribute
  1011. * .init.text => __init
  1012. * .cpuinit.data => __cpudata
  1013. * .memexitconst => __memconst
  1014. * etc.
  1015. */
  1016. static char *sec2annotation(const char *s)
  1017. {
  1018. if (match(s, init_exit_sections)) {
  1019. char *p = malloc(20);
  1020. char *r = p;
  1021. *p++ = '_';
  1022. *p++ = '_';
  1023. if (*s == '.')
  1024. s++;
  1025. while (*s && *s != '.')
  1026. *p++ = *s++;
  1027. *p = '\0';
  1028. if (*s == '.')
  1029. s++;
  1030. if (strstr(s, "rodata") != NULL)
  1031. strcat(p, "const ");
  1032. else if (strstr(s, "data") != NULL)
  1033. strcat(p, "data ");
  1034. else
  1035. strcat(p, " ");
  1036. return r; /* we leak her but we do not care */
  1037. } else {
  1038. return "";
  1039. }
  1040. }
  1041. static int is_function(Elf_Sym *sym)
  1042. {
  1043. if (sym)
  1044. return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
  1045. else
  1046. return -1;
  1047. }
  1048. /*
  1049. * Print a warning about a section mismatch.
  1050. * Try to find symbols near it so user can find it.
  1051. * Check whitelist before warning - it may be a false positive.
  1052. */
  1053. static void report_sec_mismatch(const char *modname,
  1054. const struct sectioncheck *mismatch,
  1055. const char *fromsec,
  1056. unsigned long long fromaddr,
  1057. const char *fromsym,
  1058. int from_is_func,
  1059. const char *tosec, const char *tosym,
  1060. int to_is_func)
  1061. {
  1062. const char *from, *from_p;
  1063. const char *to, *to_p;
  1064. switch (from_is_func) {
  1065. case 0: from = "variable"; from_p = ""; break;
  1066. case 1: from = "function"; from_p = "()"; break;
  1067. default: from = "(unknown reference)"; from_p = ""; break;
  1068. }
  1069. switch (to_is_func) {
  1070. case 0: to = "variable"; to_p = ""; break;
  1071. case 1: to = "function"; to_p = "()"; break;
  1072. default: to = "(unknown reference)"; to_p = ""; break;
  1073. }
  1074. sec_mismatch_count++;
  1075. if (!sec_mismatch_verbose)
  1076. return;
  1077. warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
  1078. "to the %s %s:%s%s\n",
  1079. modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
  1080. tosym, to_p);
  1081. switch (mismatch->mismatch) {
  1082. case TEXT_TO_ANY_INIT:
  1083. fprintf(stderr,
  1084. "The function %s%s() references\n"
  1085. "the %s %s%s%s.\n"
  1086. "This is often because %s lacks a %s\n"
  1087. "annotation or the annotation of %s is wrong.\n",
  1088. sec2annotation(fromsec), fromsym,
  1089. to, sec2annotation(tosec), tosym, to_p,
  1090. fromsym, sec2annotation(tosec), tosym);
  1091. break;
  1092. case DATA_TO_ANY_INIT: {
  1093. const char *const *s = mismatch->symbol_white_list;
  1094. fprintf(stderr,
  1095. "The variable %s references\n"
  1096. "the %s %s%s%s\n"
  1097. "If the reference is valid then annotate the\n"
  1098. "variable with __init* or __refdata (see linux/init.h) "
  1099. "or name the variable:\n",
  1100. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1101. while (*s)
  1102. fprintf(stderr, "%s, ", *s++);
  1103. fprintf(stderr, "\n");
  1104. break;
  1105. }
  1106. case TEXT_TO_ANY_EXIT:
  1107. fprintf(stderr,
  1108. "The function %s() references a %s in an exit section.\n"
  1109. "Often the %s %s%s has valid usage outside the exit section\n"
  1110. "and the fix is to remove the %sannotation of %s.\n",
  1111. fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
  1112. break;
  1113. case DATA_TO_ANY_EXIT: {
  1114. const char *const *s = mismatch->symbol_white_list;
  1115. fprintf(stderr,
  1116. "The variable %s references\n"
  1117. "the %s %s%s%s\n"
  1118. "If the reference is valid then annotate the\n"
  1119. "variable with __exit* (see linux/init.h) or "
  1120. "name the variable:\n",
  1121. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1122. while (*s)
  1123. fprintf(stderr, "%s, ", *s++);
  1124. fprintf(stderr, "\n");
  1125. break;
  1126. }
  1127. case XXXINIT_TO_SOME_INIT:
  1128. case XXXEXIT_TO_SOME_EXIT:
  1129. fprintf(stderr,
  1130. "The %s %s%s%s references\n"
  1131. "a %s %s%s%s.\n"
  1132. "If %s is only used by %s then\n"
  1133. "annotate %s with a matching annotation.\n",
  1134. from, sec2annotation(fromsec), fromsym, from_p,
  1135. to, sec2annotation(tosec), tosym, to_p,
  1136. tosym, fromsym, tosym);
  1137. break;
  1138. case ANY_INIT_TO_ANY_EXIT:
  1139. fprintf(stderr,
  1140. "The %s %s%s%s references\n"
  1141. "a %s %s%s%s.\n"
  1142. "This is often seen when error handling "
  1143. "in the init function\n"
  1144. "uses functionality in the exit path.\n"
  1145. "The fix is often to remove the %sannotation of\n"
  1146. "%s%s so it may be used outside an exit section.\n",
  1147. from, sec2annotation(fromsec), fromsym, from_p,
  1148. to, sec2annotation(tosec), tosym, to_p,
  1149. sec2annotation(tosec), tosym, to_p);
  1150. break;
  1151. case ANY_EXIT_TO_ANY_INIT:
  1152. fprintf(stderr,
  1153. "The %s %s%s%s references\n"
  1154. "a %s %s%s%s.\n"
  1155. "This is often seen when error handling "
  1156. "in the exit function\n"
  1157. "uses functionality in the init path.\n"
  1158. "The fix is often to remove the %sannotation of\n"
  1159. "%s%s so it may be used outside an init section.\n",
  1160. from, sec2annotation(fromsec), fromsym, from_p,
  1161. to, sec2annotation(tosec), tosym, to_p,
  1162. sec2annotation(tosec), tosym, to_p);
  1163. break;
  1164. case EXPORT_TO_INIT_EXIT:
  1165. fprintf(stderr,
  1166. "The symbol %s is exported and annotated %s\n"
  1167. "Fix this by removing the %sannotation of %s "
  1168. "or drop the export.\n",
  1169. tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
  1170. break;
  1171. }
  1172. fprintf(stderr, "\n");
  1173. }
  1174. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1175. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1176. {
  1177. const char *tosec;
  1178. const struct sectioncheck *mismatch;
  1179. tosec = sec_name(elf, sym->st_shndx);
  1180. mismatch = section_mismatch(fromsec, tosec);
  1181. if (mismatch) {
  1182. Elf_Sym *to;
  1183. Elf_Sym *from;
  1184. const char *tosym;
  1185. const char *fromsym;
  1186. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1187. fromsym = sym_name(elf, from);
  1188. to = find_elf_symbol(elf, r->r_addend, sym);
  1189. tosym = sym_name(elf, to);
  1190. /* check whitelist - we may ignore it */
  1191. if (secref_whitelist(mismatch,
  1192. fromsec, fromsym, tosec, tosym)) {
  1193. report_sec_mismatch(modname, mismatch,
  1194. fromsec, r->r_offset, fromsym,
  1195. is_function(from), tosec, tosym,
  1196. is_function(to));
  1197. }
  1198. }
  1199. }
  1200. static unsigned int *reloc_location(struct elf_info *elf,
  1201. Elf_Shdr *sechdr, Elf_Rela *r)
  1202. {
  1203. Elf_Shdr *sechdrs = elf->sechdrs;
  1204. int section = sechdr->sh_info;
  1205. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1206. r->r_offset - sechdrs[section].sh_addr;
  1207. }
  1208. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1209. {
  1210. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1211. unsigned int *location = reloc_location(elf, sechdr, r);
  1212. switch (r_typ) {
  1213. case R_386_32:
  1214. r->r_addend = TO_NATIVE(*location);
  1215. break;
  1216. case R_386_PC32:
  1217. r->r_addend = TO_NATIVE(*location) + 4;
  1218. /* For CONFIG_RELOCATABLE=y */
  1219. if (elf->hdr->e_type == ET_EXEC)
  1220. r->r_addend += r->r_offset;
  1221. break;
  1222. }
  1223. return 0;
  1224. }
  1225. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1226. {
  1227. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1228. switch (r_typ) {
  1229. case R_ARM_ABS32:
  1230. /* From ARM ABI: (S + A) | T */
  1231. r->r_addend = (int)(long)
  1232. (elf->symtab_start + ELF_R_SYM(r->r_info));
  1233. break;
  1234. case R_ARM_PC24:
  1235. /* From ARM ABI: ((S + A) | T) - P */
  1236. r->r_addend = (int)(long)(elf->hdr +
  1237. sechdr->sh_offset +
  1238. (r->r_offset - sechdr->sh_addr));
  1239. break;
  1240. default:
  1241. return 1;
  1242. }
  1243. return 0;
  1244. }
  1245. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1246. {
  1247. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1248. unsigned int *location = reloc_location(elf, sechdr, r);
  1249. unsigned int inst;
  1250. if (r_typ == R_MIPS_HI16)
  1251. return 1; /* skip this */
  1252. inst = TO_NATIVE(*location);
  1253. switch (r_typ) {
  1254. case R_MIPS_LO16:
  1255. r->r_addend = inst & 0xffff;
  1256. break;
  1257. case R_MIPS_26:
  1258. r->r_addend = (inst & 0x03ffffff) << 2;
  1259. break;
  1260. case R_MIPS_32:
  1261. r->r_addend = inst;
  1262. break;
  1263. }
  1264. return 0;
  1265. }
  1266. static void section_rela(const char *modname, struct elf_info *elf,
  1267. Elf_Shdr *sechdr)
  1268. {
  1269. Elf_Sym *sym;
  1270. Elf_Rela *rela;
  1271. Elf_Rela r;
  1272. unsigned int r_sym;
  1273. const char *fromsec;
  1274. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1275. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1276. fromsec = sech_name(elf, sechdr);
  1277. fromsec += strlen(".rela");
  1278. /* if from section (name) is know good then skip it */
  1279. if (match(fromsec, section_white_list))
  1280. return;
  1281. for (rela = start; rela < stop; rela++) {
  1282. r.r_offset = TO_NATIVE(rela->r_offset);
  1283. #if KERNEL_ELFCLASS == ELFCLASS64
  1284. if (elf->hdr->e_machine == EM_MIPS) {
  1285. unsigned int r_typ;
  1286. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1287. r_sym = TO_NATIVE(r_sym);
  1288. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1289. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1290. } else {
  1291. r.r_info = TO_NATIVE(rela->r_info);
  1292. r_sym = ELF_R_SYM(r.r_info);
  1293. }
  1294. #else
  1295. r.r_info = TO_NATIVE(rela->r_info);
  1296. r_sym = ELF_R_SYM(r.r_info);
  1297. #endif
  1298. r.r_addend = TO_NATIVE(rela->r_addend);
  1299. sym = elf->symtab_start + r_sym;
  1300. /* Skip special sections */
  1301. if (sym->st_shndx >= SHN_LORESERVE)
  1302. continue;
  1303. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1304. }
  1305. }
  1306. static void section_rel(const char *modname, struct elf_info *elf,
  1307. Elf_Shdr *sechdr)
  1308. {
  1309. Elf_Sym *sym;
  1310. Elf_Rel *rel;
  1311. Elf_Rela r;
  1312. unsigned int r_sym;
  1313. const char *fromsec;
  1314. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1315. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1316. fromsec = sech_name(elf, sechdr);
  1317. fromsec += strlen(".rel");
  1318. /* if from section (name) is know good then skip it */
  1319. if (match(fromsec, section_white_list))
  1320. return;
  1321. for (rel = start; rel < stop; rel++) {
  1322. r.r_offset = TO_NATIVE(rel->r_offset);
  1323. #if KERNEL_ELFCLASS == ELFCLASS64
  1324. if (elf->hdr->e_machine == EM_MIPS) {
  1325. unsigned int r_typ;
  1326. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1327. r_sym = TO_NATIVE(r_sym);
  1328. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1329. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1330. } else {
  1331. r.r_info = TO_NATIVE(rel->r_info);
  1332. r_sym = ELF_R_SYM(r.r_info);
  1333. }
  1334. #else
  1335. r.r_info = TO_NATIVE(rel->r_info);
  1336. r_sym = ELF_R_SYM(r.r_info);
  1337. #endif
  1338. r.r_addend = 0;
  1339. switch (elf->hdr->e_machine) {
  1340. case EM_386:
  1341. if (addend_386_rel(elf, sechdr, &r))
  1342. continue;
  1343. break;
  1344. case EM_ARM:
  1345. if (addend_arm_rel(elf, sechdr, &r))
  1346. continue;
  1347. break;
  1348. case EM_MIPS:
  1349. if (addend_mips_rel(elf, sechdr, &r))
  1350. continue;
  1351. break;
  1352. }
  1353. sym = elf->symtab_start + r_sym;
  1354. /* Skip special sections */
  1355. if (sym->st_shndx >= SHN_LORESERVE)
  1356. continue;
  1357. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1358. }
  1359. }
  1360. /**
  1361. * A module includes a number of sections that are discarded
  1362. * either when loaded or when used as built-in.
  1363. * For loaded modules all functions marked __init and all data
  1364. * marked __initdata will be discarded when the module has been intialized.
  1365. * Likewise for modules used built-in the sections marked __exit
  1366. * are discarded because __exit marked function are supposed to be called
  1367. * only when a module is unloaded which never happens for built-in modules.
  1368. * The check_sec_ref() function traverses all relocation records
  1369. * to find all references to a section that reference a section that will
  1370. * be discarded and warns about it.
  1371. **/
  1372. static void check_sec_ref(struct module *mod, const char *modname,
  1373. struct elf_info *elf)
  1374. {
  1375. int i;
  1376. Elf_Shdr *sechdrs = elf->sechdrs;
  1377. /* Walk through all sections */
  1378. for (i = 0; i < elf->hdr->e_shnum; i++) {
  1379. check_section(modname, elf, &elf->sechdrs[i]);
  1380. /* We want to process only relocation sections and not .init */
  1381. if (sechdrs[i].sh_type == SHT_RELA)
  1382. section_rela(modname, elf, &elf->sechdrs[i]);
  1383. else if (sechdrs[i].sh_type == SHT_REL)
  1384. section_rel(modname, elf, &elf->sechdrs[i]);
  1385. }
  1386. }
  1387. static void read_symbols(char *modname)
  1388. {
  1389. const char *symname;
  1390. char *version;
  1391. char *license;
  1392. struct module *mod;
  1393. struct elf_info info = { };
  1394. Elf_Sym *sym;
  1395. if (!parse_elf(&info, modname))
  1396. return;
  1397. mod = new_module(modname);
  1398. /* When there's no vmlinux, don't print warnings about
  1399. * unresolved symbols (since there'll be too many ;) */
  1400. if (is_vmlinux(modname)) {
  1401. have_vmlinux = 1;
  1402. mod->skip = 1;
  1403. }
  1404. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1405. if (info.modinfo && !license && !is_vmlinux(modname))
  1406. warn("modpost: missing MODULE_LICENSE() in %s\n"
  1407. "see include/linux/module.h for "
  1408. "more information\n", modname);
  1409. while (license) {
  1410. if (license_is_gpl_compatible(license))
  1411. mod->gpl_compatible = 1;
  1412. else {
  1413. mod->gpl_compatible = 0;
  1414. break;
  1415. }
  1416. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1417. "license", license);
  1418. }
  1419. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1420. symname = info.strtab + sym->st_name;
  1421. handle_modversions(mod, &info, sym, symname);
  1422. handle_moddevtable(mod, &info, sym, symname);
  1423. }
  1424. if (!is_vmlinux(modname) ||
  1425. (is_vmlinux(modname) && vmlinux_section_warnings))
  1426. check_sec_ref(mod, modname, &info);
  1427. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1428. if (version)
  1429. maybe_frob_rcs_version(modname, version, info.modinfo,
  1430. version - (char *)info.hdr);
  1431. if (version || (all_versions && !is_vmlinux(modname)))
  1432. get_src_version(modname, mod->srcversion,
  1433. sizeof(mod->srcversion)-1);
  1434. parse_elf_finish(&info);
  1435. /* Our trick to get versioning for module struct etc. - it's
  1436. * never passed as an argument to an exported function, so
  1437. * the automatic versioning doesn't pick it up, but it's really
  1438. * important anyhow */
  1439. if (modversions)
  1440. mod->unres = alloc_symbol("module_layout", 0, mod->unres);
  1441. }
  1442. #define SZ 500
  1443. /* We first write the generated file into memory using the
  1444. * following helper, then compare to the file on disk and
  1445. * only update the later if anything changed */
  1446. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1447. const char *fmt, ...)
  1448. {
  1449. char tmp[SZ];
  1450. int len;
  1451. va_list ap;
  1452. va_start(ap, fmt);
  1453. len = vsnprintf(tmp, SZ, fmt, ap);
  1454. buf_write(buf, tmp, len);
  1455. va_end(ap);
  1456. }
  1457. void buf_write(struct buffer *buf, const char *s, int len)
  1458. {
  1459. if (buf->size - buf->pos < len) {
  1460. buf->size += len + SZ;
  1461. buf->p = realloc(buf->p, buf->size);
  1462. }
  1463. strncpy(buf->p + buf->pos, s, len);
  1464. buf->pos += len;
  1465. }
  1466. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1467. {
  1468. const char *e = is_vmlinux(m) ?"":".ko";
  1469. switch (exp) {
  1470. case export_gpl:
  1471. fatal("modpost: GPL-incompatible module %s%s "
  1472. "uses GPL-only symbol '%s'\n", m, e, s);
  1473. break;
  1474. case export_unused_gpl:
  1475. fatal("modpost: GPL-incompatible module %s%s "
  1476. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1477. break;
  1478. case export_gpl_future:
  1479. warn("modpost: GPL-incompatible module %s%s "
  1480. "uses future GPL-only symbol '%s'\n", m, e, s);
  1481. break;
  1482. case export_plain:
  1483. case export_unused:
  1484. case export_unknown:
  1485. /* ignore */
  1486. break;
  1487. }
  1488. }
  1489. static void check_for_unused(enum export exp, const char *m, const char *s)
  1490. {
  1491. const char *e = is_vmlinux(m) ?"":".ko";
  1492. switch (exp) {
  1493. case export_unused:
  1494. case export_unused_gpl:
  1495. warn("modpost: module %s%s "
  1496. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1497. break;
  1498. default:
  1499. /* ignore */
  1500. break;
  1501. }
  1502. }
  1503. static void check_exports(struct module *mod)
  1504. {
  1505. struct symbol *s, *exp;
  1506. for (s = mod->unres; s; s = s->next) {
  1507. const char *basename;
  1508. exp = find_symbol(s->name);
  1509. if (!exp || exp->module == mod)
  1510. continue;
  1511. basename = strrchr(mod->name, '/');
  1512. if (basename)
  1513. basename++;
  1514. else
  1515. basename = mod->name;
  1516. if (!mod->gpl_compatible)
  1517. check_for_gpl_usage(exp->export, basename, exp->name);
  1518. check_for_unused(exp->export, basename, exp->name);
  1519. }
  1520. }
  1521. /**
  1522. * Header for the generated file
  1523. **/
  1524. static void add_header(struct buffer *b, struct module *mod)
  1525. {
  1526. buf_printf(b, "#include <linux/module.h>\n");
  1527. buf_printf(b, "#include <linux/vermagic.h>\n");
  1528. buf_printf(b, "#include <linux/compiler.h>\n");
  1529. buf_printf(b, "\n");
  1530. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1531. buf_printf(b, "\n");
  1532. buf_printf(b, "struct module __this_module\n");
  1533. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1534. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1535. if (mod->has_init)
  1536. buf_printf(b, " .init = init_module,\n");
  1537. if (mod->has_cleanup)
  1538. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1539. " .exit = cleanup_module,\n"
  1540. "#endif\n");
  1541. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1542. buf_printf(b, "};\n");
  1543. }
  1544. static void add_staging_flag(struct buffer *b, const char *name)
  1545. {
  1546. static const char *staging_dir = "drivers/staging";
  1547. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  1548. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  1549. }
  1550. /**
  1551. * Record CRCs for unresolved symbols
  1552. **/
  1553. static int add_versions(struct buffer *b, struct module *mod)
  1554. {
  1555. struct symbol *s, *exp;
  1556. int err = 0;
  1557. for (s = mod->unres; s; s = s->next) {
  1558. exp = find_symbol(s->name);
  1559. if (!exp || exp->module == mod) {
  1560. if (have_vmlinux && !s->weak) {
  1561. if (warn_unresolved) {
  1562. warn("\"%s\" [%s.ko] undefined!\n",
  1563. s->name, mod->name);
  1564. } else {
  1565. merror("\"%s\" [%s.ko] undefined!\n",
  1566. s->name, mod->name);
  1567. err = 1;
  1568. }
  1569. }
  1570. continue;
  1571. }
  1572. s->module = exp->module;
  1573. s->crc_valid = exp->crc_valid;
  1574. s->crc = exp->crc;
  1575. }
  1576. if (!modversions)
  1577. return err;
  1578. buf_printf(b, "\n");
  1579. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1580. buf_printf(b, "__used\n");
  1581. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1582. for (s = mod->unres; s; s = s->next) {
  1583. if (!s->module)
  1584. continue;
  1585. if (!s->crc_valid) {
  1586. warn("\"%s\" [%s.ko] has no CRC!\n",
  1587. s->name, mod->name);
  1588. continue;
  1589. }
  1590. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1591. }
  1592. buf_printf(b, "};\n");
  1593. return err;
  1594. }
  1595. static void add_depends(struct buffer *b, struct module *mod,
  1596. struct module *modules)
  1597. {
  1598. struct symbol *s;
  1599. struct module *m;
  1600. int first = 1;
  1601. for (m = modules; m; m = m->next)
  1602. m->seen = is_vmlinux(m->name);
  1603. buf_printf(b, "\n");
  1604. buf_printf(b, "static const char __module_depends[]\n");
  1605. buf_printf(b, "__used\n");
  1606. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1607. buf_printf(b, "\"depends=");
  1608. for (s = mod->unres; s; s = s->next) {
  1609. const char *p;
  1610. if (!s->module)
  1611. continue;
  1612. if (s->module->seen)
  1613. continue;
  1614. s->module->seen = 1;
  1615. p = strrchr(s->module->name, '/');
  1616. if (p)
  1617. p++;
  1618. else
  1619. p = s->module->name;
  1620. buf_printf(b, "%s%s", first ? "" : ",", p);
  1621. first = 0;
  1622. }
  1623. buf_printf(b, "\";\n");
  1624. }
  1625. static void add_srcversion(struct buffer *b, struct module *mod)
  1626. {
  1627. if (mod->srcversion[0]) {
  1628. buf_printf(b, "\n");
  1629. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1630. mod->srcversion);
  1631. }
  1632. }
  1633. static void write_if_changed(struct buffer *b, const char *fname)
  1634. {
  1635. char *tmp;
  1636. FILE *file;
  1637. struct stat st;
  1638. file = fopen(fname, "r");
  1639. if (!file)
  1640. goto write;
  1641. if (fstat(fileno(file), &st) < 0)
  1642. goto close_write;
  1643. if (st.st_size != b->pos)
  1644. goto close_write;
  1645. tmp = NOFAIL(malloc(b->pos));
  1646. if (fread(tmp, 1, b->pos, file) != b->pos)
  1647. goto free_write;
  1648. if (memcmp(tmp, b->p, b->pos) != 0)
  1649. goto free_write;
  1650. free(tmp);
  1651. fclose(file);
  1652. return;
  1653. free_write:
  1654. free(tmp);
  1655. close_write:
  1656. fclose(file);
  1657. write:
  1658. file = fopen(fname, "w");
  1659. if (!file) {
  1660. perror(fname);
  1661. exit(1);
  1662. }
  1663. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1664. perror(fname);
  1665. exit(1);
  1666. }
  1667. fclose(file);
  1668. }
  1669. /* parse Module.symvers file. line format:
  1670. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1671. **/
  1672. static void read_dump(const char *fname, unsigned int kernel)
  1673. {
  1674. unsigned long size, pos = 0;
  1675. void *file = grab_file(fname, &size);
  1676. char *line;
  1677. if (!file)
  1678. /* No symbol versions, silently ignore */
  1679. return;
  1680. while ((line = get_next_line(&pos, file, size))) {
  1681. char *symname, *modname, *d, *export, *end;
  1682. unsigned int crc;
  1683. struct module *mod;
  1684. struct symbol *s;
  1685. if (!(symname = strchr(line, '\t')))
  1686. goto fail;
  1687. *symname++ = '\0';
  1688. if (!(modname = strchr(symname, '\t')))
  1689. goto fail;
  1690. *modname++ = '\0';
  1691. if ((export = strchr(modname, '\t')) != NULL)
  1692. *export++ = '\0';
  1693. if (export && ((end = strchr(export, '\t')) != NULL))
  1694. *end = '\0';
  1695. crc = strtoul(line, &d, 16);
  1696. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1697. goto fail;
  1698. mod = find_module(modname);
  1699. if (!mod) {
  1700. if (is_vmlinux(modname))
  1701. have_vmlinux = 1;
  1702. mod = new_module(modname);
  1703. mod->skip = 1;
  1704. }
  1705. s = sym_add_exported(symname, mod, export_no(export));
  1706. s->kernel = kernel;
  1707. s->preloaded = 1;
  1708. sym_update_crc(symname, mod, crc, export_no(export));
  1709. }
  1710. return;
  1711. fail:
  1712. fatal("parse error in symbol dump file\n");
  1713. }
  1714. /* For normal builds always dump all symbols.
  1715. * For external modules only dump symbols
  1716. * that are not read from kernel Module.symvers.
  1717. **/
  1718. static int dump_sym(struct symbol *sym)
  1719. {
  1720. if (!external_module)
  1721. return 1;
  1722. if (sym->vmlinux || sym->kernel)
  1723. return 0;
  1724. return 1;
  1725. }
  1726. static void write_dump(const char *fname)
  1727. {
  1728. struct buffer buf = { };
  1729. struct symbol *symbol;
  1730. int n;
  1731. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1732. symbol = symbolhash[n];
  1733. while (symbol) {
  1734. if (dump_sym(symbol))
  1735. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1736. symbol->crc, symbol->name,
  1737. symbol->module->name,
  1738. export_str(symbol->export));
  1739. symbol = symbol->next;
  1740. }
  1741. }
  1742. write_if_changed(&buf, fname);
  1743. }
  1744. struct ext_sym_list {
  1745. struct ext_sym_list *next;
  1746. const char *file;
  1747. };
  1748. int main(int argc, char **argv)
  1749. {
  1750. struct module *mod;
  1751. struct buffer buf = { };
  1752. char *kernel_read = NULL, *module_read = NULL;
  1753. char *dump_write = NULL;
  1754. int opt;
  1755. int err;
  1756. struct ext_sym_list *extsym_iter;
  1757. struct ext_sym_list *extsym_start = NULL;
  1758. while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
  1759. switch (opt) {
  1760. case 'i':
  1761. kernel_read = optarg;
  1762. break;
  1763. case 'I':
  1764. module_read = optarg;
  1765. external_module = 1;
  1766. break;
  1767. case 'c':
  1768. cross_build = 1;
  1769. break;
  1770. case 'e':
  1771. external_module = 1;
  1772. extsym_iter =
  1773. NOFAIL(malloc(sizeof(*extsym_iter)));
  1774. extsym_iter->next = extsym_start;
  1775. extsym_iter->file = optarg;
  1776. extsym_start = extsym_iter;
  1777. break;
  1778. case 'm':
  1779. modversions = 1;
  1780. break;
  1781. case 'o':
  1782. dump_write = optarg;
  1783. break;
  1784. case 'a':
  1785. all_versions = 1;
  1786. break;
  1787. case 's':
  1788. vmlinux_section_warnings = 0;
  1789. break;
  1790. case 'S':
  1791. sec_mismatch_verbose = 0;
  1792. break;
  1793. case 'w':
  1794. warn_unresolved = 1;
  1795. break;
  1796. default:
  1797. exit(1);
  1798. }
  1799. }
  1800. if (kernel_read)
  1801. read_dump(kernel_read, 1);
  1802. if (module_read)
  1803. read_dump(module_read, 0);
  1804. while (extsym_start) {
  1805. read_dump(extsym_start->file, 0);
  1806. extsym_iter = extsym_start->next;
  1807. free(extsym_start);
  1808. extsym_start = extsym_iter;
  1809. }
  1810. while (optind < argc)
  1811. read_symbols(argv[optind++]);
  1812. for (mod = modules; mod; mod = mod->next) {
  1813. if (mod->skip)
  1814. continue;
  1815. check_exports(mod);
  1816. }
  1817. err = 0;
  1818. for (mod = modules; mod; mod = mod->next) {
  1819. char fname[strlen(mod->name) + 10];
  1820. if (mod->skip)
  1821. continue;
  1822. buf.pos = 0;
  1823. add_header(&buf, mod);
  1824. add_staging_flag(&buf, mod->name);
  1825. err |= add_versions(&buf, mod);
  1826. add_depends(&buf, mod, modules);
  1827. add_moddevtable(&buf, mod);
  1828. add_srcversion(&buf, mod);
  1829. sprintf(fname, "%s.mod.c", mod->name);
  1830. write_if_changed(&buf, fname);
  1831. }
  1832. if (dump_write)
  1833. write_dump(dump_write);
  1834. if (sec_mismatch_count && !sec_mismatch_verbose)
  1835. warn("modpost: Found %d section mismatch(es).\n"
  1836. "To see full details build your kernel with:\n"
  1837. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1838. sec_mismatch_count);
  1839. return err;
  1840. }