Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig Bindgen - references #1

Open
kassane opened this issue Jun 25, 2023 · 3 comments
Open

Zig Bindgen - references #1

kassane opened this issue Jun 25, 2023 · 3 comments

Comments

@kassane
Copy link
Contributor

kassane commented Jun 25, 2023

Previously I've been working with cbindgen which transpiles rust code into other languages, commonly used for C. However I've added zig support.

During this experiment I realized that zig can read mangled functions in both C++ and Rust (in the same way as c++filt) with the help of lld it can demangling functions (linked to libc++ + libunwind).

The best thing is the possibility of static linking in libraries written in these languages. (e.g. incubator-opendal w/ zig binding and zFFI)

Another Reference

@lassade
Copy link
Owner

lassade commented Jun 26, 2023

Very interesting! I will give a look into it but I'm maily interested in c++

one of my ideas with c2z is to have a copy of the memory model of c++ classes and structures in the zig side so we can just access them without the need to use opaque pointers

@kassane
Copy link
Contributor Author

kassane commented Jun 26, 2023

I don't know if it would be possible to retrieve the name of the translated functions using @extern to rename mangled functions and namespace.

Making it shorter, it is a possible alternative:

- extern fn @"_Z9enumerateRNSt3__16vectorIhNS_9allocatorIhEEEEm"(out_buf: *cpp.Vector(u8), count: usize) bool;
- pub const enumerate = @"_Z9enumerateRNSt3__16vectorIhNS_9allocatorIhEEEEm";
+ pub const enumerate = @extern(*const fn (out_buf: *cpp.Vector(u8), count: usize) bool, .{ .name = "_Z9enumerateRNSt3__16vectorIhNS_9allocatorIhEEEEm" });

I remember partially translating a C solution for demangling C++ and Rust functions. Original project used in mold linker is updated to v0 mangling, not C++ compatible.

https://github.com/kassane/rsDemangle-zig (legacy mangling, c++ compatible) based in old gist version.

@kassane
Copy link
Contributor Author

kassane commented Jun 28, 2023

Mangling for x86/x86_64 ABI:

abi_tag

sample
#include <vector>

__attribute__((abi_tag("cxx11")))
bool enumerate(std::vector<uint8_t>& out_buf, size_t count);

output

$>objdump -x c013_cpp_vector.obj | grep 'enumerate'
[924](sec  1)(fl 0x00)(ty   20)(scl   2) (nx 0) 0x0000000000000000 _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEy
00000000000000c4 IMAGE_REL_AMD64_SECREL  _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEy
00000000000000c8 IMAGE_REL_AMD64_SECTION  _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEy
0000000000000180 IMAGE_REL_AMD64_SECREL  _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEy
0000000000000184 IMAGE_REL_AMD64_SECTION  _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEy
$> objdump -x c013_cpp_vector.o| grep 'enumerate'                                      
0000000000000000 g     F .text  00000000000000b9 _Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEm

Z9enumerateB5cxx11RNSt3__16vectorIhNS_9allocatorIhEEEEm | => cxx11 tag added

GNU x LLVM x MSVC

Issue related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants