modpost.c 55 KB

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