module.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730
  1. /*
  2. Copyright (C) 2002 Richard Henderson
  3. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/init.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/elf.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/capability.h>
  30. #include <linux/cpu.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/errno.h>
  33. #include <linux/err.h>
  34. #include <linux/vermagic.h>
  35. #include <linux/notifier.h>
  36. #include <linux/sched.h>
  37. #include <linux/stop_machine.h>
  38. #include <linux/device.h>
  39. #include <linux/string.h>
  40. #include <linux/mutex.h>
  41. #include <linux/unwind.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/cacheflush.h>
  44. #include <linux/license.h>
  45. #include <asm/sections.h>
  46. #if 0
  47. #define DEBUGP printk
  48. #else
  49. #define DEBUGP(fmt , a...)
  50. #endif
  51. #ifndef ARCH_SHF_SMALL
  52. #define ARCH_SHF_SMALL 0
  53. #endif
  54. /* If this is set, the section belongs in the init part of the module */
  55. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  56. /* List of modules, protected by module_mutex or preempt_disable
  57. * (add/delete uses stop_machine). */
  58. static DEFINE_MUTEX(module_mutex);
  59. static LIST_HEAD(modules);
  60. /* Waiting for a module to finish initializing? */
  61. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  62. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  63. /* Bounds of module allocation, for speeding __module_text_address */
  64. static unsigned long module_addr_min = -1UL, module_addr_max = 0;
  65. int register_module_notifier(struct notifier_block * nb)
  66. {
  67. return blocking_notifier_chain_register(&module_notify_list, nb);
  68. }
  69. EXPORT_SYMBOL(register_module_notifier);
  70. int unregister_module_notifier(struct notifier_block * nb)
  71. {
  72. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  73. }
  74. EXPORT_SYMBOL(unregister_module_notifier);
  75. /* We require a truly strong try_module_get(): 0 means failure due to
  76. ongoing or failed initialization etc. */
  77. static inline int strong_try_module_get(struct module *mod)
  78. {
  79. if (mod && mod->state == MODULE_STATE_COMING)
  80. return -EBUSY;
  81. if (try_module_get(mod))
  82. return 0;
  83. else
  84. return -ENOENT;
  85. }
  86. static inline void add_taint_module(struct module *mod, unsigned flag)
  87. {
  88. add_taint(flag);
  89. mod->taints |= flag;
  90. }
  91. /*
  92. * A thread that wants to hold a reference to a module only while it
  93. * is running can call this to safely exit. nfsd and lockd use this.
  94. */
  95. void __module_put_and_exit(struct module *mod, long code)
  96. {
  97. module_put(mod);
  98. do_exit(code);
  99. }
  100. EXPORT_SYMBOL(__module_put_and_exit);
  101. /* Find a module section: 0 means not found. */
  102. static unsigned int find_sec(Elf_Ehdr *hdr,
  103. Elf_Shdr *sechdrs,
  104. const char *secstrings,
  105. const char *name)
  106. {
  107. unsigned int i;
  108. for (i = 1; i < hdr->e_shnum; i++)
  109. /* Alloc bit cleared means "ignore it." */
  110. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  111. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  112. return i;
  113. return 0;
  114. }
  115. /* Provided by the linker */
  116. extern const struct kernel_symbol __start___ksymtab[];
  117. extern const struct kernel_symbol __stop___ksymtab[];
  118. extern const struct kernel_symbol __start___ksymtab_gpl[];
  119. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  120. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  121. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  122. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  123. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  124. extern const unsigned long __start___kcrctab[];
  125. extern const unsigned long __start___kcrctab_gpl[];
  126. extern const unsigned long __start___kcrctab_gpl_future[];
  127. #ifdef CONFIG_UNUSED_SYMBOLS
  128. extern const struct kernel_symbol __start___ksymtab_unused[];
  129. extern const struct kernel_symbol __stop___ksymtab_unused[];
  130. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  131. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  132. extern const unsigned long __start___kcrctab_unused[];
  133. extern const unsigned long __start___kcrctab_unused_gpl[];
  134. #endif
  135. #ifndef CONFIG_MODVERSIONS
  136. #define symversion(base, idx) NULL
  137. #else
  138. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  139. #endif
  140. struct symsearch {
  141. const struct kernel_symbol *start, *stop;
  142. const unsigned long *crcs;
  143. enum {
  144. NOT_GPL_ONLY,
  145. GPL_ONLY,
  146. WILL_BE_GPL_ONLY,
  147. } licence;
  148. bool unused;
  149. };
  150. static bool each_symbol_in_section(const struct symsearch *arr,
  151. unsigned int arrsize,
  152. struct module *owner,
  153. bool (*fn)(const struct symsearch *syms,
  154. struct module *owner,
  155. unsigned int symnum, void *data),
  156. void *data)
  157. {
  158. unsigned int i, j;
  159. for (j = 0; j < arrsize; j++) {
  160. for (i = 0; i < arr[j].stop - arr[j].start; i++)
  161. if (fn(&arr[j], owner, i, data))
  162. return true;
  163. }
  164. return false;
  165. }
  166. /* Returns true as soon as fn returns true, otherwise false. */
  167. static bool each_symbol(bool (*fn)(const struct symsearch *arr,
  168. struct module *owner,
  169. unsigned int symnum, void *data),
  170. void *data)
  171. {
  172. struct module *mod;
  173. const struct symsearch arr[] = {
  174. { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
  175. NOT_GPL_ONLY, false },
  176. { __start___ksymtab_gpl, __stop___ksymtab_gpl,
  177. __start___kcrctab_gpl,
  178. GPL_ONLY, false },
  179. { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
  180. __start___kcrctab_gpl_future,
  181. WILL_BE_GPL_ONLY, false },
  182. #ifdef CONFIG_UNUSED_SYMBOLS
  183. { __start___ksymtab_unused, __stop___ksymtab_unused,
  184. __start___kcrctab_unused,
  185. NOT_GPL_ONLY, true },
  186. { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
  187. __start___kcrctab_unused_gpl,
  188. GPL_ONLY, true },
  189. #endif
  190. };
  191. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
  192. return true;
  193. list_for_each_entry(mod, &modules, list) {
  194. struct symsearch arr[] = {
  195. { mod->syms, mod->syms + mod->num_syms, mod->crcs,
  196. NOT_GPL_ONLY, false },
  197. { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
  198. mod->gpl_crcs,
  199. GPL_ONLY, false },
  200. { mod->gpl_future_syms,
  201. mod->gpl_future_syms + mod->num_gpl_future_syms,
  202. mod->gpl_future_crcs,
  203. WILL_BE_GPL_ONLY, false },
  204. #ifdef CONFIG_UNUSED_SYMBOLS
  205. { mod->unused_syms,
  206. mod->unused_syms + mod->num_unused_syms,
  207. mod->unused_crcs,
  208. NOT_GPL_ONLY, true },
  209. { mod->unused_gpl_syms,
  210. mod->unused_gpl_syms + mod->num_unused_gpl_syms,
  211. mod->unused_gpl_crcs,
  212. GPL_ONLY, true },
  213. #endif
  214. };
  215. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
  216. return true;
  217. }
  218. return false;
  219. }
  220. struct find_symbol_arg {
  221. /* Input */
  222. const char *name;
  223. bool gplok;
  224. bool warn;
  225. /* Output */
  226. struct module *owner;
  227. const unsigned long *crc;
  228. unsigned long value;
  229. };
  230. static bool find_symbol_in_section(const struct symsearch *syms,
  231. struct module *owner,
  232. unsigned int symnum, void *data)
  233. {
  234. struct find_symbol_arg *fsa = data;
  235. if (strcmp(syms->start[symnum].name, fsa->name) != 0)
  236. return false;
  237. if (!fsa->gplok) {
  238. if (syms->licence == GPL_ONLY)
  239. return false;
  240. if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
  241. printk(KERN_WARNING "Symbol %s is being used "
  242. "by a non-GPL module, which will not "
  243. "be allowed in the future\n", fsa->name);
  244. printk(KERN_WARNING "Please see the file "
  245. "Documentation/feature-removal-schedule.txt "
  246. "in the kernel source tree for more details.\n");
  247. }
  248. }
  249. #ifdef CONFIG_UNUSED_SYMBOLS
  250. if (syms->unused && fsa->warn) {
  251. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  252. "however this module is using it.\n", fsa->name);
  253. printk(KERN_WARNING
  254. "This symbol will go away in the future.\n");
  255. printk(KERN_WARNING
  256. "Please evalute if this is the right api to use and if "
  257. "it really is, submit a report the linux kernel "
  258. "mailinglist together with submitting your code for "
  259. "inclusion.\n");
  260. }
  261. #endif
  262. fsa->owner = owner;
  263. fsa->crc = symversion(syms->crcs, symnum);
  264. fsa->value = syms->start[symnum].value;
  265. return true;
  266. }
  267. /* Find a symbol, return value, (optional) crc and (optional) module
  268. * which owns it */
  269. static unsigned long find_symbol(const char *name,
  270. struct module **owner,
  271. const unsigned long **crc,
  272. bool gplok,
  273. bool warn)
  274. {
  275. struct find_symbol_arg fsa;
  276. fsa.name = name;
  277. fsa.gplok = gplok;
  278. fsa.warn = warn;
  279. if (each_symbol(find_symbol_in_section, &fsa)) {
  280. if (owner)
  281. *owner = fsa.owner;
  282. if (crc)
  283. *crc = fsa.crc;
  284. return fsa.value;
  285. }
  286. DEBUGP("Failed to find symbol %s\n", name);
  287. return -ENOENT;
  288. }
  289. /* Search for module by name: must hold module_mutex. */
  290. static struct module *find_module(const char *name)
  291. {
  292. struct module *mod;
  293. list_for_each_entry(mod, &modules, list) {
  294. if (strcmp(mod->name, name) == 0)
  295. return mod;
  296. }
  297. return NULL;
  298. }
  299. #ifdef CONFIG_SMP
  300. /* Number of blocks used and allocated. */
  301. static unsigned int pcpu_num_used, pcpu_num_allocated;
  302. /* Size of each block. -ve means used. */
  303. static int *pcpu_size;
  304. static int split_block(unsigned int i, unsigned short size)
  305. {
  306. /* Reallocation required? */
  307. if (pcpu_num_used + 1 > pcpu_num_allocated) {
  308. int *new;
  309. new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
  310. GFP_KERNEL);
  311. if (!new)
  312. return 0;
  313. pcpu_num_allocated *= 2;
  314. pcpu_size = new;
  315. }
  316. /* Insert a new subblock */
  317. memmove(&pcpu_size[i+1], &pcpu_size[i],
  318. sizeof(pcpu_size[0]) * (pcpu_num_used - i));
  319. pcpu_num_used++;
  320. pcpu_size[i+1] -= size;
  321. pcpu_size[i] = size;
  322. return 1;
  323. }
  324. static inline unsigned int block_size(int val)
  325. {
  326. if (val < 0)
  327. return -val;
  328. return val;
  329. }
  330. static void *percpu_modalloc(unsigned long size, unsigned long align,
  331. const char *name)
  332. {
  333. unsigned long extra;
  334. unsigned int i;
  335. void *ptr;
  336. if (align > PAGE_SIZE) {
  337. printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
  338. name, align, PAGE_SIZE);
  339. align = PAGE_SIZE;
  340. }
  341. ptr = __per_cpu_start;
  342. for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  343. /* Extra for alignment requirement. */
  344. extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
  345. BUG_ON(i == 0 && extra != 0);
  346. if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
  347. continue;
  348. /* Transfer extra to previous block. */
  349. if (pcpu_size[i-1] < 0)
  350. pcpu_size[i-1] -= extra;
  351. else
  352. pcpu_size[i-1] += extra;
  353. pcpu_size[i] -= extra;
  354. ptr += extra;
  355. /* Split block if warranted */
  356. if (pcpu_size[i] - size > sizeof(unsigned long))
  357. if (!split_block(i, size))
  358. return NULL;
  359. /* Mark allocated */
  360. pcpu_size[i] = -pcpu_size[i];
  361. return ptr;
  362. }
  363. printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
  364. size);
  365. return NULL;
  366. }
  367. static void percpu_modfree(void *freeme)
  368. {
  369. unsigned int i;
  370. void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
  371. /* First entry is core kernel percpu data. */
  372. for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  373. if (ptr == freeme) {
  374. pcpu_size[i] = -pcpu_size[i];
  375. goto free;
  376. }
  377. }
  378. BUG();
  379. free:
  380. /* Merge with previous? */
  381. if (pcpu_size[i-1] >= 0) {
  382. pcpu_size[i-1] += pcpu_size[i];
  383. pcpu_num_used--;
  384. memmove(&pcpu_size[i], &pcpu_size[i+1],
  385. (pcpu_num_used - i) * sizeof(pcpu_size[0]));
  386. i--;
  387. }
  388. /* Merge with next? */
  389. if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
  390. pcpu_size[i] += pcpu_size[i+1];
  391. pcpu_num_used--;
  392. memmove(&pcpu_size[i+1], &pcpu_size[i+2],
  393. (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
  394. }
  395. }
  396. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  397. Elf_Shdr *sechdrs,
  398. const char *secstrings)
  399. {
  400. return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
  401. }
  402. static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
  403. {
  404. int cpu;
  405. for_each_possible_cpu(cpu)
  406. memcpy(pcpudest + per_cpu_offset(cpu), from, size);
  407. }
  408. static int percpu_modinit(void)
  409. {
  410. pcpu_num_used = 2;
  411. pcpu_num_allocated = 2;
  412. pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
  413. GFP_KERNEL);
  414. /* Static in-kernel percpu data (used). */
  415. pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
  416. /* Free room. */
  417. pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
  418. if (pcpu_size[1] < 0) {
  419. printk(KERN_ERR "No per-cpu room for modules.\n");
  420. pcpu_num_used = 1;
  421. }
  422. return 0;
  423. }
  424. __initcall(percpu_modinit);
  425. #else /* ... !CONFIG_SMP */
  426. static inline void *percpu_modalloc(unsigned long size, unsigned long align,
  427. const char *name)
  428. {
  429. return NULL;
  430. }
  431. static inline void percpu_modfree(void *pcpuptr)
  432. {
  433. BUG();
  434. }
  435. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  436. Elf_Shdr *sechdrs,
  437. const char *secstrings)
  438. {
  439. return 0;
  440. }
  441. static inline void percpu_modcopy(void *pcpudst, const void *src,
  442. unsigned long size)
  443. {
  444. /* pcpusec should be 0, and size of that section should be 0. */
  445. BUG_ON(size != 0);
  446. }
  447. #endif /* CONFIG_SMP */
  448. #define MODINFO_ATTR(field) \
  449. static void setup_modinfo_##field(struct module *mod, const char *s) \
  450. { \
  451. mod->field = kstrdup(s, GFP_KERNEL); \
  452. } \
  453. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  454. struct module *mod, char *buffer) \
  455. { \
  456. return sprintf(buffer, "%s\n", mod->field); \
  457. } \
  458. static int modinfo_##field##_exists(struct module *mod) \
  459. { \
  460. return mod->field != NULL; \
  461. } \
  462. static void free_modinfo_##field(struct module *mod) \
  463. { \
  464. kfree(mod->field); \
  465. mod->field = NULL; \
  466. } \
  467. static struct module_attribute modinfo_##field = { \
  468. .attr = { .name = __stringify(field), .mode = 0444 }, \
  469. .show = show_modinfo_##field, \
  470. .setup = setup_modinfo_##field, \
  471. .test = modinfo_##field##_exists, \
  472. .free = free_modinfo_##field, \
  473. };
  474. MODINFO_ATTR(version);
  475. MODINFO_ATTR(srcversion);
  476. static char last_unloaded_module[MODULE_NAME_LEN+1];
  477. #ifdef CONFIG_MODULE_UNLOAD
  478. /* Init the unload section of the module. */
  479. static void module_unload_init(struct module *mod)
  480. {
  481. unsigned int i;
  482. INIT_LIST_HEAD(&mod->modules_which_use_me);
  483. for (i = 0; i < NR_CPUS; i++)
  484. local_set(&mod->ref[i].count, 0);
  485. /* Hold reference count during initialization. */
  486. local_set(&mod->ref[raw_smp_processor_id()].count, 1);
  487. /* Backwards compatibility macros put refcount during init. */
  488. mod->waiter = current;
  489. }
  490. /* modules using other modules */
  491. struct module_use
  492. {
  493. struct list_head list;
  494. struct module *module_which_uses;
  495. };
  496. /* Does a already use b? */
  497. static int already_uses(struct module *a, struct module *b)
  498. {
  499. struct module_use *use;
  500. list_for_each_entry(use, &b->modules_which_use_me, list) {
  501. if (use->module_which_uses == a) {
  502. DEBUGP("%s uses %s!\n", a->name, b->name);
  503. return 1;
  504. }
  505. }
  506. DEBUGP("%s does not use %s!\n", a->name, b->name);
  507. return 0;
  508. }
  509. /* Module a uses b */
  510. static int use_module(struct module *a, struct module *b)
  511. {
  512. struct module_use *use;
  513. int no_warn, err;
  514. if (b == NULL || already_uses(a, b)) return 1;
  515. /* If we're interrupted or time out, we fail. */
  516. if (wait_event_interruptible_timeout(
  517. module_wq, (err = strong_try_module_get(b)) != -EBUSY,
  518. 30 * HZ) <= 0) {
  519. printk("%s: gave up waiting for init of module %s.\n",
  520. a->name, b->name);
  521. return 0;
  522. }
  523. /* If strong_try_module_get() returned a different error, we fail. */
  524. if (err)
  525. return 0;
  526. DEBUGP("Allocating new usage for %s.\n", a->name);
  527. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  528. if (!use) {
  529. printk("%s: out of memory loading\n", a->name);
  530. module_put(b);
  531. return 0;
  532. }
  533. use->module_which_uses = a;
  534. list_add(&use->list, &b->modules_which_use_me);
  535. no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
  536. return 1;
  537. }
  538. /* Clear the unload stuff of the module. */
  539. static void module_unload_free(struct module *mod)
  540. {
  541. struct module *i;
  542. list_for_each_entry(i, &modules, list) {
  543. struct module_use *use;
  544. list_for_each_entry(use, &i->modules_which_use_me, list) {
  545. if (use->module_which_uses == mod) {
  546. DEBUGP("%s unusing %s\n", mod->name, i->name);
  547. module_put(i);
  548. list_del(&use->list);
  549. kfree(use);
  550. sysfs_remove_link(i->holders_dir, mod->name);
  551. /* There can be at most one match. */
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  558. static inline int try_force_unload(unsigned int flags)
  559. {
  560. int ret = (flags & O_TRUNC);
  561. if (ret)
  562. add_taint(TAINT_FORCED_RMMOD);
  563. return ret;
  564. }
  565. #else
  566. static inline int try_force_unload(unsigned int flags)
  567. {
  568. return 0;
  569. }
  570. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  571. struct stopref
  572. {
  573. struct module *mod;
  574. int flags;
  575. int *forced;
  576. };
  577. /* Whole machine is stopped with interrupts off when this runs. */
  578. static int __try_stop_module(void *_sref)
  579. {
  580. struct stopref *sref = _sref;
  581. /* If it's not unused, quit unless we're forcing. */
  582. if (module_refcount(sref->mod) != 0) {
  583. if (!(*sref->forced = try_force_unload(sref->flags)))
  584. return -EWOULDBLOCK;
  585. }
  586. /* Mark it as dying. */
  587. sref->mod->state = MODULE_STATE_GOING;
  588. return 0;
  589. }
  590. static int try_stop_module(struct module *mod, int flags, int *forced)
  591. {
  592. if (flags & O_NONBLOCK) {
  593. struct stopref sref = { mod, flags, forced };
  594. return stop_machine(__try_stop_module, &sref, NULL);
  595. } else {
  596. /* We don't need to stop the machine for this. */
  597. mod->state = MODULE_STATE_GOING;
  598. synchronize_sched();
  599. return 0;
  600. }
  601. }
  602. unsigned int module_refcount(struct module *mod)
  603. {
  604. unsigned int i, total = 0;
  605. for (i = 0; i < NR_CPUS; i++)
  606. total += local_read(&mod->ref[i].count);
  607. return total;
  608. }
  609. EXPORT_SYMBOL(module_refcount);
  610. /* This exists whether we can unload or not */
  611. static void free_module(struct module *mod);
  612. static void wait_for_zero_refcount(struct module *mod)
  613. {
  614. /* Since we might sleep for some time, release the mutex first */
  615. mutex_unlock(&module_mutex);
  616. for (;;) {
  617. DEBUGP("Looking at refcount...\n");
  618. set_current_state(TASK_UNINTERRUPTIBLE);
  619. if (module_refcount(mod) == 0)
  620. break;
  621. schedule();
  622. }
  623. current->state = TASK_RUNNING;
  624. mutex_lock(&module_mutex);
  625. }
  626. asmlinkage long
  627. sys_delete_module(const char __user *name_user, unsigned int flags)
  628. {
  629. struct module *mod;
  630. char name[MODULE_NAME_LEN];
  631. int ret, forced = 0;
  632. if (!capable(CAP_SYS_MODULE))
  633. return -EPERM;
  634. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  635. return -EFAULT;
  636. name[MODULE_NAME_LEN-1] = '\0';
  637. if (mutex_lock_interruptible(&module_mutex) != 0)
  638. return -EINTR;
  639. mod = find_module(name);
  640. if (!mod) {
  641. ret = -ENOENT;
  642. goto out;
  643. }
  644. if (!list_empty(&mod->modules_which_use_me)) {
  645. /* Other modules depend on us: get rid of them first. */
  646. ret = -EWOULDBLOCK;
  647. goto out;
  648. }
  649. /* Doing init or already dying? */
  650. if (mod->state != MODULE_STATE_LIVE) {
  651. /* FIXME: if (force), slam module count and wake up
  652. waiter --RR */
  653. DEBUGP("%s already dying\n", mod->name);
  654. ret = -EBUSY;
  655. goto out;
  656. }
  657. /* If it has an init func, it must have an exit func to unload */
  658. if (mod->init && !mod->exit) {
  659. forced = try_force_unload(flags);
  660. if (!forced) {
  661. /* This module can't be removed */
  662. ret = -EBUSY;
  663. goto out;
  664. }
  665. }
  666. /* Set this up before setting mod->state */
  667. mod->waiter = current;
  668. /* Stop the machine so refcounts can't move and disable module. */
  669. ret = try_stop_module(mod, flags, &forced);
  670. if (ret != 0)
  671. goto out;
  672. /* Never wait if forced. */
  673. if (!forced && module_refcount(mod) != 0)
  674. wait_for_zero_refcount(mod);
  675. mutex_unlock(&module_mutex);
  676. /* Final destruction now noone is using it. */
  677. if (mod->exit != NULL)
  678. mod->exit();
  679. blocking_notifier_call_chain(&module_notify_list,
  680. MODULE_STATE_GOING, mod);
  681. mutex_lock(&module_mutex);
  682. /* Store the name of the last unloaded module for diagnostic purposes */
  683. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  684. free_module(mod);
  685. out:
  686. mutex_unlock(&module_mutex);
  687. return ret;
  688. }
  689. static void print_unload_info(struct seq_file *m, struct module *mod)
  690. {
  691. struct module_use *use;
  692. int printed_something = 0;
  693. seq_printf(m, " %u ", module_refcount(mod));
  694. /* Always include a trailing , so userspace can differentiate
  695. between this and the old multi-field proc format. */
  696. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  697. printed_something = 1;
  698. seq_printf(m, "%s,", use->module_which_uses->name);
  699. }
  700. if (mod->init != NULL && mod->exit == NULL) {
  701. printed_something = 1;
  702. seq_printf(m, "[permanent],");
  703. }
  704. if (!printed_something)
  705. seq_printf(m, "-");
  706. }
  707. void __symbol_put(const char *symbol)
  708. {
  709. struct module *owner;
  710. preempt_disable();
  711. if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
  712. BUG();
  713. module_put(owner);
  714. preempt_enable();
  715. }
  716. EXPORT_SYMBOL(__symbol_put);
  717. void symbol_put_addr(void *addr)
  718. {
  719. struct module *modaddr;
  720. if (core_kernel_text((unsigned long)addr))
  721. return;
  722. if (!(modaddr = module_text_address((unsigned long)addr)))
  723. BUG();
  724. module_put(modaddr);
  725. }
  726. EXPORT_SYMBOL_GPL(symbol_put_addr);
  727. static ssize_t show_refcnt(struct module_attribute *mattr,
  728. struct module *mod, char *buffer)
  729. {
  730. return sprintf(buffer, "%u\n", module_refcount(mod));
  731. }
  732. static struct module_attribute refcnt = {
  733. .attr = { .name = "refcnt", .mode = 0444 },
  734. .show = show_refcnt,
  735. };
  736. void module_put(struct module *module)
  737. {
  738. if (module) {
  739. unsigned int cpu = get_cpu();
  740. local_dec(&module->ref[cpu].count);
  741. /* Maybe they're waiting for us to drop reference? */
  742. if (unlikely(!module_is_live(module)))
  743. wake_up_process(module->waiter);
  744. put_cpu();
  745. }
  746. }
  747. EXPORT_SYMBOL(module_put);
  748. #else /* !CONFIG_MODULE_UNLOAD */
  749. static void print_unload_info(struct seq_file *m, struct module *mod)
  750. {
  751. /* We don't know the usage count, or what modules are using. */
  752. seq_printf(m, " - -");
  753. }
  754. static inline void module_unload_free(struct module *mod)
  755. {
  756. }
  757. static inline int use_module(struct module *a, struct module *b)
  758. {
  759. return strong_try_module_get(b) == 0;
  760. }
  761. static inline void module_unload_init(struct module *mod)
  762. {
  763. }
  764. #endif /* CONFIG_MODULE_UNLOAD */
  765. static ssize_t show_initstate(struct module_attribute *mattr,
  766. struct module *mod, char *buffer)
  767. {
  768. const char *state = "unknown";
  769. switch (mod->state) {
  770. case MODULE_STATE_LIVE:
  771. state = "live";
  772. break;
  773. case MODULE_STATE_COMING:
  774. state = "coming";
  775. break;
  776. case MODULE_STATE_GOING:
  777. state = "going";
  778. break;
  779. }
  780. return sprintf(buffer, "%s\n", state);
  781. }
  782. static struct module_attribute initstate = {
  783. .attr = { .name = "initstate", .mode = 0444 },
  784. .show = show_initstate,
  785. };
  786. static struct module_attribute *modinfo_attrs[] = {
  787. &modinfo_version,
  788. &modinfo_srcversion,
  789. &initstate,
  790. #ifdef CONFIG_MODULE_UNLOAD
  791. &refcnt,
  792. #endif
  793. NULL,
  794. };
  795. static const char vermagic[] = VERMAGIC_STRING;
  796. static int try_to_force_load(struct module *mod, const char *symname)
  797. {
  798. #ifdef CONFIG_MODULE_FORCE_LOAD
  799. if (!(tainted & TAINT_FORCED_MODULE))
  800. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  801. mod->name, symname);
  802. add_taint_module(mod, TAINT_FORCED_MODULE);
  803. return 0;
  804. #else
  805. return -ENOEXEC;
  806. #endif
  807. }
  808. #ifdef CONFIG_MODVERSIONS
  809. static int check_version(Elf_Shdr *sechdrs,
  810. unsigned int versindex,
  811. const char *symname,
  812. struct module *mod,
  813. const unsigned long *crc)
  814. {
  815. unsigned int i, num_versions;
  816. struct modversion_info *versions;
  817. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  818. if (!crc)
  819. return 1;
  820. /* No versions at all? modprobe --force does this. */
  821. if (versindex == 0)
  822. return try_to_force_load(mod, symname) == 0;
  823. versions = (void *) sechdrs[versindex].sh_addr;
  824. num_versions = sechdrs[versindex].sh_size
  825. / sizeof(struct modversion_info);
  826. for (i = 0; i < num_versions; i++) {
  827. if (strcmp(versions[i].name, symname) != 0)
  828. continue;
  829. if (versions[i].crc == *crc)
  830. return 1;
  831. DEBUGP("Found checksum %lX vs module %lX\n",
  832. *crc, versions[i].crc);
  833. goto bad_version;
  834. }
  835. printk(KERN_WARNING "%s: no symbol version for %s\n",
  836. mod->name, symname);
  837. return 0;
  838. bad_version:
  839. printk("%s: disagrees about version of symbol %s\n",
  840. mod->name, symname);
  841. return 0;
  842. }
  843. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  844. unsigned int versindex,
  845. struct module *mod)
  846. {
  847. const unsigned long *crc;
  848. if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
  849. BUG();
  850. return check_version(sechdrs, versindex, "struct_module", mod, crc);
  851. }
  852. /* First part is kernel version, which we ignore if module has crcs. */
  853. static inline int same_magic(const char *amagic, const char *bmagic,
  854. bool has_crcs)
  855. {
  856. if (has_crcs) {
  857. amagic += strcspn(amagic, " ");
  858. bmagic += strcspn(bmagic, " ");
  859. }
  860. return strcmp(amagic, bmagic) == 0;
  861. }
  862. #else
  863. static inline int check_version(Elf_Shdr *sechdrs,
  864. unsigned int versindex,
  865. const char *symname,
  866. struct module *mod,
  867. const unsigned long *crc)
  868. {
  869. return 1;
  870. }
  871. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  872. unsigned int versindex,
  873. struct module *mod)
  874. {
  875. return 1;
  876. }
  877. static inline int same_magic(const char *amagic, const char *bmagic,
  878. bool has_crcs)
  879. {
  880. return strcmp(amagic, bmagic) == 0;
  881. }
  882. #endif /* CONFIG_MODVERSIONS */
  883. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  884. Must be holding module_mutex. */
  885. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  886. unsigned int versindex,
  887. const char *name,
  888. struct module *mod)
  889. {
  890. struct module *owner;
  891. unsigned long ret;
  892. const unsigned long *crc;
  893. ret = find_symbol(name, &owner, &crc,
  894. !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
  895. if (!IS_ERR_VALUE(ret)) {
  896. /* use_module can fail due to OOM,
  897. or module initialization or unloading */
  898. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  899. !use_module(mod, owner))
  900. ret = -EINVAL;
  901. }
  902. return ret;
  903. }
  904. /*
  905. * /sys/module/foo/sections stuff
  906. * J. Corbet <corbet@lwn.net>
  907. */
  908. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  909. struct module_sect_attr
  910. {
  911. struct module_attribute mattr;
  912. char *name;
  913. unsigned long address;
  914. };
  915. struct module_sect_attrs
  916. {
  917. struct attribute_group grp;
  918. unsigned int nsections;
  919. struct module_sect_attr attrs[0];
  920. };
  921. static ssize_t module_sect_show(struct module_attribute *mattr,
  922. struct module *mod, char *buf)
  923. {
  924. struct module_sect_attr *sattr =
  925. container_of(mattr, struct module_sect_attr, mattr);
  926. return sprintf(buf, "0x%lx\n", sattr->address);
  927. }
  928. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  929. {
  930. unsigned int section;
  931. for (section = 0; section < sect_attrs->nsections; section++)
  932. kfree(sect_attrs->attrs[section].name);
  933. kfree(sect_attrs);
  934. }
  935. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  936. char *secstrings, Elf_Shdr *sechdrs)
  937. {
  938. unsigned int nloaded = 0, i, size[2];
  939. struct module_sect_attrs *sect_attrs;
  940. struct module_sect_attr *sattr;
  941. struct attribute **gattr;
  942. /* Count loaded sections and allocate structures */
  943. for (i = 0; i < nsect; i++)
  944. if (sechdrs[i].sh_flags & SHF_ALLOC)
  945. nloaded++;
  946. size[0] = ALIGN(sizeof(*sect_attrs)
  947. + nloaded * sizeof(sect_attrs->attrs[0]),
  948. sizeof(sect_attrs->grp.attrs[0]));
  949. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  950. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  951. if (sect_attrs == NULL)
  952. return;
  953. /* Setup section attributes. */
  954. sect_attrs->grp.name = "sections";
  955. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  956. sect_attrs->nsections = 0;
  957. sattr = &sect_attrs->attrs[0];
  958. gattr = &sect_attrs->grp.attrs[0];
  959. for (i = 0; i < nsect; i++) {
  960. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  961. continue;
  962. sattr->address = sechdrs[i].sh_addr;
  963. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  964. GFP_KERNEL);
  965. if (sattr->name == NULL)
  966. goto out;
  967. sect_attrs->nsections++;
  968. sattr->mattr.show = module_sect_show;
  969. sattr->mattr.store = NULL;
  970. sattr->mattr.attr.name = sattr->name;
  971. sattr->mattr.attr.mode = S_IRUGO;
  972. *(gattr++) = &(sattr++)->mattr.attr;
  973. }
  974. *gattr = NULL;
  975. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  976. goto out;
  977. mod->sect_attrs = sect_attrs;
  978. return;
  979. out:
  980. free_sect_attrs(sect_attrs);
  981. }
  982. static void remove_sect_attrs(struct module *mod)
  983. {
  984. if (mod->sect_attrs) {
  985. sysfs_remove_group(&mod->mkobj.kobj,
  986. &mod->sect_attrs->grp);
  987. /* We are positive that no one is using any sect attrs
  988. * at this point. Deallocate immediately. */
  989. free_sect_attrs(mod->sect_attrs);
  990. mod->sect_attrs = NULL;
  991. }
  992. }
  993. /*
  994. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  995. */
  996. struct module_notes_attrs {
  997. struct kobject *dir;
  998. unsigned int notes;
  999. struct bin_attribute attrs[0];
  1000. };
  1001. static ssize_t module_notes_read(struct kobject *kobj,
  1002. struct bin_attribute *bin_attr,
  1003. char *buf, loff_t pos, size_t count)
  1004. {
  1005. /*
  1006. * The caller checked the pos and count against our size.
  1007. */
  1008. memcpy(buf, bin_attr->private + pos, count);
  1009. return count;
  1010. }
  1011. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1012. unsigned int i)
  1013. {
  1014. if (notes_attrs->dir) {
  1015. while (i-- > 0)
  1016. sysfs_remove_bin_file(notes_attrs->dir,
  1017. &notes_attrs->attrs[i]);
  1018. kobject_del(notes_attrs->dir);
  1019. }
  1020. kfree(notes_attrs);
  1021. }
  1022. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  1023. char *secstrings, Elf_Shdr *sechdrs)
  1024. {
  1025. unsigned int notes, loaded, i;
  1026. struct module_notes_attrs *notes_attrs;
  1027. struct bin_attribute *nattr;
  1028. /* Count notes sections and allocate structures. */
  1029. notes = 0;
  1030. for (i = 0; i < nsect; i++)
  1031. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  1032. (sechdrs[i].sh_type == SHT_NOTE))
  1033. ++notes;
  1034. if (notes == 0)
  1035. return;
  1036. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1037. + notes * sizeof(notes_attrs->attrs[0]),
  1038. GFP_KERNEL);
  1039. if (notes_attrs == NULL)
  1040. return;
  1041. notes_attrs->notes = notes;
  1042. nattr = &notes_attrs->attrs[0];
  1043. for (loaded = i = 0; i < nsect; ++i) {
  1044. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1045. continue;
  1046. if (sechdrs[i].sh_type == SHT_NOTE) {
  1047. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1048. nattr->attr.mode = S_IRUGO;
  1049. nattr->size = sechdrs[i].sh_size;
  1050. nattr->private = (void *) sechdrs[i].sh_addr;
  1051. nattr->read = module_notes_read;
  1052. ++nattr;
  1053. }
  1054. ++loaded;
  1055. }
  1056. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1057. if (!notes_attrs->dir)
  1058. goto out;
  1059. for (i = 0; i < notes; ++i)
  1060. if (sysfs_create_bin_file(notes_attrs->dir,
  1061. &notes_attrs->attrs[i]))
  1062. goto out;
  1063. mod->notes_attrs = notes_attrs;
  1064. return;
  1065. out:
  1066. free_notes_attrs(notes_attrs, i);
  1067. }
  1068. static void remove_notes_attrs(struct module *mod)
  1069. {
  1070. if (mod->notes_attrs)
  1071. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1072. }
  1073. #else
  1074. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1075. char *sectstrings, Elf_Shdr *sechdrs)
  1076. {
  1077. }
  1078. static inline void remove_sect_attrs(struct module *mod)
  1079. {
  1080. }
  1081. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1082. char *sectstrings, Elf_Shdr *sechdrs)
  1083. {
  1084. }
  1085. static inline void remove_notes_attrs(struct module *mod)
  1086. {
  1087. }
  1088. #endif
  1089. #ifdef CONFIG_SYSFS
  1090. int module_add_modinfo_attrs(struct module *mod)
  1091. {
  1092. struct module_attribute *attr;
  1093. struct module_attribute *temp_attr;
  1094. int error = 0;
  1095. int i;
  1096. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1097. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1098. GFP_KERNEL);
  1099. if (!mod->modinfo_attrs)
  1100. return -ENOMEM;
  1101. temp_attr = mod->modinfo_attrs;
  1102. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1103. if (!attr->test ||
  1104. (attr->test && attr->test(mod))) {
  1105. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1106. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1107. ++temp_attr;
  1108. }
  1109. }
  1110. return error;
  1111. }
  1112. void module_remove_modinfo_attrs(struct module *mod)
  1113. {
  1114. struct module_attribute *attr;
  1115. int i;
  1116. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1117. /* pick a field to test for end of list */
  1118. if (!attr->attr.name)
  1119. break;
  1120. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1121. if (attr->free)
  1122. attr->free(mod);
  1123. }
  1124. kfree(mod->modinfo_attrs);
  1125. }
  1126. int mod_sysfs_init(struct module *mod)
  1127. {
  1128. int err;
  1129. struct kobject *kobj;
  1130. if (!module_sysfs_initialized) {
  1131. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1132. mod->name);
  1133. err = -EINVAL;
  1134. goto out;
  1135. }
  1136. kobj = kset_find_obj(module_kset, mod->name);
  1137. if (kobj) {
  1138. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1139. kobject_put(kobj);
  1140. err = -EINVAL;
  1141. goto out;
  1142. }
  1143. mod->mkobj.mod = mod;
  1144. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1145. mod->mkobj.kobj.kset = module_kset;
  1146. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1147. "%s", mod->name);
  1148. if (err)
  1149. kobject_put(&mod->mkobj.kobj);
  1150. /* delay uevent until full sysfs population */
  1151. out:
  1152. return err;
  1153. }
  1154. int mod_sysfs_setup(struct module *mod,
  1155. struct kernel_param *kparam,
  1156. unsigned int num_params)
  1157. {
  1158. int err;
  1159. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1160. if (!mod->holders_dir) {
  1161. err = -ENOMEM;
  1162. goto out_unreg;
  1163. }
  1164. err = module_param_sysfs_setup(mod, kparam, num_params);
  1165. if (err)
  1166. goto out_unreg_holders;
  1167. err = module_add_modinfo_attrs(mod);
  1168. if (err)
  1169. goto out_unreg_param;
  1170. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1171. return 0;
  1172. out_unreg_param:
  1173. module_param_sysfs_remove(mod);
  1174. out_unreg_holders:
  1175. kobject_put(mod->holders_dir);
  1176. out_unreg:
  1177. kobject_put(&mod->mkobj.kobj);
  1178. return err;
  1179. }
  1180. static void mod_sysfs_fini(struct module *mod)
  1181. {
  1182. kobject_put(&mod->mkobj.kobj);
  1183. }
  1184. #else /* CONFIG_SYSFS */
  1185. static void mod_sysfs_fini(struct module *mod)
  1186. {
  1187. }
  1188. #endif /* CONFIG_SYSFS */
  1189. static void mod_kobject_remove(struct module *mod)
  1190. {
  1191. module_remove_modinfo_attrs(mod);
  1192. module_param_sysfs_remove(mod);
  1193. kobject_put(mod->mkobj.drivers_dir);
  1194. kobject_put(mod->holders_dir);
  1195. mod_sysfs_fini(mod);
  1196. }
  1197. /*
  1198. * link the module with the whole machine is stopped with interrupts off
  1199. * - this defends against kallsyms not taking locks
  1200. */
  1201. static int __link_module(void *_mod)
  1202. {
  1203. struct module *mod = _mod;
  1204. list_add(&mod->list, &modules);
  1205. return 0;
  1206. }
  1207. /*
  1208. * unlink the module with the whole machine is stopped with interrupts off
  1209. * - this defends against kallsyms not taking locks
  1210. */
  1211. static int __unlink_module(void *_mod)
  1212. {
  1213. struct module *mod = _mod;
  1214. list_del(&mod->list);
  1215. return 0;
  1216. }
  1217. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1218. static void free_module(struct module *mod)
  1219. {
  1220. /* Delete from various lists */
  1221. stop_machine(__unlink_module, mod, NULL);
  1222. remove_notes_attrs(mod);
  1223. remove_sect_attrs(mod);
  1224. mod_kobject_remove(mod);
  1225. unwind_remove_table(mod->unwind_info, 0);
  1226. /* Arch-specific cleanup. */
  1227. module_arch_cleanup(mod);
  1228. /* Module unload stuff */
  1229. module_unload_free(mod);
  1230. /* This may be NULL, but that's OK */
  1231. module_free(mod, mod->module_init);
  1232. kfree(mod->args);
  1233. if (mod->percpu)
  1234. percpu_modfree(mod->percpu);
  1235. /* Free lock-classes: */
  1236. lockdep_free_key_range(mod->module_core, mod->core_size);
  1237. /* Finally, free the core (containing the module structure) */
  1238. module_free(mod, mod->module_core);
  1239. }
  1240. void *__symbol_get(const char *symbol)
  1241. {
  1242. struct module *owner;
  1243. unsigned long value;
  1244. preempt_disable();
  1245. value = find_symbol(symbol, &owner, NULL, true, true);
  1246. if (IS_ERR_VALUE(value))
  1247. value = 0;
  1248. else if (strong_try_module_get(owner))
  1249. value = 0;
  1250. preempt_enable();
  1251. return (void *)value;
  1252. }
  1253. EXPORT_SYMBOL_GPL(__symbol_get);
  1254. /*
  1255. * Ensure that an exported symbol [global namespace] does not already exist
  1256. * in the kernel or in some other module's exported symbol table.
  1257. */
  1258. static int verify_export_symbols(struct module *mod)
  1259. {
  1260. unsigned int i;
  1261. struct module *owner;
  1262. const struct kernel_symbol *s;
  1263. struct {
  1264. const struct kernel_symbol *sym;
  1265. unsigned int num;
  1266. } arr[] = {
  1267. { mod->syms, mod->num_syms },
  1268. { mod->gpl_syms, mod->num_gpl_syms },
  1269. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1270. #ifdef CONFIG_UNUSED_SYMBOLS
  1271. { mod->unused_syms, mod->num_unused_syms },
  1272. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1273. #endif
  1274. };
  1275. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1276. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1277. if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
  1278. NULL, true, false))) {
  1279. printk(KERN_ERR
  1280. "%s: exports duplicate symbol %s"
  1281. " (owned by %s)\n",
  1282. mod->name, s->name, module_name(owner));
  1283. return -ENOEXEC;
  1284. }
  1285. }
  1286. }
  1287. return 0;
  1288. }
  1289. /* Change all symbols so that st_value encodes the pointer directly. */
  1290. static int simplify_symbols(Elf_Shdr *sechdrs,
  1291. unsigned int symindex,
  1292. const char *strtab,
  1293. unsigned int versindex,
  1294. unsigned int pcpuindex,
  1295. struct module *mod)
  1296. {
  1297. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1298. unsigned long secbase;
  1299. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1300. int ret = 0;
  1301. for (i = 1; i < n; i++) {
  1302. switch (sym[i].st_shndx) {
  1303. case SHN_COMMON:
  1304. /* We compiled with -fno-common. These are not
  1305. supposed to happen. */
  1306. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1307. printk("%s: please compile with -fno-common\n",
  1308. mod->name);
  1309. ret = -ENOEXEC;
  1310. break;
  1311. case SHN_ABS:
  1312. /* Don't need to do anything */
  1313. DEBUGP("Absolute symbol: 0x%08lx\n",
  1314. (long)sym[i].st_value);
  1315. break;
  1316. case SHN_UNDEF:
  1317. sym[i].st_value
  1318. = resolve_symbol(sechdrs, versindex,
  1319. strtab + sym[i].st_name, mod);
  1320. /* Ok if resolved. */
  1321. if (!IS_ERR_VALUE(sym[i].st_value))
  1322. break;
  1323. /* Ok if weak. */
  1324. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1325. break;
  1326. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1327. mod->name, strtab + sym[i].st_name);
  1328. ret = -ENOENT;
  1329. break;
  1330. default:
  1331. /* Divert to percpu allocation if a percpu var. */
  1332. if (sym[i].st_shndx == pcpuindex)
  1333. secbase = (unsigned long)mod->percpu;
  1334. else
  1335. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1336. sym[i].st_value += secbase;
  1337. break;
  1338. }
  1339. }
  1340. return ret;
  1341. }
  1342. /* Update size with this section: return offset. */
  1343. static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
  1344. {
  1345. long ret;
  1346. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1347. *size = ret + sechdr->sh_size;
  1348. return ret;
  1349. }
  1350. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1351. might -- code, read-only data, read-write data, small data. Tally
  1352. sizes, and place the offsets into sh_entsize fields: high bit means it
  1353. belongs in init. */
  1354. static void layout_sections(struct module *mod,
  1355. const Elf_Ehdr *hdr,
  1356. Elf_Shdr *sechdrs,
  1357. const char *secstrings)
  1358. {
  1359. static unsigned long const masks[][2] = {
  1360. /* NOTE: all executable code must be the first section
  1361. * in this array; otherwise modify the text_size
  1362. * finder in the two loops below */
  1363. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1364. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1365. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1366. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1367. };
  1368. unsigned int m, i;
  1369. for (i = 0; i < hdr->e_shnum; i++)
  1370. sechdrs[i].sh_entsize = ~0UL;
  1371. DEBUGP("Core section allocation order:\n");
  1372. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1373. for (i = 0; i < hdr->e_shnum; ++i) {
  1374. Elf_Shdr *s = &sechdrs[i];
  1375. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1376. || (s->sh_flags & masks[m][1])
  1377. || s->sh_entsize != ~0UL
  1378. || strncmp(secstrings + s->sh_name,
  1379. ".init", 5) == 0)
  1380. continue;
  1381. s->sh_entsize = get_offset(&mod->core_size, s);
  1382. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1383. }
  1384. if (m == 0)
  1385. mod->core_text_size = mod->core_size;
  1386. }
  1387. DEBUGP("Init section allocation order:\n");
  1388. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1389. for (i = 0; i < hdr->e_shnum; ++i) {
  1390. Elf_Shdr *s = &sechdrs[i];
  1391. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1392. || (s->sh_flags & masks[m][1])
  1393. || s->sh_entsize != ~0UL
  1394. || strncmp(secstrings + s->sh_name,
  1395. ".init", 5) != 0)
  1396. continue;
  1397. s->sh_entsize = (get_offset(&mod->init_size, s)
  1398. | INIT_OFFSET_MASK);
  1399. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1400. }
  1401. if (m == 0)
  1402. mod->init_text_size = mod->init_size;
  1403. }
  1404. }
  1405. static void set_license(struct module *mod, const char *license)
  1406. {
  1407. if (!license)
  1408. license = "unspecified";
  1409. if (!license_is_gpl_compatible(license)) {
  1410. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1411. printk(KERN_WARNING "%s: module license '%s' taints "
  1412. "kernel.\n", mod->name, license);
  1413. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1414. }
  1415. }
  1416. /* Parse tag=value strings from .modinfo section */
  1417. static char *next_string(char *string, unsigned long *secsize)
  1418. {
  1419. /* Skip non-zero chars */
  1420. while (string[0]) {
  1421. string++;
  1422. if ((*secsize)-- <= 1)
  1423. return NULL;
  1424. }
  1425. /* Skip any zero padding. */
  1426. while (!string[0]) {
  1427. string++;
  1428. if ((*secsize)-- <= 1)
  1429. return NULL;
  1430. }
  1431. return string;
  1432. }
  1433. static char *get_modinfo(Elf_Shdr *sechdrs,
  1434. unsigned int info,
  1435. const char *tag)
  1436. {
  1437. char *p;
  1438. unsigned int taglen = strlen(tag);
  1439. unsigned long size = sechdrs[info].sh_size;
  1440. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1441. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1442. return p + taglen + 1;
  1443. }
  1444. return NULL;
  1445. }
  1446. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1447. unsigned int infoindex)
  1448. {
  1449. struct module_attribute *attr;
  1450. int i;
  1451. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1452. if (attr->setup)
  1453. attr->setup(mod,
  1454. get_modinfo(sechdrs,
  1455. infoindex,
  1456. attr->attr.name));
  1457. }
  1458. }
  1459. #ifdef CONFIG_KALLSYMS
  1460. /* lookup symbol in given range of kernel_symbols */
  1461. static const struct kernel_symbol *lookup_symbol(const char *name,
  1462. const struct kernel_symbol *start,
  1463. const struct kernel_symbol *stop)
  1464. {
  1465. const struct kernel_symbol *ks = start;
  1466. for (; ks < stop; ks++)
  1467. if (strcmp(ks->name, name) == 0)
  1468. return ks;
  1469. return NULL;
  1470. }
  1471. static int is_exported(const char *name, const struct module *mod)
  1472. {
  1473. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1474. return 1;
  1475. else
  1476. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1477. return 1;
  1478. else
  1479. return 0;
  1480. }
  1481. /* As per nm */
  1482. static char elf_type(const Elf_Sym *sym,
  1483. Elf_Shdr *sechdrs,
  1484. const char *secstrings,
  1485. struct module *mod)
  1486. {
  1487. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1488. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1489. return 'v';
  1490. else
  1491. return 'w';
  1492. }
  1493. if (sym->st_shndx == SHN_UNDEF)
  1494. return 'U';
  1495. if (sym->st_shndx == SHN_ABS)
  1496. return 'a';
  1497. if (sym->st_shndx >= SHN_LORESERVE)
  1498. return '?';
  1499. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1500. return 't';
  1501. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1502. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1503. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1504. return 'r';
  1505. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1506. return 'g';
  1507. else
  1508. return 'd';
  1509. }
  1510. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1511. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1512. return 's';
  1513. else
  1514. return 'b';
  1515. }
  1516. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1517. ".debug", strlen(".debug")) == 0)
  1518. return 'n';
  1519. return '?';
  1520. }
  1521. static void add_kallsyms(struct module *mod,
  1522. Elf_Shdr *sechdrs,
  1523. unsigned int symindex,
  1524. unsigned int strindex,
  1525. const char *secstrings)
  1526. {
  1527. unsigned int i;
  1528. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1529. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1530. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1531. /* Set types up while we still have access to sections. */
  1532. for (i = 0; i < mod->num_symtab; i++)
  1533. mod->symtab[i].st_info
  1534. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1535. }
  1536. #else
  1537. static inline void add_kallsyms(struct module *mod,
  1538. Elf_Shdr *sechdrs,
  1539. unsigned int symindex,
  1540. unsigned int strindex,
  1541. const char *secstrings)
  1542. {
  1543. }
  1544. #endif /* CONFIG_KALLSYMS */
  1545. static void *module_alloc_update_bounds(unsigned long size)
  1546. {
  1547. void *ret = module_alloc(size);
  1548. if (ret) {
  1549. /* Update module bounds. */
  1550. if ((unsigned long)ret < module_addr_min)
  1551. module_addr_min = (unsigned long)ret;
  1552. if ((unsigned long)ret + size > module_addr_max)
  1553. module_addr_max = (unsigned long)ret + size;
  1554. }
  1555. return ret;
  1556. }
  1557. /* Allocate and load the module: note that size of section 0 is always
  1558. zero, and we rely on this for optional sections. */
  1559. static noinline struct module *load_module(void __user *umod,
  1560. unsigned long len,
  1561. const char __user *uargs)
  1562. {
  1563. Elf_Ehdr *hdr;
  1564. Elf_Shdr *sechdrs;
  1565. char *secstrings, *args, *modmagic, *strtab = NULL;
  1566. char *staging;
  1567. unsigned int i;
  1568. unsigned int symindex = 0;
  1569. unsigned int strindex = 0;
  1570. unsigned int setupindex;
  1571. unsigned int exindex;
  1572. unsigned int exportindex;
  1573. unsigned int modindex;
  1574. unsigned int obsparmindex;
  1575. unsigned int infoindex;
  1576. unsigned int gplindex;
  1577. unsigned int crcindex;
  1578. unsigned int gplcrcindex;
  1579. unsigned int versindex;
  1580. unsigned int pcpuindex;
  1581. unsigned int gplfutureindex;
  1582. unsigned int gplfuturecrcindex;
  1583. unsigned int unwindex = 0;
  1584. #ifdef CONFIG_UNUSED_SYMBOLS
  1585. unsigned int unusedindex;
  1586. unsigned int unusedcrcindex;
  1587. unsigned int unusedgplindex;
  1588. unsigned int unusedgplcrcindex;
  1589. #endif
  1590. unsigned int markersindex;
  1591. unsigned int markersstringsindex;
  1592. struct module *mod;
  1593. long err = 0;
  1594. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1595. struct exception_table_entry *extable;
  1596. mm_segment_t old_fs;
  1597. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1598. umod, len, uargs);
  1599. if (len < sizeof(*hdr))
  1600. return ERR_PTR(-ENOEXEC);
  1601. /* Suck in entire file: we'll want most of it. */
  1602. /* vmalloc barfs on "unusual" numbers. Check here */
  1603. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1604. return ERR_PTR(-ENOMEM);
  1605. if (copy_from_user(hdr, umod, len) != 0) {
  1606. err = -EFAULT;
  1607. goto free_hdr;
  1608. }
  1609. /* Sanity checks against insmoding binaries or wrong arch,
  1610. weird elf version */
  1611. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1612. || hdr->e_type != ET_REL
  1613. || !elf_check_arch(hdr)
  1614. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1615. err = -ENOEXEC;
  1616. goto free_hdr;
  1617. }
  1618. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1619. goto truncated;
  1620. /* Convenience variables */
  1621. sechdrs = (void *)hdr + hdr->e_shoff;
  1622. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1623. sechdrs[0].sh_addr = 0;
  1624. for (i = 1; i < hdr->e_shnum; i++) {
  1625. if (sechdrs[i].sh_type != SHT_NOBITS
  1626. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1627. goto truncated;
  1628. /* Mark all sections sh_addr with their address in the
  1629. temporary image. */
  1630. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1631. /* Internal symbols and strings. */
  1632. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1633. symindex = i;
  1634. strindex = sechdrs[i].sh_link;
  1635. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1636. }
  1637. #ifndef CONFIG_MODULE_UNLOAD
  1638. /* Don't load .exit sections */
  1639. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1640. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1641. #endif
  1642. }
  1643. modindex = find_sec(hdr, sechdrs, secstrings,
  1644. ".gnu.linkonce.this_module");
  1645. if (!modindex) {
  1646. printk(KERN_WARNING "No module found in object\n");
  1647. err = -ENOEXEC;
  1648. goto free_hdr;
  1649. }
  1650. mod = (void *)sechdrs[modindex].sh_addr;
  1651. if (symindex == 0) {
  1652. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1653. mod->name);
  1654. err = -ENOEXEC;
  1655. goto free_hdr;
  1656. }
  1657. /* Optional sections */
  1658. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1659. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1660. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1661. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1662. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1663. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1664. #ifdef CONFIG_UNUSED_SYMBOLS
  1665. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1666. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1667. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1668. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1669. #endif
  1670. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1671. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1672. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1673. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1674. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1675. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1676. #ifdef ARCH_UNWIND_SECTION_NAME
  1677. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1678. #endif
  1679. /* Don't keep modinfo and version sections. */
  1680. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1681. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1682. #ifdef CONFIG_KALLSYMS
  1683. /* Keep symbol and string tables for decoding later. */
  1684. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1685. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1686. #endif
  1687. if (unwindex)
  1688. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1689. /* Check module struct version now, before we try to use module. */
  1690. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1691. err = -ENOEXEC;
  1692. goto free_hdr;
  1693. }
  1694. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1695. /* This is allowed: modprobe --force will invalidate it. */
  1696. if (!modmagic) {
  1697. err = try_to_force_load(mod, "magic");
  1698. if (err)
  1699. goto free_hdr;
  1700. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1701. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1702. mod->name, modmagic, vermagic);
  1703. err = -ENOEXEC;
  1704. goto free_hdr;
  1705. }
  1706. staging = get_modinfo(sechdrs, infoindex, "staging");
  1707. if (staging) {
  1708. add_taint_module(mod, TAINT_CRAP);
  1709. printk(KERN_WARNING "%s: module is from the staging directory,"
  1710. " the quality is unknown, you have been warned.\n",
  1711. mod->name);
  1712. }
  1713. /* Now copy in args */
  1714. args = strndup_user(uargs, ~0UL >> 1);
  1715. if (IS_ERR(args)) {
  1716. err = PTR_ERR(args);
  1717. goto free_hdr;
  1718. }
  1719. if (find_module(mod->name)) {
  1720. err = -EEXIST;
  1721. goto free_mod;
  1722. }
  1723. mod->state = MODULE_STATE_COMING;
  1724. /* Allow arches to frob section contents and sizes. */
  1725. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1726. if (err < 0)
  1727. goto free_mod;
  1728. if (pcpuindex) {
  1729. /* We have a special allocation for this section. */
  1730. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1731. sechdrs[pcpuindex].sh_addralign,
  1732. mod->name);
  1733. if (!percpu) {
  1734. err = -ENOMEM;
  1735. goto free_mod;
  1736. }
  1737. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1738. mod->percpu = percpu;
  1739. }
  1740. /* Determine total sizes, and put offsets in sh_entsize. For now
  1741. this is done generically; there doesn't appear to be any
  1742. special cases for the architectures. */
  1743. layout_sections(mod, hdr, sechdrs, secstrings);
  1744. /* Do the allocs. */
  1745. ptr = module_alloc_update_bounds(mod->core_size);
  1746. if (!ptr) {
  1747. err = -ENOMEM;
  1748. goto free_percpu;
  1749. }
  1750. memset(ptr, 0, mod->core_size);
  1751. mod->module_core = ptr;
  1752. ptr = module_alloc_update_bounds(mod->init_size);
  1753. if (!ptr && mod->init_size) {
  1754. err = -ENOMEM;
  1755. goto free_core;
  1756. }
  1757. memset(ptr, 0, mod->init_size);
  1758. mod->module_init = ptr;
  1759. /* Transfer each section which specifies SHF_ALLOC */
  1760. DEBUGP("final section addresses:\n");
  1761. for (i = 0; i < hdr->e_shnum; i++) {
  1762. void *dest;
  1763. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1764. continue;
  1765. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1766. dest = mod->module_init
  1767. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1768. else
  1769. dest = mod->module_core + sechdrs[i].sh_entsize;
  1770. if (sechdrs[i].sh_type != SHT_NOBITS)
  1771. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1772. sechdrs[i].sh_size);
  1773. /* Update sh_addr to point to copy in image. */
  1774. sechdrs[i].sh_addr = (unsigned long)dest;
  1775. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1776. }
  1777. /* Module has been moved. */
  1778. mod = (void *)sechdrs[modindex].sh_addr;
  1779. /* Now we've moved module, initialize linked lists, etc. */
  1780. module_unload_init(mod);
  1781. /* add kobject, so we can reference it. */
  1782. err = mod_sysfs_init(mod);
  1783. if (err)
  1784. goto free_unload;
  1785. /* Set up license info based on the info section */
  1786. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1787. /*
  1788. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1789. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1790. * using GPL-only symbols it needs.
  1791. */
  1792. if (strcmp(mod->name, "ndiswrapper") == 0)
  1793. add_taint(TAINT_PROPRIETARY_MODULE);
  1794. /* driverloader was caught wrongly pretending to be under GPL */
  1795. if (strcmp(mod->name, "driverloader") == 0)
  1796. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1797. /* Set up MODINFO_ATTR fields */
  1798. setup_modinfo(mod, sechdrs, infoindex);
  1799. /* Fix up syms, so that st_value is a pointer to location. */
  1800. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1801. mod);
  1802. if (err < 0)
  1803. goto cleanup;
  1804. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1805. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1806. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1807. if (crcindex)
  1808. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1809. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1810. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1811. if (gplcrcindex)
  1812. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1813. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1814. sizeof(*mod->gpl_future_syms);
  1815. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1816. if (gplfuturecrcindex)
  1817. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1818. #ifdef CONFIG_UNUSED_SYMBOLS
  1819. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1820. sizeof(*mod->unused_syms);
  1821. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1822. sizeof(*mod->unused_gpl_syms);
  1823. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1824. if (unusedcrcindex)
  1825. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1826. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1827. if (unusedgplcrcindex)
  1828. mod->unused_gpl_crcs
  1829. = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1830. #endif
  1831. #ifdef CONFIG_MODVERSIONS
  1832. if ((mod->num_syms && !crcindex)
  1833. || (mod->num_gpl_syms && !gplcrcindex)
  1834. || (mod->num_gpl_future_syms && !gplfuturecrcindex)
  1835. #ifdef CONFIG_UNUSED_SYMBOLS
  1836. || (mod->num_unused_syms && !unusedcrcindex)
  1837. || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
  1838. #endif
  1839. ) {
  1840. printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
  1841. err = try_to_force_load(mod, "nocrc");
  1842. if (err)
  1843. goto cleanup;
  1844. }
  1845. #endif
  1846. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1847. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1848. "__markers_strings");
  1849. /* Now do relocations. */
  1850. for (i = 1; i < hdr->e_shnum; i++) {
  1851. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1852. unsigned int info = sechdrs[i].sh_info;
  1853. /* Not a valid relocation section? */
  1854. if (info >= hdr->e_shnum)
  1855. continue;
  1856. /* Don't bother with non-allocated sections */
  1857. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1858. continue;
  1859. if (sechdrs[i].sh_type == SHT_REL)
  1860. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1861. else if (sechdrs[i].sh_type == SHT_RELA)
  1862. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1863. mod);
  1864. if (err < 0)
  1865. goto cleanup;
  1866. }
  1867. #ifdef CONFIG_MARKERS
  1868. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1869. mod->num_markers =
  1870. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1871. #endif
  1872. /* Find duplicate symbols */
  1873. err = verify_export_symbols(mod);
  1874. if (err < 0)
  1875. goto cleanup;
  1876. /* Set up and sort exception table */
  1877. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1878. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1879. sort_extable(extable, extable + mod->num_exentries);
  1880. /* Finally, copy percpu area over. */
  1881. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1882. sechdrs[pcpuindex].sh_size);
  1883. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1884. #ifdef CONFIG_MARKERS
  1885. if (!mod->taints)
  1886. marker_update_probe_range(mod->markers,
  1887. mod->markers + mod->num_markers);
  1888. #endif
  1889. err = module_finalize(hdr, sechdrs, mod);
  1890. if (err < 0)
  1891. goto cleanup;
  1892. /* flush the icache in correct context */
  1893. old_fs = get_fs();
  1894. set_fs(KERNEL_DS);
  1895. /*
  1896. * Flush the instruction cache, since we've played with text.
  1897. * Do it before processing of module parameters, so the module
  1898. * can provide parameter accessor functions of its own.
  1899. */
  1900. if (mod->module_init)
  1901. flush_icache_range((unsigned long)mod->module_init,
  1902. (unsigned long)mod->module_init
  1903. + mod->init_size);
  1904. flush_icache_range((unsigned long)mod->module_core,
  1905. (unsigned long)mod->module_core + mod->core_size);
  1906. set_fs(old_fs);
  1907. mod->args = args;
  1908. if (obsparmindex)
  1909. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1910. mod->name);
  1911. /* Now sew it into the lists so we can get lockdep and oops
  1912. * info during argument parsing. Noone should access us, since
  1913. * strong_try_module_get() will fail. */
  1914. stop_machine(__link_module, mod, NULL);
  1915. /* Size of section 0 is 0, so this works well if no params */
  1916. err = parse_args(mod->name, mod->args,
  1917. (struct kernel_param *)
  1918. sechdrs[setupindex].sh_addr,
  1919. sechdrs[setupindex].sh_size
  1920. / sizeof(struct kernel_param),
  1921. NULL);
  1922. if (err < 0)
  1923. goto unlink;
  1924. err = mod_sysfs_setup(mod,
  1925. (struct kernel_param *)
  1926. sechdrs[setupindex].sh_addr,
  1927. sechdrs[setupindex].sh_size
  1928. / sizeof(struct kernel_param));
  1929. if (err < 0)
  1930. goto unlink;
  1931. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1932. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1933. /* Size of section 0 is 0, so this works well if no unwind info. */
  1934. mod->unwind_info = unwind_add_table(mod,
  1935. (void *)sechdrs[unwindex].sh_addr,
  1936. sechdrs[unwindex].sh_size);
  1937. /* Get rid of temporary copy */
  1938. vfree(hdr);
  1939. /* Done! */
  1940. return mod;
  1941. unlink:
  1942. stop_machine(__unlink_module, mod, NULL);
  1943. module_arch_cleanup(mod);
  1944. cleanup:
  1945. kobject_del(&mod->mkobj.kobj);
  1946. kobject_put(&mod->mkobj.kobj);
  1947. free_unload:
  1948. module_unload_free(mod);
  1949. module_free(mod, mod->module_init);
  1950. free_core:
  1951. module_free(mod, mod->module_core);
  1952. free_percpu:
  1953. if (percpu)
  1954. percpu_modfree(percpu);
  1955. free_mod:
  1956. kfree(args);
  1957. free_hdr:
  1958. vfree(hdr);
  1959. return ERR_PTR(err);
  1960. truncated:
  1961. printk(KERN_ERR "Module len %lu truncated\n", len);
  1962. err = -ENOEXEC;
  1963. goto free_hdr;
  1964. }
  1965. /* This is where the real work happens */
  1966. asmlinkage long
  1967. sys_init_module(void __user *umod,
  1968. unsigned long len,
  1969. const char __user *uargs)
  1970. {
  1971. struct module *mod;
  1972. int ret = 0;
  1973. /* Must have permission */
  1974. if (!capable(CAP_SYS_MODULE))
  1975. return -EPERM;
  1976. /* Only one module load at a time, please */
  1977. if (mutex_lock_interruptible(&module_mutex) != 0)
  1978. return -EINTR;
  1979. /* Do all the hard work */
  1980. mod = load_module(umod, len, uargs);
  1981. if (IS_ERR(mod)) {
  1982. mutex_unlock(&module_mutex);
  1983. return PTR_ERR(mod);
  1984. }
  1985. /* Drop lock so they can recurse */
  1986. mutex_unlock(&module_mutex);
  1987. blocking_notifier_call_chain(&module_notify_list,
  1988. MODULE_STATE_COMING, mod);
  1989. /* Start the module */
  1990. if (mod->init != NULL)
  1991. ret = do_one_initcall(mod->init);
  1992. if (ret < 0) {
  1993. /* Init routine failed: abort. Try to protect us from
  1994. buggy refcounters. */
  1995. mod->state = MODULE_STATE_GOING;
  1996. synchronize_sched();
  1997. module_put(mod);
  1998. blocking_notifier_call_chain(&module_notify_list,
  1999. MODULE_STATE_GOING, mod);
  2000. mutex_lock(&module_mutex);
  2001. free_module(mod);
  2002. mutex_unlock(&module_mutex);
  2003. wake_up(&module_wq);
  2004. return ret;
  2005. }
  2006. if (ret > 0) {
  2007. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  2008. "it should follow 0/-E convention\n"
  2009. KERN_WARNING "%s: loading module anyway...\n",
  2010. __func__, mod->name, ret,
  2011. __func__);
  2012. dump_stack();
  2013. }
  2014. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2015. mod->state = MODULE_STATE_LIVE;
  2016. wake_up(&module_wq);
  2017. mutex_lock(&module_mutex);
  2018. /* Drop initial reference. */
  2019. module_put(mod);
  2020. unwind_remove_table(mod->unwind_info, 1);
  2021. module_free(mod, mod->module_init);
  2022. mod->module_init = NULL;
  2023. mod->init_size = 0;
  2024. mod->init_text_size = 0;
  2025. mutex_unlock(&module_mutex);
  2026. return 0;
  2027. }
  2028. static inline int within(unsigned long addr, void *start, unsigned long size)
  2029. {
  2030. return ((void *)addr >= start && (void *)addr < start + size);
  2031. }
  2032. #ifdef CONFIG_KALLSYMS
  2033. /*
  2034. * This ignores the intensely annoying "mapping symbols" found
  2035. * in ARM ELF files: $a, $t and $d.
  2036. */
  2037. static inline int is_arm_mapping_symbol(const char *str)
  2038. {
  2039. return str[0] == '$' && strchr("atd", str[1])
  2040. && (str[2] == '\0' || str[2] == '.');
  2041. }
  2042. static const char *get_ksymbol(struct module *mod,
  2043. unsigned long addr,
  2044. unsigned long *size,
  2045. unsigned long *offset)
  2046. {
  2047. unsigned int i, best = 0;
  2048. unsigned long nextval;
  2049. /* At worse, next value is at end of module */
  2050. if (within(addr, mod->module_init, mod->init_size))
  2051. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2052. else
  2053. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2054. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2055. starts real symbols at 1). */
  2056. for (i = 1; i < mod->num_symtab; i++) {
  2057. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2058. continue;
  2059. /* We ignore unnamed symbols: they're uninformative
  2060. * and inserted at a whim. */
  2061. if (mod->symtab[i].st_value <= addr
  2062. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2063. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2064. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2065. best = i;
  2066. if (mod->symtab[i].st_value > addr
  2067. && mod->symtab[i].st_value < nextval
  2068. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2069. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2070. nextval = mod->symtab[i].st_value;
  2071. }
  2072. if (!best)
  2073. return NULL;
  2074. if (size)
  2075. *size = nextval - mod->symtab[best].st_value;
  2076. if (offset)
  2077. *offset = addr - mod->symtab[best].st_value;
  2078. return mod->strtab + mod->symtab[best].st_name;
  2079. }
  2080. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2081. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2082. const char *module_address_lookup(unsigned long addr,
  2083. unsigned long *size,
  2084. unsigned long *offset,
  2085. char **modname,
  2086. char *namebuf)
  2087. {
  2088. struct module *mod;
  2089. const char *ret = NULL;
  2090. preempt_disable();
  2091. list_for_each_entry(mod, &modules, list) {
  2092. if (within(addr, mod->module_init, mod->init_size)
  2093. || within(addr, mod->module_core, mod->core_size)) {
  2094. if (modname)
  2095. *modname = mod->name;
  2096. ret = get_ksymbol(mod, addr, size, offset);
  2097. break;
  2098. }
  2099. }
  2100. /* Make a copy in here where it's safe */
  2101. if (ret) {
  2102. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2103. ret = namebuf;
  2104. }
  2105. preempt_enable();
  2106. return ret;
  2107. }
  2108. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2109. {
  2110. struct module *mod;
  2111. preempt_disable();
  2112. list_for_each_entry(mod, &modules, list) {
  2113. if (within(addr, mod->module_init, mod->init_size) ||
  2114. within(addr, mod->module_core, mod->core_size)) {
  2115. const char *sym;
  2116. sym = get_ksymbol(mod, addr, NULL, NULL);
  2117. if (!sym)
  2118. goto out;
  2119. strlcpy(symname, sym, KSYM_NAME_LEN);
  2120. preempt_enable();
  2121. return 0;
  2122. }
  2123. }
  2124. out:
  2125. preempt_enable();
  2126. return -ERANGE;
  2127. }
  2128. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2129. unsigned long *offset, char *modname, char *name)
  2130. {
  2131. struct module *mod;
  2132. preempt_disable();
  2133. list_for_each_entry(mod, &modules, list) {
  2134. if (within(addr, mod->module_init, mod->init_size) ||
  2135. within(addr, mod->module_core, mod->core_size)) {
  2136. const char *sym;
  2137. sym = get_ksymbol(mod, addr, size, offset);
  2138. if (!sym)
  2139. goto out;
  2140. if (modname)
  2141. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2142. if (name)
  2143. strlcpy(name, sym, KSYM_NAME_LEN);
  2144. preempt_enable();
  2145. return 0;
  2146. }
  2147. }
  2148. out:
  2149. preempt_enable();
  2150. return -ERANGE;
  2151. }
  2152. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2153. char *name, char *module_name, int *exported)
  2154. {
  2155. struct module *mod;
  2156. preempt_disable();
  2157. list_for_each_entry(mod, &modules, list) {
  2158. if (symnum < mod->num_symtab) {
  2159. *value = mod->symtab[symnum].st_value;
  2160. *type = mod->symtab[symnum].st_info;
  2161. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2162. KSYM_NAME_LEN);
  2163. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2164. *exported = is_exported(name, mod);
  2165. preempt_enable();
  2166. return 0;
  2167. }
  2168. symnum -= mod->num_symtab;
  2169. }
  2170. preempt_enable();
  2171. return -ERANGE;
  2172. }
  2173. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2174. {
  2175. unsigned int i;
  2176. for (i = 0; i < mod->num_symtab; i++)
  2177. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2178. mod->symtab[i].st_info != 'U')
  2179. return mod->symtab[i].st_value;
  2180. return 0;
  2181. }
  2182. /* Look for this name: can be of form module:name. */
  2183. unsigned long module_kallsyms_lookup_name(const char *name)
  2184. {
  2185. struct module *mod;
  2186. char *colon;
  2187. unsigned long ret = 0;
  2188. /* Don't lock: we're in enough trouble already. */
  2189. preempt_disable();
  2190. if ((colon = strchr(name, ':')) != NULL) {
  2191. *colon = '\0';
  2192. if ((mod = find_module(name)) != NULL)
  2193. ret = mod_find_symname(mod, colon+1);
  2194. *colon = ':';
  2195. } else {
  2196. list_for_each_entry(mod, &modules, list)
  2197. if ((ret = mod_find_symname(mod, name)) != 0)
  2198. break;
  2199. }
  2200. preempt_enable();
  2201. return ret;
  2202. }
  2203. #endif /* CONFIG_KALLSYMS */
  2204. /* Called by the /proc file system to return a list of modules. */
  2205. static void *m_start(struct seq_file *m, loff_t *pos)
  2206. {
  2207. mutex_lock(&module_mutex);
  2208. return seq_list_start(&modules, *pos);
  2209. }
  2210. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2211. {
  2212. return seq_list_next(p, &modules, pos);
  2213. }
  2214. static void m_stop(struct seq_file *m, void *p)
  2215. {
  2216. mutex_unlock(&module_mutex);
  2217. }
  2218. static char *module_flags(struct module *mod, char *buf)
  2219. {
  2220. int bx = 0;
  2221. if (mod->taints ||
  2222. mod->state == MODULE_STATE_GOING ||
  2223. mod->state == MODULE_STATE_COMING) {
  2224. buf[bx++] = '(';
  2225. if (mod->taints & TAINT_PROPRIETARY_MODULE)
  2226. buf[bx++] = 'P';
  2227. if (mod->taints & TAINT_FORCED_MODULE)
  2228. buf[bx++] = 'F';
  2229. if (mod->taints & TAINT_CRAP)
  2230. buf[bx++] = 'C';
  2231. /*
  2232. * TAINT_FORCED_RMMOD: could be added.
  2233. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2234. * apply to modules.
  2235. */
  2236. /* Show a - for module-is-being-unloaded */
  2237. if (mod->state == MODULE_STATE_GOING)
  2238. buf[bx++] = '-';
  2239. /* Show a + for module-is-being-loaded */
  2240. if (mod->state == MODULE_STATE_COMING)
  2241. buf[bx++] = '+';
  2242. buf[bx++] = ')';
  2243. }
  2244. buf[bx] = '\0';
  2245. return buf;
  2246. }
  2247. static int m_show(struct seq_file *m, void *p)
  2248. {
  2249. struct module *mod = list_entry(p, struct module, list);
  2250. char buf[8];
  2251. seq_printf(m, "%s %u",
  2252. mod->name, mod->init_size + mod->core_size);
  2253. print_unload_info(m, mod);
  2254. /* Informative for users. */
  2255. seq_printf(m, " %s",
  2256. mod->state == MODULE_STATE_GOING ? "Unloading":
  2257. mod->state == MODULE_STATE_COMING ? "Loading":
  2258. "Live");
  2259. /* Used by oprofile and other similar tools. */
  2260. seq_printf(m, " 0x%p", mod->module_core);
  2261. /* Taints info */
  2262. if (mod->taints)
  2263. seq_printf(m, " %s", module_flags(mod, buf));
  2264. seq_printf(m, "\n");
  2265. return 0;
  2266. }
  2267. /* Format: modulename size refcount deps address
  2268. Where refcount is a number or -, and deps is a comma-separated list
  2269. of depends or -.
  2270. */
  2271. const struct seq_operations modules_op = {
  2272. .start = m_start,
  2273. .next = m_next,
  2274. .stop = m_stop,
  2275. .show = m_show
  2276. };
  2277. /* Given an address, look for it in the module exception tables. */
  2278. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2279. {
  2280. const struct exception_table_entry *e = NULL;
  2281. struct module *mod;
  2282. preempt_disable();
  2283. list_for_each_entry(mod, &modules, list) {
  2284. if (mod->num_exentries == 0)
  2285. continue;
  2286. e = search_extable(mod->extable,
  2287. mod->extable + mod->num_exentries - 1,
  2288. addr);
  2289. if (e)
  2290. break;
  2291. }
  2292. preempt_enable();
  2293. /* Now, if we found one, we are running inside it now, hence
  2294. we cannot unload the module, hence no refcnt needed. */
  2295. return e;
  2296. }
  2297. /*
  2298. * Is this a valid module address?
  2299. */
  2300. int is_module_address(unsigned long addr)
  2301. {
  2302. struct module *mod;
  2303. preempt_disable();
  2304. list_for_each_entry(mod, &modules, list) {
  2305. if (within(addr, mod->module_core, mod->core_size)) {
  2306. preempt_enable();
  2307. return 1;
  2308. }
  2309. }
  2310. preempt_enable();
  2311. return 0;
  2312. }
  2313. /* Is this a valid kernel address? */
  2314. struct module *__module_text_address(unsigned long addr)
  2315. {
  2316. struct module *mod;
  2317. if (addr < module_addr_min || addr > module_addr_max)
  2318. return NULL;
  2319. list_for_each_entry(mod, &modules, list)
  2320. if (within(addr, mod->module_init, mod->init_text_size)
  2321. || within(addr, mod->module_core, mod->core_text_size))
  2322. return mod;
  2323. return NULL;
  2324. }
  2325. struct module *module_text_address(unsigned long addr)
  2326. {
  2327. struct module *mod;
  2328. preempt_disable();
  2329. mod = __module_text_address(addr);
  2330. preempt_enable();
  2331. return mod;
  2332. }
  2333. /* Don't grab lock, we're oopsing. */
  2334. void print_modules(void)
  2335. {
  2336. struct module *mod;
  2337. char buf[8];
  2338. printk("Modules linked in:");
  2339. list_for_each_entry(mod, &modules, list)
  2340. printk(" %s%s", mod->name, module_flags(mod, buf));
  2341. if (last_unloaded_module[0])
  2342. printk(" [last unloaded: %s]", last_unloaded_module);
  2343. printk("\n");
  2344. }
  2345. #ifdef CONFIG_MODVERSIONS
  2346. /* Generate the signature for struct module here, too, for modversions. */
  2347. void struct_module(struct module *mod) { return; }
  2348. EXPORT_SYMBOL(struct_module);
  2349. #endif
  2350. #ifdef CONFIG_MARKERS
  2351. void module_update_markers(void)
  2352. {
  2353. struct module *mod;
  2354. mutex_lock(&module_mutex);
  2355. list_for_each_entry(mod, &modules, list)
  2356. if (!mod->taints)
  2357. marker_update_probe_range(mod->markers,
  2358. mod->markers + mod->num_markers);
  2359. mutex_unlock(&module_mutex);
  2360. }
  2361. #endif