From: Fangrui Song <maskray@google.com>
To: Borislav Petkov <bp@alien8.de>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>,
x86@kernel.org, linux-kernel@vger.kernel.org,
clang-built-linux@googlegroups.com, Arnd Bergmann <arnd@arndb.de>,
Nick Desaulniers <ndesaulniers@google.com>,
Nathan Chancellor <natechancellor@gmail.com>,
Michael Matz <matz@suse.de>
Subject: Re: [PATCH v3] x86: Treat R_386_PLT32 as R_386_PC32
Date: Mon, 25 Jan 2021 09:29:56 -0800 [thread overview]
Message-ID: <20210125172956.j2prlchhqwfcgzuc@google.com> (raw)
In-Reply-To: <20210125142302.GA23070@zn.tnic>
On 2021-01-25, Borislav Petkov wrote:
>It's a good thing I have a toolchain guy who can explain to me what you
>guys are doing because you need to start writing those commit messages
>for !toolchain developers.
How about this following message? I'll answer your questions in line as
well. Explaining everything in the message will be quite long... If
someone is interested, I have put every possibly related matter in
https://maskray.me/blog/2021-01-09-copy-relocations-canonical-plt-entries-and-protected
This is similar to commit b21ebf2fb4cd ("x86: Treat R_X86_64_PLT32 as
R_X86_64_PC32"), but for i386. As far as Linux kernel is concerned,
R_386_PLT32 can be treated the same as R_386_PC32.
R_386_PLT32/R_X86_64_PLT32 are PC-relative relocation types which can
only be used by branches. If the referenced symbol is defined
externally, a PLT will be used.
R_386_PC32/R_X86_64_PC32 are PC-relative relocation types which can be
used by address taking operations and branches. If the referenced symbol
is defined externally, a copy relocation/canonical PLT entry will be
created in the executable.
On x86-64, there is no PIC vs non-PIC PLT distinction and an
R_X86_64_PLT32 relocation is produced for both `call/jmp foo` and
`call/jmp foo@PLT` with newer (2018) GNU as/LLVM integrated assembler.
This avoids copy relocations/canonical PLT entries.
On i386, there are 2 types of PLTs, PIC and non-PIC. Currently the
GCC/GNU as convention is to use R_386_PC32 for non-PIC PLT and
R_386_PLT32 for PIC PLT. Copy relocations/canonical PLT entries are
possible ABI issues but GCC/GNU as will likely keep the status quo
because (1) the ABI is legacy (2) the change will drop a GNU ld
diagnostic for non-default visibility ifunc in shared objects.
https://sourceware.org/bugzilla/show_bug.cgi?id=27169
clang-12 -fno-pic (since
https://github.com/llvm/llvm-project/commit/a084c0388e2a59b9556f2de0083333232da3f1d6)
can emit R_386_PLT32 for compiler generated function declarations,
because preventing canonical PLT entries is weighed over the rare ifunc
diagnostic.
Link: https://github.com/ClangBuiltLinux/linux/issues/1210
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Fangrui Song <maskray@google.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
>On Thu, Jan 14, 2021 at 02:48:19PM -0800, Fangrui Song wrote:
>> This is similar to commit b21ebf2fb4cd ("x86: Treat R_X86_64_PLT32 as
>> R_X86_64_PC32"), but for i386. As far as Linux kernel is concerned,
>> R_386_PLT32 can be treated the same as R_386_PC32.
>>
>> R_386_PC32/R_X86_64_PC32 are PC-relative relocation types with the
>> requirement that the symbol address is significant.
>> R_386_PLT32/R_X86_64_PLT32 are PC-relative relocation types without the
>> address significance requirement.
>
>I was told what "significant" means in that context and while it is
>clear to you, I'm pretty sure it is not clear to kernel developers who
>haven't looked at toolchains in depth. So please elaborate.
Expanded "significant" to more words. See above.
>> On x86-64, there is no PIC vs non-PIC PLT distinction and an
>> R_X86_64_PLT32 relocation is produced for both `call/jmp foo` and
>> `call/jmp foo@PLT` with newer (2018) GNU as/LLVM integrated assembler.
>
>Also, please explain in short why LLVM is generating R_X86_64_PLT32
>relocs now? I.e., is it the same reason as why binutils does that?
>
>I.e., mentioning the big picture of things would help as to why you're
>doing this.
It has been explained. The LLVM change was in 2018, roughly the same
time when GNU as emitted R_X86_64_PLT32. I think it does not need
extended explanation because of the separate canonical PLT entries
paragraph.
>> On i386, there are 2 types of PLTs, PIC and non-PIC. Currently the
>> convention is to use R_386_PC32 for non-PIC PLT and R_386_PLT32 for PIC
>> PLT.
>
>Convention in general or convention for LLVM?
Changed to "GCC/GNU as convention".
>> clang-12 -fno-pic (since
>> https://github.com/llvm/llvm-project/commit/a084c0388e2a59b9556f2de0083333232da3f1d6)
>> can emit R_386_PLT32 for compiler generated function declarations as
>> well to avoid a canonical PLT entry (st_shndx=0, st_value!=0) if the
>> symbol turns out to be defined externally. GCC/GNU as will likely keep
>> using R_386_PC32 because (1) the ABI is legacy (2) the change will drop
>> a GNU ld non-default visibility ifunc for shared objects.
>> https://sourceware.org/bugzilla/show_bug.cgi?id=27169
>
>Not sure how useful this paragraph is for kernel developers...
Reorganize it a bit...
>> Link: https://github.com/ClangBuiltLinux/linux/issues/1210
>> Reported-by: Arnd Bergmann <arnd@arndb.de>
>> Signed-off-by: Fangrui Song <maskray@google.com>
>> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
>> Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
>> Tested-by: Nick Desaulniers <ndesaulniers@google.com>
>> Tested-by: Nathan Chancellor <natechancellor@gmail.com>
>>
>> ---
>> Change in v2:
>> * Improve commit message
>> ---
>> Change in v3:
>> * Change the GCC link to the more relevant GNU as link.
>> * Fix the relevant llvm-project commit id.
>> ---
>> arch/x86/kernel/module.c | 1 +
>> arch/x86/tools/relocs.c | 2 ++
>> 2 files changed, 3 insertions(+)
>>
>> diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
>> index 34b153cbd4ac..5e9a34b5bd74 100644
>> --- a/arch/x86/kernel/module.c
>> +++ b/arch/x86/kernel/module.c
>> @@ -114,6 +114,7 @@ int apply_relocate(Elf32_Shdr *sechdrs,
>> *location += sym->st_value;
>> break;
>> case R_386_PC32:
>> + case R_386_PLT32:
>> /* Add the value, subtract its position */
>> *location += sym->st_value - (uint32_t)location;
>> break;
>> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>> index ce7188cbdae5..717e48ca28b6 100644
>> --- a/arch/x86/tools/relocs.c
>> +++ b/arch/x86/tools/relocs.c
>> @@ -867,6 +867,7 @@ static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
>> case R_386_PC32:
>> case R_386_PC16:
>> case R_386_PC8:
>> + case R_386_PLT32:
>> /*
>> * NONE can be ignored and PC relative relocations don't
>> * need to be adjusted.
>
>That comment might need adjustment.
>
>> @@ -910,6 +911,7 @@ static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
>> case R_386_PC32:
>> case R_386_PC16:
>> case R_386_PC8:
>> + case R_386_PLT32:
>> /*
>> * NONE can be ignored and PC relative relocations don't
>> * need to be adjusted.
>
>Ditto.
>
>--
>Regards/Gruss,
> Boris.
>
>https://people.kernel.org/tglx/notes-about-netiquette
next prev parent reply other threads:[~2021-01-25 17:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-07 0:17 [PATCH] " Fangrui Song
2021-01-07 1:31 ` Nick Desaulniers
2021-01-07 2:31 ` Nathan Chancellor
2021-01-07 18:55 ` [PATCH v2] " Fangrui Song
2021-01-14 22:48 ` [PATCH v3] " Fangrui Song
2021-01-22 8:49 ` Fāng-ruì Sòng
2021-01-25 14:23 ` Borislav Petkov
2021-01-25 17:29 ` Fangrui Song [this message]
2021-01-27 20:56 ` [PATCH v4] " Fangrui Song
2021-01-27 22:37 ` Nick Desaulniers
2021-01-28 1:10 ` Sedat Dilek
2021-01-28 11:59 ` [tip: x86/build] x86/build: Treat R_386_PLT32 relocation " tip-bot2 for Fangrui Song
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=20210125172956.j2prlchhqwfcgzuc@google.com \
--to=maskray@google.com \
--cc=arnd@arndb.de \
--cc=bp@alien8.de \
--cc=clang-built-linux@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=matz@suse.de \
--cc=mingo@redhat.com \
--cc=natechancellor@gmail.com \
--cc=ndesaulniers@google.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/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®