From patchwork Mon Sep 30 21:00:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_Storsj=C3=B6?= X-Patchwork-Id: 98186 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 518833860760 for ; Mon, 30 Sep 2024 21:00:50 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id D9B1D385EC15 for ; Mon, 30 Sep 2024 21:00:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D9B1D385EC15 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=martin.st Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=martin.st ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D9B1D385EC15 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727730022; cv=none; b=OP9T9lNSlVamWl5OWYovh/bJ8mSc2cNaXTKNzyHwlZ9K2MZAxzI07UEJLtRP9eGG1JRGrKohdloDUI82/gNNPoIaDK6l0sUaVjxpc81E+2EPGncz6/ALDICVRGc3sQ7NDe2Mp+eXhjAnOHkS1ER0mWkp7rbDjS26JBZizf7tFik= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727730022; c=relaxed/simple; bh=PJH4Y6xwLP+8N87jzyVm/+XlQU/6BVFRMXdczSwkIAY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=bUcrUNYJTUl70ydZx9gRTDeB0evGR806lJQ54W/OJWe8lSLgXk9gNnCGqdtQyswBLDIbxJuH9wxKxIILk4SRIKIDadRDFXgc/Q1kSNbV+73vHMrxVPlK4MFkinZe4KjKyhsHgjGLLnctA5bXLVaVEArfGKffqnUSIWRr+icVQ/g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-53959a88668so4013941e87.2 for ; Mon, 30 Sep 2024 14:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1727730014; x=1728334814; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=KtcIUuBUwc39ocWrl+vn6Fa6r/bpj7MLAUtteTc076Y=; b=0jqMd2YC26Jg0AYrtiAAkTIw6P/SmG5hRf9kSW30Ewh4H7nz//xf7T/yOITp+aalJx /wSnU6+jMuLGj8Fd0Ka7lTwVmouXlyU2mFgNGLWc38yxESXt618aPzrLrHmq2aVAF0IP 7Jmx3eYNnfqv2l8shPrJwbI+T5CtM0kTJmw7tggtxEajPjluKaCLZ+tWvUp9INNvk35D H64QFq8J5mzw23JjzSbyDtSWWBz0Br6+1qwTIGfE0AURWMy1oY9iHJIG8+QO9CS9S0M9 BMWmYfse13EzC1PeaJVC9Sm9YE7yaZBq4hKsJnzJssLiAU31c1pKepIyEArpXd3vf9P2 WqPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727730014; x=1728334814; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KtcIUuBUwc39ocWrl+vn6Fa6r/bpj7MLAUtteTc076Y=; b=AFX3KLgWeTTRcdpSBitDrTYpkZJdSQNiVfO/UowjHH0/mug54Hwe6DdBRlSJokZmki x3+XdEZ5SrK1l2l3PmVElTA+lrK0pl/5Y+IBKV6Mqaet14vYjMqhaybvWsZ4AQECJi+4 /9l8Z9mqiZacr28wIQrB3gZtDY2zwG/TIDZvWARmy7wk1wizyleGEojXbTPaCDbfEdAI Lpdab1hktSwsSIl1TlgFr44k4sovHdjeA3KnNe+9lVFpb8bIOA5YVKezd3qgjjR/Wmg/ wPnaRA53BtIR3Qb9nmI+tYyh8nxHPdmx2MEMAuKjdkFNwd1Rw6GRF59zVSRLkuHdMB67 Vysg== X-Gm-Message-State: AOJu0YxSpfWtJNrPALr35Fj02ywnmqNK5EbpOQMfidx1JH0IFm2x2HIY cCEu1/NJjIVM+9UBFk2lOoQxBnOhazjoDF2oX0Cmp/dvfCM1MelXoYISEseWKuszmy7W7pcRk/d g9A== X-Google-Smtp-Source: AGHT+IEADqiGsOYdnkSLXr7bIuGtYh7bqwuc/aqV7zmM+4/WTyu2EoZlpAZKfYkRkhDqWtLTX5aDCg== X-Received: by 2002:a05:6512:3c8e:b0:52f:154:661b with SMTP id 2adb3069b0e04-5389fc35906mr6422966e87.11.1727730014104; Mon, 30 Sep 2024 14:00:14 -0700 (PDT) Received: from localhost (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5389fd5dcb0sm1367928e87.83.2024.09.30.14.00.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2024 14:00:13 -0700 (PDT) From: =?utf-8?q?Martin_Storsj=C3=B6?= To: binutils@sourceware.org Subject: [PATCH] Add support for IMPORT_NAME_EXPORTAS in ILF (MSVC style) import libraries Date: Tue, 1 Oct 2024 00:00:11 +0300 Message-Id: <20240930210012.2598605-1-martin@martin.st> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org This import name type is formally yet undocumented, but MSVC produces/supports it, primarily for ARM64EC import libraries. LLVM/LLD also supports this import name type. Since recently, llvm-dlltool also uses this type for certain kinds of renamed imports (that are easy to do in the long style import libraries produced by GNU dlltool, but require this name type in short import libraries). This name type contains a third string, in addition to the symbol name and the DLL name, indicating the actual imported name to reference in the import tables - which now can be distinct different from the symbol name on the object file level. Signed-off-by: Martin Storsjö --- bfd/peicode.h | 43 ++++++++++++++++++++++++++++++++++++------- include/coff/pe.h | 1 + 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/bfd/peicode.h b/bfd/peicode.h index 11807efa62e..d6d752dfd42 100644 --- a/bfd/peicode.h +++ b/bfd/peicode.h @@ -409,7 +409,7 @@ pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd) There will be two symbols for the imported value, one the symbol name and one with _imp__ prefixed. Allowing for the terminating nul's this - is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll). + is strlen (import_name) * 2 + 8 + 21 + strlen (source_dll). The strings in the string table must start STRING__SIZE_SIZE bytes into the table in order to for the string lookup code in coffgen/coffcode to @@ -425,7 +425,7 @@ pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd) #define SIZEOF_ILF_EXT_SYMS (NUM_ILF_SYMS * sizeof (* vars.esym_table)) #define SIZEOF_ILF_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.reltab)) #define SIZEOF_ILF_INT_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.int_reltab)) -#define SIZEOF_ILF_STRINGS (strlen (symbol_name) * 2 + 8 \ +#define SIZEOF_ILF_STRINGS (strlen (import_name) * 2 + 8 \ + 21 + strlen (source_dll) \ + NUM_ILF_SECTIONS * 9 \ + STRING_SIZE_SIZE) @@ -440,7 +440,7 @@ pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd) #define SIZEOF_IDATA5 (1 * 4) #endif -#define SIZEOF_IDATA6 (2 + strlen (symbol_name) + 1 + 1) +#define SIZEOF_IDATA6 (2 + strlen (import_name) + 1 + 1) #define SIZEOF_IDATA7 (strlen (source_dll) + 1 + 1) #define SIZEOF_ILF_SECTIONS (NUM_ILF_SECTIONS * sizeof (struct coff_section_tdata)) @@ -792,7 +792,8 @@ pe_ILF_build_a_bfd (bfd * abfd, char * symbol_name, char * source_dll, unsigned int ordinal, - unsigned int types) + unsigned int types, + char * import_name) { bfd_byte * ptr; pe_ILF_vars vars; @@ -834,6 +835,17 @@ pe_ILF_build_a_bfd (bfd * abfd, case IMPORT_NAME: case IMPORT_NAME_NOPREFIX: case IMPORT_NAME_UNDECORATE: + import_name = symbol_name; + break; + + case IMPORT_NAME_EXPORTAS: + if (!import_name || !import_name[0]) + { + _bfd_error_handler (_("%pB: missing import name for " + "IMPORT_NAME_EXPORTAS for %s"), + abfd, symbol_name); + return false; + } break; default: @@ -955,7 +967,7 @@ pe_ILF_build_a_bfd (bfd * abfd, goto error_return; /* If necessary, trim the import symbol name. */ - symbol = symbol_name; + symbol = import_name; /* As used by MS compiler, '_', '@', and '?' are alternative forms of USER_LABEL_PREFIX, with '?' for c++ mangled names, @@ -964,7 +976,8 @@ pe_ILF_build_a_bfd (bfd * abfd, IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the PE COFF 6.0 spec (section 8.3, Import Name Type). */ - if (import_name_type != IMPORT_NAME) + if (import_name_type != IMPORT_NAME && + import_name_type != IMPORT_NAME_EXPORTAS) { char c = symbol[0]; @@ -1179,6 +1192,7 @@ pe_ILF_object_p (bfd * abfd) bfd_byte * ptr; char * symbol_name; char * source_dll; + char * import_name; unsigned int machine; bfd_size_type size; unsigned int ordinal; @@ -1340,9 +1354,24 @@ pe_ILF_object_p (bfd * abfd) return NULL; } + /* An ILF file may contain a third string, after source_dll; this is used + * for IMPORT_NAME_EXPORTAS. We know from above that the whole block of + * data is null terminated, ptr[size-1]==0, but we don't know how many + * individual null terminated strings we have in there. + * + * First find the end of source_dll. */ + import_name = source_dll + strlen (source_dll) + 1; + if ((bfd_byte *) import_name >= ptr + size) + { + /* If this points at the end of the ptr+size block, we only had two + * strings. */ + import_name = NULL; + } + /* Now construct the bfd. */ if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name, - source_dll, ordinal, types)) + source_dll, ordinal, types, + import_name)) { bfd_release (abfd, ptr); return NULL; diff --git a/include/coff/pe.h b/include/coff/pe.h index 37446e4579d..848c07be373 100644 --- a/include/coff/pe.h +++ b/include/coff/pe.h @@ -338,6 +338,7 @@ typedef struct #define IMPORT_NAME 1 #define IMPORT_NAME_NOPREFIX 2 #define IMPORT_NAME_UNDECORATE 3 +#define IMPORT_NAME_EXPORTAS 4 /* Weak external characteristics. */ #define IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY 1