modpost.c 53 KB

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