modpost.c 54 KB

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