From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Aleix_Conchillo_Flaqu=C3=A9?= Newsgroups: gmane.lisp.guile.devel Subject: Re: crash on macOS with dlsym RTLD_LOCAL Date: Sat, 11 Sep 2021 22:14:26 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6019"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Sun Sep 12 07:14:58 2021 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mPHpS-0001Lw-AN for guile-devel@m.gmane-mx.org; Sun, 12 Sep 2021 07:14:58 +0200 Original-Received: from localhost ([::1]:59598 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mPHpQ-0003Jj-7S for guile-devel@m.gmane-mx.org; Sun, 12 Sep 2021 01:14:56 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47092) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mPHpB-0003JX-59 for guile-devel@gnu.org; Sun, 12 Sep 2021 01:14:41 -0400 Original-Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]:40880) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mPHp9-0006AN-8z for guile-devel@gnu.org; Sun, 12 Sep 2021 01:14:40 -0400 Original-Received: by mail-pf1-x431.google.com with SMTP id y8so335440pfa.7 for ; Sat, 11 Sep 2021 22:14:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=0uMbuyZyu4Xv/h7KNdl/5g1V/MwvJQbXyyIZsKh+vK4=; b=BqQ4VLJkIyLq8l8L274bjcKIsFLkcjYpPYLUkb8zrBgqzLWw3fRLjs+ERa/HlUTKpu wZ1kt3EB87ZJCmUH0j0Kqt3QRBz3CoTh6fNNFZCWiZKSbykoAeoaQyq4YiYMClYxo7Fg CoWDentTBAhRfVRdlyIobMkB+Cqr2du4X+qdxFefAelvwO31xVlgH+qjEBXXQe7CQVx2 9QJYf2RbK8l3Ahois2jr8XY9ubsRhMQJV7mCt6bc/9YXG9Gay00FLvp0j945HldyIaQ0 G+Zupv95TMCAxNfLevhGN5uPSdNw17arEcWiw642Is8bSNetQG9AO3nDWcbNLO/jwF1n SH0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=0uMbuyZyu4Xv/h7KNdl/5g1V/MwvJQbXyyIZsKh+vK4=; b=T4Z9v9K4fIdzvkXmHww5tPp6juOIJbUBy3WNbpeNH1ud4+Tsy7unLifK5InJ9rSlAi 2x6+/HnwRpiRh1c4UbTt6gzdzOwKPABOc1Q1f9xFBlay/I0w1nPma6pjWTjbvu1le9kg /CGQZVwdFDsLKQO1u6XBe1if7C7fuOovNNYvbbry02BcZAy3jJfh3lILrpEIEcCzopsi p7THlQ8uvPRyfZhKWaqRB5bAnUzxrj9Nz8nkdILuLWOQUEhUCNpIsqiXOJXkOXZpvR8g DN0e8htcL4zpdjBITQY/OnPC8M0wlO5N18RJHHte0xjxRpw2HNmOSo3omXnEZ5hp3Nvg G/sw== X-Gm-Message-State: AOAM533Z4EYmcobzo4KPWCqvKeSbpONkqTmA3+Mi5EgkzeXpxfBBPbl7 MsltLhfJvpFVf+jppBeMXmdVUgDYydwpZ4vJckzcVG622No= X-Google-Smtp-Source: ABdhPJw4pJIrDTaCIh92DmKo0jv3v4WBRBiSmBvC9Ln6l7flaml81GlRfra9nno6PfrqxzVJqCtLkRyB3K9ynIMPnC4= X-Received: by 2002:a63:35cc:: with SMTP id c195mr5184996pga.373.1631423677245; Sat, 11 Sep 2021 22:14:37 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=aconchillo@gmail.com; helo=mail-pf1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20857 Archived-At: I did a little bit more research. The combination that actually fails is RTLD_LAZY | RTLD_GLOBAL (what guile uses). So, you can actually use: - RTLD_LAZY | RTLD_GLOBAL - RTLD_NOW | RTLD_GLOBAL - RTLD_NOW | RTLD_LOCAL I found the source code of dyld https://opensource.apple.com/source/dyld/dyld-852.2/ But I have no clue how to build this, I tried it but failed with missing dependencies. In any case, I provided a patch so at least we can use Guile on macOS. Best, Aleix On Sat, Sep 4, 2021 at 10:48 PM Aleix Conchillo Flaqu=C3=A9 wrote: > > Hi, > > I'm trying to load guile-grcypt with guile 3.0.7 and after my previous > fix (https://git.savannah.gnu.org/cgit/guile.git/commit/?id=3D1f100a4f20c= 3a6e57922fb26fce212997e2a03cb) > guile-gcrypt still does not load properly. > > TLDR; Using RTLD_LOCAL instead of RTLD_GLOBAL (default) causes issues on = macOS. > > https://git.savannah.gnu.org/cgit/guile.git/tree/module/system/foreign-li= brary.scm#n181 > > In the case of guile-gcrypt this is what I'm getting: > > ------------------------- > scheme@(guile-user)> (use-modules (gcrypt hmac)) > dyld: lazy symbol binding failed: Symbol not found: __gcry_check_version > Referenced from: /usr/local/lib/libgcrypt.dylib > Expected in: flat namespace > > dyld: Symbol not found: __gcry_check_version > Referenced from: /usr/local/lib/libgcrypt.dylib > Expected in: flat namespace > ------------------------- > > Looking at gcrypt symbols I can see: > > =E2=9D=AF nm -gU /usr/local/lib/libgcrypt.dylib | grep gcry_check_version > 0000000000005954 T __gcry_check_version > 0000000000002aa7 T _gcry_check_version > > Actually in libgcrypt the public functions are: gcry_check_version and > _gcry_check_version (no extra underscore). I think extra underscores > are automatically added when building the library but I'm not sure why > and when and what systems. And when you call dlsym() you don't need to > add those extra underscores. > > The following code (which uses RTLD_GLOBAL) works fine: > > ./a.out gcry_check_version > > ------------------------- > #include > #include > > int > main(int argc, char *argv[]) > { > char* (*fptr)(char *); > void *handle =3D dlopen("/usr/local/lib/libgcrypt.dylib", RTLD_LAZY | > RTLD_GLOBAL); > if (handle =3D=3D NULL) { > printf("OOOPS: %s\n", dlerror()); > } else { > *(void **)(&fptr) =3D dlsym(handle, argv[1]); > if (fptr =3D=3D NULL) { > printf("NOT FOUND: %s : %s\n", argv[1], dlerror()); > } else { > printf("FOUND: %s %s\n", argv[1], (*fptr)(NULL)); > } > } > return 0; > } > ------------------------- > > But if we change to RTLD_LOCAL we get a crash like in guile's case. > > Sorry for being too vague but I'm not familiar with how all this > works. All I know is that using RTLD_GLOBAL fixes the issue. > > Also, from dlopen man page I read: > > ------------------------- > RTLD_GLOBAL Symbols exported from this image (dynamic library or > bundle) will be available to any images build with -flat_namespace > option to > ld(1) or to calls to dlsym() when using a special handl= e. > > RTLD_LOCAL Symbols exported from this image (dynamic library or > bundle) are generally hidden and only availble to dlsym() when > directly using > the handle returned by this call to dlopen(). > ------------------------- > > But I don't fully get what this means. > > Any help would be really appreciated. > > Thank you, > > Aleix