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

Linking libraries not working? #17

Open
xsawyerx opened this issue Jun 15, 2022 · 3 comments
Open

Linking libraries not working? #17

xsawyerx opened this issue Jun 15, 2022 · 3 comments

Comments

@xsawyerx
Copy link

I've installed FFI::TinyCC and wrote the following code:

use FFI::Platypus 1.00;
use FFI::TinyCC;

my $tcc = FFI::TinyCC->new();
$tcc->detect_sysinclude_path();
$tcc->add_library('ssl');
$tcc->add_library('crypto');

$tcc->compile_string(q!
#include <openssl/bio.h>

BIO *create_bio_buffer(const char *string) {
    BIO* bio = BIO_new_mem_buf( string, -1 );
    return bio;
}
!);

my $ffi     = FFI::Platypus->new();
my $address = $tcc->get_symbol('create_bio_buffer');
$ffi->attach( [ $address => 'create_bio_buffer' ] => ['string'] => 'opaque' );
my $bio_buf = create_bio_buffer('hello world');

This dies with an empty string as the error. The $tcc->{'error'} is empty, so the exception object is empty. The entire error string is empty because as_string adds a newline to it.

If I try to use $tcc->set_options('-lcrypto -lssl') I get:

tcc: error: undefined symbol 'BIO_new_mem_buf'
tcc: error: '_etext' defined twice
tcc: error: '_edata' defined twice
tcc: error: '_end' defined twice
tcc: error: '__preinit_array_start' defined twice
tcc: error: '__preinit_array_end' defined twice
tcc: error: '__init_array_start' defined twice
tcc: error: '__init_array_end' defined twice
tcc: error: '__fini_array_start' defined twice
tcc: error: '__fini_array_end' defined twice

Which shows it didn't link the libraries.

However, this works:

$tcc->add_file('/usr/lib/x86_64-linux-gnu/libcrypto.so');
$tcc->add_file('/usr/lib/x86_64-linux-gnu/libssl.so');

If I try tcc directly, it does work, which is why I'm thinking the add_library and set_options themselves don't work right:

/.../Alien-TinyCC/bin/tcc -I/.../Alien-TinyCC/lib/tcc/include/ -I/usr/include -lcrypto -lssl -c -shared foo.c -o foo.so
# then use Platypus::FFI with foo.so as expected
@plicease
Copy link
Member

plicease commented Jun 16, 2022

Hrm. I think we just pass these functions directly to libtcc so we might not be able to do anything about it. I will investigate further though. As a workaround FFI::CheckLib combined with add file would allow you to do the same without having to provide the full paths:

use FFI::CheckLib qw( find_lib_or_die );
$tcc->add_file(find_lib_or_die lib => $_) for qw( crypto ssl );

@xsawyerx
Copy link
Author

That's what I ended up doing. I should've updated the ticket with that info. Sorry.

@plicease
Copy link
Member

That's what I ended up doing. I should've updated the ticket with that info. Sorry.

no problem, I just wanted to make the suggested in case this was blocking you. I still want to just make sure there isn't anything we can't do in this module, but probably won't be able to investigate more closely until after TPC.

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

No branches or pull requests

2 participants