dl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /* dl.c - loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* Force native word size */
  20. #define GRUB_TARGET_WORDSIZE (8 * GRUB_CPU_SIZEOF_VOID_P)
  21. #include <config.h>
  22. #include <grub/elf.h>
  23. #include <grub/dl.h>
  24. #include <grub/misc.h>
  25. #include <grub/mm.h>
  26. #include <grub/err.h>
  27. #include <grub/types.h>
  28. #include <grub/symbol.h>
  29. #include <grub/file.h>
  30. #include <grub/env.h>
  31. #include <grub/cache.h>
  32. #include <grub/i18n.h>
  33. /* Platforms where modules are in a readonly area of memory. */
  34. #if defined(GRUB_MACHINE_QEMU)
  35. #define GRUB_MODULES_MACHINE_READONLY
  36. #endif
  37. #pragma GCC diagnostic ignored "-Wcast-align"
  38. grub_dl_t grub_dl_head = 0;
  39. grub_err_t
  40. grub_dl_add (grub_dl_t mod);
  41. /* Keep global so that GDB scripts work. */
  42. grub_err_t
  43. grub_dl_add (grub_dl_t mod)
  44. {
  45. if (grub_dl_get (mod->name))
  46. return grub_error (GRUB_ERR_BAD_MODULE,
  47. "`%s' is already loaded", mod->name);
  48. return GRUB_ERR_NONE;
  49. }
  50. static void
  51. grub_dl_remove (grub_dl_t mod)
  52. {
  53. grub_dl_t *p, q;
  54. for (p = &grub_dl_head, q = *p; q; p = &q->next, q = *p)
  55. if (q == mod)
  56. {
  57. *p = q->next;
  58. return;
  59. }
  60. }
  61. struct grub_symbol
  62. {
  63. struct grub_symbol *next;
  64. const char *name;
  65. void *addr;
  66. int isfunc;
  67. grub_dl_t mod; /* The module to which this symbol belongs. */
  68. };
  69. typedef struct grub_symbol *grub_symbol_t;
  70. /* The size of the symbol table. */
  71. #define GRUB_SYMTAB_SIZE 509
  72. /* The symbol table (using an open-hash). */
  73. static struct grub_symbol *grub_symtab[GRUB_SYMTAB_SIZE];
  74. /* Simple hash function. */
  75. static unsigned
  76. grub_symbol_hash (const char *s)
  77. {
  78. unsigned key = 0;
  79. while (*s)
  80. key = key * 65599 + *s++;
  81. return (key + (key >> 5)) % GRUB_SYMTAB_SIZE;
  82. }
  83. /* Resolve the symbol name NAME and return the address.
  84. Return NULL, if not found. */
  85. static grub_symbol_t
  86. grub_dl_resolve_symbol (const char *name)
  87. {
  88. grub_symbol_t sym;
  89. for (sym = grub_symtab[grub_symbol_hash (name)]; sym; sym = sym->next)
  90. if (grub_strcmp (sym->name, name) == 0)
  91. return sym;
  92. return 0;
  93. }
  94. /* Register a symbol with the name NAME and the address ADDR. */
  95. grub_err_t
  96. grub_dl_register_symbol (const char *name, void *addr, int isfunc,
  97. grub_dl_t mod)
  98. {
  99. grub_symbol_t sym;
  100. unsigned k;
  101. sym = (grub_symbol_t) grub_malloc (sizeof (*sym));
  102. if (! sym)
  103. return grub_errno;
  104. if (mod)
  105. {
  106. sym->name = grub_strdup (name);
  107. if (! sym->name)
  108. {
  109. grub_free (sym);
  110. return grub_errno;
  111. }
  112. }
  113. else
  114. sym->name = name;
  115. sym->addr = addr;
  116. sym->mod = mod;
  117. sym->isfunc = isfunc;
  118. k = grub_symbol_hash (name);
  119. sym->next = grub_symtab[k];
  120. grub_symtab[k] = sym;
  121. return GRUB_ERR_NONE;
  122. }
  123. /* Unregister all the symbols defined in the module MOD. */
  124. static void
  125. grub_dl_unregister_symbols (grub_dl_t mod)
  126. {
  127. unsigned i;
  128. if (! mod)
  129. grub_fatal ("core symbols cannot be unregistered");
  130. for (i = 0; i < GRUB_SYMTAB_SIZE; i++)
  131. {
  132. grub_symbol_t sym, *p, q;
  133. for (p = &grub_symtab[i], sym = *p; sym; sym = q)
  134. {
  135. q = sym->next;
  136. if (sym->mod == mod)
  137. {
  138. *p = q;
  139. grub_free ((void *) sym->name);
  140. grub_free (sym);
  141. }
  142. else
  143. p = &sym->next;
  144. }
  145. }
  146. }
  147. /* Return the address of a section whose index is N. */
  148. static void *
  149. grub_dl_get_section_addr (grub_dl_t mod, unsigned n)
  150. {
  151. grub_dl_segment_t seg;
  152. for (seg = mod->segment; seg; seg = seg->next)
  153. if (seg->section == n)
  154. return seg->addr;
  155. return 0;
  156. }
  157. /* Check if EHDR is a valid ELF header. */
  158. static grub_err_t
  159. grub_dl_check_header (void *ehdr, grub_size_t size)
  160. {
  161. Elf_Ehdr *e = ehdr;
  162. grub_err_t err;
  163. /* Check the header size. */
  164. if (size < sizeof (Elf_Ehdr))
  165. return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected");
  166. /* Check the magic numbers. */
  167. if (e->e_ident[EI_MAG0] != ELFMAG0
  168. || e->e_ident[EI_MAG1] != ELFMAG1
  169. || e->e_ident[EI_MAG2] != ELFMAG2
  170. || e->e_ident[EI_MAG3] != ELFMAG3
  171. || e->e_ident[EI_VERSION] != EV_CURRENT
  172. || e->e_version != EV_CURRENT)
  173. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
  174. err = grub_arch_dl_check_header (ehdr);
  175. if (err)
  176. return err;
  177. return GRUB_ERR_NONE;
  178. }
  179. /* Load all segments from memory specified by E. */
  180. static grub_err_t
  181. grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  182. {
  183. unsigned i;
  184. const Elf_Shdr *s;
  185. grub_size_t tsize = 0, talign = 1;
  186. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
  187. grub_size_t tramp;
  188. grub_size_t got;
  189. grub_err_t err;
  190. #endif
  191. char *ptr;
  192. for (i = 0, s = (const Elf_Shdr *)((const char *) e + e->e_shoff);
  193. i < e->e_shnum;
  194. i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize))
  195. {
  196. tsize = ALIGN_UP (tsize, s->sh_addralign) + s->sh_size;
  197. if (talign < s->sh_addralign)
  198. talign = s->sh_addralign;
  199. }
  200. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
  201. err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
  202. if (err)
  203. return err;
  204. tsize += ALIGN_UP (tramp, GRUB_ARCH_DL_TRAMP_ALIGN);
  205. if (talign < GRUB_ARCH_DL_TRAMP_ALIGN)
  206. talign = GRUB_ARCH_DL_TRAMP_ALIGN;
  207. tsize += ALIGN_UP (got, GRUB_ARCH_DL_GOT_ALIGN);
  208. if (talign < GRUB_ARCH_DL_GOT_ALIGN)
  209. talign = GRUB_ARCH_DL_GOT_ALIGN;
  210. #endif
  211. #ifdef GRUB_MACHINE_EMU
  212. mod->base = grub_osdep_dl_memalign (talign, tsize);
  213. #else
  214. mod->base = grub_memalign (talign, tsize);
  215. #endif
  216. if (!mod->base)
  217. return grub_errno;
  218. mod->sz = tsize;
  219. ptr = mod->base;
  220. for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
  221. i < e->e_shnum;
  222. i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
  223. {
  224. if (s->sh_flags & SHF_ALLOC)
  225. {
  226. grub_dl_segment_t seg;
  227. seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg));
  228. if (! seg)
  229. return grub_errno;
  230. if (s->sh_size)
  231. {
  232. void *addr;
  233. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, s->sh_addralign);
  234. addr = ptr;
  235. ptr += s->sh_size;
  236. switch (s->sh_type)
  237. {
  238. case SHT_PROGBITS:
  239. grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
  240. break;
  241. case SHT_NOBITS:
  242. grub_memset (addr, 0, s->sh_size);
  243. break;
  244. }
  245. seg->addr = addr;
  246. }
  247. else
  248. seg->addr = 0;
  249. seg->size = s->sh_size;
  250. seg->section = i;
  251. seg->next = mod->segment;
  252. mod->segment = seg;
  253. }
  254. }
  255. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
  256. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
  257. mod->tramp = ptr;
  258. mod->trampptr = ptr;
  259. ptr += tramp;
  260. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
  261. mod->got = ptr;
  262. mod->gotptr = ptr;
  263. ptr += got;
  264. #endif
  265. return GRUB_ERR_NONE;
  266. }
  267. static grub_err_t
  268. grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
  269. {
  270. unsigned i;
  271. Elf_Shdr *s;
  272. Elf_Sym *sym;
  273. const char *str;
  274. Elf_Word size, entsize;
  275. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  276. i < e->e_shnum;
  277. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  278. if (s->sh_type == SHT_SYMTAB)
  279. break;
  280. /* Module without symbol table may still be used to pull in dependencies.
  281. We verify at build time that such modules do not contain any relocations
  282. that may reference symbol table. */
  283. if (i == e->e_shnum)
  284. return GRUB_ERR_NONE;
  285. #ifdef GRUB_MODULES_MACHINE_READONLY
  286. mod->symtab = grub_malloc (s->sh_size);
  287. if (!mod->symtab)
  288. return grub_errno;
  289. grub_memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size);
  290. #else
  291. mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset);
  292. #endif
  293. mod->symsize = s->sh_entsize;
  294. sym = mod->symtab;
  295. size = s->sh_size;
  296. entsize = s->sh_entsize;
  297. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  298. str = (char *) e + s->sh_offset;
  299. for (i = 0;
  300. i < size / entsize;
  301. i++, sym = (Elf_Sym *) ((char *) sym + entsize))
  302. {
  303. unsigned char type = ELF_ST_TYPE (sym->st_info);
  304. unsigned char bind = ELF_ST_BIND (sym->st_info);
  305. const char *name = str + sym->st_name;
  306. switch (type)
  307. {
  308. case STT_NOTYPE:
  309. case STT_OBJECT:
  310. /* Resolve a global symbol. */
  311. if (sym->st_name != 0 && sym->st_shndx == 0)
  312. {
  313. grub_symbol_t nsym = grub_dl_resolve_symbol (name);
  314. if (! nsym)
  315. return grub_error (GRUB_ERR_BAD_MODULE,
  316. N_("symbol `%s' not found"), name);
  317. sym->st_value = (Elf_Addr) nsym->addr;
  318. if (nsym->isfunc)
  319. sym->st_info = ELF_ST_INFO (bind, STT_FUNC);
  320. }
  321. else
  322. {
  323. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  324. sym->st_shndx);
  325. if (bind != STB_LOCAL)
  326. if (grub_dl_register_symbol (name, (void *) sym->st_value, 0, mod))
  327. return grub_errno;
  328. }
  329. break;
  330. case STT_FUNC:
  331. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  332. sym->st_shndx);
  333. #ifdef __ia64__
  334. {
  335. /* FIXME: free descriptor once it's not used anymore. */
  336. char **desc;
  337. desc = grub_malloc (2 * sizeof (char *));
  338. if (!desc)
  339. return grub_errno;
  340. desc[0] = (void *) sym->st_value;
  341. desc[1] = mod->base;
  342. sym->st_value = (grub_addr_t) desc;
  343. }
  344. #endif
  345. if (bind != STB_LOCAL)
  346. if (grub_dl_register_symbol (name, (void *) sym->st_value, 1, mod))
  347. return grub_errno;
  348. if (grub_strcmp (name, "grub_mod_init") == 0)
  349. mod->init = (void (*) (grub_dl_t)) sym->st_value;
  350. else if (grub_strcmp (name, "grub_mod_fini") == 0)
  351. mod->fini = (void (*) (void)) sym->st_value;
  352. break;
  353. case STT_SECTION:
  354. sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod,
  355. sym->st_shndx);
  356. break;
  357. case STT_FILE:
  358. sym->st_value = 0;
  359. break;
  360. default:
  361. return grub_error (GRUB_ERR_BAD_MODULE,
  362. "unknown symbol type `%d'", (int) type);
  363. }
  364. }
  365. return GRUB_ERR_NONE;
  366. }
  367. static Elf_Shdr *
  368. grub_dl_find_section (Elf_Ehdr *e, const char *name)
  369. {
  370. Elf_Shdr *s;
  371. const char *str;
  372. unsigned i;
  373. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
  374. str = (char *) e + s->sh_offset;
  375. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  376. i < e->e_shnum;
  377. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  378. if (grub_strcmp (str + s->sh_name, name) == 0)
  379. return s;
  380. return NULL;
  381. }
  382. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  383. GNU module policy. Note that this license check is informative only.
  384. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  385. multi-licensed under other licences as well) independently of the
  386. presence of this check and solely by linking (module loading in GRUB
  387. constitutes linking) and GRUB core being licensed under GPLv3+.
  388. Be sure to understand your license obligations.
  389. */
  390. static grub_err_t
  391. grub_dl_check_license (Elf_Ehdr *e)
  392. {
  393. Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
  394. if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
  395. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
  396. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
  397. return GRUB_ERR_NONE;
  398. return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
  399. }
  400. static grub_err_t
  401. grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
  402. {
  403. Elf_Shdr *s;
  404. s = grub_dl_find_section (e, ".modname");
  405. if (!s)
  406. return grub_error (GRUB_ERR_BAD_MODULE, "no module name found");
  407. mod->name = grub_strdup ((char *) e + s->sh_offset);
  408. if (! mod->name)
  409. return grub_errno;
  410. return GRUB_ERR_NONE;
  411. }
  412. static grub_err_t
  413. grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e)
  414. {
  415. Elf_Shdr *s;
  416. s = grub_dl_find_section (e, ".moddeps");
  417. if (!s)
  418. return GRUB_ERR_NONE;
  419. const char *name = (char *) e + s->sh_offset;
  420. const char *max = name + s->sh_size;
  421. while ((name < max) && (*name))
  422. {
  423. grub_dl_t m;
  424. grub_dl_dep_t dep;
  425. m = grub_dl_load (name);
  426. if (! m)
  427. return grub_errno;
  428. grub_dl_ref (m);
  429. dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep));
  430. if (! dep)
  431. return grub_errno;
  432. dep->mod = m;
  433. dep->next = mod->dep;
  434. mod->dep = dep;
  435. name += grub_strlen (name) + 1;
  436. }
  437. return GRUB_ERR_NONE;
  438. }
  439. int
  440. grub_dl_ref (grub_dl_t mod)
  441. {
  442. grub_dl_dep_t dep;
  443. if (!mod)
  444. return 0;
  445. for (dep = mod->dep; dep; dep = dep->next)
  446. grub_dl_ref (dep->mod);
  447. return ++mod->ref_count;
  448. }
  449. int
  450. grub_dl_unref (grub_dl_t mod)
  451. {
  452. grub_dl_dep_t dep;
  453. if (!mod)
  454. return 0;
  455. for (dep = mod->dep; dep; dep = dep->next)
  456. grub_dl_unref (dep->mod);
  457. return --mod->ref_count;
  458. }
  459. int
  460. grub_dl_ref_count (grub_dl_t mod)
  461. {
  462. if (mod == NULL)
  463. return 0;
  464. return mod->ref_count;
  465. }
  466. static void
  467. grub_dl_flush_cache (grub_dl_t mod)
  468. {
  469. grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
  470. (unsigned long) mod->sz, mod->base);
  471. grub_arch_sync_caches (mod->base, mod->sz);
  472. }
  473. static grub_err_t
  474. grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
  475. {
  476. Elf_Ehdr *e = ehdr;
  477. Elf_Shdr *s;
  478. unsigned i;
  479. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  480. i < e->e_shnum;
  481. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  482. if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
  483. {
  484. grub_dl_segment_t seg;
  485. grub_err_t err;
  486. /* Find the target segment. */
  487. for (seg = mod->segment; seg; seg = seg->next)
  488. if (seg->section == s->sh_info)
  489. break;
  490. if (seg)
  491. {
  492. if (!mod->symtab)
  493. return grub_error (GRUB_ERR_BAD_MODULE, "relocation without symbol table");
  494. err = grub_arch_dl_relocate_symbols (mod, ehdr, s, seg);
  495. if (err)
  496. return err;
  497. }
  498. }
  499. return GRUB_ERR_NONE;
  500. }
  501. /* Load a module from core memory. */
  502. grub_dl_t
  503. grub_dl_load_core_noinit (void *addr, grub_size_t size)
  504. {
  505. Elf_Ehdr *e;
  506. grub_dl_t mod;
  507. grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr,
  508. (unsigned long) size);
  509. e = addr;
  510. if (grub_dl_check_header (e, size))
  511. return 0;
  512. if (e->e_type != ET_REL)
  513. {
  514. grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
  515. return 0;
  516. }
  517. /* Make sure that every section is within the core. */
  518. if (size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
  519. {
  520. grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
  521. return 0;
  522. }
  523. mod = (grub_dl_t) grub_zalloc (sizeof (*mod));
  524. if (! mod)
  525. return 0;
  526. mod->ref_count = 1;
  527. grub_dprintf ("modules", "relocating to %p\n", mod);
  528. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  529. GNU module policy. Note that this license check is informative only.
  530. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  531. multi-licensed under other licences as well) independently of the
  532. presence of this check and solely by linking (module loading in GRUB
  533. constitutes linking) and GRUB core being licensed under GPLv3+.
  534. Be sure to understand your license obligations.
  535. */
  536. if (grub_dl_check_license (e)
  537. || grub_dl_resolve_name (mod, e)
  538. || grub_dl_resolve_dependencies (mod, e)
  539. || grub_dl_load_segments (mod, e)
  540. || grub_dl_resolve_symbols (mod, e)
  541. || grub_dl_relocate_symbols (mod, e))
  542. {
  543. mod->fini = 0;
  544. grub_dl_unload (mod);
  545. return 0;
  546. }
  547. grub_dl_flush_cache (mod);
  548. grub_dprintf ("modules", "module name: %s\n", mod->name);
  549. grub_dprintf ("modules", "init function: %p\n", mod->init);
  550. if (grub_dl_add (mod))
  551. {
  552. grub_dl_unload (mod);
  553. return 0;
  554. }
  555. return mod;
  556. }
  557. grub_dl_t
  558. grub_dl_load_core (void *addr, grub_size_t size)
  559. {
  560. grub_dl_t mod;
  561. grub_boot_time ("Parsing module");
  562. mod = grub_dl_load_core_noinit (addr, size);
  563. if (!mod)
  564. return NULL;
  565. grub_boot_time ("Initing module %s", mod->name);
  566. grub_dl_init (mod);
  567. grub_boot_time ("Module %s inited", mod->name);
  568. return mod;
  569. }
  570. /* Load a module from the file FILENAME. */
  571. grub_dl_t
  572. grub_dl_load_file (const char *filename)
  573. {
  574. grub_file_t file = NULL;
  575. grub_ssize_t size;
  576. void *core = 0;
  577. grub_dl_t mod = 0;
  578. grub_boot_time ("Loading module %s", filename);
  579. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE);
  580. if (! file)
  581. return 0;
  582. size = grub_file_size (file);
  583. core = grub_malloc (size);
  584. if (! core)
  585. {
  586. grub_file_close (file);
  587. return 0;
  588. }
  589. if (grub_file_read (file, core, size) != (int) size)
  590. {
  591. grub_file_close (file);
  592. grub_free (core);
  593. return 0;
  594. }
  595. /* We must close this before we try to process dependencies.
  596. Some disk backends do not handle gracefully multiple concurrent
  597. opens of the same device. */
  598. grub_file_close (file);
  599. mod = grub_dl_load_core (core, size);
  600. grub_free (core);
  601. if (! mod)
  602. return 0;
  603. mod->ref_count--;
  604. return mod;
  605. }
  606. /* Load a module using a symbolic name. */
  607. grub_dl_t
  608. grub_dl_load (const char *name)
  609. {
  610. char *filename;
  611. grub_dl_t mod;
  612. const char *grub_dl_dir = grub_env_get ("prefix");
  613. mod = grub_dl_get (name);
  614. if (mod)
  615. return mod;
  616. if (grub_no_modules)
  617. return 0;
  618. if (! grub_dl_dir) {
  619. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
  620. return 0;
  621. }
  622. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
  623. grub_dl_dir, name);
  624. if (! filename)
  625. return 0;
  626. mod = grub_dl_load_file (filename);
  627. grub_free (filename);
  628. if (! mod)
  629. return 0;
  630. if (grub_strcmp (mod->name, name) != 0)
  631. grub_error (GRUB_ERR_BAD_MODULE, "mismatched names");
  632. return mod;
  633. }
  634. /* Unload the module MOD. */
  635. int
  636. grub_dl_unload (grub_dl_t mod)
  637. {
  638. grub_dl_dep_t dep, depn;
  639. if (mod->ref_count > 0)
  640. return 0;
  641. if (mod->fini)
  642. (mod->fini) ();
  643. grub_dl_remove (mod);
  644. grub_dl_unregister_symbols (mod);
  645. for (dep = mod->dep; dep; dep = depn)
  646. {
  647. depn = dep->next;
  648. grub_dl_unload (dep->mod);
  649. grub_free (dep);
  650. }
  651. #ifdef GRUB_MACHINE_EMU
  652. grub_dl_osdep_dl_free (mod->base);
  653. #else
  654. grub_free (mod->base);
  655. #endif
  656. grub_free (mod->name);
  657. #ifdef GRUB_MODULES_MACHINE_READONLY
  658. grub_free (mod->symtab);
  659. #endif
  660. grub_free (mod);
  661. return 1;
  662. }
  663. /* Unload unneeded modules. */
  664. void
  665. grub_dl_unload_unneeded (void)
  666. {
  667. /* Because grub_dl_remove modifies the list of modules, this
  668. implementation is tricky. */
  669. grub_dl_t p = grub_dl_head;
  670. while (p)
  671. {
  672. if (grub_dl_unload (p))
  673. {
  674. p = grub_dl_head;
  675. continue;
  676. }
  677. p = p->next;
  678. }
  679. }