Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2201134ybk; Sun, 17 May 2020 13:27:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8IZn/T9XgAW/m0C2fudTacaxoma/5FGtmqlKCQ5ZcRk9f8OMeWBnrPmZOWJbquSvAmpIi X-Received: by 2002:a17:906:4995:: with SMTP id p21mr12092869eju.19.1589747271862; Sun, 17 May 2020 13:27:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589747271; cv=none; d=google.com; s=arc-20160816; b=WtZD+XcqQJitr8ZHXxA5sV5THOcIbgPUHkFppQ6ONjtW6eTI4aKl9XV77u/gx+Hfq4 nTlp3Ojn/XNQNDo9efbNZ3Kh3RvI9RZodcgVygyrqkDnegZyQF8Gqk9FseXHWjLVTZIa 6bOqTLc/cC6fNVHjkcVECHzx70PRTg04sgbeTdA3r4+8yKKdT66K14ktnK1FIKbu645R cc0h2JV7r9XA36c2UW3+5RJAt9Hyfrg0KbVff3d1GeLWQbI6We5wtTJdZYivvCEkXLia 0nZiUB6xiUA5m1Vqjr3xNdYkLzNdsmxjY1s9wbMyF0GEmMY7pO49BLQCaMjWwGKlVouM O7cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:date:from :dkim-signature; bh=pdFkZ3z8hbCWgEtAKY5gJCVXhkdUK7J2l2u0edhAVKY=; b=Hxtq6RIIsfHoXUEcp0UIKnLMnoxWDNBNTNS8DWNrJIdcxnGv1nMXpxg4pgeegptlKK 49tDo3c9Jorh4apNaHe5ltzDYVww/chzzwqEXBIIlzK877KBRvMTcwK0kLGKXmz6z4v4 JvF/bkLZVHInHwEHYBafjKGyi9B3+zH3SsY83tugsv7faJLtwAgoDLaEoKQYsDozVOrj ey4TjR8VBW6Jh6Q/SLkNPipGHubtuWzxHzbvxgRj28vPXLDwJ5D0ASjAFXHFPbqps4r1 DuWfLSo7qXlYOKwkd4QNSr8ATF6omXjNEB/vMEoGnNClIXYETgCT2L6TEeIyWxzxyh2s gQpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=RJRl6ID2; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ds11si3302155ejc.16.2020.05.17.13.27.28; Sun, 17 May 2020 13:27:51 -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=fail header.i=@gmail.com header.s=20161025 header.b=RJRl6ID2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726727AbgEQUZb (ORCPT + 99 others); Sun, 17 May 2020 16:25:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726269AbgEQUZa (ORCPT ); Sun, 17 May 2020 16:25:30 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82EF5C061A0C for ; Sun, 17 May 2020 13:25:30 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id v4so6510396qte.3 for ; Sun, 17 May 2020 13:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pdFkZ3z8hbCWgEtAKY5gJCVXhkdUK7J2l2u0edhAVKY=; b=RJRl6ID28Pxd5kKOkc8IERBuhMxeaW7jyOUlSo0Uebc0bJ1c+HrkvunsvGSuLA1hXo pLak9nwWP/Rl+RASlBSPFq/jB3fdchGI0F4cRHdUz/xHUrJxWvlKEHvc0rOjBHKhFJAI kZBbMPLJDQ4rU6JBEOWuld4WGiC1BkwppKxUVLy4Wo7N2eS0Zuq4CnpCZSgM5Lj3l7gj iZVZ1zYNiTgywlXJRyRKzBf/Dgbmr9ZZg9PUbGEZCkntszzqw6ogsg4Noygqx2zZzuAE Fv3O0HLHeQ61e3AwdLHLsvYv6ndCht2FgIfaXnaRiJp3iYAKHAV1X2Q7s5xsJM0MSnUo 53qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:date:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=pdFkZ3z8hbCWgEtAKY5gJCVXhkdUK7J2l2u0edhAVKY=; b=iVQSOSpDOK3RJyrRZt8zt4m5efzRrnKmHWEf1B4rRZXw389zVGePYDRYeLIsRLoWSj nySpraufYCFddrUtKKk1hl6JRYYM4ZBLb6+GuDTynXUOPGZcWPESIhflXh1bSOxgKh2k zslKrO37l3TdIzknVVxfG6oNcP6zZBgmPIdzqxrPiFm7rrPNfmON9/DADNKfjDiyKMEz i2SMKaQsEl9Z9JXTaM+xbFpeelQtY0adEjSCNngROIRTA7Dj99tvpejlyP0PtVcA1FQd sFK2be1wDQvpTjXk5RM5FxmzVigNRC0PLLUEl/TSCEmvjkMIzmKfgyQHcH2/Rnk8H+OL YHLw== X-Gm-Message-State: AOAM530KA722h0/WMopmHz5Z8vV3FiFBep9rmSfhDQD1BZUOuDYe5+94 6UqJEVg/68dWOaQl8dXFNaU= X-Received: by 2002:ac8:5517:: with SMTP id j23mr3008528qtq.215.1589747129446; Sun, 17 May 2020 13:25:29 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id y185sm6662678qkd.29.2020.05.17.13.25.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2020 13:25:29 -0700 (PDT) From: Arvind Sankar X-Google-Original-From: Arvind Sankar Date: Sun, 17 May 2020 16:25:27 -0400 To: Fangrui Song Cc: Dmitry Golovin , Borislav Petkov , "clang-built-linux@googlegroups.com" , Thomas Gleixner , Ingo Molnar , "x86@kernel.org" , "H. Peter Anvin" , Nick Desaulniers , Ard Biesheuvel , Masahiro Yamada , Daniel Kiper , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] x86/boot: allow a relocatable kernel to be linked with lld Message-ID: <20200517202527.GA2563549@rani.riverdale.lan> References: <20200501084215.242-1-dima@golovin.in> <20200515185051.GC19017@zn.tnic> <602331589572661@mail.yandex.ru> <20200517194429.scwhfr4l4bv4h3ux@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200517194429.scwhfr4l4bv4h3ux@google.com> 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 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. Thanks.