modpost.c 56 KB

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