public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* -fpatchable-function-entry should set SHF_WRITE and create one __patchable_function_entries per function
@ 2020-01-07  6:06 Fangrui Song
  2020-01-07  7:26 ` Fangrui Song
  0 siblings, 1 reply; 6+ messages in thread
From: Fangrui Song @ 2020-01-07  6:06 UTC (permalink / raw)
  To: gcc
  Cc: Martin Liška, Alexander Monakov, Torsten Duwe,
	Maxim Kuvyrkov, Nick Clifton

The addresses of NOPs are collected in a section named __patchable_function_entries.
A __patchable_function_entries entry is relocated by a symbolic relocation (e.g. R_X86_64_64, R_AARCH64_ABS64, R_PPC64_ADDR64).
In -shared or -pie mode, the linker will create a dynamic relocation (non-preemptible: relative relocation (e.g. R_X86_64_RELATIVE); preemptible: symbolic relocation (e.g. R_X86_64_64)).

In either case, the section contents will be modified at runtime.
Thus, the section should have the SHF_WRITE flag to avoid text relocations (DF_TEXTREL).



When -ffunction-sections is used, ideally GCC should emit one __patchable_function_entries (SHF_LINK_ORDER) per .text.foo .
If the corresponding .text.foo is discarded (--gc-sections, COMDAT, /DISCARD/), the linker can discard the associated __patchable_function_entries. This can be seen as a lightweight COMDAT section group. (A section group adds an extra section and costs 3 words)
Currently lld (LLVM linker) has implemented such SHF_LINK_ORDER collecting features. GNU ld and gold don't have the features.

I have summarized the feature requests in this post https://sourceware.org/ml/binutils/2019-11/msg00266.html

gcc -fpatchable-function-entry=2 -ffunction-sections -c a.c

   [ 4] .text.f0          PROGBITS        0000000000000000 000040 000009 00  AX  0   0  1
   ### No W flag
   ### One __patchable_function_entries instead of 3.
   [ 5] __patchable_function_entries PROGBITS        0000000000000000 000049 000018 00   A  0   0  1
   [ 6] .rela__patchable_function_entries RELA            0000000000000000 000280 000048 18   I 13   5  8
   [ 7] .text.f1          PROGBITS        0000000000000000 000061 000009 00  AX  0   0  1
   [ 8] .text.f2          PROGBITS        0000000000000000 00006a 000009 00  AX  0   0  1

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-01-10  6:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-07  6:06 -fpatchable-function-entry should set SHF_WRITE and create one __patchable_function_entries per function Fangrui Song
2020-01-07  7:26 ` Fangrui Song
2020-01-07 10:32   ` Szabolcs Nagy
2020-01-08  9:16     ` __patchable_function_entries is flawed Fangrui Song
2020-01-10  3:53       ` Fangrui Song
2020-01-10  6:28         ` Fangrui Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).