From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by sourceware.org (Postfix) with ESMTPS id B7B0B3858C83 for ; Tue, 1 Feb 2022 17:14:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B7B0B3858C83 Received: by mail-yb1-xb35.google.com with SMTP id p5so52892202ybd.13 for ; Tue, 01 Feb 2022 09:14:35 -0800 (PST) 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:cc; bh=tP2GK0cf4jMzoKzDGCYbVUyrKB8ppOr9k84lAgPXjK8=; b=OrVXLVd8e0SEMhFqF9T9KwZU5s7V+e7JymYxrsHqyCoiKRiRPp8LZZmrm+C1E76vuj G8UjqyrClDGloICV/zBQY3IESrbBbkYewxrQ0gl0rAOsDtrgmUtkIVWY++tVE3i0qmeL TQEE+j+J0bCu6qfCZHZOTXcvELPGKUOyd/RIcblLAd0vneXp01nL78ZNhJYuAsypi2cn e82xZzPDxHaiILDmeJ88LZhJTPs4+M2WnP8McysSe2FzMNfebnJIILZwveNG5LYybuJM 8vmvuLgcfFSWwhg57iTnD+OcIJcqEDc6qnZfNt97nEv6sRelJuD+NYAQLGpD7h1DpGE5 fdHw== X-Gm-Message-State: AOAM532gKFxgCpvnGN4NO5LQbyLmAfymZX9FK5iwXCemJFs/LEzsSG8d M1Yfgq8vOAi5BV0K2KNMOCGP/hW1RjHqO7X1B71Zkw== X-Google-Smtp-Source: ABdhPJxwJnOJgXwLLwa9EeDeikB3/SW+4X6VNX4vqJ+BavgmWgIhswLULfQXJdgL/4XmqjCx2OldRrYRG84Fz3JCcBI= X-Received: by 2002:a25:5057:: with SMTP id e84mr38918904ybb.404.1643735675030; Tue, 01 Feb 2022 09:14:35 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Farid Zakaria Date: Tue, 1 Feb 2022 09:14:24 -0800 Message-ID: Subject: Re: How can I wrap ld-linux or execve into it? To: Adhemerval Zanella Cc: Libc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: Tue, 01 Feb 2022 17:14:37 -0000 Thank you -- I need to parse your solution more in depth. I will also take a look more at rtld -- however I was hoping to be compatible with musl as well. I was told by a colleague that execve solution may work if the static binary has "no relocations". How can I control that through the compilation of a program beyond limiting myself to a single object-file? On Tue, Feb 1, 2022 at 4:43 AM Adhemerval Zanella wrote: > > > > On 31/01/2022 17:10, Farid Zakaria via Libc-help wrote: > > Hi, > > > > I am looking to perform some functionality before the dynamic linker > > (linux-ld/ld.so) is invoked. > > > > My naive assessment was that I would be able to set in the PT_INTERP > > section of a binary, my *static binary*, which will then execve into > > the dynamic linker after doing some precanned actions. > > > > Unfortunately, trying this has resulted in some SIGSEGV... > > > > I came across https://github.com/Mic92/nix-ld which seems to do > > something similar, but I was curious why it has to do a lot more to > > achieve the same effect with a jump. > > > > I have also been pointed to LD_AUDIT however I am also interested in > > having it agnostic to libc (glibc vs. musl) > > > > Thank you for any tips, guidance or links you can provide. > > FZ > > The issue is for static linking _dl_aux_init will setup the _dl_phdr > to the loaded binary (since it was done by the kernel) passed on auxiliary > vectors: > > elf/dl-support.c: > > 246 void > 247 _dl_aux_init (ElfW(auxv_t) *av) > 248 { > [...] > 269 case AT_PHDR: > 270 GL(dl_phdr) = (const void *) av->a_un.a_val; > 271 break; > [...] > > And this is later used to setup the TCB: > > csu/libc-tls.c > > 104 void > 105 __libc_setup_tls (void) > 106 { > [...] > 120 /* Look through the TLS segment if there is any. */ > 121 if (_dl_phdr != NULL) > 122 for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) > 123 if (phdr->p_type == PT_TLS) > 124 { > 125 /* Remember the values we need. */ > 126 memsz = phdr->p_memsz; > 127 filesz = phdr->p_filesz; > 128 initimage = (void *) phdr->p_vaddr + main_map->l_addr; > 129 align = phdr->p_align; > 130 if (phdr->p_align > max_align) > 131 max_align = phdr->p_align; > 132 break; > 133 } > > The problem is seice _dl_phdr is not pointing to the static programs acting > as loader, the PT_TLS is not considered and thus not initialized correctly. > That's why once __ctype_init tries to access TLS variables it triggers an > invalid memory reference: > > (gdb) c > Continuing. > > Program received signal SIGSEGV, Segmentation fault. > 0x000000000045f464 in __ctype_init () at ctype-info.c:31 > 31 *bp = (const uint16_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_CLASS) + 128; > > > And I don't think this would be easy to support without changing a *lot* > on static linking organization. If you check the loader code, it avoids > to use TLS exactly to avoid this initialization issue. > > I think the best option to work by checking elt/rtld.c and see if you can > hack a way link you code after its initialization.