modpost.c 60 KB

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