From patchwork Tue Jun 23 15:30:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 50241 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B3ECD21575 for ; Tue, 23 Jun 2015 15:31:40 +0000 (UTC) Received: by wgbhy7 with SMTP id hy7sf3860011wgb.3 for ; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:reply-to:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :content-type:content-transfer-encoding:errors-to:x-original-sender :x-original-authentication-results:mailing-list; bh=wyoUzaWIoQ6i7SnMlT2SWq7taTFYfYbztuekQ5KTpp8=; b=d6IAsAnGuSsDP55NWUUAyB1Xy38ZPdToFuunQNupdWEay6BkZ03UOnJc7BTFc7n6Gi hN/BxpcSJCe2b1cDdpduoe9jJ3nCnATvpg93kHerulBNHU7FJr4CzMRDITaATlazIPis wlclQZ+6S02+dC5n9pQ5GJuZCBO3jdKZ1qMr5Oun48Ss+m5AZlPp33/X78Q+9fj96AKX nDBXifjdVlg9VUih8iAaXmGbq9NfPnKJZdkCXgZgm/LSGgEhE7Gwy5M2nipIx29UDas3 BSqLKb+C/O+SU3EImj37Ey23YWFRg0S1aAYK/YiRYDlivktzhs8bNamrO+VlMJ8ImthD I2mg== X-Gm-Message-State: ALoCoQmVJ3MM4zp3JXeIAnk7bDl31emva5kQd+FmisTOZtMuhvEhVfgyVmWkoBtwNpa0tGq3InOD X-Received: by 10.112.200.163 with SMTP id jt3mr21972247lbc.17.1435073499815; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.42.131 with SMTP id o3ls74417lal.101.gmail; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) X-Received: by 10.112.126.136 with SMTP id my8mr35512525lbb.18.1435073499666; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com. [209.85.217.177]) by mx.google.com with ESMTPS id t1si19596385lbk.72.2015.06.23.08.31.39 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Jun 2015 08:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) client-ip=209.85.217.177; Received: by lbbvz5 with SMTP id vz5so9159753lbb.0 for ; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) X-Received: by 10.112.220.7 with SMTP id ps7mr35264898lbc.72.1435073499415; Tue, 23 Jun 2015 08:31:39 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp3155777lbb; Tue, 23 Jun 2015 08:31:38 -0700 (PDT) X-Received: by 10.50.4.66 with SMTP id i2mr3024884igi.40.1435073485756; Tue, 23 Jun 2015 08:31:25 -0700 (PDT) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id a186si6357696ioa.96.2015.06.23.08.31.25 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 23 Jun 2015 08:31:25 -0700 (PDT) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1Z7QAM-0004Rp-6f; Tue, 23 Jun 2015 15:31:14 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1Z7QAL-0004Rj-5i for edk2-devel@lists.sourceforge.net; Tue, 23 Jun 2015 15:31:13 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of linaro.org designates 74.125.82.51 as permitted sender) client-ip=74.125.82.51; envelope-from=ard.biesheuvel@linaro.org; helo=mail-wg0-f51.google.com; Received: from mail-wg0-f51.google.com ([74.125.82.51]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76) id 1Z7QAJ-00086z-7z for edk2-devel@lists.sourceforge.net; Tue, 23 Jun 2015 15:31:13 +0000 Received: by wgck11 with SMTP id k11so13017174wgc.0 for ; Tue, 23 Jun 2015 08:31:05 -0700 (PDT) X-Received: by 10.194.23.225 with SMTP id p1mr63419825wjf.155.1435073465031; Tue, 23 Jun 2015 08:31:05 -0700 (PDT) Received: from localhost.localdomain ([185.13.106.68]) by mx.google.com with ESMTPSA id ei8sm36242290wjd.32.2015.06.23.08.31.02 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 23 Jun 2015 08:31:04 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.sourceforge.net, olivier.martin@arm.com, leif.lindholm@linaro.org Date: Tue, 23 Jun 2015 17:30:52 +0200 Message-Id: <1435073455-3268-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435073455-3268-1-git-send-email-ard.biesheuvel@linaro.org> References: <1435073455-3268-1-git-send-email-ard.biesheuvel@linaro.org> X-Spam-Score: -1.5 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1Z7QAJ-00086z-7z Subject: [edk2] [RFC PATCH 1/4] BaseTools: AArch64: conditionally allow page-based PC relative relocations X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list Reply-To: edk2-devel@lists.sourceforge.net List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 This updates GenFw's ELF conversion for AArch64 so that relocation pairs that operate on ADRP/ADD, ADRP/LDR or ADRP/STR instruction combinations are allowed under the following conditions: - the relative alignment to the nearest 4 KB boundary of the target of the relocation must be identical between the ELF and the PE/COFF versions of the binary; - the offset between the relocation target and the symbol it refers to is identical between the ELF and the PE/COFF versions, even if the symbol lives in another section. These two conditions can be met by using a carefully crafted GNU ld script that reflects the placement logic of GenFw. Note that such binaries need to be loaded at a 4 KB aligned load address. This is a preliminary step towards allowing AArch64 binaries to be built with the standard 'small' C model. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- BaseTools/Source/C/GenFw/Elf64Convert.c | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c index 25b90e2f7b51..2266e487cec7 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -734,13 +734,33 @@ WriteSections64 ( break; case R_AARCH64_ADR_PREL_PG_HI21: - // TODO : AArch64 'small' memory model. - Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName); - break; - case R_AARCH64_ADD_ABS_LO12_NC: - // TODO : AArch64 'small' memory model. - Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName); + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: + // + // These are static PC relative relocation pairs that will already have been resolved + // during static linking of the ELF shared object. So no need to do anything here if + // a) the original ELF .text section and the derived PE/COFF .text section are placed at + // the same relative alignment with respect to the nearest 4K aligned boundary; + // b) the section containing the symbol that the relocation pair refers to is at + // the same relative offset in both binaries; + // c) it is guaranteed that the load time alignment of the PE/COFF binary is + // at least 4 KB. + // + // While conditions a) and b) could be met by recalculating the immediates based on + // the actual placement of the PE/COFF sections if their offsets and/or alignments + // deviate, condition c) is a necessary condition that applies universally when + // ADRP based symbol references are used. It thus requires the appropriate 'Align=4K' + // declarations in the platform .FDF. + // + if ((SecOffset % 4096) != (SecShdr->sh_addr % 4096) || + (mCoffSectionsOffset[Sym->st_shndx] - SecOffset) != (SymShdr->sh_addr - SecShdr->sh_addr)) { + // TODO: recalculate the two immediates based on the actual placement + Error (NULL, 0, 3000, "Invalid", "AArch64: ADRP/ADD and ADRP/LDR pairs must retain relative sym offset and relative alignment to 4 KB!."); + } break; // Absolute relocations. @@ -825,13 +845,24 @@ WriteRelocations64 ( break; case R_AARCH64_ADR_PREL_PG_HI21: - // TODO : AArch64 'small' memory model. - Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName); break; case R_AARCH64_ADD_ABS_LO12_NC: - // TODO : AArch64 'small' memory model. - Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName); + break; + + case R_AARCH64_LDST8_ABS_LO12_NC: + break; + + case R_AARCH64_LDST16_ABS_LO12_NC: + break; + + case R_AARCH64_LDST32_ABS_LO12_NC: + break; + + case R_AARCH64_LDST64_ABS_LO12_NC: + break; + + case R_AARCH64_LDST128_ABS_LO12_NC: break; case R_AARCH64_ABS64: