From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5106 invoked by alias); 24 Jul 2012 15:21:56 -0000 Received: (qmail 5098 invoked by uid 22791); 24 Jul 2012 15:21:55 -0000 X-SWARE-Spam-Status: No, hits=-3.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,KHOP_THREADED X-Spam-Check-By: sourceware.org Received: from localhost (HELO sourceware.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 24 Jul 2012 15:21:44 +0000 From: "luto at mit dot edu" To: glibc-bugs@sources.redhat.com Subject: [Bug dynamic-link/14379] shared object constructors are called in the wrong order Date: Tue, 24 Jul 2012 15:21:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: dynamic-link X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: luto at mit dot edu X-Bugzilla-Status: WAITING X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact glibc-bugs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: glibc-bugs-owner@sourceware.org X-SW-Source: 2012-07/txt/msg00201.txt.bz2 http://sourceware.org/bugzilla/show_bug.cgi?id=14379 --- Comment #2 from Andy Lutomirski 2012-07-24 15:21:19 UTC --- Here's a silly malloc replacement library: #include #include static char *arena; __attribute__((constructor)) static void init() { arena = (char*)mmap(0, 1 << 24, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } void *malloc(size_t n) { void *ret = arena; arena += n; return ret; } void *realloc(void *ptr, size_t size) { abort(); } void free(void *p) {} IMO it should work (as long as no one tries to use realloc, calloc, or threads, but this is just an example). Here's a rather ordinary shared library: #include __attribute__((constructor)) static void init() { *(char*)malloc(1) = 0; } This is, of course, silly, but in C++, dynamic allocation due to global object constructors is common. If I try to use that library with the malloc replacement LD_PRELOADed, it crashes: 19797: calling init: /lib64/ld-linux-x86-64.so.2 19797: 19797: 19797: calling init: /lib64/libc.so.6 19797: 19797: 19797: calling init: /lib64/libgcc_s.so.1 19797: 19797: 19797: calling init: /lib64/libm.so.6 19797: 19797: 19797: calling init: /lib64/libstdc++.so.6 19797: 19797: 19797: calling init: ./test_lib.so 19797: Segmentation fault The malloc library (silly_malloc.so in this case) wasn't initialized. I think that, in any sensible initialization order, LD_PRELOADed libraries would initialize first, not last. (Maybe libraries in the DT_NEEDED list of the LD_PRELOADed library should initialize even sooner, but that's irrelevant here.) My comment about initializing deeper dependencies first is something that current glibc does right. I don't think that should change. -- Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are on the CC list for the bug.