modpost.c 56 KB

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