Skip to content

Commit

Permalink
add -fIBT switch and emit ENDBR for function and thunk prologs
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jul 15, 2023
1 parent b6a9706 commit 9471a8a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/src/dmd/backend/backconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nothrow:
2 for fake it with C symbolic debug info
alwaysframe = always create standard function frame
stackstomp = add stack stomping code
ibt = generate Indirect Branch Tracking code
avx = use AVX instruction set (0, 1, 2)
pic = position independence level (0, 1, 2)
useModuleInfo = implement ModuleInfo
Expand All @@ -68,6 +69,7 @@ extern (C) void out_config_init(
int symdebug,
bool alwaysframe,
bool stackstomp,
bool ibt,
ubyte avx,
ubyte pic,
bool useModuleInfo,
Expand Down Expand Up @@ -99,6 +101,8 @@ extern (C) void out_config_init(
model &= 32 | 64;
if (generatedMain)
cfg.flags2 |= CFG2genmain;
if (ibt)
cfg.flags3 |= CFG3ibt;

Check warning on line 105 in compiler/src/dmd/backend/backconfig.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/backconfig.d#L105

Added line #L105 was not covered by tests

if (dwarf < 3 || dwarf > 5)
{
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/backend/cdef.d
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ enum
CFG3semirelax = 0x40000, // moderate relaxed type checking (non-Windows targets)
CFG3pic = 0x80000, // position independent code
CFG3pie = 0x10_0000, // position independent executable (CFG3pic also set)
CFG3ibt = 0x20_0000, // indirect branch tracking
}

alias config_flags4_t = uint;
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/backend/cgcod.d
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ void prolog(ref CodeBuilder cdb)
tym_t tym = tybasic(tyf);
const farfunc = tyfarfunc(tym) != 0;

if (config.flags3 & CFG3ibt && !I16)
cdb.gen1(I32 ? ENDBR32 : ENDBR64);

Check warning on line 682 in compiler/src/dmd/backend/cgcod.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/cgcod.d#L682

Added line #L682 was not covered by tests

// Special Intel 64 bit ABI prolog setup for variadic functions
Symbol *sv64 = null; // set to __va_argsave
if (I64 && variadic(funcsym_p.Stype))
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/backend/cod3.d
Original file line number Diff line number Diff line change
Expand Up @@ -4729,6 +4729,9 @@ void cod3_thunk(Symbol *sthunk,Symbol *sfunc,uint p,tym_t thisty,
MOV EAX, d2[EAX] EAX = this.vptr
JMP i[EAX] jump to virtual function
*/
if (config.flags3 & CFG3ibt)
cdb.gen1(I32 ? ENDBR32 : ENDBR64);

Check warning on line 4733 in compiler/src/dmd/backend/cod3.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/cod3.d#L4733

Added line #L4733 was not covered by tests

reg_t reg = 0;
if (cast(int)d < 0)
{
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ dmd -cov -unittest myprog.d
Option("extern-std=[h|help|?]",
"list all supported standards"
),
Option("fIBT",
"generate Indirect Branch Tracking code"
),
Option("fPIC",
"generate position independent code",
cast(TargetOS) (TargetOS.all & ~(TargetOS.Windows | TargetOS.OSX))
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/dmdparams.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct DMDparams

bool optimize; // run optimizer
bool nofloat; // code should not pull in floating point support
bool ibt; // generate indirect branch tracking
PIC pic = PIC.fixed; // generate fixed, pic or pie code
bool stackstomp; // add stack stomping code

Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/dmsc.d
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void backend_init()
driverParams.symdebug,
driverParams.alwaysframe,
driverParams.stackstomp,
driverParams.ibt,
target.cpu >= CPU.avx2 ? 2 : target.cpu >= CPU.avx ? 1 : 0,
driverParams.pic,
params.useModuleInfo && Module.moduleinfo,
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,10 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
}
else if (arg == "-shared")
driverParams.dll = true;
else if (arg == "-fIBT")
{
driverParams.ibt = true;
}
else if (arg == "-fPIC")
{
driverParams.pic = PIC.pic;
Expand Down

0 comments on commit 9471a8a

Please sign in to comment.