LoongsonNBL  0.8
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
nbl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025, 2026, Loongson Technology. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  */
6 
7 #ifndef NBL_H
8 #define NBL_H
9 
100 #include <dlfcn.h>
101 #include <elf.h>
102 #include <fcntl.h>
103 #include <signal.h>
104 #include <stdbool.h>
105 #include <stdint.h>
106 #include <unistd.h>
107 
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 
118 typedef enum {
127 
129 #define NBL_MAXPATHLEN 2048
130 
138 typedef struct {
149  void* (*dlopen)(const char* filename, int mode);
150 
158  int (*dlclose)(void* handle);
159 
173  int (*dlinfo)(void* handle, int request, void* arg);
174 
186  char* (*dlerror)(void);
187 
203  void* (*dlsym)(void* handle, const char* name);
204 
224  void* (*dlsym_with_signature)(void* handle, const char* name, const char* nbl_signature);
225 
237  bool (*register_hostfunc)(const void* entry, const char* name, const char* nbl_signature);
238 
250  bool (*register_hostdata)(const void* address, const char* name);
251 
283  bool (*register_hostfunc_with_callback)(const void* entry, const char* name, const char* nbl_signature, const void* c_entry, const char* c_signature);
284 
341  bool (*register_hostfunc_with_callback_arg)(
342  const void* entry,
343  const char* name,
344  const char* nbl_signature,
345  const void* c_entry,
346  const char* c_signature,
347  int callback_arg_index);
348 
372  bool (*register_hostfunc_with_vaargs_policy)(
373  const void* entry,
374  const char* name,
375  const char* nbl_signature,
376  NblVaargsPolicy policy,
377  char vaargs_type,
378  bool include_terminator);
379 
390  bool (*unregister_hostfunc)(const void* entry, const char* name);
391 
403  void* (*method_stub)(const char* nbl_signature, const void* entry);
404 
415  bool (*is_tr_handle)(void* handle);
416 
426  bool (*is_tr_symbol)(const void* addr);
427 
437  bool (*is_tr_signal)(const void* addr);
438 
454  bool (*tr_signalhandler)(int sig, siginfo_t* info, ucontext_t* uc);
455 
463  const void* (*get_extension)(uint32_t extension_id);
464 } Nbl;
465 
490 Nbl* nbl_load(int tr_mode, const char* nbl_lib_base, bool host_dispatch_signal);
491 
496 void nbl_unload(Nbl* nbl);
497 
507 static inline bool is_x86_64_arch(const char *filename) {
508  int fd = open(filename, O_RDONLY);
509  if (fd == -1) return false;
510 
511  bool ret = false;
512  Elf64_Ehdr header;
513  if (read(fd, &header, sizeof(header)) == sizeof(header)) {
514  if (header.e_machine == EM_X86_64) ret = true;
515  }
516 
517  close(fd);
518  return ret;
519 }
520 
536 void set_host_sighandle(int (*host_sig_handler)(int signo, void* siginfo, void* ucontext, int abort_if_unrecognized));
537 #ifdef __cplusplus
538 }
539 #endif
540 
541 #endif
Nbl * nbl_load(int tr_mode, const char *nbl_lib_base, bool host_dispatch_signal)
Create and initialise an Nbl instance.
Definition: nbl.h:120
void set_host_sighandle(int(*host_sig_handler)(int signo, void *siginfo, void *ucontext, int abort_if_unrecognized))
Register a host (LoongArch) signal handler with the current NBL instance so that the translator can d...
NblVaargsPolicy
Variadic-argument handling policy for host function registration.
Definition: nbl.h:118
void nbl_unload(Nbl *nbl)
Destroy an Nbl instance previously created by nbl_load() and release all associated resources...
Definition: nbl.h:125
Dispatch table of NBL runtime functions.
Definition: nbl.h:138