Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2991778ybk; Mon, 18 May 2020 13:02:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4hJa2ibBnPDUGUbbsYHbtCutS+4TVCio1201ivxkFeJWeoHTMyG/pKOLIcKv39dZxUa7+ X-Received: by 2002:a50:b586:: with SMTP id a6mr14661214ede.292.1589832123358; Mon, 18 May 2020 13:02:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589832123; cv=none; d=google.com; s=arc-20160816; b=S8bIoGKtYbCo9Zt/L1NWgXssJyTupcdFViv7QJDJdS/7sCWzUgyN8ejhFPBF8VJIoW eTJZXlaW9WxjJDsgwimAFprXrJHYJh7MQN8BzIV70u12nEK9S2bnf266pwiKqRjjSCE/ vRN1EIeK1wF4wijwx5KzDz1lLXQF7dDuu5SoJFH/924Go4LecM3D2gxCblN/rim7jwH0 Fwg/djR8p9UI0F0yBsnj1TnexUEXG7ElrsfATxQjpfLZBcpNgcybhJPDjga1YfQltOG1 IDk7dX7Qhge9oRyZoy8GTjp8Tn44+QgK0jPs5oXefgltkhKL4ep9vr7q9ChaPpXV5ZHR FM6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=B9WsWP6dnEY5vCfQiuvwQaqKnNQto5CQpnFS4KzI7DY=; b=rYxeuHA+5B04Yl+D2Q4UjMGYvefSSgLMTZ4whxFwrx8HVsAF1hfUxRp0X8wD9K3tp9 IkBK81KMhZv7QR9Xa9PL1rIaSqGlxZeOKojVq5PaKJKmURoJZ/A4odZf13OfRvv2M8zN 7dxg/nMseoZ286XtigTbzk85lBHELtbiTZAwOHEAGYx2eLdymJv8z8dBeoqijn/IhfSx pvw0it4r8YOVjOeGNqdDWOHx3SP3fnzjnalo9CV2IDovoiln8AlnaDNRRuwas3waPfA3 pe24O6OKtrjmvHV8E5Jh9FQMo+GujLbRdu48Ci8Ey/+BBz8AeBqGYLHSmPMDzZac6mDV BkLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cHuezSmh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z2si8252295edp.280.2020.05.18.13.01.40; Mon, 18 May 2020 13:02:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cHuezSmh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726860AbgERTKZ (ORCPT + 99 others); Mon, 18 May 2020 15:10:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726703AbgERTKY (ORCPT ); Mon, 18 May 2020 15:10:24 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98ABFC061A0C for ; Mon, 18 May 2020 12:10:24 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id u35so5278259pgk.6 for ; Mon, 18 May 2020 12:10:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=B9WsWP6dnEY5vCfQiuvwQaqKnNQto5CQpnFS4KzI7DY=; b=cHuezSmhHKbWR5zfm6IDklGMDHYGApFLx8L38GBmK/KvWwczsIOz9r+mKvBfs74RJL BAdZjB2n9mgd6yvaDhrjqZ5OzERYPrwQRDjO4L5+8q4x+FBbqU90q7KDbMF84tMYo8UH A4MeUZpi6YBF/ipNi/Ecf5Yif0+t9MsPIyZGE+NxT+n1l+2thrQrMtx98GajwAdSOCDo WE5mm3O+JkATr0nxhhWtk6YUmP+nv6DlZVrEjkzyjT8ZzE/87cmXLsc6HPdCVmHmnV+J X2hEd+Htukvq2JyEZHSWZq9BBB3qEUG/cE5YgTgYcZyQMDrnwcEdrXpCGPYy29m2d//2 4Y2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=B9WsWP6dnEY5vCfQiuvwQaqKnNQto5CQpnFS4KzI7DY=; b=BwkZqyQ+NVH0YHy9uUyTgQUgcEsuiGkRrm42VP9oWjPdO6LGiud3UzYhMHSO62MZ1c jYQvJzdlx81oDoKZUrQR7ihQJiFkdIcqt6r2KkX6uV+fRsnnhsA2G/VQwcHqT4Y6LIgX +mb6L+ssYJ4LIGqHTTbCD6mBMMOEljZ08c7nRqu3U0E51Of9zUK3sligHEL+zljwqr4A pwheYREjA/lLRbmXaizaJkOfaJ0nFbJuGZuMCQG51CHkg0SOAmKVmmnIgrlCyQdDE507 t1hB2n+CaAABsfT4Ke5y7Nat/XcNE4ycX1QznWlf8yTSrGaTCSrkWjLIKtvPoeX63P2i nuag== X-Gm-Message-State: AOAM533J4U652vtSiC0TdbzpPxJOmdWg143uLp2zKfs8mxY1/w56k+36 mCNKxy3tBpxi1F7pymHS1U9ux3Xx9fo/p0F0Qr8XZw== X-Received: by 2002:a63:4f09:: with SMTP id d9mr3246799pgb.10.1589829023749; Mon, 18 May 2020 12:10:23 -0700 (PDT) MIME-Version: 1.0 References: <20200501084215.242-1-dima@golovin.in> <20200515185051.GC19017@zn.tnic> <602331589572661@mail.yandex.ru> <20200517194429.scwhfr4l4bv4h3ux@google.com> <20200517202527.GA2563549@rani.riverdale.lan> In-Reply-To: <20200517202527.GA2563549@rani.riverdale.lan> From: Nick Desaulniers Date: Mon, 18 May 2020 12:10:11 -0700 Message-ID: Subject: Re: [PATCH] x86/boot: allow a relocatable kernel to be linked with lld To: Arvind Sankar Cc: Fangrui Song , Dmitry Golovin , Borislav Petkov , "clang-built-linux@googlegroups.com" , Thomas Gleixner , Ingo Molnar , "x86@kernel.org" , "H. Peter Anvin" , Ard Biesheuvel , Masahiro Yamada , Daniel Kiper , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, May 17, 2020 at 1:25 PM Arvind Sankar wrote: > > On Sun, May 17, 2020 at 12:44:29PM -0700, Fangrui Song wrote: > > On 2020-05-16, Dmitry Golovin wrote: > > >15.05.2020, 21:50, "Borislav Petkov" : > > >> > > >> I need more info here about which segment is read-only? > > >> > > >> Is this something LLD does by default or what's happening? > > >> > > > > > >Probably should have quoted the original error message: > > > > > > ld.lld: error: can't create dynamic relocation R_386_32 against > > > symbol: _bss in readonly segment; recompile object files with -fPIC > > > or pass '-Wl,-z,notext' to allow text relocations in the output > > > > Do we know where do these R_386_32 come from? > > > > When linking in -shared mode, the linker assumes the image is a shared > > object and has undetermined image base at runtime. An absolute > > relocation needs a text relocation (a relocation against a readonly > > segment). > > > > When neither -z notext nor -z text is specified, GNU ld is in an > > indefinite state where it will enable text relocations (DT_TEXTREL > > DF_TEXTREL) on demand. It is not considered a good practice for > > userspace applications to do this. > > > > Of course the kernel is different....... I know little about the kernel, > > but if there is a way to make the sections containing R_386_32 > > relocations writable (SHF_WRITE), that will be a better solution to me. > > In LLD, -z notext is like making every section SHF_WRITE. > > The assembly files head_32.S and head_64.S in arch/x86/boot/compressed > create bogus relocations in .head.text and .text. > > This is the source of the common warning when using the bfd linker, for > eg on 64-bit: > LD arch/x86/boot/compressed/vmlinux > ld: arch/x86/boot/compressed/head_64.o: warning: relocation in read-only section `.head.text' > ld: warning: creating a DT_TEXTREL in object > > These relocations are "bogus", i.e. they're unwanted and the kernel > won't actually boot if anything were to perform those relocations before > running it. They're also the cause of the 64-bit kernel requiring linker > support for the -z noreloc-overflow option to link it as PIE. > > From arch/x86/boot/compressed/Makefile: > > # To build 64-bit compressed kernel as PIE, we disable relocation > # overflow check to avoid relocation overflow error with a new linker > # command-line option, -z noreloc-overflow. > KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z > noreloc-overflow" \ > && echo "-z noreloc-overflow -pie --no-dynamic-linker") > > The relocations come from code like > leal gdt(%ebp), %eax > which should really be > leal (gdt-startup_32)(%ebp), %eax > to be technically correct. > > I've played around with fixing the head code to avoid creating the > relocations in the first place, but never got around to submitting > anything: if there is interest in this, I can polish it up and send it > around. We'd be happy to help test and review. :) -- Thanks, ~Nick Desaulniers