modpost.c 55 KB

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