/* dl.h - types and prototypes for loadable module support */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2002,2004,2005,2007,2008,2009 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #ifndef VAS_EBOOT_DL_H #define VAS_EBOOT_DL_H 1 #include #ifndef ASM_FILE #include #include #include #include #include #endif /* * Macros VAS_EBOOT_MOD_INIT and VAS_EBOOT_MOD_FINI are also used by build rules * to collect module names, so we define them only when they are not * defined already. */ #ifndef ASM_FILE #ifndef VAS_EBOOT_MOD_INIT #if !defined (VAS_EBOOT_UTIL) && !defined (VAS_EBOOT_MACHINE_EMU) && !defined (VAS_EBOOT_KERNEL) #define VAS_EBOOT_MOD_INIT(name) \ static void VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \ static void \ VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) #define VAS_EBOOT_MOD_FINI(name) \ static void VasEBoot_mod_fini (void) __attribute__ ((used)); \ static void \ VasEBoot_mod_fini (void) #elif defined (VAS_EBOOT_KERNEL) #define VAS_EBOOT_MOD_INIT(name) \ static void VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \ void \ VasEBoot_##name##_init (void) { VasEBoot_mod_init (0); } \ static void \ VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) #define VAS_EBOOT_MOD_FINI(name) \ static void VasEBoot_mod_fini (void) __attribute__ ((used)); \ void \ VasEBoot_##name##_fini (void) { VasEBoot_mod_fini (); } \ static void \ VasEBoot_mod_fini (void) #else #define VAS_EBOOT_MOD_INIT(name) \ static void VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \ void VasEBoot_##name##_init (void); \ void \ VasEBoot_##name##_init (void) { VasEBoot_mod_init (0); } \ static void \ VasEBoot_mod_init (VasEBoot_dl_t mod __attribute__ ((unused))) #define VAS_EBOOT_MOD_FINI(name) \ static void VasEBoot_mod_fini (void) __attribute__ ((used)); \ void VasEBoot_##name##_fini (void); \ void \ VasEBoot_##name##_fini (void) { VasEBoot_mod_fini (); } \ static void \ VasEBoot_mod_fini (void) #endif #endif #endif #ifndef ASM_FILE #ifdef __APPLE__ #define VAS_EBOOT_MOD_SECTION(x) "_" #x ", _" #x "" #else #define VAS_EBOOT_MOD_SECTION(x) "." #x #endif #else #ifdef __APPLE__ #define VAS_EBOOT_MOD_SECTION(x) _ ## x , _ ##x #else #define VAS_EBOOT_MOD_SECTION(x) . ## x #endif #endif /* Me, Vladimir Serbinenko, hereby I add this module check as per new GNU module policy. Note that this license check is informative only. Modules have to be licensed under GPLv3 or GPLv3+ (optionally multi-licensed under other licences as well) independently of the presence of this check and solely by linking (module loading in VAS_EBOOT constitutes linking) and VAS_EBOOT core being licensed under GPLv3+. Be sure to understand your license obligations. */ #ifndef ASM_FILE #if GNUC_PREREQ (3,2) #define ATTRIBUTE_USED __used__ #else #define ATTRIBUTE_USED __unused__ #endif #define VAS_EBOOT_MOD_LICENSE(license) \ static const char VasEBoot_module_license[] __attribute__ ((section (VAS_EBOOT_MOD_SECTION (module_license)), ATTRIBUTE_USED)) = "LICENSE=" license; #define VAS_EBOOT_MOD_DEP(name) \ static const char VasEBoot_module_depend_##name[] \ __attribute__((section(VAS_EBOOT_MOD_SECTION(moddeps)), ATTRIBUTE_USED)) = #name #define VAS_EBOOT_MOD_NAME(name) \ static const char VasEBoot_module_name_##name[] \ __attribute__((section(VAS_EBOOT_MOD_SECTION(modname)), __used__)) = #name #else #ifdef __APPLE__ .macro VAS_EBOOT_MOD_LICENSE .section VAS_EBOOT_MOD_SECTION(module_license) .ascii "LICENSE=" .ascii $0 .byte 0 .endm #else .macro VAS_EBOOT_MOD_LICENSE license .section VAS_EBOOT_MOD_SECTION(module_license), "a" .ascii "LICENSE=" .ascii "\license" .byte 0 .endm #endif #endif /* Under GPL license obligations you have to distribute your module under GPLv3(+). However, you can also distribute the same code under another license as long as GPLv3(+) version is provided. */ #define VAS_EBOOT_MOD_DUAL_LICENSE(x) #ifndef ASM_FILE struct VasEBoot_dl_segment { struct VasEBoot_dl_segment *next; void *addr; VasEBoot_size_t size; unsigned section; }; typedef struct VasEBoot_dl_segment *VasEBoot_dl_segment_t; struct VasEBoot_dl; struct VasEBoot_dl_dep { struct VasEBoot_dl_dep *next; struct VasEBoot_dl *mod; }; typedef struct VasEBoot_dl_dep *VasEBoot_dl_dep_t; #ifndef VAS_EBOOT_UTIL struct VasEBoot_dl { char *name; VasEBoot_uint64_t ref_count; int persistent; VasEBoot_dl_dep_t dep; VasEBoot_dl_segment_t segment; Elf_Sym *symtab; VasEBoot_size_t symsize; void (*init) (struct VasEBoot_dl *mod); void (*fini) (void); #if !defined (__i386__) && !defined (__x86_64__) void *got; void *gotptr; void *tramp; void *trampptr; #endif #ifdef __mips__ VasEBoot_uint32_t *reginfo; #endif void *base; VasEBoot_size_t sz; struct VasEBoot_dl *next; }; #endif typedef struct VasEBoot_dl *VasEBoot_dl_t; VasEBoot_dl_t VasEBoot_dl_load_file (const char *filename); VasEBoot_dl_t EXPORT_FUNC(VasEBoot_dl_load) (const char *name); VasEBoot_dl_t VasEBoot_dl_load_core (void *addr, VasEBoot_size_t size); VasEBoot_dl_t EXPORT_FUNC(VasEBoot_dl_load_core_noinit) (void *addr, VasEBoot_size_t size); int EXPORT_FUNC(VasEBoot_dl_unload) (VasEBoot_dl_t mod); extern VasEBoot_uint64_t EXPORT_FUNC(VasEBoot_dl_ref) (VasEBoot_dl_t mod); extern VasEBoot_uint64_t EXPORT_FUNC(VasEBoot_dl_unref) (VasEBoot_dl_t mod); extern VasEBoot_uint64_t EXPORT_FUNC(VasEBoot_dl_ref_count) (VasEBoot_dl_t mod); extern VasEBoot_dl_t EXPORT_VAR(VasEBoot_dl_head); #ifndef VAS_EBOOT_UTIL #define FOR_DL_MODULES(var) FOR_LIST_ELEMENTS ((var), (VasEBoot_dl_head)) #ifdef VAS_EBOOT_MACHINE_EMU void * VasEBoot_osdep_dl_memalign (VasEBoot_size_t align, VasEBoot_size_t size); void VasEBoot_dl_osdep_dl_free (void *ptr); #endif static inline void VasEBoot_dl_init (VasEBoot_dl_t mod) { if (mod->init) (mod->init) (mod); mod->next = VasEBoot_dl_head; VasEBoot_dl_head = mod; } static inline VasEBoot_dl_t VasEBoot_dl_get (const char *name) { VasEBoot_dl_t l; FOR_DL_MODULES(l) if (VasEBoot_strcmp (name, l->name) == 0) return l; return 0; } #ifdef VAS_EBOOT_MACHINE_EMU /* * Under VasEBoot-emu, modules are faked and NULL is passed to VAS_EBOOT_MOD_INIT. * So we fake this out to avoid a NULL deref. */ static inline void VasEBoot_dl_set_persistent (VasEBoot_dl_t mod __attribute__((unused))) { } /* * Under VasEBoot-emu, modules are faked and NULL is passed to VAS_EBOOT_MOD_INIT. * So we fake this out to avoid a NULL deref. */ static inline int VasEBoot_dl_is_persistent (VasEBoot_dl_t mod __attribute__((unused))) { return 0; } #else static inline void VasEBoot_dl_set_persistent (VasEBoot_dl_t mod) { mod->persistent = 1; } static inline int VasEBoot_dl_is_persistent (VasEBoot_dl_t mod) { return mod->persistent; } #endif #endif VasEBoot_err_t VasEBoot_dl_register_symbol (const char *name, void *addr, int isfunc, VasEBoot_dl_t mod); VasEBoot_err_t VasEBoot_arch_dl_check_header (void *ehdr); #ifndef VAS_EBOOT_UTIL VasEBoot_err_t VasEBoot_arch_dl_relocate_symbols (VasEBoot_dl_t mod, void *ehdr, Elf_Shdr *s, VasEBoot_dl_segment_t seg); #endif #if defined (_mips) #define VAS_EBOOT_LINKER_HAVE_INIT 1 void VasEBoot_arch_dl_init_linker (void); #endif #define VAS_EBOOT_IA64_DL_TRAMP_ALIGN 16 #define VAS_EBOOT_IA64_DL_GOT_ALIGN 16 VasEBoot_err_t VasEBoot_ia64_dl_get_tramp_got_size (const void *ehdr, VasEBoot_size_t *tramp, VasEBoot_size_t *got); VasEBoot_err_t VasEBoot_arm64_dl_get_tramp_got_size (const void *ehdr, VasEBoot_size_t *tramp, VasEBoot_size_t *got); #if defined (__ia64__) #define VAS_EBOOT_ARCH_DL_TRAMP_ALIGN VAS_EBOOT_IA64_DL_TRAMP_ALIGN #define VAS_EBOOT_ARCH_DL_GOT_ALIGN VAS_EBOOT_IA64_DL_GOT_ALIGN #define VasEBoot_arch_dl_get_tramp_got_size VasEBoot_ia64_dl_get_tramp_got_size #elif defined (__aarch64__) #define VasEBoot_arch_dl_get_tramp_got_size VasEBoot_arm64_dl_get_tramp_got_size #else VasEBoot_err_t VasEBoot_arch_dl_get_tramp_got_size (const void *ehdr, VasEBoot_size_t *tramp, VasEBoot_size_t *got); #endif #if defined (__powerpc__) || defined (__mips__) || defined (__arm__) || \ (defined(__riscv) && (__riscv_xlen == 32)) #define VAS_EBOOT_ARCH_DL_TRAMP_ALIGN 4 #define VAS_EBOOT_ARCH_DL_GOT_ALIGN 4 #endif #if defined (__aarch64__) || defined (__sparc__) || \ defined (__loongarch_lp64) || \ (defined(__riscv) && (__riscv_xlen == 64)) #define VAS_EBOOT_ARCH_DL_TRAMP_ALIGN 8 #define VAS_EBOOT_ARCH_DL_GOT_ALIGN 8 #endif #endif #endif /* ! VAS_EBOOT_DL_H */