dl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. !defined (__loongarch__)
  188. grub_size_t tramp;
  189. grub_size_t got;
  190. grub_err_t err;
  191. #endif
  192. char *ptr;
  193. for (i = 0, s = (const Elf_Shdr *)((const char *) e + e->e_shoff);
  194. i < e->e_shnum;
  195. i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize))
  196. {
  197. tsize = ALIGN_UP (tsize, s->sh_addralign) + s->sh_size;
  198. if (talign < s->sh_addralign)
  199. talign = s->sh_addralign;
  200. }
  201. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  202. !defined (__loongarch__)
  203. err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
  204. if (err)
  205. return err;
  206. tsize += ALIGN_UP (tramp, GRUB_ARCH_DL_TRAMP_ALIGN);
  207. if (talign < GRUB_ARCH_DL_TRAMP_ALIGN)
  208. talign = GRUB_ARCH_DL_TRAMP_ALIGN;
  209. tsize += ALIGN_UP (got, GRUB_ARCH_DL_GOT_ALIGN);
  210. if (talign < GRUB_ARCH_DL_GOT_ALIGN)
  211. talign = GRUB_ARCH_DL_GOT_ALIGN;
  212. #endif
  213. #ifdef GRUB_MACHINE_EMU
  214. mod->base = grub_osdep_dl_memalign (talign, tsize);
  215. #else
  216. mod->base = grub_memalign (talign, tsize);
  217. #endif
  218. if (!mod->base)
  219. return grub_errno;
  220. mod->sz = tsize;
  221. ptr = mod->base;
  222. for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
  223. i < e->e_shnum;
  224. i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
  225. {
  226. if (s->sh_flags & SHF_ALLOC)
  227. {
  228. grub_dl_segment_t seg;
  229. seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg));
  230. if (! seg)
  231. return grub_errno;
  232. if (s->sh_size)
  233. {
  234. void *addr;
  235. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, s->sh_addralign);
  236. addr = ptr;
  237. ptr += s->sh_size;
  238. switch (s->sh_type)
  239. {
  240. case SHT_PROGBITS:
  241. grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
  242. break;
  243. case SHT_NOBITS:
  244. grub_memset (addr, 0, s->sh_size);
  245. break;
  246. }
  247. seg->addr = addr;
  248. }
  249. else
  250. seg->addr = 0;
  251. seg->size = s->sh_size;
  252. seg->section = i;
  253. seg->next = mod->segment;
  254. mod->segment = seg;
  255. }
  256. }
  257. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  258. !defined (__loongarch__)
  259. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
  260. mod->tramp = ptr;
  261. mod->trampptr = ptr;
  262. ptr += tramp;
  263. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
  264. mod->got = ptr;
  265. mod->gotptr = ptr;
  266. ptr += got;
  267. #endif
  268. return GRUB_ERR_NONE;
  269. }
  270. static grub_err_t
  271. grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
  272. {
  273. unsigned i;
  274. Elf_Shdr *s;
  275. Elf_Sym *sym;
  276. const char *str;
  277. Elf_Word size, entsize;
  278. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  279. i < e->e_shnum;
  280. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  281. if (s->sh_type == SHT_SYMTAB)
  282. break;
  283. /* Module without symbol table may still be used to pull in dependencies.
  284. We verify at build time that such modules do not contain any relocations
  285. that may reference symbol table. */
  286. if (i == e->e_shnum)
  287. return GRUB_ERR_NONE;
  288. #ifdef GRUB_MODULES_MACHINE_READONLY
  289. mod->symtab = grub_malloc (s->sh_size);
  290. if (!mod->symtab)
  291. return grub_errno;
  292. grub_memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size);
  293. #else
  294. mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset);
  295. #endif
  296. mod->symsize = s->sh_entsize;
  297. sym = mod->symtab;
  298. size = s->sh_size;
  299. entsize = s->sh_entsize;
  300. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  301. str = (char *) e + s->sh_offset;
  302. for (i = 0;
  303. i < size / entsize;
  304. i++, sym = (Elf_Sym *) ((char *) sym + entsize))
  305. {
  306. unsigned char type = ELF_ST_TYPE (sym->st_info);
  307. unsigned char bind = ELF_ST_BIND (sym->st_info);
  308. const char *name = str + sym->st_name;
  309. switch (type)
  310. {
  311. case STT_NOTYPE:
  312. case STT_OBJECT:
  313. /* Resolve a global symbol. */
  314. if (sym->st_name != 0 && sym->st_shndx == 0)
  315. {
  316. grub_symbol_t nsym = grub_dl_resolve_symbol (name);
  317. if (! nsym)
  318. return grub_error (GRUB_ERR_BAD_MODULE,
  319. N_("symbol `%s' not found"), name);
  320. sym->st_value = (Elf_Addr) nsym->addr;
  321. if (nsym->isfunc)
  322. sym->st_info = ELF_ST_INFO (bind, STT_FUNC);
  323. }
  324. else
  325. {
  326. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  327. sym->st_shndx);
  328. if (bind != STB_LOCAL)
  329. if (grub_dl_register_symbol (name, (void *) sym->st_value, 0, mod))
  330. return grub_errno;
  331. }
  332. break;
  333. case STT_FUNC:
  334. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  335. sym->st_shndx);
  336. #ifdef __ia64__
  337. {
  338. /* FIXME: free descriptor once it's not used anymore. */
  339. char **desc;
  340. desc = grub_malloc (2 * sizeof (char *));
  341. if (!desc)
  342. return grub_errno;
  343. desc[0] = (void *) sym->st_value;
  344. desc[1] = mod->base;
  345. sym->st_value = (grub_addr_t) desc;
  346. }
  347. #endif
  348. if (bind != STB_LOCAL)
  349. if (grub_dl_register_symbol (name, (void *) sym->st_value, 1, mod))
  350. return grub_errno;
  351. if (grub_strcmp (name, "grub_mod_init") == 0)
  352. mod->init = (void (*) (grub_dl_t)) sym->st_value;
  353. else if (grub_strcmp (name, "grub_mod_fini") == 0)
  354. mod->fini = (void (*) (void)) sym->st_value;
  355. break;
  356. case STT_SECTION:
  357. sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod,
  358. sym->st_shndx);
  359. break;
  360. case STT_FILE:
  361. sym->st_value = 0;
  362. break;
  363. default:
  364. return grub_error (GRUB_ERR_BAD_MODULE,
  365. "unknown symbol type `%d'", (int) type);
  366. }
  367. }
  368. return GRUB_ERR_NONE;
  369. }
  370. static Elf_Shdr *
  371. grub_dl_find_section (Elf_Ehdr *e, const char *name)
  372. {
  373. Elf_Shdr *s;
  374. const char *str;
  375. unsigned i;
  376. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
  377. str = (char *) e + s->sh_offset;
  378. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  379. i < e->e_shnum;
  380. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  381. if (grub_strcmp (str + s->sh_name, name) == 0)
  382. return s;
  383. return NULL;
  384. }
  385. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  386. GNU module policy. Note that this license check is informative only.
  387. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  388. multi-licensed under other licences as well) independently of the
  389. presence of this check and solely by linking (module loading in GRUB
  390. constitutes linking) and GRUB core being licensed under GPLv3+.
  391. Be sure to understand your license obligations.
  392. */
  393. static grub_err_t
  394. grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
  395. {
  396. Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
  397. if (s == NULL)
  398. return grub_error (GRUB_ERR_BAD_MODULE,
  399. "no license section in module %.63s", mod->name);
  400. if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
  401. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
  402. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)
  403. return GRUB_ERR_NONE;
  404. return grub_error (GRUB_ERR_BAD_MODULE,
  405. "incompatible license in module %.63s: %.63s", mod->name,
  406. (char *) e + s->sh_offset);
  407. }
  408. static grub_err_t
  409. grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
  410. {
  411. Elf_Shdr *s;
  412. s = grub_dl_find_section (e, ".modname");
  413. if (!s)
  414. return grub_error (GRUB_ERR_BAD_MODULE, "no module name found");
  415. mod->name = grub_strdup ((char *) e + s->sh_offset);
  416. if (! mod->name)
  417. return grub_errno;
  418. return GRUB_ERR_NONE;
  419. }
  420. static grub_err_t
  421. grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e)
  422. {
  423. Elf_Shdr *s;
  424. s = grub_dl_find_section (e, ".moddeps");
  425. if (!s)
  426. return GRUB_ERR_NONE;
  427. const char *name = (char *) e + s->sh_offset;
  428. const char *max = name + s->sh_size;
  429. while ((name < max) && (*name))
  430. {
  431. grub_dl_t m;
  432. grub_dl_dep_t dep;
  433. m = grub_dl_load (name);
  434. if (! m)
  435. return grub_errno;
  436. grub_dl_ref (m);
  437. dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep));
  438. if (! dep)
  439. return grub_errno;
  440. dep->mod = m;
  441. dep->next = mod->dep;
  442. mod->dep = dep;
  443. name += grub_strlen (name) + 1;
  444. }
  445. return GRUB_ERR_NONE;
  446. }
  447. int
  448. grub_dl_ref (grub_dl_t mod)
  449. {
  450. grub_dl_dep_t dep;
  451. if (!mod)
  452. return 0;
  453. for (dep = mod->dep; dep; dep = dep->next)
  454. grub_dl_ref (dep->mod);
  455. return ++mod->ref_count;
  456. }
  457. int
  458. grub_dl_unref (grub_dl_t mod)
  459. {
  460. grub_dl_dep_t dep;
  461. if (!mod)
  462. return 0;
  463. for (dep = mod->dep; dep; dep = dep->next)
  464. grub_dl_unref (dep->mod);
  465. return --mod->ref_count;
  466. }
  467. int
  468. grub_dl_ref_count (grub_dl_t mod)
  469. {
  470. if (mod == NULL)
  471. return 0;
  472. return mod->ref_count;
  473. }
  474. static void
  475. grub_dl_flush_cache (grub_dl_t mod)
  476. {
  477. grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
  478. (unsigned long) mod->sz, mod->base);
  479. grub_arch_sync_caches (mod->base, mod->sz);
  480. }
  481. static grub_err_t
  482. grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
  483. {
  484. Elf_Ehdr *e = ehdr;
  485. Elf_Shdr *s;
  486. unsigned i;
  487. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  488. i < e->e_shnum;
  489. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  490. if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
  491. {
  492. grub_dl_segment_t seg;
  493. grub_err_t err;
  494. /* Find the target segment. */
  495. for (seg = mod->segment; seg; seg = seg->next)
  496. if (seg->section == s->sh_info)
  497. break;
  498. if (seg)
  499. {
  500. if (!mod->symtab)
  501. return grub_error (GRUB_ERR_BAD_MODULE, "relocation without symbol table");
  502. err = grub_arch_dl_relocate_symbols (mod, ehdr, s, seg);
  503. if (err)
  504. return err;
  505. }
  506. }
  507. return GRUB_ERR_NONE;
  508. }
  509. /* Load a module from core memory. */
  510. grub_dl_t
  511. grub_dl_load_core_noinit (void *addr, grub_size_t size)
  512. {
  513. Elf_Ehdr *e;
  514. grub_dl_t mod;
  515. grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr,
  516. (unsigned long) size);
  517. e = addr;
  518. if (grub_dl_check_header (e, size))
  519. return 0;
  520. if (e->e_type != ET_REL)
  521. {
  522. grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
  523. return 0;
  524. }
  525. /* Make sure that every section is within the core. */
  526. if (size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
  527. {
  528. grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
  529. return 0;
  530. }
  531. mod = (grub_dl_t) grub_zalloc (sizeof (*mod));
  532. if (! mod)
  533. return 0;
  534. mod->ref_count = 1;
  535. grub_dprintf ("modules", "relocating to %p\n", mod);
  536. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  537. GNU module policy. Note that this license check is informative only.
  538. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  539. multi-licensed under other licences as well) independently of the
  540. presence of this check and solely by linking (module loading in GRUB
  541. constitutes linking) and GRUB core being licensed under GPLv3+.
  542. Be sure to understand your license obligations.
  543. */
  544. if (grub_dl_resolve_name (mod, e)
  545. || grub_dl_check_license (mod, e)
  546. || grub_dl_resolve_dependencies (mod, e)
  547. || grub_dl_load_segments (mod, e)
  548. || grub_dl_resolve_symbols (mod, e)
  549. || grub_dl_relocate_symbols (mod, e))
  550. {
  551. mod->fini = 0;
  552. grub_dl_unload (mod);
  553. return 0;
  554. }
  555. grub_dl_flush_cache (mod);
  556. grub_dprintf ("modules", "module name: %s\n", mod->name);
  557. grub_dprintf ("modules", "init function: %p\n", mod->init);
  558. if (grub_dl_add (mod))
  559. {
  560. grub_dl_unload (mod);
  561. return 0;
  562. }
  563. return mod;
  564. }
  565. grub_dl_t
  566. grub_dl_load_core (void *addr, grub_size_t size)
  567. {
  568. grub_dl_t mod;
  569. grub_boot_time ("Parsing module");
  570. mod = grub_dl_load_core_noinit (addr, size);
  571. if (!mod)
  572. return NULL;
  573. grub_boot_time ("Initing module %s", mod->name);
  574. grub_dl_init (mod);
  575. grub_boot_time ("Module %s inited", mod->name);
  576. return mod;
  577. }
  578. /* Load a module from the file FILENAME. */
  579. grub_dl_t
  580. grub_dl_load_file (const char *filename)
  581. {
  582. grub_file_t file = NULL;
  583. grub_ssize_t size;
  584. void *core = 0;
  585. grub_dl_t mod = 0;
  586. grub_boot_time ("Loading module %s", filename);
  587. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE);
  588. if (! file)
  589. return 0;
  590. size = grub_file_size (file);
  591. core = grub_malloc (size);
  592. if (! core)
  593. {
  594. grub_file_close (file);
  595. return 0;
  596. }
  597. if (grub_file_read (file, core, size) != (int) size)
  598. {
  599. grub_file_close (file);
  600. grub_free (core);
  601. return 0;
  602. }
  603. /* We must close this before we try to process dependencies.
  604. Some disk backends do not handle gracefully multiple concurrent
  605. opens of the same device. */
  606. grub_file_close (file);
  607. mod = grub_dl_load_core (core, size);
  608. grub_free (core);
  609. if (! mod)
  610. return 0;
  611. mod->ref_count--;
  612. return mod;
  613. }
  614. /* Load a module using a symbolic name. */
  615. grub_dl_t
  616. grub_dl_load (const char *name)
  617. {
  618. char *filename;
  619. grub_dl_t mod;
  620. const char *grub_dl_dir = grub_env_get ("prefix");
  621. mod = grub_dl_get (name);
  622. if (mod)
  623. return mod;
  624. if (grub_no_modules)
  625. return 0;
  626. if (! grub_dl_dir) {
  627. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
  628. return 0;
  629. }
  630. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
  631. grub_dl_dir, name);
  632. if (! filename)
  633. return 0;
  634. mod = grub_dl_load_file (filename);
  635. grub_free (filename);
  636. if (! mod)
  637. return 0;
  638. if (grub_strcmp (mod->name, name) != 0)
  639. grub_error (GRUB_ERR_BAD_MODULE, "mismatched names");
  640. return mod;
  641. }
  642. /* Unload the module MOD. */
  643. int
  644. grub_dl_unload (grub_dl_t mod)
  645. {
  646. grub_dl_dep_t dep, depn;
  647. if (mod->ref_count > 0)
  648. return 0;
  649. if (mod->fini)
  650. (mod->fini) ();
  651. grub_dl_remove (mod);
  652. grub_dl_unregister_symbols (mod);
  653. for (dep = mod->dep; dep; dep = depn)
  654. {
  655. depn = dep->next;
  656. grub_dl_unload (dep->mod);
  657. grub_free (dep);
  658. }
  659. #ifdef GRUB_MACHINE_EMU
  660. grub_dl_osdep_dl_free (mod->base);
  661. #else
  662. grub_free (mod->base);
  663. #endif
  664. grub_free (mod->name);
  665. #ifdef GRUB_MODULES_MACHINE_READONLY
  666. grub_free (mod->symtab);
  667. #endif
  668. grub_free (mod);
  669. return 1;
  670. }