module.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  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 |= (1U << 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 (!test_taint(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 & (1 << 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 (!test_taint(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. unsigned int i;
  1567. unsigned int symindex = 0;
  1568. unsigned int strindex = 0;
  1569. unsigned int setupindex;
  1570. unsigned int exindex;
  1571. unsigned int exportindex;
  1572. unsigned int modindex;
  1573. unsigned int obsparmindex;
  1574. unsigned int infoindex;
  1575. unsigned int gplindex;
  1576. unsigned int crcindex;
  1577. unsigned int gplcrcindex;
  1578. unsigned int versindex;
  1579. unsigned int pcpuindex;
  1580. unsigned int gplfutureindex;
  1581. unsigned int gplfuturecrcindex;
  1582. unsigned int unwindex = 0;
  1583. #ifdef CONFIG_UNUSED_SYMBOLS
  1584. unsigned int unusedindex;
  1585. unsigned int unusedcrcindex;
  1586. unsigned int unusedgplindex;
  1587. unsigned int unusedgplcrcindex;
  1588. #endif
  1589. unsigned int markersindex;
  1590. unsigned int markersstringsindex;
  1591. struct module *mod;
  1592. long err = 0;
  1593. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1594. struct exception_table_entry *extable;
  1595. mm_segment_t old_fs;
  1596. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1597. umod, len, uargs);
  1598. if (len < sizeof(*hdr))
  1599. return ERR_PTR(-ENOEXEC);
  1600. /* Suck in entire file: we'll want most of it. */
  1601. /* vmalloc barfs on "unusual" numbers. Check here */
  1602. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1603. return ERR_PTR(-ENOMEM);
  1604. if (copy_from_user(hdr, umod, len) != 0) {
  1605. err = -EFAULT;
  1606. goto free_hdr;
  1607. }
  1608. /* Sanity checks against insmoding binaries or wrong arch,
  1609. weird elf version */
  1610. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1611. || hdr->e_type != ET_REL
  1612. || !elf_check_arch(hdr)
  1613. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1614. err = -ENOEXEC;
  1615. goto free_hdr;
  1616. }
  1617. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1618. goto truncated;
  1619. /* Convenience variables */
  1620. sechdrs = (void *)hdr + hdr->e_shoff;
  1621. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1622. sechdrs[0].sh_addr = 0;
  1623. for (i = 1; i < hdr->e_shnum; i++) {
  1624. if (sechdrs[i].sh_type != SHT_NOBITS
  1625. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1626. goto truncated;
  1627. /* Mark all sections sh_addr with their address in the
  1628. temporary image. */
  1629. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1630. /* Internal symbols and strings. */
  1631. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1632. symindex = i;
  1633. strindex = sechdrs[i].sh_link;
  1634. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1635. }
  1636. #ifndef CONFIG_MODULE_UNLOAD
  1637. /* Don't load .exit sections */
  1638. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1639. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1640. #endif
  1641. }
  1642. modindex = find_sec(hdr, sechdrs, secstrings,
  1643. ".gnu.linkonce.this_module");
  1644. if (!modindex) {
  1645. printk(KERN_WARNING "No module found in object\n");
  1646. err = -ENOEXEC;
  1647. goto free_hdr;
  1648. }
  1649. mod = (void *)sechdrs[modindex].sh_addr;
  1650. if (symindex == 0) {
  1651. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1652. mod->name);
  1653. err = -ENOEXEC;
  1654. goto free_hdr;
  1655. }
  1656. /* Optional sections */
  1657. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1658. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1659. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1660. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1661. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1662. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1663. #ifdef CONFIG_UNUSED_SYMBOLS
  1664. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1665. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1666. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1667. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1668. #endif
  1669. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1670. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1671. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1672. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1673. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1674. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1675. #ifdef ARCH_UNWIND_SECTION_NAME
  1676. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1677. #endif
  1678. /* Don't keep modinfo and version sections. */
  1679. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1680. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1681. #ifdef CONFIG_KALLSYMS
  1682. /* Keep symbol and string tables for decoding later. */
  1683. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1684. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1685. #endif
  1686. if (unwindex)
  1687. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1688. /* Check module struct version now, before we try to use module. */
  1689. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1690. err = -ENOEXEC;
  1691. goto free_hdr;
  1692. }
  1693. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1694. /* This is allowed: modprobe --force will invalidate it. */
  1695. if (!modmagic) {
  1696. err = try_to_force_load(mod, "magic");
  1697. if (err)
  1698. goto free_hdr;
  1699. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1700. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1701. mod->name, modmagic, vermagic);
  1702. err = -ENOEXEC;
  1703. goto free_hdr;
  1704. }
  1705. /* Now copy in args */
  1706. args = strndup_user(uargs, ~0UL >> 1);
  1707. if (IS_ERR(args)) {
  1708. err = PTR_ERR(args);
  1709. goto free_hdr;
  1710. }
  1711. if (find_module(mod->name)) {
  1712. err = -EEXIST;
  1713. goto free_mod;
  1714. }
  1715. mod->state = MODULE_STATE_COMING;
  1716. /* Allow arches to frob section contents and sizes. */
  1717. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1718. if (err < 0)
  1719. goto free_mod;
  1720. if (pcpuindex) {
  1721. /* We have a special allocation for this section. */
  1722. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1723. sechdrs[pcpuindex].sh_addralign,
  1724. mod->name);
  1725. if (!percpu) {
  1726. err = -ENOMEM;
  1727. goto free_mod;
  1728. }
  1729. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1730. mod->percpu = percpu;
  1731. }
  1732. /* Determine total sizes, and put offsets in sh_entsize. For now
  1733. this is done generically; there doesn't appear to be any
  1734. special cases for the architectures. */
  1735. layout_sections(mod, hdr, sechdrs, secstrings);
  1736. /* Do the allocs. */
  1737. ptr = module_alloc_update_bounds(mod->core_size);
  1738. if (!ptr) {
  1739. err = -ENOMEM;
  1740. goto free_percpu;
  1741. }
  1742. memset(ptr, 0, mod->core_size);
  1743. mod->module_core = ptr;
  1744. ptr = module_alloc_update_bounds(mod->init_size);
  1745. if (!ptr && mod->init_size) {
  1746. err = -ENOMEM;
  1747. goto free_core;
  1748. }
  1749. memset(ptr, 0, mod->init_size);
  1750. mod->module_init = ptr;
  1751. /* Transfer each section which specifies SHF_ALLOC */
  1752. DEBUGP("final section addresses:\n");
  1753. for (i = 0; i < hdr->e_shnum; i++) {
  1754. void *dest;
  1755. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1756. continue;
  1757. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1758. dest = mod->module_init
  1759. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1760. else
  1761. dest = mod->module_core + sechdrs[i].sh_entsize;
  1762. if (sechdrs[i].sh_type != SHT_NOBITS)
  1763. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1764. sechdrs[i].sh_size);
  1765. /* Update sh_addr to point to copy in image. */
  1766. sechdrs[i].sh_addr = (unsigned long)dest;
  1767. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1768. }
  1769. /* Module has been moved. */
  1770. mod = (void *)sechdrs[modindex].sh_addr;
  1771. /* Now we've moved module, initialize linked lists, etc. */
  1772. module_unload_init(mod);
  1773. /* add kobject, so we can reference it. */
  1774. err = mod_sysfs_init(mod);
  1775. if (err)
  1776. goto free_unload;
  1777. /* Set up license info based on the info section */
  1778. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1779. /*
  1780. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1781. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1782. * using GPL-only symbols it needs.
  1783. */
  1784. if (strcmp(mod->name, "ndiswrapper") == 0)
  1785. add_taint(TAINT_PROPRIETARY_MODULE);
  1786. /* driverloader was caught wrongly pretending to be under GPL */
  1787. if (strcmp(mod->name, "driverloader") == 0)
  1788. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1789. /* Set up MODINFO_ATTR fields */
  1790. setup_modinfo(mod, sechdrs, infoindex);
  1791. /* Fix up syms, so that st_value is a pointer to location. */
  1792. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1793. mod);
  1794. if (err < 0)
  1795. goto cleanup;
  1796. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1797. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1798. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1799. if (crcindex)
  1800. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1801. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1802. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1803. if (gplcrcindex)
  1804. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1805. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1806. sizeof(*mod->gpl_future_syms);
  1807. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1808. if (gplfuturecrcindex)
  1809. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1810. #ifdef CONFIG_UNUSED_SYMBOLS
  1811. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1812. sizeof(*mod->unused_syms);
  1813. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1814. sizeof(*mod->unused_gpl_syms);
  1815. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1816. if (unusedcrcindex)
  1817. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1818. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1819. if (unusedgplcrcindex)
  1820. mod->unused_gpl_crcs
  1821. = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1822. #endif
  1823. #ifdef CONFIG_MODVERSIONS
  1824. if ((mod->num_syms && !crcindex)
  1825. || (mod->num_gpl_syms && !gplcrcindex)
  1826. || (mod->num_gpl_future_syms && !gplfuturecrcindex)
  1827. #ifdef CONFIG_UNUSED_SYMBOLS
  1828. || (mod->num_unused_syms && !unusedcrcindex)
  1829. || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
  1830. #endif
  1831. ) {
  1832. printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
  1833. err = try_to_force_load(mod, "nocrc");
  1834. if (err)
  1835. goto cleanup;
  1836. }
  1837. #endif
  1838. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1839. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1840. "__markers_strings");
  1841. /* Now do relocations. */
  1842. for (i = 1; i < hdr->e_shnum; i++) {
  1843. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1844. unsigned int info = sechdrs[i].sh_info;
  1845. /* Not a valid relocation section? */
  1846. if (info >= hdr->e_shnum)
  1847. continue;
  1848. /* Don't bother with non-allocated sections */
  1849. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1850. continue;
  1851. if (sechdrs[i].sh_type == SHT_REL)
  1852. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1853. else if (sechdrs[i].sh_type == SHT_RELA)
  1854. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1855. mod);
  1856. if (err < 0)
  1857. goto cleanup;
  1858. }
  1859. #ifdef CONFIG_MARKERS
  1860. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1861. mod->num_markers =
  1862. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1863. #endif
  1864. /* Find duplicate symbols */
  1865. err = verify_export_symbols(mod);
  1866. if (err < 0)
  1867. goto cleanup;
  1868. /* Set up and sort exception table */
  1869. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1870. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1871. sort_extable(extable, extable + mod->num_exentries);
  1872. /* Finally, copy percpu area over. */
  1873. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1874. sechdrs[pcpuindex].sh_size);
  1875. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1876. #ifdef CONFIG_MARKERS
  1877. if (!mod->taints)
  1878. marker_update_probe_range(mod->markers,
  1879. mod->markers + mod->num_markers);
  1880. #endif
  1881. err = module_finalize(hdr, sechdrs, mod);
  1882. if (err < 0)
  1883. goto cleanup;
  1884. /* flush the icache in correct context */
  1885. old_fs = get_fs();
  1886. set_fs(KERNEL_DS);
  1887. /*
  1888. * Flush the instruction cache, since we've played with text.
  1889. * Do it before processing of module parameters, so the module
  1890. * can provide parameter accessor functions of its own.
  1891. */
  1892. if (mod->module_init)
  1893. flush_icache_range((unsigned long)mod->module_init,
  1894. (unsigned long)mod->module_init
  1895. + mod->init_size);
  1896. flush_icache_range((unsigned long)mod->module_core,
  1897. (unsigned long)mod->module_core + mod->core_size);
  1898. set_fs(old_fs);
  1899. mod->args = args;
  1900. if (obsparmindex)
  1901. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1902. mod->name);
  1903. /* Now sew it into the lists so we can get lockdep and oops
  1904. * info during argument parsing. Noone should access us, since
  1905. * strong_try_module_get() will fail. */
  1906. stop_machine(__link_module, mod, NULL);
  1907. /* Size of section 0 is 0, so this works well if no params */
  1908. err = parse_args(mod->name, mod->args,
  1909. (struct kernel_param *)
  1910. sechdrs[setupindex].sh_addr,
  1911. sechdrs[setupindex].sh_size
  1912. / sizeof(struct kernel_param),
  1913. NULL);
  1914. if (err < 0)
  1915. goto unlink;
  1916. err = mod_sysfs_setup(mod,
  1917. (struct kernel_param *)
  1918. sechdrs[setupindex].sh_addr,
  1919. sechdrs[setupindex].sh_size
  1920. / sizeof(struct kernel_param));
  1921. if (err < 0)
  1922. goto unlink;
  1923. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1924. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1925. /* Size of section 0 is 0, so this works well if no unwind info. */
  1926. mod->unwind_info = unwind_add_table(mod,
  1927. (void *)sechdrs[unwindex].sh_addr,
  1928. sechdrs[unwindex].sh_size);
  1929. /* Get rid of temporary copy */
  1930. vfree(hdr);
  1931. /* Done! */
  1932. return mod;
  1933. unlink:
  1934. stop_machine(__unlink_module, mod, NULL);
  1935. module_arch_cleanup(mod);
  1936. cleanup:
  1937. kobject_del(&mod->mkobj.kobj);
  1938. kobject_put(&mod->mkobj.kobj);
  1939. free_unload:
  1940. module_unload_free(mod);
  1941. module_free(mod, mod->module_init);
  1942. free_core:
  1943. module_free(mod, mod->module_core);
  1944. free_percpu:
  1945. if (percpu)
  1946. percpu_modfree(percpu);
  1947. free_mod:
  1948. kfree(args);
  1949. free_hdr:
  1950. vfree(hdr);
  1951. return ERR_PTR(err);
  1952. truncated:
  1953. printk(KERN_ERR "Module len %lu truncated\n", len);
  1954. err = -ENOEXEC;
  1955. goto free_hdr;
  1956. }
  1957. /* This is where the real work happens */
  1958. asmlinkage long
  1959. sys_init_module(void __user *umod,
  1960. unsigned long len,
  1961. const char __user *uargs)
  1962. {
  1963. struct module *mod;
  1964. int ret = 0;
  1965. /* Must have permission */
  1966. if (!capable(CAP_SYS_MODULE))
  1967. return -EPERM;
  1968. /* Only one module load at a time, please */
  1969. if (mutex_lock_interruptible(&module_mutex) != 0)
  1970. return -EINTR;
  1971. /* Do all the hard work */
  1972. mod = load_module(umod, len, uargs);
  1973. if (IS_ERR(mod)) {
  1974. mutex_unlock(&module_mutex);
  1975. return PTR_ERR(mod);
  1976. }
  1977. /* Drop lock so they can recurse */
  1978. mutex_unlock(&module_mutex);
  1979. blocking_notifier_call_chain(&module_notify_list,
  1980. MODULE_STATE_COMING, mod);
  1981. /* Start the module */
  1982. if (mod->init != NULL)
  1983. ret = do_one_initcall(mod->init);
  1984. if (ret < 0) {
  1985. /* Init routine failed: abort. Try to protect us from
  1986. buggy refcounters. */
  1987. mod->state = MODULE_STATE_GOING;
  1988. synchronize_sched();
  1989. module_put(mod);
  1990. blocking_notifier_call_chain(&module_notify_list,
  1991. MODULE_STATE_GOING, mod);
  1992. mutex_lock(&module_mutex);
  1993. free_module(mod);
  1994. mutex_unlock(&module_mutex);
  1995. wake_up(&module_wq);
  1996. return ret;
  1997. }
  1998. if (ret > 0) {
  1999. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  2000. "it should follow 0/-E convention\n"
  2001. KERN_WARNING "%s: loading module anyway...\n",
  2002. __func__, mod->name, ret,
  2003. __func__);
  2004. dump_stack();
  2005. }
  2006. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2007. mod->state = MODULE_STATE_LIVE;
  2008. wake_up(&module_wq);
  2009. mutex_lock(&module_mutex);
  2010. /* Drop initial reference. */
  2011. module_put(mod);
  2012. unwind_remove_table(mod->unwind_info, 1);
  2013. module_free(mod, mod->module_init);
  2014. mod->module_init = NULL;
  2015. mod->init_size = 0;
  2016. mod->init_text_size = 0;
  2017. mutex_unlock(&module_mutex);
  2018. return 0;
  2019. }
  2020. static inline int within(unsigned long addr, void *start, unsigned long size)
  2021. {
  2022. return ((void *)addr >= start && (void *)addr < start + size);
  2023. }
  2024. #ifdef CONFIG_KALLSYMS
  2025. /*
  2026. * This ignores the intensely annoying "mapping symbols" found
  2027. * in ARM ELF files: $a, $t and $d.
  2028. */
  2029. static inline int is_arm_mapping_symbol(const char *str)
  2030. {
  2031. return str[0] == '$' && strchr("atd", str[1])
  2032. && (str[2] == '\0' || str[2] == '.');
  2033. }
  2034. static const char *get_ksymbol(struct module *mod,
  2035. unsigned long addr,
  2036. unsigned long *size,
  2037. unsigned long *offset)
  2038. {
  2039. unsigned int i, best = 0;
  2040. unsigned long nextval;
  2041. /* At worse, next value is at end of module */
  2042. if (within(addr, mod->module_init, mod->init_size))
  2043. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2044. else
  2045. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2046. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2047. starts real symbols at 1). */
  2048. for (i = 1; i < mod->num_symtab; i++) {
  2049. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2050. continue;
  2051. /* We ignore unnamed symbols: they're uninformative
  2052. * and inserted at a whim. */
  2053. if (mod->symtab[i].st_value <= addr
  2054. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2055. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2056. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2057. best = i;
  2058. if (mod->symtab[i].st_value > addr
  2059. && mod->symtab[i].st_value < nextval
  2060. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2061. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2062. nextval = mod->symtab[i].st_value;
  2063. }
  2064. if (!best)
  2065. return NULL;
  2066. if (size)
  2067. *size = nextval - mod->symtab[best].st_value;
  2068. if (offset)
  2069. *offset = addr - mod->symtab[best].st_value;
  2070. return mod->strtab + mod->symtab[best].st_name;
  2071. }
  2072. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2073. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2074. const char *module_address_lookup(unsigned long addr,
  2075. unsigned long *size,
  2076. unsigned long *offset,
  2077. char **modname,
  2078. char *namebuf)
  2079. {
  2080. struct module *mod;
  2081. const char *ret = NULL;
  2082. preempt_disable();
  2083. list_for_each_entry(mod, &modules, list) {
  2084. if (within(addr, mod->module_init, mod->init_size)
  2085. || within(addr, mod->module_core, mod->core_size)) {
  2086. if (modname)
  2087. *modname = mod->name;
  2088. ret = get_ksymbol(mod, addr, size, offset);
  2089. break;
  2090. }
  2091. }
  2092. /* Make a copy in here where it's safe */
  2093. if (ret) {
  2094. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2095. ret = namebuf;
  2096. }
  2097. preempt_enable();
  2098. return ret;
  2099. }
  2100. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2101. {
  2102. struct module *mod;
  2103. preempt_disable();
  2104. list_for_each_entry(mod, &modules, list) {
  2105. if (within(addr, mod->module_init, mod->init_size) ||
  2106. within(addr, mod->module_core, mod->core_size)) {
  2107. const char *sym;
  2108. sym = get_ksymbol(mod, addr, NULL, NULL);
  2109. if (!sym)
  2110. goto out;
  2111. strlcpy(symname, sym, KSYM_NAME_LEN);
  2112. preempt_enable();
  2113. return 0;
  2114. }
  2115. }
  2116. out:
  2117. preempt_enable();
  2118. return -ERANGE;
  2119. }
  2120. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2121. unsigned long *offset, char *modname, char *name)
  2122. {
  2123. struct module *mod;
  2124. preempt_disable();
  2125. list_for_each_entry(mod, &modules, list) {
  2126. if (within(addr, mod->module_init, mod->init_size) ||
  2127. within(addr, mod->module_core, mod->core_size)) {
  2128. const char *sym;
  2129. sym = get_ksymbol(mod, addr, size, offset);
  2130. if (!sym)
  2131. goto out;
  2132. if (modname)
  2133. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2134. if (name)
  2135. strlcpy(name, sym, KSYM_NAME_LEN);
  2136. preempt_enable();
  2137. return 0;
  2138. }
  2139. }
  2140. out:
  2141. preempt_enable();
  2142. return -ERANGE;
  2143. }
  2144. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2145. char *name, char *module_name, int *exported)
  2146. {
  2147. struct module *mod;
  2148. preempt_disable();
  2149. list_for_each_entry(mod, &modules, list) {
  2150. if (symnum < mod->num_symtab) {
  2151. *value = mod->symtab[symnum].st_value;
  2152. *type = mod->symtab[symnum].st_info;
  2153. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2154. KSYM_NAME_LEN);
  2155. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2156. *exported = is_exported(name, mod);
  2157. preempt_enable();
  2158. return 0;
  2159. }
  2160. symnum -= mod->num_symtab;
  2161. }
  2162. preempt_enable();
  2163. return -ERANGE;
  2164. }
  2165. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2166. {
  2167. unsigned int i;
  2168. for (i = 0; i < mod->num_symtab; i++)
  2169. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2170. mod->symtab[i].st_info != 'U')
  2171. return mod->symtab[i].st_value;
  2172. return 0;
  2173. }
  2174. /* Look for this name: can be of form module:name. */
  2175. unsigned long module_kallsyms_lookup_name(const char *name)
  2176. {
  2177. struct module *mod;
  2178. char *colon;
  2179. unsigned long ret = 0;
  2180. /* Don't lock: we're in enough trouble already. */
  2181. preempt_disable();
  2182. if ((colon = strchr(name, ':')) != NULL) {
  2183. *colon = '\0';
  2184. if ((mod = find_module(name)) != NULL)
  2185. ret = mod_find_symname(mod, colon+1);
  2186. *colon = ':';
  2187. } else {
  2188. list_for_each_entry(mod, &modules, list)
  2189. if ((ret = mod_find_symname(mod, name)) != 0)
  2190. break;
  2191. }
  2192. preempt_enable();
  2193. return ret;
  2194. }
  2195. #endif /* CONFIG_KALLSYMS */
  2196. /* Called by the /proc file system to return a list of modules. */
  2197. static void *m_start(struct seq_file *m, loff_t *pos)
  2198. {
  2199. mutex_lock(&module_mutex);
  2200. return seq_list_start(&modules, *pos);
  2201. }
  2202. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2203. {
  2204. return seq_list_next(p, &modules, pos);
  2205. }
  2206. static void m_stop(struct seq_file *m, void *p)
  2207. {
  2208. mutex_unlock(&module_mutex);
  2209. }
  2210. static char *module_flags(struct module *mod, char *buf)
  2211. {
  2212. int bx = 0;
  2213. if (mod->taints ||
  2214. mod->state == MODULE_STATE_GOING ||
  2215. mod->state == MODULE_STATE_COMING) {
  2216. buf[bx++] = '(';
  2217. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2218. buf[bx++] = 'P';
  2219. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2220. buf[bx++] = 'F';
  2221. /*
  2222. * TAINT_FORCED_RMMOD: could be added.
  2223. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2224. * apply to modules.
  2225. */
  2226. /* Show a - for module-is-being-unloaded */
  2227. if (mod->state == MODULE_STATE_GOING)
  2228. buf[bx++] = '-';
  2229. /* Show a + for module-is-being-loaded */
  2230. if (mod->state == MODULE_STATE_COMING)
  2231. buf[bx++] = '+';
  2232. buf[bx++] = ')';
  2233. }
  2234. buf[bx] = '\0';
  2235. return buf;
  2236. }
  2237. static int m_show(struct seq_file *m, void *p)
  2238. {
  2239. struct module *mod = list_entry(p, struct module, list);
  2240. char buf[8];
  2241. seq_printf(m, "%s %u",
  2242. mod->name, mod->init_size + mod->core_size);
  2243. print_unload_info(m, mod);
  2244. /* Informative for users. */
  2245. seq_printf(m, " %s",
  2246. mod->state == MODULE_STATE_GOING ? "Unloading":
  2247. mod->state == MODULE_STATE_COMING ? "Loading":
  2248. "Live");
  2249. /* Used by oprofile and other similar tools. */
  2250. seq_printf(m, " 0x%p", mod->module_core);
  2251. /* Taints info */
  2252. if (mod->taints)
  2253. seq_printf(m, " %s", module_flags(mod, buf));
  2254. seq_printf(m, "\n");
  2255. return 0;
  2256. }
  2257. /* Format: modulename size refcount deps address
  2258. Where refcount is a number or -, and deps is a comma-separated list
  2259. of depends or -.
  2260. */
  2261. const struct seq_operations modules_op = {
  2262. .start = m_start,
  2263. .next = m_next,
  2264. .stop = m_stop,
  2265. .show = m_show
  2266. };
  2267. /* Given an address, look for it in the module exception tables. */
  2268. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2269. {
  2270. const struct exception_table_entry *e = NULL;
  2271. struct module *mod;
  2272. preempt_disable();
  2273. list_for_each_entry(mod, &modules, list) {
  2274. if (mod->num_exentries == 0)
  2275. continue;
  2276. e = search_extable(mod->extable,
  2277. mod->extable + mod->num_exentries - 1,
  2278. addr);
  2279. if (e)
  2280. break;
  2281. }
  2282. preempt_enable();
  2283. /* Now, if we found one, we are running inside it now, hence
  2284. we cannot unload the module, hence no refcnt needed. */
  2285. return e;
  2286. }
  2287. /*
  2288. * Is this a valid module address?
  2289. */
  2290. int is_module_address(unsigned long addr)
  2291. {
  2292. struct module *mod;
  2293. preempt_disable();
  2294. list_for_each_entry(mod, &modules, list) {
  2295. if (within(addr, mod->module_core, mod->core_size)) {
  2296. preempt_enable();
  2297. return 1;
  2298. }
  2299. }
  2300. preempt_enable();
  2301. return 0;
  2302. }
  2303. /* Is this a valid kernel address? */
  2304. struct module *__module_text_address(unsigned long addr)
  2305. {
  2306. struct module *mod;
  2307. if (addr < module_addr_min || addr > module_addr_max)
  2308. return NULL;
  2309. list_for_each_entry(mod, &modules, list)
  2310. if (within(addr, mod->module_init, mod->init_text_size)
  2311. || within(addr, mod->module_core, mod->core_text_size))
  2312. return mod;
  2313. return NULL;
  2314. }
  2315. struct module *module_text_address(unsigned long addr)
  2316. {
  2317. struct module *mod;
  2318. preempt_disable();
  2319. mod = __module_text_address(addr);
  2320. preempt_enable();
  2321. return mod;
  2322. }
  2323. /* Don't grab lock, we're oopsing. */
  2324. void print_modules(void)
  2325. {
  2326. struct module *mod;
  2327. char buf[8];
  2328. printk("Modules linked in:");
  2329. list_for_each_entry(mod, &modules, list)
  2330. printk(" %s%s", mod->name, module_flags(mod, buf));
  2331. if (last_unloaded_module[0])
  2332. printk(" [last unloaded: %s]", last_unloaded_module);
  2333. printk("\n");
  2334. }
  2335. #ifdef CONFIG_MODVERSIONS
  2336. /* Generate the signature for struct module here, too, for modversions. */
  2337. void struct_module(struct module *mod) { return; }
  2338. EXPORT_SYMBOL(struct_module);
  2339. #endif
  2340. #ifdef CONFIG_MARKERS
  2341. void module_update_markers(void)
  2342. {
  2343. struct module *mod;
  2344. mutex_lock(&module_mutex);
  2345. list_for_each_entry(mod, &modules, list)
  2346. if (!mod->taints)
  2347. marker_update_probe_range(mod->markers,
  2348. mod->markers + mod->num_markers);
  2349. mutex_unlock(&module_mutex);
  2350. }
  2351. #endif