modpost.c 54 KB

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