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 B8AA23851C0C for ; Fri, 20 Nov 2020 06:52:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B8AA23851C0C Received: from smtp-out.all-for-one.com ([91.229.168.76]) by mx-relay96-hz2.antispameurope.com; Fri, 20 Nov 2020 07:52:43 +0100 Received: from bruexc101.brumgt.local (10.251.3.120) by bruexc101.brumgt.local (10.251.3.120) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 20 Nov 2020 07:52:35 +0100 Received: from bruexc101.brumgt.local ([fe80::8c25:ac7:3af5:21bb]) by bruexc101.brumgt.local ([fe80::8c25:ac7:3af5:21bb%19]) with mapi id 15.00.1497.007; Fri, 20 Nov 2020 07:52:35 +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, 20 Nov 2020 06:52:35 +0000 Message-ID: <11e3703d900d48149d0f81ae7682480f@bruexc101.brumgt.local> 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 A10569A0064 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:.5261 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, HTML_MESSAGE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS 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, 20 Nov 2020 06:52:47 -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 : The destructor ~shared_library() calls dlclose(), but I suspect the library= stays loaded. Overwriting the file creates a new file node and my program = wants to load the same library again (at the same location but with a diffe= rent file node/handle). 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