From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx-relay96-hz2.antispameurope.com (mx-relay96-hz2.antispameurope.com [94.100.136.196]) by sourceware.org (Postfix) with ESMTPS id D8EE63857C5F for ; Fri, 4 Dec 2020 10:39:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D8EE63857C5F Received: from smtp-out.all-for-one.com ([91.229.168.76]) by mx-relay96-hz2.antispameurope.com; Fri, 04 Dec 2020 11:39:29 +0100 Received: from bruexc101.brumgt.local (10.251.3.120) by bruexc102.brumgt.local (10.251.3.117) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 4 Dec 2020 11:39:17 +0100 Received: from bruexc101.brumgt.local ([fe80::d813:865c:81af:78ee]) by bruexc101.brumgt.local ([fe80::d813:865c:81af:78ee%19]) with mapi id 15.00.1497.008; Fri, 4 Dec 2020 11:39:18 +0100 From: "Wendeborn, Jonathan" To: "libc-help@sourceware.org" Subject: dlopen: Segfault due to overwriting .so file after it was loaded and loading it again Thread-Topic: dlopen: Segfault due to overwriting .so file after it was loaded and loading it again Thread-Index: Ada/CSa/2mu6BVvbR5Wacq3j1xIxJQ== Date: Fri, 4 Dec 2020 10:39:17 +0000 Message-ID: Accept-Language: de-DE, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.251.3.71] MIME-Version: 1.0 X-cloud-security-sender: jonathan.wendeborn@bruker.com X-cloud-security-recipient: libc-help@sourceware.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mx-relay96-hz2.antispameurope.com with A20309A0422 X-cloud-security-connect: smtp-out.all-for-one.com[91.229.168.76], TLS=1, IP=91.229.168.76 X-cloud-security: scantime:.5474 X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, HTML_MESSAGE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Dec 2020 10:39:39 -0000 Hi, I am a C++ developer but usually programming and debugging on Windows (so p= lease excuse any wrong terms). Now I'm compiling my program on Linux (gcc 9= .3.0 on Debian Bullseye with Boost 1.70) for the first time and get a Segfa= ult in my unit tests. Luckily I was able to write a reproducer and boil it down to my code overwr= iting the .so file after having it loaded (and unloaded): #include #include #include void doit() { boost::filesystem::copy_file("~/project/target/references/bin/libSometh= ing.so", "~/project/build/bin/ linux-x86_64-gcc9-debug/ libSomething.so", b= oost::filesystem::copy_option::overwrite_if_exists); boost::dll::shared_library l; std::cout << "pre load" << std::endl; l.load("./libSomething.so"); std::cout << "loaded" << std::endl; } int main() { doit(); doit(); return 0; } Output: pre load loaded pre load loaded Segmentation fault When removing the copy_file() call everything is fine. The destructor ~shar= ed_library() calls dlclose(), but I suspect the library stays loaded. Overw= riting the file creates a new file node and my program wants to load the sa= me library again (at the same location but with a different file node/handl= e). This works on Windows because the library is really unloaded after ~shared_= library() (otherwise copy_file() would fail as Windows does not support ove= rwriting files in use anyway). I did debug into dlopen() and think the error gets visible in dl_lookup_x()= : In there the strtab and symtab pointers don't have valid pointers the sec= ond time, i.e. they have the quite small value from the beginning of elf_ge= t_dynamic_info() (l.51), the l_addr offset from the second part of elf_get_= dynamic_info() wasn't added (l.104). Sure I'm going to rewrite my tests (I'm going to not copy the files at all = anymore) but I thought this could be of interest for you. Best regards, Jonathan