From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 6AA44740037 for ; Thu, 30 Nov 2023 22:42:24 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=+ZHb/7aewhWHBHYj2+YuZVHX50gZM2Ain+D6fRARy18=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1701384143; v=1; b=vvppuo6pSE1eS+U2pCpAmkphrg/Tc1qYI+ObxntwZ2FDMDr7XUlwz+Q+yAK7jXcxzxh832Vy QJcyb5g/MguvhZkeg4PR5qhEkvpYXes6NeOH4fN9Qvg7lqFbn/KFwO7HjaioJEKpkCUaUOKxE5j yd8XF3urPMyKBNWnzQuZy0dY= X-Received: by 127.0.0.2 with SMTP id Oz8BYY7687511x4auuyNvDfC; Thu, 30 Nov 2023 14:42:23 -0800 X-Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by mx.groups.io with SMTP id smtpd.web10.9342.1701384142282831258 for ; Thu, 30 Nov 2023 14:42:22 -0800 X-Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40a4848c6e1so12865645e9.1 for ; Thu, 30 Nov 2023 14:42:22 -0800 (PST) X-Gm-Message-State: 1AVQByCMTjunIslhmn7bRhm3x7686176AA= X-Google-Smtp-Source: AGHT+IG8lGqAFPR9g9hEWqg7V07lro8DGa+wZHc+xrGhcn7MQtnhJ0jlnGanrbz+MqjBjYuhW0V4hw== X-Received: by 2002:adf:e981:0:b0:332:f9e8:ce1c with SMTP id h1-20020adfe981000000b00332f9e8ce1cmr174568wrm.12.1701384139945; Thu, 30 Nov 2023 14:42:19 -0800 (PST) X-Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id k26-20020adfb35a000000b003332faefd86sm547243wrd.0.2023.11.30.14.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 14:42:19 -0800 (PST) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , Michael D Kinney , Michael Kubacki , Sean Brogan Subject: [edk2-devel] [PATCH 1/2] UnitTestFrameworkPkg: Fix Google Test components with multiple files Date: Thu, 30 Nov 2023 22:42:13 +0000 Message-ID: <20231130224214.86027-2-pedro.falcato@gmail.com> In-Reply-To: <20231130224214.86027-1-pedro.falcato@gmail.com> References: <20231130224214.86027-1-pedro.falcato@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,pedro.falcato@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=vvppuo6p; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Google Test hides test registration in global constructors on global objects. Global constructors are traditionally implemented by placing references to the global constructor's symbol in special sections (traditionally named .ctors or .init_array). These sections are not explicitly referenced by the linker, and libc only looks at special start and end symbols (and calls them). This works fine if you're linking a program manually using gcc a.o b.o c.o -o test_suite but fails miserably when using static libraries (such as what EDK2 does), because traditional static archive symbol resolution rules don't allow for object files to be pulled in to the link if there isn't an undefined symbol reference to that .o elsewhere. Fix it by passing --whole-archive (GCC) and /WHOLEARCHIVE (MSVC). These options force the linker to pull in the entire static library, thus including previously-unreferenced constructors and making sure multi-file gtest EDK2 components work. BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4610 Signed-off-by: Pedro Falcato Cc: Michael D Kinney Cc: Michael Kubacki Cc: Sean Brogan --- Note: /WHOLEARCHIVE should work for VS2015 and newer. I haven't been able to test it, so if someone could test on msft it would be much appreciated. Testing is as simple as taking this patch set (https://openfw.io/edk2-devel/20231130024611.67135-1-pedro.falcato@gmail.com/) and removing the #include "TestCheckSum.cpp" in TestBaseLibMain.cpp. If everything worked correctly, you should have 4 tests and not 0. UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc b/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc index 5217de31e491..0f11706e7324 100644 --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc @@ -37,7 +37,7 @@ # MSFT # MSFT:*_*_*_CC_FLAGS = /EHsc - MSFT:*_*_*_DLINK_FLAGS == /out:"$(BIN_DIR)\$(MODULE_NAME_GUID).exe" /pdb:"$(BIN_DIR)\$(MODULE_NAME_GUID).pdb" /IGNORE:4001 /NOLOGO /SUBSYSTEM:CONSOLE /DEBUG /STACK:0x40000,0x40000 /NODEFAULTLIB:libcmt.lib libcmtd.lib + MSFT:*_*_*_DLINK_FLAGS == /out:"$(BIN_DIR)\$(MODULE_NAME_GUID).exe" /pdb:"$(BIN_DIR)\$(MODULE_NAME_GUID).pdb" /IGNORE:4001 /NOLOGO /SUBSYSTEM:CONSOLE /DEBUG /STACK:0x40000,0x40000 /NODEFAULTLIB:libcmt.lib libcmtd.lib /WHOLEARCHIVE MSFT:*_*_IA32_DLINK_FLAGS = /MACHINE:I386 MSFT:*_*_X64_DLINK_FLAGS = /MACHINE:AMD64 @@ -56,7 +56,12 @@ # GCC:*_*_IA32_DLINK_FLAGS == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m32 -no-pie GCC:*_*_X64_DLINK_FLAGS == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m64 -no-pie - GCC:*_*_*_DLINK2_FLAGS == -lgcov -lpthread -lstdc++ -lm + # + # Surround our static libraries with whole-archive, so constructor-based test registration works properly. + # Note that we need to --no-whole-archive before linking system libraries. + # + GCC:*_*_*_DLINK_FLAGS = -Wl,--whole-archive + GCC:*_*_*_DLINK2_FLAGS == -Wl,--no-whole-archive -lgcov -lpthread -lstdc++ -lm # # Need to do this link via gcc and not ld as the pathing to libraries changes from OS version to OS version -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111916): https://edk2.groups.io/g/devel/message/111916 Mute This Topic: https://groups.io/mt/102904623/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-