modpost.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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 <elf.h>
  15. #include <fnmatch.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <string.h>
  19. #include <limits.h>
  20. #include <stdbool.h>
  21. #include <errno.h>
  22. #include <hashtable.h>
  23. #include <list.h>
  24. #include <xalloc.h>
  25. #include "modpost.h"
  26. #include "../../include/linux/license.h"
  27. static bool module_enabled;
  28. /* Are we using CONFIG_MODVERSIONS? */
  29. static bool modversions;
  30. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  31. static bool all_versions;
  32. /* If we are modposting external module set to 1 */
  33. static bool external_module;
  34. /* Only warn about unresolved symbols */
  35. static bool warn_unresolved;
  36. static int sec_mismatch_count;
  37. static bool sec_mismatch_warn_only = true;
  38. /* Trim EXPORT_SYMBOLs that are unused by in-tree modules */
  39. static bool trim_unused_exports;
  40. /* ignore missing files */
  41. static bool ignore_missing_files;
  42. /* If set to 1, only warn (instead of error) about missing ns imports */
  43. static bool allow_missing_ns_imports;
  44. static bool error_occurred;
  45. static bool extra_warn;
  46. bool target_is_big_endian;
  47. bool host_is_big_endian;
  48. /*
  49. * Cut off the warnings when there are too many. This typically occurs when
  50. * vmlinux is missing. ('make modules' without building vmlinux.)
  51. */
  52. #define MAX_UNRESOLVED_REPORTS 10
  53. static unsigned int nr_unresolved;
  54. /* In kernel, this size is defined in linux/module.h;
  55. * here we use Elf_Addr instead of long for covering cross-compile
  56. */
  57. #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
  58. void modpost_log(bool is_error, const char *fmt, ...)
  59. {
  60. va_list arglist;
  61. if (is_error) {
  62. fprintf(stderr, "ERROR: ");
  63. error_occurred = true;
  64. } else {
  65. fprintf(stderr, "WARNING: ");
  66. }
  67. fprintf(stderr, "modpost: ");
  68. va_start(arglist, fmt);
  69. vfprintf(stderr, fmt, arglist);
  70. va_end(arglist);
  71. }
  72. static inline bool strends(const char *str, const char *postfix)
  73. {
  74. if (strlen(str) < strlen(postfix))
  75. return false;
  76. return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
  77. }
  78. char *read_text_file(const char *filename)
  79. {
  80. struct stat st;
  81. size_t nbytes;
  82. int fd;
  83. char *buf;
  84. fd = open(filename, O_RDONLY);
  85. if (fd < 0) {
  86. perror(filename);
  87. exit(1);
  88. }
  89. if (fstat(fd, &st) < 0) {
  90. perror(filename);
  91. exit(1);
  92. }
  93. buf = xmalloc(st.st_size + 1);
  94. nbytes = st.st_size;
  95. while (nbytes) {
  96. ssize_t bytes_read;
  97. bytes_read = read(fd, buf, nbytes);
  98. if (bytes_read < 0) {
  99. perror(filename);
  100. exit(1);
  101. }
  102. nbytes -= bytes_read;
  103. }
  104. buf[st.st_size] = '\0';
  105. close(fd);
  106. return buf;
  107. }
  108. char *get_line(char **stringp)
  109. {
  110. char *orig = *stringp, *next;
  111. /* do not return the unwanted extra line at EOF */
  112. if (!orig || *orig == '\0')
  113. return NULL;
  114. /* don't use strsep here, it is not available everywhere */
  115. next = strchr(orig, '\n');
  116. if (next)
  117. *next++ = '\0';
  118. *stringp = next;
  119. return orig;
  120. }
  121. /* A list of all modules we processed */
  122. LIST_HEAD(modules);
  123. static struct module *find_module(const char *modname)
  124. {
  125. struct module *mod;
  126. list_for_each_entry(mod, &modules, list) {
  127. if (strcmp(mod->name, modname) == 0)
  128. return mod;
  129. }
  130. return NULL;
  131. }
  132. static struct module *new_module(const char *name, size_t namelen)
  133. {
  134. struct module *mod;
  135. mod = xmalloc(sizeof(*mod) + namelen + 1);
  136. memset(mod, 0, sizeof(*mod));
  137. INIT_LIST_HEAD(&mod->exported_symbols);
  138. INIT_LIST_HEAD(&mod->unresolved_symbols);
  139. INIT_LIST_HEAD(&mod->missing_namespaces);
  140. INIT_LIST_HEAD(&mod->imported_namespaces);
  141. memcpy(mod->name, name, namelen);
  142. mod->name[namelen] = '\0';
  143. mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
  144. /*
  145. * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
  146. * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
  147. * modpost will exit wiht error anyway.
  148. */
  149. mod->is_gpl_compatible = true;
  150. list_add_tail(&mod->list, &modules);
  151. return mod;
  152. }
  153. struct symbol {
  154. struct hlist_node hnode;/* link to hash table */
  155. struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
  156. struct module *module;
  157. char *namespace;
  158. unsigned int crc;
  159. bool crc_valid;
  160. bool weak;
  161. bool is_func;
  162. bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
  163. bool used; /* there exists a user of this symbol */
  164. char name[];
  165. };
  166. static HASHTABLE_DEFINE(symbol_hashtable, 1U << 10);
  167. /* This is based on the hash algorithm from gdbm, via tdb */
  168. static inline unsigned int tdb_hash(const char *name)
  169. {
  170. unsigned value; /* Used to compute the hash value. */
  171. unsigned i; /* Used to cycle through random values. */
  172. /* Set the initial value from the key size. */
  173. for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
  174. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  175. return (1103515243 * value + 12345);
  176. }
  177. /**
  178. * Allocate a new symbols for use in the hash of exported symbols or
  179. * the list of unresolved symbols per module
  180. **/
  181. static struct symbol *alloc_symbol(const char *name)
  182. {
  183. struct symbol *s = xmalloc(sizeof(*s) + strlen(name) + 1);
  184. memset(s, 0, sizeof(*s));
  185. strcpy(s->name, name);
  186. return s;
  187. }
  188. /* For the hash of exported symbols */
  189. static void hash_add_symbol(struct symbol *sym)
  190. {
  191. hash_add(symbol_hashtable, &sym->hnode, tdb_hash(sym->name));
  192. }
  193. static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
  194. {
  195. struct symbol *sym;
  196. sym = alloc_symbol(name);
  197. sym->weak = weak;
  198. list_add_tail(&sym->list, &mod->unresolved_symbols);
  199. }
  200. static struct symbol *sym_find_with_module(const char *name, struct module *mod)
  201. {
  202. struct symbol *s;
  203. /* For our purposes, .foo matches foo. PPC64 needs this. */
  204. if (name[0] == '.')
  205. name++;
  206. hash_for_each_possible(symbol_hashtable, s, hnode, tdb_hash(name)) {
  207. if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
  208. return s;
  209. }
  210. return NULL;
  211. }
  212. static struct symbol *find_symbol(const char *name)
  213. {
  214. return sym_find_with_module(name, NULL);
  215. }
  216. struct namespace_list {
  217. struct list_head list;
  218. char namespace[];
  219. };
  220. static bool contains_namespace(struct list_head *head, const char *namespace)
  221. {
  222. struct namespace_list *list;
  223. /*
  224. * The default namespace is null string "", which is always implicitly
  225. * contained.
  226. */
  227. if (!namespace[0])
  228. return true;
  229. list_for_each_entry(list, head, list) {
  230. if (!strcmp(list->namespace, namespace))
  231. return true;
  232. }
  233. return false;
  234. }
  235. static void add_namespace(struct list_head *head, const char *namespace)
  236. {
  237. struct namespace_list *ns_entry;
  238. if (!contains_namespace(head, namespace)) {
  239. ns_entry = xmalloc(sizeof(*ns_entry) + strlen(namespace) + 1);
  240. strcpy(ns_entry->namespace, namespace);
  241. list_add_tail(&ns_entry->list, head);
  242. }
  243. }
  244. static void *sym_get_data_by_offset(const struct elf_info *info,
  245. unsigned int secindex, unsigned long offset)
  246. {
  247. Elf_Shdr *sechdr = &info->sechdrs[secindex];
  248. return (void *)info->hdr + sechdr->sh_offset + offset;
  249. }
  250. void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
  251. {
  252. return sym_get_data_by_offset(info, get_secindex(info, sym),
  253. sym->st_value);
  254. }
  255. static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
  256. {
  257. return sym_get_data_by_offset(info, info->secindex_strings,
  258. sechdr->sh_name);
  259. }
  260. static const char *sec_name(const struct elf_info *info, unsigned int secindex)
  261. {
  262. /*
  263. * If sym->st_shndx is a special section index, there is no
  264. * corresponding section header.
  265. * Return "" if the index is out of range of info->sechdrs[] array.
  266. */
  267. if (secindex >= info->num_sections)
  268. return "";
  269. return sech_name(info, &info->sechdrs[secindex]);
  270. }
  271. #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
  272. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  273. bool gpl_only, const char *namespace)
  274. {
  275. struct symbol *s = find_symbol(name);
  276. if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
  277. error("%s: '%s' exported twice. Previous export was in %s%s\n",
  278. mod->name, name, s->module->name,
  279. s->module->is_vmlinux ? "" : ".ko");
  280. }
  281. s = alloc_symbol(name);
  282. s->module = mod;
  283. s->is_gpl_only = gpl_only;
  284. s->namespace = xstrdup(namespace);
  285. list_add_tail(&s->list, &mod->exported_symbols);
  286. hash_add_symbol(s);
  287. return s;
  288. }
  289. static void sym_set_crc(struct symbol *sym, unsigned int crc)
  290. {
  291. sym->crc = crc;
  292. sym->crc_valid = true;
  293. }
  294. static void *grab_file(const char *filename, size_t *size)
  295. {
  296. struct stat st;
  297. void *map = MAP_FAILED;
  298. int fd;
  299. fd = open(filename, O_RDONLY);
  300. if (fd < 0)
  301. return NULL;
  302. if (fstat(fd, &st))
  303. goto failed;
  304. *size = st.st_size;
  305. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  306. failed:
  307. close(fd);
  308. if (map == MAP_FAILED)
  309. return NULL;
  310. return map;
  311. }
  312. static void release_file(void *file, size_t size)
  313. {
  314. munmap(file, size);
  315. }
  316. static int parse_elf(struct elf_info *info, const char *filename)
  317. {
  318. unsigned int i;
  319. Elf_Ehdr *hdr;
  320. Elf_Shdr *sechdrs;
  321. Elf_Sym *sym;
  322. const char *secstrings;
  323. unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
  324. hdr = grab_file(filename, &info->size);
  325. if (!hdr) {
  326. if (ignore_missing_files) {
  327. fprintf(stderr, "%s: %s (ignored)\n", filename,
  328. strerror(errno));
  329. return 0;
  330. }
  331. perror(filename);
  332. exit(1);
  333. }
  334. info->hdr = hdr;
  335. if (info->size < sizeof(*hdr)) {
  336. /* file too small, assume this is an empty .o file */
  337. return 0;
  338. }
  339. /* Is this a valid ELF file? */
  340. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  341. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  342. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  343. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  344. /* Not an ELF file - silently ignore it */
  345. return 0;
  346. }
  347. switch (hdr->e_ident[EI_DATA]) {
  348. case ELFDATA2LSB:
  349. target_is_big_endian = false;
  350. break;
  351. case ELFDATA2MSB:
  352. target_is_big_endian = true;
  353. break;
  354. default:
  355. fatal("target endian is unknown\n");
  356. }
  357. /* Fix endianness in ELF header */
  358. hdr->e_type = TO_NATIVE(hdr->e_type);
  359. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  360. hdr->e_version = TO_NATIVE(hdr->e_version);
  361. hdr->e_entry = TO_NATIVE(hdr->e_entry);
  362. hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
  363. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  364. hdr->e_flags = TO_NATIVE(hdr->e_flags);
  365. hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
  366. hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
  367. hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
  368. hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
  369. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  370. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  371. sechdrs = (void *)hdr + hdr->e_shoff;
  372. info->sechdrs = sechdrs;
  373. /* modpost only works for relocatable objects */
  374. if (hdr->e_type != ET_REL)
  375. fatal("%s: not relocatable object.", filename);
  376. /* Check if file offset is correct */
  377. if (hdr->e_shoff > info->size)
  378. fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
  379. (unsigned long)hdr->e_shoff, filename, info->size);
  380. if (hdr->e_shnum == SHN_UNDEF) {
  381. /*
  382. * There are more than 64k sections,
  383. * read count from .sh_size.
  384. */
  385. info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
  386. }
  387. else {
  388. info->num_sections = hdr->e_shnum;
  389. }
  390. if (hdr->e_shstrndx == SHN_XINDEX) {
  391. info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
  392. }
  393. else {
  394. info->secindex_strings = hdr->e_shstrndx;
  395. }
  396. /* Fix endianness in section headers */
  397. for (i = 0; i < info->num_sections; i++) {
  398. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  399. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  400. sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
  401. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  402. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  403. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  404. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  405. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  406. sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
  407. sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
  408. }
  409. /* Find symbol table. */
  410. secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
  411. for (i = 1; i < info->num_sections; i++) {
  412. const char *secname;
  413. int nobits = sechdrs[i].sh_type == SHT_NOBITS;
  414. if (!nobits && sechdrs[i].sh_offset > info->size)
  415. fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
  416. filename, (unsigned long)sechdrs[i].sh_offset,
  417. sizeof(*hdr));
  418. secname = secstrings + sechdrs[i].sh_name;
  419. if (strcmp(secname, ".modinfo") == 0) {
  420. if (nobits)
  421. fatal("%s has NOBITS .modinfo\n", filename);
  422. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  423. info->modinfo_len = sechdrs[i].sh_size;
  424. } else if (!strcmp(secname, ".export_symbol")) {
  425. info->export_symbol_secndx = i;
  426. }
  427. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  428. unsigned int sh_link_idx;
  429. symtab_idx = i;
  430. info->symtab_start = (void *)hdr +
  431. sechdrs[i].sh_offset;
  432. info->symtab_stop = (void *)hdr +
  433. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  434. sh_link_idx = sechdrs[i].sh_link;
  435. info->strtab = (void *)hdr +
  436. sechdrs[sh_link_idx].sh_offset;
  437. }
  438. /* 32bit section no. table? ("more than 64k sections") */
  439. if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
  440. symtab_shndx_idx = i;
  441. info->symtab_shndx_start = (void *)hdr +
  442. sechdrs[i].sh_offset;
  443. info->symtab_shndx_stop = (void *)hdr +
  444. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  445. }
  446. }
  447. if (!info->symtab_start)
  448. fatal("%s has no symtab?\n", filename);
  449. /* Fix endianness in symbols */
  450. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  451. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  452. sym->st_name = TO_NATIVE(sym->st_name);
  453. sym->st_value = TO_NATIVE(sym->st_value);
  454. sym->st_size = TO_NATIVE(sym->st_size);
  455. }
  456. if (symtab_shndx_idx != ~0U) {
  457. Elf32_Word *p;
  458. if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
  459. fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
  460. filename, sechdrs[symtab_shndx_idx].sh_link,
  461. symtab_idx);
  462. /* Fix endianness */
  463. for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
  464. p++)
  465. *p = TO_NATIVE(*p);
  466. }
  467. symsearch_init(info);
  468. return 1;
  469. }
  470. static void parse_elf_finish(struct elf_info *info)
  471. {
  472. symsearch_finish(info);
  473. release_file(info->hdr, info->size);
  474. }
  475. static int ignore_undef_symbol(struct elf_info *info, const char *symname)
  476. {
  477. /* ignore __this_module, it will be resolved shortly */
  478. if (strcmp(symname, "__this_module") == 0)
  479. return 1;
  480. /* ignore global offset table */
  481. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  482. return 1;
  483. if (info->hdr->e_machine == EM_PPC)
  484. /* Special register function linked on all modules during final link of .ko */
  485. if (strstarts(symname, "_restgpr_") ||
  486. strstarts(symname, "_savegpr_") ||
  487. strstarts(symname, "_rest32gpr_") ||
  488. strstarts(symname, "_save32gpr_") ||
  489. strstarts(symname, "_restvr_") ||
  490. strstarts(symname, "_savevr_"))
  491. return 1;
  492. if (info->hdr->e_machine == EM_PPC64)
  493. /* Special register function linked on all modules during final link of .ko */
  494. if (strstarts(symname, "_restgpr0_") ||
  495. strstarts(symname, "_savegpr0_") ||
  496. strstarts(symname, "_restvr_") ||
  497. strstarts(symname, "_savevr_") ||
  498. strcmp(symname, ".TOC.") == 0)
  499. return 1;
  500. /* Do not ignore this symbol */
  501. return 0;
  502. }
  503. static void handle_symbol(struct module *mod, struct elf_info *info,
  504. const Elf_Sym *sym, const char *symname)
  505. {
  506. switch (sym->st_shndx) {
  507. case SHN_COMMON:
  508. if (strstarts(symname, "__gnu_lto_")) {
  509. /* Should warn here, but modpost runs before the linker */
  510. } else
  511. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  512. break;
  513. case SHN_UNDEF:
  514. /* undefined symbol */
  515. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  516. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  517. break;
  518. if (ignore_undef_symbol(info, symname))
  519. break;
  520. if (info->hdr->e_machine == EM_SPARC ||
  521. info->hdr->e_machine == EM_SPARCV9) {
  522. /* Ignore register directives. */
  523. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  524. break;
  525. if (symname[0] == '.') {
  526. char *munged = xstrdup(symname);
  527. munged[0] = '_';
  528. munged[1] = toupper(munged[1]);
  529. symname = munged;
  530. }
  531. }
  532. sym_add_unresolved(symname, mod,
  533. ELF_ST_BIND(sym->st_info) == STB_WEAK);
  534. break;
  535. default:
  536. if (strcmp(symname, "init_module") == 0)
  537. mod->has_init = true;
  538. if (strcmp(symname, "cleanup_module") == 0)
  539. mod->has_cleanup = true;
  540. break;
  541. }
  542. }
  543. /**
  544. * Parse tag=value strings from .modinfo section
  545. **/
  546. static char *next_string(char *string, unsigned long *secsize)
  547. {
  548. /* Skip non-zero chars */
  549. while (string[0]) {
  550. string++;
  551. if ((*secsize)-- <= 1)
  552. return NULL;
  553. }
  554. /* Skip any zero padding. */
  555. while (!string[0]) {
  556. string++;
  557. if ((*secsize)-- <= 1)
  558. return NULL;
  559. }
  560. return string;
  561. }
  562. static char *get_next_modinfo(struct elf_info *info, const char *tag,
  563. char *prev)
  564. {
  565. char *p;
  566. unsigned int taglen = strlen(tag);
  567. char *modinfo = info->modinfo;
  568. unsigned long size = info->modinfo_len;
  569. if (prev) {
  570. size -= prev - modinfo;
  571. modinfo = next_string(prev, &size);
  572. }
  573. for (p = modinfo; p; p = next_string(p, &size)) {
  574. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  575. return p + taglen + 1;
  576. }
  577. return NULL;
  578. }
  579. static char *get_modinfo(struct elf_info *info, const char *tag)
  580. {
  581. return get_next_modinfo(info, tag, NULL);
  582. }
  583. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  584. {
  585. return sym ? elf->strtab + sym->st_name : "";
  586. }
  587. /*
  588. * Check whether the 'string' argument matches one of the 'patterns',
  589. * an array of shell wildcard patterns (glob).
  590. *
  591. * Return true is there is a match.
  592. */
  593. static bool match(const char *string, const char *const patterns[])
  594. {
  595. const char *pattern;
  596. while ((pattern = *patterns++)) {
  597. if (!fnmatch(pattern, string, 0))
  598. return true;
  599. }
  600. return false;
  601. }
  602. /* useful to pass patterns to match() directly */
  603. #define PATTERNS(...) \
  604. ({ \
  605. static const char *const patterns[] = {__VA_ARGS__, NULL}; \
  606. patterns; \
  607. })
  608. /* sections that we do not want to do full section mismatch check on */
  609. static const char *const section_white_list[] =
  610. {
  611. ".comment*",
  612. ".debug*",
  613. ".zdebug*", /* Compressed debug sections. */
  614. ".GCC.command.line", /* record-gcc-switches */
  615. ".mdebug*", /* alpha, score, mips etc. */
  616. ".pdr", /* alpha, score, mips etc. */
  617. ".stab*",
  618. ".note*",
  619. ".got*",
  620. ".toc*",
  621. ".xt.prop", /* xtensa */
  622. ".xt.lit", /* xtensa */
  623. ".arcextmap*", /* arc */
  624. ".gnu.linkonce.arcext*", /* arc : modules */
  625. ".cmem*", /* EZchip */
  626. ".fmt_slot*", /* EZchip */
  627. ".gnu.lto*",
  628. ".discard.*",
  629. ".llvm.call-graph-profile", /* call graph */
  630. NULL
  631. };
  632. /*
  633. * This is used to find sections missing the SHF_ALLOC flag.
  634. * The cause of this is often a section specified in assembler
  635. * without "ax" / "aw".
  636. */
  637. static void check_section(const char *modname, struct elf_info *elf,
  638. Elf_Shdr *sechdr)
  639. {
  640. const char *sec = sech_name(elf, sechdr);
  641. if (sechdr->sh_type == SHT_PROGBITS &&
  642. !(sechdr->sh_flags & SHF_ALLOC) &&
  643. !match(sec, section_white_list)) {
  644. warn("%s (%s): unexpected non-allocatable section.\n"
  645. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  646. "Note that for example <linux/init.h> contains\n"
  647. "section definitions for use in .S files.\n\n",
  648. modname, sec);
  649. }
  650. }
  651. #define ALL_INIT_DATA_SECTIONS \
  652. ".init.setup", ".init.rodata", ".init.data"
  653. #define ALL_PCI_INIT_SECTIONS \
  654. ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
  655. ".pci_fixup_enable", ".pci_fixup_resume", \
  656. ".pci_fixup_resume_early", ".pci_fixup_suspend"
  657. #define ALL_INIT_SECTIONS ".init.*"
  658. #define ALL_EXIT_SECTIONS ".exit.*"
  659. #define DATA_SECTIONS ".data", ".data.rel"
  660. #define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
  661. ".kprobes.text", ".cpuidle.text", ".noinstr.text", \
  662. ".ltext", ".ltext.*"
  663. #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
  664. ".fixup", ".entry.text", ".exception.text", \
  665. ".coldtext", ".softirqentry.text", ".irqentry.text"
  666. #define ALL_TEXT_SECTIONS ".init.text", ".exit.text", \
  667. TEXT_SECTIONS, OTHER_TEXT_SECTIONS
  668. enum mismatch {
  669. TEXTDATA_TO_ANY_INIT_EXIT,
  670. XXXINIT_TO_SOME_INIT,
  671. ANY_INIT_TO_ANY_EXIT,
  672. ANY_EXIT_TO_ANY_INIT,
  673. EXTABLE_TO_NON_TEXT,
  674. };
  675. /**
  676. * Describe how to match sections on different criteria:
  677. *
  678. * @fromsec: Array of sections to be matched.
  679. *
  680. * @bad_tosec: Relocations applied to a section in @fromsec to a section in
  681. * this array is forbidden (black-list). Can be empty.
  682. *
  683. * @good_tosec: Relocations applied to a section in @fromsec must be
  684. * targeting sections in this array (white-list). Can be empty.
  685. *
  686. * @mismatch: Type of mismatch.
  687. */
  688. struct sectioncheck {
  689. const char *fromsec[20];
  690. const char *bad_tosec[20];
  691. const char *good_tosec[20];
  692. enum mismatch mismatch;
  693. };
  694. static const struct sectioncheck sectioncheck[] = {
  695. /* Do not reference init/exit code/data from
  696. * normal code and data
  697. */
  698. {
  699. .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
  700. .bad_tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
  701. .mismatch = TEXTDATA_TO_ANY_INIT_EXIT,
  702. },
  703. /* Do not use exit code/data from init code */
  704. {
  705. .fromsec = { ALL_INIT_SECTIONS, NULL },
  706. .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
  707. .mismatch = ANY_INIT_TO_ANY_EXIT,
  708. },
  709. /* Do not use init code/data from exit code */
  710. {
  711. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  712. .bad_tosec = { ALL_INIT_SECTIONS, NULL },
  713. .mismatch = ANY_EXIT_TO_ANY_INIT,
  714. },
  715. {
  716. .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
  717. .bad_tosec = { ALL_INIT_SECTIONS, NULL },
  718. .mismatch = ANY_INIT_TO_ANY_EXIT,
  719. },
  720. {
  721. .fromsec = { "__ex_table", NULL },
  722. /* If you're adding any new black-listed sections in here, consider
  723. * adding a special 'printer' for them in scripts/check_extable.
  724. */
  725. .bad_tosec = { ".altinstr_replacement", NULL },
  726. .good_tosec = {ALL_TEXT_SECTIONS , NULL},
  727. .mismatch = EXTABLE_TO_NON_TEXT,
  728. }
  729. };
  730. static const struct sectioncheck *section_mismatch(
  731. const char *fromsec, const char *tosec)
  732. {
  733. int i;
  734. /*
  735. * The target section could be the SHT_NUL section when we're
  736. * handling relocations to un-resolved symbols, trying to match it
  737. * doesn't make much sense and causes build failures on parisc
  738. * architectures.
  739. */
  740. if (*tosec == '\0')
  741. return NULL;
  742. for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
  743. const struct sectioncheck *check = &sectioncheck[i];
  744. if (match(fromsec, check->fromsec)) {
  745. if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
  746. return check;
  747. if (check->good_tosec[0] && !match(tosec, check->good_tosec))
  748. return check;
  749. }
  750. }
  751. return NULL;
  752. }
  753. /**
  754. * Whitelist to allow certain references to pass with no warning.
  755. *
  756. * Pattern 1:
  757. * If a module parameter is declared __initdata and permissions=0
  758. * then this is legal despite the warning generated.
  759. * We cannot see value of permissions here, so just ignore
  760. * this pattern.
  761. * The pattern is identified by:
  762. * tosec = .init.data
  763. * fromsec = .data*
  764. * atsym =__param*
  765. *
  766. * Pattern 1a:
  767. * module_param_call() ops can refer to __init set function if permissions=0
  768. * The pattern is identified by:
  769. * tosec = .init.text
  770. * fromsec = .data*
  771. * atsym = __param_ops_*
  772. *
  773. * Pattern 3:
  774. * Whitelist all references from .head.text to any init section
  775. *
  776. * Pattern 4:
  777. * Some symbols belong to init section but still it is ok to reference
  778. * these from non-init sections as these symbols don't have any memory
  779. * allocated for them and symbol address and value are same. So even
  780. * if init section is freed, its ok to reference those symbols.
  781. * For ex. symbols marking the init section boundaries.
  782. * This pattern is identified by
  783. * refsymname = __init_begin, _sinittext, _einittext
  784. *
  785. * Pattern 5:
  786. * GCC may optimize static inlines when fed constant arg(s) resulting
  787. * in functions like cpumask_empty() -- generating an associated symbol
  788. * cpumask_empty.constprop.3 that appears in the audit. If the const that
  789. * is passed in comes from __init, like say nmi_ipi_mask, we get a
  790. * meaningless section warning. May need to add isra symbols too...
  791. * This pattern is identified by
  792. * tosec = init section
  793. * fromsec = text section
  794. * refsymname = *.constprop.*
  795. *
  796. **/
  797. static int secref_whitelist(const char *fromsec, const char *fromsym,
  798. const char *tosec, const char *tosym)
  799. {
  800. /* Check for pattern 1 */
  801. if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) &&
  802. match(fromsec, PATTERNS(DATA_SECTIONS)) &&
  803. strstarts(fromsym, "__param"))
  804. return 0;
  805. /* Check for pattern 1a */
  806. if (strcmp(tosec, ".init.text") == 0 &&
  807. match(fromsec, PATTERNS(DATA_SECTIONS)) &&
  808. strstarts(fromsym, "__param_ops_"))
  809. return 0;
  810. /* symbols in data sections that may refer to any init/exit sections */
  811. if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
  812. match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
  813. match(fromsym, PATTERNS("*_ops", "*_probe", "*_console")))
  814. return 0;
  815. /* Check for pattern 3 */
  816. if (strstarts(fromsec, ".head.text") &&
  817. match(tosec, PATTERNS(ALL_INIT_SECTIONS)))
  818. return 0;
  819. /* Check for pattern 4 */
  820. if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext")))
  821. return 0;
  822. /* Check for pattern 5 */
  823. if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
  824. match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
  825. match(fromsym, PATTERNS("*.constprop.*")))
  826. return 0;
  827. return 1;
  828. }
  829. static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr,
  830. unsigned int secndx)
  831. {
  832. return symsearch_find_nearest(elf, addr, secndx, false, ~0);
  833. }
  834. static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
  835. {
  836. Elf_Sym *new_sym;
  837. /* If the supplied symbol has a valid name, return it */
  838. if (is_valid_name(elf, sym))
  839. return sym;
  840. /*
  841. * Strive to find a better symbol name, but the resulting name may not
  842. * match the symbol referenced in the original code.
  843. */
  844. new_sym = symsearch_find_nearest(elf, addr, get_secindex(elf, sym),
  845. true, 20);
  846. return new_sym ? new_sym : sym;
  847. }
  848. static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
  849. {
  850. if (secndx >= elf->num_sections)
  851. return false;
  852. return (elf->sechdrs[secndx].sh_flags & SHF_EXECINSTR) != 0;
  853. }
  854. static void default_mismatch_handler(const char *modname, struct elf_info *elf,
  855. const struct sectioncheck* const mismatch,
  856. Elf_Sym *tsym,
  857. unsigned int fsecndx, const char *fromsec, Elf_Addr faddr,
  858. const char *tosec, Elf_Addr taddr)
  859. {
  860. Elf_Sym *from;
  861. const char *tosym;
  862. const char *fromsym;
  863. char taddr_str[16];
  864. from = find_fromsym(elf, faddr, fsecndx);
  865. fromsym = sym_name(elf, from);
  866. tsym = find_tosym(elf, taddr, tsym);
  867. tosym = sym_name(elf, tsym);
  868. /* check whitelist - we may ignore it */
  869. if (!secref_whitelist(fromsec, fromsym, tosec, tosym))
  870. return;
  871. sec_mismatch_count++;
  872. if (!tosym[0])
  873. snprintf(taddr_str, sizeof(taddr_str), "0x%x", (unsigned int)taddr);
  874. /*
  875. * The format for the reference source: <symbol_name>+<offset> or <address>
  876. * The format for the reference destination: <symbol_name> or <address>
  877. */
  878. warn("%s: section mismatch in reference: %s%s0x%x (section: %s) -> %s (section: %s)\n",
  879. modname, fromsym, fromsym[0] ? "+" : "",
  880. (unsigned int)(faddr - (fromsym[0] ? from->st_value : 0)),
  881. fromsec, tosym[0] ? tosym : taddr_str, tosec);
  882. if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
  883. if (match(tosec, mismatch->bad_tosec))
  884. fatal("The relocation at %s+0x%lx references\n"
  885. "section \"%s\" which is black-listed.\n"
  886. "Something is seriously wrong and should be fixed.\n"
  887. "You might get more information about where this is\n"
  888. "coming from by using scripts/check_extable.sh %s\n",
  889. fromsec, (long)faddr, tosec, modname);
  890. else if (is_executable_section(elf, get_secindex(elf, tsym)))
  891. warn("The relocation at %s+0x%lx references\n"
  892. "section \"%s\" which is not in the list of\n"
  893. "authorized sections. If you're adding a new section\n"
  894. "and/or if this reference is valid, add \"%s\" to the\n"
  895. "list of authorized sections to jump to on fault.\n"
  896. "This can be achieved by adding \"%s\" to\n"
  897. "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
  898. fromsec, (long)faddr, tosec, tosec, tosec);
  899. else
  900. error("%s+0x%lx references non-executable section '%s'\n",
  901. fromsec, (long)faddr, tosec);
  902. }
  903. }
  904. static void check_export_symbol(struct module *mod, struct elf_info *elf,
  905. Elf_Addr faddr, const char *secname,
  906. Elf_Sym *sym)
  907. {
  908. static const char *prefix = "__export_symbol_";
  909. const char *label_name, *name, *data;
  910. Elf_Sym *label;
  911. struct symbol *s;
  912. bool is_gpl;
  913. label = find_fromsym(elf, faddr, elf->export_symbol_secndx);
  914. label_name = sym_name(elf, label);
  915. if (!strstarts(label_name, prefix)) {
  916. error("%s: .export_symbol section contains strange symbol '%s'\n",
  917. mod->name, label_name);
  918. return;
  919. }
  920. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  921. ELF_ST_BIND(sym->st_info) != STB_WEAK) {
  922. error("%s: local symbol '%s' was exported\n", mod->name,
  923. label_name + strlen(prefix));
  924. return;
  925. }
  926. name = sym_name(elf, sym);
  927. if (strcmp(label_name + strlen(prefix), name)) {
  928. error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",
  929. mod->name, name);
  930. return;
  931. }
  932. data = sym_get_data(elf, label); /* license */
  933. if (!strcmp(data, "GPL")) {
  934. is_gpl = true;
  935. } else if (!strcmp(data, "")) {
  936. is_gpl = false;
  937. } else {
  938. error("%s: unknown license '%s' was specified for '%s'\n",
  939. mod->name, data, name);
  940. return;
  941. }
  942. data += strlen(data) + 1; /* namespace */
  943. s = sym_add_exported(name, mod, is_gpl, data);
  944. /*
  945. * We need to be aware whether we are exporting a function or
  946. * a data on some architectures.
  947. */
  948. s->is_func = (ELF_ST_TYPE(sym->st_info) == STT_FUNC);
  949. /*
  950. * For parisc64, symbols prefixed $$ from the library have the symbol type
  951. * STT_LOPROC. They should be handled as functions too.
  952. */
  953. if (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64 &&
  954. elf->hdr->e_machine == EM_PARISC &&
  955. ELF_ST_TYPE(sym->st_info) == STT_LOPROC)
  956. s->is_func = true;
  957. if (match(secname, PATTERNS(ALL_INIT_SECTIONS)))
  958. warn("%s: %s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_SYMBOL.\n",
  959. mod->name, name);
  960. else if (match(secname, PATTERNS(ALL_EXIT_SECTIONS)))
  961. warn("%s: %s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_SYMBOL.\n",
  962. mod->name, name);
  963. }
  964. static void check_section_mismatch(struct module *mod, struct elf_info *elf,
  965. Elf_Sym *sym,
  966. unsigned int fsecndx, const char *fromsec,
  967. Elf_Addr faddr, Elf_Addr taddr)
  968. {
  969. const char *tosec = sec_name(elf, get_secindex(elf, sym));
  970. const struct sectioncheck *mismatch;
  971. if (module_enabled && elf->export_symbol_secndx == fsecndx) {
  972. check_export_symbol(mod, elf, faddr, tosec, sym);
  973. return;
  974. }
  975. mismatch = section_mismatch(fromsec, tosec);
  976. if (!mismatch)
  977. return;
  978. default_mismatch_handler(mod->name, elf, mismatch, sym,
  979. fsecndx, fromsec, faddr,
  980. tosec, taddr);
  981. }
  982. static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
  983. {
  984. switch (r_type) {
  985. case R_386_32:
  986. return TO_NATIVE(*location);
  987. case R_386_PC32:
  988. return TO_NATIVE(*location) + 4;
  989. }
  990. return (Elf_Addr)(-1);
  991. }
  992. static int32_t sign_extend32(int32_t value, int index)
  993. {
  994. uint8_t shift = 31 - index;
  995. return (int32_t)(value << shift) >> shift;
  996. }
  997. static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
  998. {
  999. uint32_t inst, upper, lower, sign, j1, j2;
  1000. int32_t offset;
  1001. switch (r_type) {
  1002. case R_ARM_ABS32:
  1003. case R_ARM_REL32:
  1004. inst = TO_NATIVE(*(uint32_t *)loc);
  1005. return inst + sym->st_value;
  1006. case R_ARM_MOVW_ABS_NC:
  1007. case R_ARM_MOVT_ABS:
  1008. inst = TO_NATIVE(*(uint32_t *)loc);
  1009. offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff),
  1010. 15);
  1011. return offset + sym->st_value;
  1012. case R_ARM_PC24:
  1013. case R_ARM_CALL:
  1014. case R_ARM_JUMP24:
  1015. inst = TO_NATIVE(*(uint32_t *)loc);
  1016. offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
  1017. return offset + sym->st_value + 8;
  1018. case R_ARM_THM_MOVW_ABS_NC:
  1019. case R_ARM_THM_MOVT_ABS:
  1020. upper = TO_NATIVE(*(uint16_t *)loc);
  1021. lower = TO_NATIVE(*((uint16_t *)loc + 1));
  1022. offset = sign_extend32(((upper & 0x000f) << 12) |
  1023. ((upper & 0x0400) << 1) |
  1024. ((lower & 0x7000) >> 4) |
  1025. (lower & 0x00ff),
  1026. 15);
  1027. return offset + sym->st_value;
  1028. case R_ARM_THM_JUMP19:
  1029. /*
  1030. * Encoding T3:
  1031. * S = upper[10]
  1032. * imm6 = upper[5:0]
  1033. * J1 = lower[13]
  1034. * J2 = lower[11]
  1035. * imm11 = lower[10:0]
  1036. * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
  1037. */
  1038. upper = TO_NATIVE(*(uint16_t *)loc);
  1039. lower = TO_NATIVE(*((uint16_t *)loc + 1));
  1040. sign = (upper >> 10) & 1;
  1041. j1 = (lower >> 13) & 1;
  1042. j2 = (lower >> 11) & 1;
  1043. offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) |
  1044. ((upper & 0x03f) << 12) |
  1045. ((lower & 0x07ff) << 1),
  1046. 20);
  1047. return offset + sym->st_value + 4;
  1048. case R_ARM_THM_PC22:
  1049. case R_ARM_THM_JUMP24:
  1050. /*
  1051. * Encoding T4:
  1052. * S = upper[10]
  1053. * imm10 = upper[9:0]
  1054. * J1 = lower[13]
  1055. * J2 = lower[11]
  1056. * imm11 = lower[10:0]
  1057. * I1 = NOT(J1 XOR S)
  1058. * I2 = NOT(J2 XOR S)
  1059. * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
  1060. */
  1061. upper = TO_NATIVE(*(uint16_t *)loc);
  1062. lower = TO_NATIVE(*((uint16_t *)loc + 1));
  1063. sign = (upper >> 10) & 1;
  1064. j1 = (lower >> 13) & 1;
  1065. j2 = (lower >> 11) & 1;
  1066. offset = sign_extend32((sign << 24) |
  1067. ((~(j1 ^ sign) & 1) << 23) |
  1068. ((~(j2 ^ sign) & 1) << 22) |
  1069. ((upper & 0x03ff) << 12) |
  1070. ((lower & 0x07ff) << 1),
  1071. 24);
  1072. return offset + sym->st_value + 4;
  1073. }
  1074. return (Elf_Addr)(-1);
  1075. }
  1076. static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
  1077. {
  1078. uint32_t inst;
  1079. inst = TO_NATIVE(*location);
  1080. switch (r_type) {
  1081. case R_MIPS_LO16:
  1082. return inst & 0xffff;
  1083. case R_MIPS_26:
  1084. return (inst & 0x03ffffff) << 2;
  1085. case R_MIPS_32:
  1086. return inst;
  1087. }
  1088. return (Elf_Addr)(-1);
  1089. }
  1090. #ifndef EM_RISCV
  1091. #define EM_RISCV 243
  1092. #endif
  1093. #ifndef R_RISCV_SUB32
  1094. #define R_RISCV_SUB32 39
  1095. #endif
  1096. #ifndef EM_LOONGARCH
  1097. #define EM_LOONGARCH 258
  1098. #endif
  1099. #ifndef R_LARCH_SUB32
  1100. #define R_LARCH_SUB32 55
  1101. #endif
  1102. #ifndef R_LARCH_RELAX
  1103. #define R_LARCH_RELAX 100
  1104. #endif
  1105. #ifndef R_LARCH_ALIGN
  1106. #define R_LARCH_ALIGN 102
  1107. #endif
  1108. static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
  1109. unsigned int *r_type, unsigned int *r_sym)
  1110. {
  1111. typedef struct {
  1112. Elf64_Word r_sym; /* Symbol index */
  1113. unsigned char r_ssym; /* Special symbol for 2nd relocation */
  1114. unsigned char r_type3; /* 3rd relocation type */
  1115. unsigned char r_type2; /* 2nd relocation type */
  1116. unsigned char r_type; /* 1st relocation type */
  1117. } Elf64_Mips_R_Info;
  1118. bool is_64bit = (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64);
  1119. if (elf->hdr->e_machine == EM_MIPS && is_64bit) {
  1120. Elf64_Mips_R_Info *mips64_r_info = (void *)&r_info;
  1121. *r_type = mips64_r_info->r_type;
  1122. *r_sym = TO_NATIVE(mips64_r_info->r_sym);
  1123. return;
  1124. }
  1125. if (is_64bit)
  1126. r_info = TO_NATIVE((Elf64_Xword)r_info);
  1127. else
  1128. r_info = TO_NATIVE((Elf32_Word)r_info);
  1129. *r_type = ELF_R_TYPE(r_info);
  1130. *r_sym = ELF_R_SYM(r_info);
  1131. }
  1132. static void section_rela(struct module *mod, struct elf_info *elf,
  1133. unsigned int fsecndx, const char *fromsec,
  1134. const Elf_Rela *start, const Elf_Rela *stop)
  1135. {
  1136. const Elf_Rela *rela;
  1137. for (rela = start; rela < stop; rela++) {
  1138. Elf_Sym *tsym;
  1139. Elf_Addr taddr, r_offset;
  1140. unsigned int r_type, r_sym;
  1141. r_offset = TO_NATIVE(rela->r_offset);
  1142. get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
  1143. tsym = elf->symtab_start + r_sym;
  1144. taddr = tsym->st_value + TO_NATIVE(rela->r_addend);
  1145. switch (elf->hdr->e_machine) {
  1146. case EM_RISCV:
  1147. if (!strcmp("__ex_table", fromsec) &&
  1148. r_type == R_RISCV_SUB32)
  1149. continue;
  1150. break;
  1151. case EM_LOONGARCH:
  1152. switch (r_type) {
  1153. case R_LARCH_SUB32:
  1154. if (!strcmp("__ex_table", fromsec))
  1155. continue;
  1156. break;
  1157. case R_LARCH_RELAX:
  1158. case R_LARCH_ALIGN:
  1159. /* These relocs do not refer to symbols */
  1160. continue;
  1161. }
  1162. break;
  1163. }
  1164. check_section_mismatch(mod, elf, tsym,
  1165. fsecndx, fromsec, r_offset, taddr);
  1166. }
  1167. }
  1168. static void section_rel(struct module *mod, struct elf_info *elf,
  1169. unsigned int fsecndx, const char *fromsec,
  1170. const Elf_Rel *start, const Elf_Rel *stop)
  1171. {
  1172. const Elf_Rel *rel;
  1173. for (rel = start; rel < stop; rel++) {
  1174. Elf_Sym *tsym;
  1175. Elf_Addr taddr, r_offset;
  1176. unsigned int r_type, r_sym;
  1177. void *loc;
  1178. r_offset = TO_NATIVE(rel->r_offset);
  1179. get_rel_type_and_sym(elf, rel->r_info, &r_type, &r_sym);
  1180. loc = sym_get_data_by_offset(elf, fsecndx, r_offset);
  1181. tsym = elf->symtab_start + r_sym;
  1182. switch (elf->hdr->e_machine) {
  1183. case EM_386:
  1184. taddr = addend_386_rel(loc, r_type);
  1185. break;
  1186. case EM_ARM:
  1187. taddr = addend_arm_rel(loc, tsym, r_type);
  1188. break;
  1189. case EM_MIPS:
  1190. taddr = addend_mips_rel(loc, r_type);
  1191. break;
  1192. default:
  1193. fatal("Please add code to calculate addend for this architecture\n");
  1194. }
  1195. check_section_mismatch(mod, elf, tsym,
  1196. fsecndx, fromsec, r_offset, taddr);
  1197. }
  1198. }
  1199. /**
  1200. * A module includes a number of sections that are discarded
  1201. * either when loaded or when used as built-in.
  1202. * For loaded modules all functions marked __init and all data
  1203. * marked __initdata will be discarded when the module has been initialized.
  1204. * Likewise for modules used built-in the sections marked __exit
  1205. * are discarded because __exit marked function are supposed to be called
  1206. * only when a module is unloaded which never happens for built-in modules.
  1207. * The check_sec_ref() function traverses all relocation records
  1208. * to find all references to a section that reference a section that will
  1209. * be discarded and warns about it.
  1210. **/
  1211. static void check_sec_ref(struct module *mod, struct elf_info *elf)
  1212. {
  1213. int i;
  1214. /* Walk through all sections */
  1215. for (i = 0; i < elf->num_sections; i++) {
  1216. Elf_Shdr *sechdr = &elf->sechdrs[i];
  1217. check_section(mod->name, elf, sechdr);
  1218. /* We want to process only relocation sections and not .init */
  1219. if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
  1220. /* section to which the relocation applies */
  1221. unsigned int secndx = sechdr->sh_info;
  1222. const char *secname = sec_name(elf, secndx);
  1223. const void *start, *stop;
  1224. /* If the section is known good, skip it */
  1225. if (match(secname, section_white_list))
  1226. continue;
  1227. start = sym_get_data_by_offset(elf, i, 0);
  1228. stop = start + sechdr->sh_size;
  1229. if (sechdr->sh_type == SHT_RELA)
  1230. section_rela(mod, elf, secndx, secname,
  1231. start, stop);
  1232. else
  1233. section_rel(mod, elf, secndx, secname,
  1234. start, stop);
  1235. }
  1236. }
  1237. }
  1238. static char *remove_dot(char *s)
  1239. {
  1240. size_t n = strcspn(s, ".");
  1241. if (n && s[n]) {
  1242. size_t m = strspn(s + n + 1, "0123456789");
  1243. if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
  1244. s[n] = 0;
  1245. }
  1246. return s;
  1247. }
  1248. /*
  1249. * The CRCs are recorded in .*.cmd files in the form of:
  1250. * #SYMVER <name> <crc>
  1251. */
  1252. static void extract_crcs_for_object(const char *object, struct module *mod)
  1253. {
  1254. char cmd_file[PATH_MAX];
  1255. char *buf, *p;
  1256. const char *base;
  1257. int dirlen, ret;
  1258. base = strrchr(object, '/');
  1259. if (base) {
  1260. base++;
  1261. dirlen = base - object;
  1262. } else {
  1263. dirlen = 0;
  1264. base = object;
  1265. }
  1266. ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
  1267. dirlen, object, base);
  1268. if (ret >= sizeof(cmd_file)) {
  1269. error("%s: too long path was truncated\n", cmd_file);
  1270. return;
  1271. }
  1272. buf = read_text_file(cmd_file);
  1273. p = buf;
  1274. while ((p = strstr(p, "\n#SYMVER "))) {
  1275. char *name;
  1276. size_t namelen;
  1277. unsigned int crc;
  1278. struct symbol *sym;
  1279. name = p + strlen("\n#SYMVER ");
  1280. p = strchr(name, ' ');
  1281. if (!p)
  1282. break;
  1283. namelen = p - name;
  1284. p++;
  1285. if (!isdigit(*p))
  1286. continue; /* skip this line */
  1287. crc = strtoul(p, &p, 0);
  1288. if (*p != '\n')
  1289. continue; /* skip this line */
  1290. name[namelen] = '\0';
  1291. /*
  1292. * sym_find_with_module() may return NULL here.
  1293. * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
  1294. * Since commit e1327a127703, genksyms calculates CRCs of all
  1295. * symbols, including trimmed ones. Ignore orphan CRCs.
  1296. */
  1297. sym = sym_find_with_module(name, mod);
  1298. if (sym)
  1299. sym_set_crc(sym, crc);
  1300. }
  1301. free(buf);
  1302. }
  1303. /*
  1304. * The symbol versions (CRC) are recorded in the .*.cmd files.
  1305. * Parse them to retrieve CRCs for the current module.
  1306. */
  1307. static void mod_set_crcs(struct module *mod)
  1308. {
  1309. char objlist[PATH_MAX];
  1310. char *buf, *p, *obj;
  1311. int ret;
  1312. if (mod->is_vmlinux) {
  1313. strcpy(objlist, ".vmlinux.objs");
  1314. } else {
  1315. /* objects for a module are listed in the *.mod file. */
  1316. ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
  1317. if (ret >= sizeof(objlist)) {
  1318. error("%s: too long path was truncated\n", objlist);
  1319. return;
  1320. }
  1321. }
  1322. buf = read_text_file(objlist);
  1323. p = buf;
  1324. while ((obj = strsep(&p, "\n")) && obj[0])
  1325. extract_crcs_for_object(obj, mod);
  1326. free(buf);
  1327. }
  1328. static void read_symbols(const char *modname)
  1329. {
  1330. const char *symname;
  1331. char *version;
  1332. char *license;
  1333. char *namespace;
  1334. struct module *mod;
  1335. struct elf_info info = { };
  1336. Elf_Sym *sym;
  1337. if (!parse_elf(&info, modname))
  1338. return;
  1339. if (!strends(modname, ".o")) {
  1340. error("%s: filename must be suffixed with .o\n", modname);
  1341. return;
  1342. }
  1343. /* strip trailing .o */
  1344. mod = new_module(modname, strlen(modname) - strlen(".o"));
  1345. if (!mod->is_vmlinux) {
  1346. license = get_modinfo(&info, "license");
  1347. if (!license)
  1348. error("missing MODULE_LICENSE() in %s\n", modname);
  1349. while (license) {
  1350. if (!license_is_gpl_compatible(license)) {
  1351. mod->is_gpl_compatible = false;
  1352. break;
  1353. }
  1354. license = get_next_modinfo(&info, "license", license);
  1355. }
  1356. namespace = get_modinfo(&info, "import_ns");
  1357. while (namespace) {
  1358. add_namespace(&mod->imported_namespaces, namespace);
  1359. namespace = get_next_modinfo(&info, "import_ns",
  1360. namespace);
  1361. }
  1362. if (extra_warn && !get_modinfo(&info, "description"))
  1363. warn("missing MODULE_DESCRIPTION() in %s\n", modname);
  1364. }
  1365. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1366. symname = remove_dot(info.strtab + sym->st_name);
  1367. handle_symbol(mod, &info, sym, symname);
  1368. handle_moddevtable(mod, &info, sym, symname);
  1369. }
  1370. check_sec_ref(mod, &info);
  1371. if (!mod->is_vmlinux) {
  1372. version = get_modinfo(&info, "version");
  1373. if (version || all_versions)
  1374. get_src_version(mod->name, mod->srcversion,
  1375. sizeof(mod->srcversion) - 1);
  1376. }
  1377. parse_elf_finish(&info);
  1378. if (modversions) {
  1379. /*
  1380. * Our trick to get versioning for module struct etc. - it's
  1381. * never passed as an argument to an exported function, so
  1382. * the automatic versioning doesn't pick it up, but it's really
  1383. * important anyhow.
  1384. */
  1385. sym_add_unresolved("module_layout", mod, false);
  1386. mod_set_crcs(mod);
  1387. }
  1388. }
  1389. static void read_symbols_from_files(const char *filename)
  1390. {
  1391. FILE *in = stdin;
  1392. char fname[PATH_MAX];
  1393. in = fopen(filename, "r");
  1394. if (!in)
  1395. fatal("Can't open filenames file %s: %m", filename);
  1396. while (fgets(fname, PATH_MAX, in) != NULL) {
  1397. if (strends(fname, "\n"))
  1398. fname[strlen(fname)-1] = '\0';
  1399. read_symbols(fname);
  1400. }
  1401. fclose(in);
  1402. }
  1403. #define SZ 500
  1404. /* We first write the generated file into memory using the
  1405. * following helper, then compare to the file on disk and
  1406. * only update the later if anything changed */
  1407. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1408. const char *fmt, ...)
  1409. {
  1410. char tmp[SZ];
  1411. int len;
  1412. va_list ap;
  1413. va_start(ap, fmt);
  1414. len = vsnprintf(tmp, SZ, fmt, ap);
  1415. buf_write(buf, tmp, len);
  1416. va_end(ap);
  1417. }
  1418. void buf_write(struct buffer *buf, const char *s, int len)
  1419. {
  1420. if (buf->size - buf->pos < len) {
  1421. buf->size += len + SZ;
  1422. buf->p = xrealloc(buf->p, buf->size);
  1423. }
  1424. strncpy(buf->p + buf->pos, s, len);
  1425. buf->pos += len;
  1426. }
  1427. static void check_exports(struct module *mod)
  1428. {
  1429. struct symbol *s, *exp;
  1430. list_for_each_entry(s, &mod->unresolved_symbols, list) {
  1431. const char *basename;
  1432. exp = find_symbol(s->name);
  1433. if (!exp) {
  1434. if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
  1435. modpost_log(!warn_unresolved,
  1436. "\"%s\" [%s.ko] undefined!\n",
  1437. s->name, mod->name);
  1438. continue;
  1439. }
  1440. if (exp->module == mod) {
  1441. error("\"%s\" [%s.ko] was exported without definition\n",
  1442. s->name, mod->name);
  1443. continue;
  1444. }
  1445. exp->used = true;
  1446. s->module = exp->module;
  1447. s->crc_valid = exp->crc_valid;
  1448. s->crc = exp->crc;
  1449. basename = strrchr(mod->name, '/');
  1450. if (basename)
  1451. basename++;
  1452. else
  1453. basename = mod->name;
  1454. if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) {
  1455. modpost_log(!allow_missing_ns_imports,
  1456. "module %s uses symbol %s from namespace %s, but does not import it.\n",
  1457. basename, exp->name, exp->namespace);
  1458. add_namespace(&mod->missing_namespaces, exp->namespace);
  1459. }
  1460. if (!mod->is_gpl_compatible && exp->is_gpl_only)
  1461. error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
  1462. basename, exp->name);
  1463. }
  1464. }
  1465. static void handle_white_list_exports(const char *white_list)
  1466. {
  1467. char *buf, *p, *name;
  1468. buf = read_text_file(white_list);
  1469. p = buf;
  1470. while ((name = strsep(&p, "\n"))) {
  1471. struct symbol *sym = find_symbol(name);
  1472. if (sym)
  1473. sym->used = true;
  1474. }
  1475. free(buf);
  1476. }
  1477. static void check_modname_len(struct module *mod)
  1478. {
  1479. const char *mod_name;
  1480. mod_name = strrchr(mod->name, '/');
  1481. if (mod_name == NULL)
  1482. mod_name = mod->name;
  1483. else
  1484. mod_name++;
  1485. if (strlen(mod_name) >= MODULE_NAME_LEN)
  1486. error("module name is too long [%s.ko]\n", mod->name);
  1487. }
  1488. /**
  1489. * Header for the generated file
  1490. **/
  1491. static void add_header(struct buffer *b, struct module *mod)
  1492. {
  1493. buf_printf(b, "#include <linux/module.h>\n");
  1494. buf_printf(b, "#include <linux/export-internal.h>\n");
  1495. buf_printf(b, "#include <linux/compiler.h>\n");
  1496. buf_printf(b, "\n");
  1497. buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
  1498. buf_printf(b, "\n");
  1499. buf_printf(b, "__visible struct module __this_module\n");
  1500. buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
  1501. buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
  1502. if (mod->has_init)
  1503. buf_printf(b, "\t.init = init_module,\n");
  1504. if (mod->has_cleanup)
  1505. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1506. "\t.exit = cleanup_module,\n"
  1507. "#endif\n");
  1508. buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
  1509. buf_printf(b, "};\n");
  1510. if (!external_module)
  1511. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  1512. if (strstarts(mod->name, "drivers/staging"))
  1513. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  1514. if (strstarts(mod->name, "tools/testing"))
  1515. buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
  1516. }
  1517. static void add_exported_symbols(struct buffer *buf, struct module *mod)
  1518. {
  1519. struct symbol *sym;
  1520. /* generate struct for exported symbols */
  1521. buf_printf(buf, "\n");
  1522. list_for_each_entry(sym, &mod->exported_symbols, list) {
  1523. if (trim_unused_exports && !sym->used)
  1524. continue;
  1525. buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n",
  1526. sym->is_func ? "FUNC" : "DATA", sym->name,
  1527. sym->is_gpl_only ? "_gpl" : "", sym->namespace);
  1528. }
  1529. if (!modversions)
  1530. return;
  1531. /* record CRCs for exported symbols */
  1532. buf_printf(buf, "\n");
  1533. list_for_each_entry(sym, &mod->exported_symbols, list) {
  1534. if (trim_unused_exports && !sym->used)
  1535. continue;
  1536. if (!sym->crc_valid)
  1537. warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
  1538. "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
  1539. sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
  1540. sym->name);
  1541. buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
  1542. sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
  1543. }
  1544. }
  1545. /**
  1546. * Record CRCs for unresolved symbols
  1547. **/
  1548. static void add_versions(struct buffer *b, struct module *mod)
  1549. {
  1550. struct symbol *s;
  1551. if (!modversions)
  1552. return;
  1553. buf_printf(b, "\n");
  1554. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1555. buf_printf(b, "__used __section(\"__versions\") = {\n");
  1556. list_for_each_entry(s, &mod->unresolved_symbols, list) {
  1557. if (!s->module)
  1558. continue;
  1559. if (!s->crc_valid) {
  1560. warn("\"%s\" [%s.ko] has no CRC!\n",
  1561. s->name, mod->name);
  1562. continue;
  1563. }
  1564. if (strlen(s->name) >= MODULE_NAME_LEN) {
  1565. error("too long symbol \"%s\" [%s.ko]\n",
  1566. s->name, mod->name);
  1567. break;
  1568. }
  1569. buf_printf(b, "\t{ %#8x, \"%s\" },\n",
  1570. s->crc, s->name);
  1571. }
  1572. buf_printf(b, "};\n");
  1573. }
  1574. static void add_depends(struct buffer *b, struct module *mod)
  1575. {
  1576. struct symbol *s;
  1577. int first = 1;
  1578. /* Clear ->seen flag of modules that own symbols needed by this. */
  1579. list_for_each_entry(s, &mod->unresolved_symbols, list) {
  1580. if (s->module)
  1581. s->module->seen = s->module->is_vmlinux;
  1582. }
  1583. buf_printf(b, "\n");
  1584. buf_printf(b, "MODULE_INFO(depends, \"");
  1585. list_for_each_entry(s, &mod->unresolved_symbols, list) {
  1586. const char *p;
  1587. if (!s->module)
  1588. continue;
  1589. if (s->module->seen)
  1590. continue;
  1591. s->module->seen = true;
  1592. p = strrchr(s->module->name, '/');
  1593. if (p)
  1594. p++;
  1595. else
  1596. p = s->module->name;
  1597. buf_printf(b, "%s%s", first ? "" : ",", p);
  1598. first = 0;
  1599. }
  1600. buf_printf(b, "\");\n");
  1601. }
  1602. static void add_srcversion(struct buffer *b, struct module *mod)
  1603. {
  1604. if (mod->srcversion[0]) {
  1605. buf_printf(b, "\n");
  1606. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1607. mod->srcversion);
  1608. }
  1609. }
  1610. static void write_buf(struct buffer *b, const char *fname)
  1611. {
  1612. FILE *file;
  1613. if (error_occurred)
  1614. return;
  1615. file = fopen(fname, "w");
  1616. if (!file) {
  1617. perror(fname);
  1618. exit(1);
  1619. }
  1620. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1621. perror(fname);
  1622. exit(1);
  1623. }
  1624. if (fclose(file) != 0) {
  1625. perror(fname);
  1626. exit(1);
  1627. }
  1628. }
  1629. static void write_if_changed(struct buffer *b, const char *fname)
  1630. {
  1631. char *tmp;
  1632. FILE *file;
  1633. struct stat st;
  1634. file = fopen(fname, "r");
  1635. if (!file)
  1636. goto write;
  1637. if (fstat(fileno(file), &st) < 0)
  1638. goto close_write;
  1639. if (st.st_size != b->pos)
  1640. goto close_write;
  1641. tmp = xmalloc(b->pos);
  1642. if (fread(tmp, 1, b->pos, file) != b->pos)
  1643. goto free_write;
  1644. if (memcmp(tmp, b->p, b->pos) != 0)
  1645. goto free_write;
  1646. free(tmp);
  1647. fclose(file);
  1648. return;
  1649. free_write:
  1650. free(tmp);
  1651. close_write:
  1652. fclose(file);
  1653. write:
  1654. write_buf(b, fname);
  1655. }
  1656. static void write_vmlinux_export_c_file(struct module *mod)
  1657. {
  1658. struct buffer buf = { };
  1659. buf_printf(&buf,
  1660. "#include <linux/export-internal.h>\n");
  1661. add_exported_symbols(&buf, mod);
  1662. write_if_changed(&buf, ".vmlinux.export.c");
  1663. free(buf.p);
  1664. }
  1665. /* do sanity checks, and generate *.mod.c file */
  1666. static void write_mod_c_file(struct module *mod)
  1667. {
  1668. struct buffer buf = { };
  1669. char fname[PATH_MAX];
  1670. int ret;
  1671. add_header(&buf, mod);
  1672. add_exported_symbols(&buf, mod);
  1673. add_versions(&buf, mod);
  1674. add_depends(&buf, mod);
  1675. add_moddevtable(&buf, mod);
  1676. add_srcversion(&buf, mod);
  1677. ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
  1678. if (ret >= sizeof(fname)) {
  1679. error("%s: too long path was truncated\n", fname);
  1680. goto free;
  1681. }
  1682. write_if_changed(&buf, fname);
  1683. free:
  1684. free(buf.p);
  1685. }
  1686. /* parse Module.symvers file. line format:
  1687. * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
  1688. **/
  1689. static void read_dump(const char *fname)
  1690. {
  1691. char *buf, *pos, *line;
  1692. buf = read_text_file(fname);
  1693. if (!buf)
  1694. /* No symbol versions, silently ignore */
  1695. return;
  1696. pos = buf;
  1697. while ((line = get_line(&pos))) {
  1698. char *symname, *namespace, *modname, *d, *export;
  1699. unsigned int crc;
  1700. struct module *mod;
  1701. struct symbol *s;
  1702. bool gpl_only;
  1703. if (!(symname = strchr(line, '\t')))
  1704. goto fail;
  1705. *symname++ = '\0';
  1706. if (!(modname = strchr(symname, '\t')))
  1707. goto fail;
  1708. *modname++ = '\0';
  1709. if (!(export = strchr(modname, '\t')))
  1710. goto fail;
  1711. *export++ = '\0';
  1712. if (!(namespace = strchr(export, '\t')))
  1713. goto fail;
  1714. *namespace++ = '\0';
  1715. crc = strtoul(line, &d, 16);
  1716. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1717. goto fail;
  1718. if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
  1719. gpl_only = true;
  1720. } else if (!strcmp(export, "EXPORT_SYMBOL")) {
  1721. gpl_only = false;
  1722. } else {
  1723. error("%s: unknown license %s. skip", symname, export);
  1724. continue;
  1725. }
  1726. mod = find_module(modname);
  1727. if (!mod) {
  1728. mod = new_module(modname, strlen(modname));
  1729. mod->from_dump = true;
  1730. }
  1731. s = sym_add_exported(symname, mod, gpl_only, namespace);
  1732. sym_set_crc(s, crc);
  1733. }
  1734. free(buf);
  1735. return;
  1736. fail:
  1737. free(buf);
  1738. fatal("parse error in symbol dump file\n");
  1739. }
  1740. static void write_dump(const char *fname)
  1741. {
  1742. struct buffer buf = { };
  1743. struct module *mod;
  1744. struct symbol *sym;
  1745. list_for_each_entry(mod, &modules, list) {
  1746. if (mod->from_dump)
  1747. continue;
  1748. list_for_each_entry(sym, &mod->exported_symbols, list) {
  1749. if (trim_unused_exports && !sym->used)
  1750. continue;
  1751. buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
  1752. sym->crc, sym->name, mod->name,
  1753. sym->is_gpl_only ? "_GPL" : "",
  1754. sym->namespace);
  1755. }
  1756. }
  1757. write_buf(&buf, fname);
  1758. free(buf.p);
  1759. }
  1760. static void write_namespace_deps_files(const char *fname)
  1761. {
  1762. struct module *mod;
  1763. struct namespace_list *ns;
  1764. struct buffer ns_deps_buf = {};
  1765. list_for_each_entry(mod, &modules, list) {
  1766. if (mod->from_dump || list_empty(&mod->missing_namespaces))
  1767. continue;
  1768. buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
  1769. list_for_each_entry(ns, &mod->missing_namespaces, list)
  1770. buf_printf(&ns_deps_buf, " %s", ns->namespace);
  1771. buf_printf(&ns_deps_buf, "\n");
  1772. }
  1773. write_if_changed(&ns_deps_buf, fname);
  1774. free(ns_deps_buf.p);
  1775. }
  1776. struct dump_list {
  1777. struct list_head list;
  1778. const char *file;
  1779. };
  1780. static void check_host_endian(void)
  1781. {
  1782. static const union {
  1783. short s;
  1784. char c[2];
  1785. } endian_test = { .c = {0x01, 0x02} };
  1786. switch (endian_test.s) {
  1787. case 0x0102:
  1788. host_is_big_endian = true;
  1789. break;
  1790. case 0x0201:
  1791. host_is_big_endian = false;
  1792. break;
  1793. default:
  1794. fatal("Unknown host endian\n");
  1795. }
  1796. }
  1797. int main(int argc, char **argv)
  1798. {
  1799. struct module *mod;
  1800. char *missing_namespace_deps = NULL;
  1801. char *unused_exports_white_list = NULL;
  1802. char *dump_write = NULL, *files_source = NULL;
  1803. int opt;
  1804. LIST_HEAD(dump_lists);
  1805. struct dump_list *dl, *dl2;
  1806. while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:")) != -1) {
  1807. switch (opt) {
  1808. case 'e':
  1809. external_module = true;
  1810. break;
  1811. case 'i':
  1812. dl = xmalloc(sizeof(*dl));
  1813. dl->file = optarg;
  1814. list_add_tail(&dl->list, &dump_lists);
  1815. break;
  1816. case 'M':
  1817. module_enabled = true;
  1818. break;
  1819. case 'm':
  1820. modversions = true;
  1821. break;
  1822. case 'n':
  1823. ignore_missing_files = true;
  1824. break;
  1825. case 'o':
  1826. dump_write = optarg;
  1827. break;
  1828. case 'a':
  1829. all_versions = true;
  1830. break;
  1831. case 'T':
  1832. files_source = optarg;
  1833. break;
  1834. case 't':
  1835. trim_unused_exports = true;
  1836. break;
  1837. case 'u':
  1838. unused_exports_white_list = optarg;
  1839. break;
  1840. case 'W':
  1841. extra_warn = true;
  1842. break;
  1843. case 'w':
  1844. warn_unresolved = true;
  1845. break;
  1846. case 'E':
  1847. sec_mismatch_warn_only = false;
  1848. break;
  1849. case 'N':
  1850. allow_missing_ns_imports = true;
  1851. break;
  1852. case 'd':
  1853. missing_namespace_deps = optarg;
  1854. break;
  1855. default:
  1856. exit(1);
  1857. }
  1858. }
  1859. check_host_endian();
  1860. list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
  1861. read_dump(dl->file);
  1862. list_del(&dl->list);
  1863. free(dl);
  1864. }
  1865. while (optind < argc)
  1866. read_symbols(argv[optind++]);
  1867. if (files_source)
  1868. read_symbols_from_files(files_source);
  1869. list_for_each_entry(mod, &modules, list) {
  1870. if (mod->from_dump || mod->is_vmlinux)
  1871. continue;
  1872. check_modname_len(mod);
  1873. check_exports(mod);
  1874. }
  1875. if (unused_exports_white_list)
  1876. handle_white_list_exports(unused_exports_white_list);
  1877. list_for_each_entry(mod, &modules, list) {
  1878. if (mod->from_dump)
  1879. continue;
  1880. if (mod->is_vmlinux)
  1881. write_vmlinux_export_c_file(mod);
  1882. else
  1883. write_mod_c_file(mod);
  1884. }
  1885. if (missing_namespace_deps)
  1886. write_namespace_deps_files(missing_namespace_deps);
  1887. if (dump_write)
  1888. write_dump(dump_write);
  1889. if (sec_mismatch_count && !sec_mismatch_warn_only)
  1890. error("Section mismatches detected.\n"
  1891. "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
  1892. if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
  1893. warn("suppressed %u unresolved symbol warnings because there were too many)\n",
  1894. nr_unresolved - MAX_UNRESOLVED_REPORTS);
  1895. return error_occurred ? 1 : 0;
  1896. }