modpost.c 57 KB

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