modpost.c 59 KB

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