mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: WANG Rui <wangrui@loongson.cn>, Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>,
	linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
	linux-kbuild@vger.kernel.org, llvm@lists.linux.dev,
	loongson-kernel@lists.loongnix.cn,
	Fangrui Song <maskray@google.com>
Subject: Re: [PATCH] LoongArch: Disable module from accessing external data directly
Date: Wed, 08 Nov 2023 16:30:05 +0800	[thread overview]
Message-ID: <d32d8a26dcd75a840727cdb50546b621d34d326b.camel@xry111.site> (raw)
In-Reply-To: <20231108040447.288870-1-wangrui@loongson.cn>

On Wed, 2023-11-08 at 12:04 +0800, WANG Rui wrote:
> When compiling module with GCC, the option `-mdirect-extern-access` is
> disabled by default. The Clang option `-fdirect-access-external-data`
> is enabled by default, so it needs to be explicitly disabled.

I'm wondering why it's enabled by default.

For this simple test case:

extern char **environ;

int main()
{
  __builtin_printf("%10s\n", environ[0]);
}

With Clang 17.0.4 and "clang t1.c -S -O2", it compiles to:

main:
	addi.d	$sp, $sp, -16
	st.d	$ra, $sp, 8
	pcalau12i	$a0, %got_pc_hi20(environ)
	ld.d	$a0, $a0, %got_pc_lo12(environ)
	ld.d	$a0, $a0, 0
	ld.d	$a1, $a0, 0
	pcalau12i	$a0, %pc_hi20(.L.str)
	addi.d	$a0, $a0, %pc_lo12(.L.str)
	bl	%plt(printf)
	move	$a0, $zero
	ld.d	$ra, $sp, 8
	addi.d	$sp, $sp, 16
	ret

So GOT is used for accessing the external variable environ.  With "clang
t1.c -S -O2 -fdirect-access-external-data", we get:

main:
	addi.d	$sp, $sp, -16
	st.d	$ra, $sp, 8
	pcalau12i	$a0, %pc_hi20(environ)
	addi.d	$a0, $a0, %pc_lo12(environ)
	ld.d	$a0, $a0, 0
	ld.d	$a1, $a0, 0
	pcalau12i	$a0, %pc_hi20(.L.str)
	addi.d	$a0, $a0, %pc_lo12(.L.str)
	bl	%plt(printf)
	move	$a0, $zero
	ld.d	$ra, $sp, 8
	addi.d	$sp, $sp, 16
	ret

then the linked binary triggers a SIGBUS.  Ideally this should be
detected by the linker at link time, but currently the BFD linker fails
to detect this error (FWIW this flaw is caused by a really nasty method
for the medium code model implementation).  So to me -fno-direct-access-
external-data is the default.  I also grepped for -fdirect-access-
external-data in the kernel building system but I've not found any
match. 

Are you using a different version of Clang, or maybe Clang has some
configuration-time option to make -fdirect-access-external-data the
default?

Note that to translate a TU for a normal (dynamically-linked user-space)
executable on LoongArch Linux, -fdirect-access-external-data should not
be used (because copy relocation is now considered a bad idea and we'll
not support it for a new architecture).  Fangrui?

-fdirect-access-external-data can be used in KBUILD_CFLAGS_KERNEL for
avoiding GOT in the main kernel image, OTOH.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2023-11-08  8:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-08  4:04 WANG Rui
2023-11-08  8:30 ` Xi Ruoyao [this message]
2023-11-08  9:20   ` WANG Rui
2023-11-08  9:25     ` Xi Ruoyao
2023-11-08  9:36       ` WANG Rui
2023-11-08  9:39         ` WANG Rui
2023-11-08  9:46         ` Xi Ruoyao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d32d8a26dcd75a840727cdb50546b621d34d326b.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=chenhuacai@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=loongarch@lists.linux.dev \
    --cc=loongson-kernel@lists.loongnix.cn \
    --cc=maskray@google.com \
    --cc=wangrui@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®