From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by sourceware.org (Postfix) with ESMTPS id 8DA063858D28 for ; Sat, 27 Jan 2024 10:56:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8DA063858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8DA063858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::52c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706353018; cv=none; b=wq1cgdHPz3XykPUYJNBn9S64g3vKRqeewhytHasXcTkZArTkGV4NoLRc4rm08yRzMCkB2EutbeP/zL/7vEFhrS2yDd6u24nbzqlhXbNcsKiPhaLnlLUFCtMAsfhnsBYeGNNcMTZZMLXWMx+Ibv+T/HdI+akgITGcBMUGBx4+N10= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706353018; c=relaxed/simple; bh=anjpOwf8/3CmUqDU7FvObfOk1YQ4H4Qo1zuhjPRuz0c=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=Z2K5uQHXkg1OUxkDXStQ53NTu8h6ZAhJ9bTNLNY71eivgDdMWlBfZC+2jysA2xdo/YXbxOuoQqSa/RtkbCuCEc6TiLJ8nRLpjXEM1/rLnEsKdvbiHkalkaxNqiic+PnbONINNtWVi7PS7aXND7THEnfqCXGZfE/Weq4cgtEflhk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-55a45a453eeso1985353a12.0 for ; Sat, 27 Jan 2024 02:56:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706353013; x=1706957813; darn=sourceware.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=OWt0zxmHqiSYhKH1/3tmV4eUYOPbryLA6tNz8CuQtrQ=; b=fAm6AjBk4YH6qIMOuYsZxzZq5CYigKq0GfJWOZOyNzeG1wGmDmAXqghYuzmgLikSlX b4Zt93uw0guPLX+8eLP+VwEJEnq5zOnEl/n2RiFb9LiC4piEjJnpXXWaLz/+zid3Y0Nc 8d5FlJUGOf/zvax1FMgQiVyDtg2qELCYLwo/xLHZ16OD9F3CCKf0CGcb+AKyVWpBHGyp cMNOTytb5QU7SSdhuEW3nTnynV3l2DsMyZcYUWa3yEj/qOJi0Gm2gAWsAgCfr7JB/nXH sJbeZqvAspSeqSdmubiUFWVkJPuKCnlC9XIdovAfTmmS75A90bIjMFskfl2G1rWtgGPC tkRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706353013; x=1706957813; h=content-transfer-encoding:cc:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=OWt0zxmHqiSYhKH1/3tmV4eUYOPbryLA6tNz8CuQtrQ=; b=fy0c/IGDVfaIm0w/lqCYJFICWQtQI7xLr7Qksj5Iaw/GCF21/NrXHQiD2rBx6k8+cV kQSblUyU4kfkF5SRbwX20g8154EjHsPcqMQ9yF2NUifu53YkmCfqMu74mpTUN1D341xm pJRLoTNqMSV/L2WIYyNioIRAY9VKIFi2qfHe7DySzlpXnEF+0xCChDTnNdgR5NEpfERP L3Q9HYSSzVzVxhKT14KyHXIgkkK/Ws5yGHX4icccuHSeTEFiXfWQ3y4SsXOffkcoWoiG IZIZkH9GxCumPTypCux7F8SB76UH167Kip45ziLH9ebocHzxDJ/C8wUVZwZcH9Ppd36i ETtw== X-Gm-Message-State: AOJu0YyGH/qc12E+MpKgfUJa5eLtCN3OU79cZMkB2pFHlVC56VCyI+2t Z5bjAIgXlWfdryGU3JJchcE2a54Yh33Zzs2cQZMuG7xzh+U/fKlG+877siHGqHDBKxXWZGfMWAD 0i0VVq1hrpaRC3oEt2QVKlAC5ACSZGgzqjOE= X-Google-Smtp-Source: AGHT+IH6vfBvNWRK0IjAdWujZ/LZ/TgdqFQC4ClbVEoldD//N7GWHIUiPfGEZbwSpJMJ7bMAAF3lOgOT9ZOD0rG/Ohs= X-Received: by 2002:aa7:cfd9:0:b0:55e:c6e4:eca2 with SMTP id r25-20020aa7cfd9000000b0055ec6e4eca2mr162000edy.23.1706353012845; Sat, 27 Jan 2024 02:56:52 -0800 (PST) MIME-Version: 1.0 References: <51e942dd-dbe0-f644-e9bb-0e12fc69f076@suse.de> In-Reply-To: Reply-To: cauldwell.thomas@gmail.com From: Frederick Virchanza Gotham Date: Sat, 27 Jan 2024 10:56:44 +0000 Message-ID: Subject: Re: Linker : Make a map of typeinfo to vtable To: binutils@sourceware.org Cc: Michael Matz Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Jan 25, 2024 at 11:02=E2=80=AFPM Frederick Virchanza Gotham wrote: > > So then after I call 'ldwrite' and have produced the final binary file, I > re-open the binary file and I scan through it until I find my magic > number (i.e. 3c1fed6f74bb6c814173c5409cb7d0aa), and I replace it with > the correct data. I wasn't able to get much help with how 'ld' or 'libbfd' works so I had to get creative. I have gotten my new feature for the GNU bfd linker working, here's my latest code: https://github.com/bminor/binutils-gdb/compare/8409b75...healytpk:linke= r-vtable:e4adc46 Here's what I had to do: (1) In 'main', I add another string to the end of 'argv' for another object file (e.g. "/tmp/linker_9821421740127.c.o"). This object file doesn't exist yet. (2) In '_bfd_generic_link_add_one_symbol', I keep a record of all typeinfo's and vtables encountered. I keep a count of matching pairs. (3) In 'load_symbols', I keep a watch out for "/tmp/linker_9821421740127.c.o", and I generate this object file at the point it's requested by using 'gcc' to compile a simple C source file which contains a definition for the array '__map_typeinfo_vtable'. The size of this array depends upon the count of matching pairs. (4) The newly-generated object file contains the symbol '__map_typeinfo_vtable' inside the section ".data.rel.ro", but the array is all zeroes. (5) After 'ldwrite' is called and the final output file has been written, I then check what the offset of each symbol is by looking at "link_info.output_bfd". I make an array of these offsets and I sort it. (6) I re-open the output file in binary mode, and I seek to the location of '__map_typeinfo_vtable', and I write the real array contents to the file. I have this tested and working. I've written a C++ program that does a binary search through '__map_typeinfo_vtable' in order to map a typeinfo to a vtable, and I can then use that to convert a virtual function pointer into a direct function pointer. It's cool that I've gotten this working but it's a little bit Frankensteinish in places. Instead of appending a new object file to the end of the command line, it would be nice if I could just call a function in 'libbfd' to manually add the symbol '__map_typeinfo_vtable' -- but I don't know how to do this. And instead of re-opening the output file and overwriting the zeroed-out array, it would be nice if I could write it correctly the first time. If anyone has any pointers here, please help me out. Here's my latest post on the C++ proposals mailing list showing my C++ code: https://lists.isocpp.org/std-proposals/2024/01/8948.php I'm hoping to either put this linker up on GodBolt or to host it on my own webspace if I can.