Skip to content

Commit 229ea9b

Browse files
committed
poc hotpatch
1 parent a6e87c5 commit 229ea9b

File tree

9 files changed

+32
-0
lines changed

9 files changed

+32
-0
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ pub fn from_fn_attrs<'ll, 'tcx>(
341341
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
342342
}
343343

344+
if cx.sess().opts.unstable_opts.ms_hotpatch && cx.sess().target.is_x86() && cx.sess().target.is_like_msvc {
345+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "patchable-function", "prologue-short-redirect"));
346+
}
347+
344348
// FIXME: none of these functions interact with source level attributes.
345349
to_add.extend(frame_pointer_type_attr(cx));
346350
to_add.extend(function_return_attr(cx));

compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl OwnedTargetMachine {
3636
emit_stack_size_section: bool,
3737
relax_elf_relocations: bool,
3838
use_init_array: bool,
39+
use_hotpatch: bool,
3940
split_dwarf_file: &CStr,
4041
output_obj_file: &CStr,
4142
debug_info_compression: &CStr,
@@ -68,6 +69,7 @@ impl OwnedTargetMachine {
6869
emit_stack_size_section,
6970
relax_elf_relocations,
7071
use_init_array,
72+
use_hotpatch,
7173
split_dwarf_file.as_ptr(),
7274
output_obj_file.as_ptr(),
7375
debug_info_compression.as_ptr(),

compiler/rustc_codegen_llvm/src/back/write.rs

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ pub fn target_machine_factory(
220220
let use_init_array =
221221
!sess.opts.unstable_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);
222222

223+
let use_hotpatch = sess.opts.unstable_opts.ms_hotpatch && sess.target.is_x86() && sess.target.is_like_msvc;
224+
223225
let path_mapping = sess.source_map().path_mapping().clone();
224226

225227
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
@@ -292,6 +294,7 @@ pub fn target_machine_factory(
292294
emit_stack_size_section,
293295
relax_elf_relocations,
294296
use_init_array,
297+
use_hotpatch,
295298
&split_dwarf_file,
296299
&output_obj_file,
297300
&debuginfo_compression,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ extern "C" {
21892189
EmitStackSizeSection: bool,
21902190
RelaxELFRelocations: bool,
21912191
UseInitArray: bool,
2192+
UseHotpatch: bool,
21922193
SplitDwarfFile: *const c_char,
21932194
OutputObjFile: *const c_char,
21942195
DebugInfoCompression: *const c_char,

compiler/rustc_codegen_ssa/src/back/link.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,17 @@ fn add_order_independent_options(
23472347

23482348
add_link_script(cmd, sess, tmpdir, crate_type);
23492349

2350+
if sess.opts.unstable_opts.ms_hotpatch && sess.target.is_x86() && sess.target.is_like_msvc {
2351+
// when using hotpatch also automatically instruct the linker to pad the functions
2352+
// so there is space for a absolute jump instructions
2353+
match flavor {
2354+
LinkerFlavor::Msvc(_) => {
2355+
cmd.arg("-functionpadmin");
2356+
},
2357+
_ => (),
2358+
}
2359+
}
2360+
23502361
if sess.target.os == "fuchsia"
23512362
&& crate_type == CrateType::Executable
23522363
&& !matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ fn test_unstable_options_tracking_hash() {
799799
tracked!(mir_opt_level, Some(4));
800800
tracked!(move_size_limit, Some(4096));
801801
tracked!(mutable_noalias, false);
802+
tracked!(ms_hotpatch, true);
802803
tracked!(
803804
next_solver,
804805
Some(NextSolverConfig { coherence: true, globally: false, dump_tree: Default::default() })

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
405405
bool EmitStackSizeSection,
406406
bool RelaxELFRelocations,
407407
bool UseInitArray,
408+
bool UseHotpatch,
408409
const char *SplitDwarfFile,
409410
const char *OutputObjFile,
410411
const char *DebugInfoCompression,
@@ -436,6 +437,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
436437
Options.MCOptions.AsmVerbose = AsmComments;
437438
Options.MCOptions.PreserveAsmComments = AsmComments;
438439
Options.MCOptions.ABIName = ABIStr;
440+
Options.Hotpatch = UseHotpatch;
439441
if (SplitDwarfFile) {
440442
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
441443
}

compiler/rustc_session/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,10 @@ options! {
18151815
"the size at which the `large_assignments` lint starts to be emitted"),
18161816
mutable_noalias: bool = (true, parse_bool, [TRACKED],
18171817
"emit noalias metadata for mutable references (default: yes)"),
1818+
ms_hotpatch: bool = (false, parse_bool, [TRACKED],
1819+
"ensures hotpatching is always possible by ensuring that the first instruction of \
1820+
each function is at least two bytes, and no jump within the function goes to the first instruction \
1821+
and adding enough padding to the beginning of each function. Only relevant for x86 with MSVC (default: false)"),
18181822
next_solver: Option<NextSolverConfig> = (None, parse_next_solver_config, [TRACKED],
18191823
"enable and configure the next generation trait solver used by rustc"),
18201824
nll_facts: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_target/src/spec/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,10 @@ impl Target {
19101910

19111911
Ok(dl)
19121912
}
1913+
pub fn is_x86(&self) -> bool {
1914+
// ["x86", "x86_64"].contains(&self.arch.as_ref())
1915+
["x86", "x86_64"].contains(&&self.arch[..])
1916+
}
19131917
}
19141918

19151919
pub trait HasTargetSpec {

0 commit comments

Comments
 (0)