From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 14390 invoked from network); 17 Oct 2023 12:24:55 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 17 Oct 2023 12:24:55 -0000 Received: (qmail 1265 invoked by uid 550); 17 Oct 2023 12:24:49 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 1176 invoked from network); 17 Oct 2023 12:24:48 -0000 Date: Tue, 17 Oct 2023 08:24:44 -0400 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20231017122444.GN4163@brightrain.aerifal.cx> References: <20231016142603.GL4163@brightrain.aerifal.cx> <20231016215307.GE1427497@port70.net> <20231016220410.GM4163@brightrain.aerifal.cx> <20231017082800.GF1427497@port70.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231017082800.GF1427497@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Getting access to section data during dynlink.c On Tue, Oct 17, 2023 at 10:28:00AM +0200, Szabolcs Nagy wrote: > * Rich Felker [2023-10-16 18:04:11 -0400]: > > On Mon, Oct 16, 2023 at 11:53:07PM +0200, Szabolcs Nagy wrote: > > > note that (not too old) bfd ld and lld defines a hidden linker symbol > > > __ehdr_start that at runtime resolves to where the ehdr is. > > > > > > example: > > > > > > #include > > > #include > > > > > > __attribute__((visibility("hidden"), weak)) extern char __ehdr_start[]; > > > > > > int main() > > > { > > > if (__ehdr_start) { > > > Elf64_Ehdr *ehdr = (void *)__ehdr_start; > > > printf("ehdr %p\n", ehdr); > > > Elf64_Phdr *phdr = (void *)(__ehdr_start + ehdr->e_phoff); > > > printf("phdr %p\n", phdr); > > > } else > > > printf("__ehdr_start is undefined\n"); > > > > > > // to compare against the actual mappings > > > char buf[9999]; > > > FILE *f = fopen("/proc/self/maps","r"); > > > size_t n = fread(buf, 1, sizeof buf, f); > > > fwrite(buf, 1, n, stdout); > > > } > > > > > > this should work for 64bit elf exe if ehdr is mapped into memory. > > > > > > if you want link time error on an old linker instead of 0 __ehdr_start, > > > then just drop "weak" and the runtime check. (the code as written assumes > > > ehdr is not at exact 0 address, which is guaranteed by usual linux setups) > > > > Interesting -- perhaps we should find a way to use this in ldso to > > find its own ehdr. > > for that use it is a bit target specific: > the symbol address computation must be pc-relative with no dynamic reloc, Indeed, that's what makes it difficult. crt_start.h could compute it along with _DYNAMIC, but that's more per-arch burden I would not like to see, and it's not clear how it would distinguish the undefined case if we're supporting that. > e.g. 'weak' would create a got reloc so not usable before relocs are done. A GOT reloc for a hidden symbol will be relative and already resolved by dlstart.c. I'm not sure if we're making use of such a property right now but it seems reasonable to do so; the symbol name cannot exist in a form satisfiable by the symbolic relocations performed later, so it must have been done at this point. At first I was thinking of storing the address in a static var that dlstart.c would have filled in, but this seems no better than (and equivalent to) just letting the GOT do its thing. Rich