-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[BOLT] Avoid EH trampolines for PIEs/DSOs #117106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -901,8 +901,42 @@ void SplitFunctions::splitFunction(BinaryFunction &BF, SplitStrategy &S) { | |
| // have to be placed in the same fragment. When we split them, create | ||
| // trampoline landing pads that will redirect the execution to real LPs. | ||
| TrampolineSetType Trampolines; | ||
| if (!BC.HasFixedLoadAddress && BF.hasEHRanges() && BF.isSplit()) | ||
| Trampolines = createEHTrampolines(BF); | ||
| if (!BC.HasFixedLoadAddress && BF.hasEHRanges() && BF.isSplit()) { | ||
| // If all landing pads for this fragment are grouped in one (potentially | ||
| // different) fragment, we can set LPStart to the start of that fragment | ||
| // and avoid trampoline code. | ||
| bool NeedsTrampolines = false; | ||
| for (FunctionFragment &FF : BF.getLayout().fragments()) { | ||
| // Vector of fragments that contain landing pads for this fragment. | ||
| SmallVector<FragmentNum, 4> LandingPadFragments; | ||
| for (const BinaryBasicBlock *BB : FF) | ||
| for (const BinaryBasicBlock *LPB : BB->landing_pads()) | ||
| LandingPadFragments.push_back(LPB->getFragmentNum()); | ||
|
|
||
| llvm::sort(LandingPadFragments); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be useful to add a comment on why this sorting is needed |
||
| auto Last = llvm::unique(LandingPadFragments); | ||
| LandingPadFragments.erase(Last, LandingPadFragments.end()); | ||
|
|
||
| if (LandingPadFragments.size() == 0) { | ||
| // If the fragment has no landing pads, we can safely set itself as its | ||
| // landing pad fragment. | ||
| BF.setLPFragment(FF.getFragmentNum(), FF.getFragmentNum()); | ||
| } else if (LandingPadFragments.size() == 1) { | ||
| BF.setLPFragment(FF.getFragmentNum(), LandingPadFragments.front()); | ||
| } else { | ||
| NeedsTrampolines = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Trampolines guarantee that all landing pads for any given fragment will | ||
| // be contained in the same fragment. | ||
| if (NeedsTrampolines) { | ||
| for (FunctionFragment &FF : BF.getLayout().fragments()) | ||
| BF.setLPFragment(FF.getFragmentNum(), FF.getFragmentNum()); | ||
| Trampolines = createEHTrampolines(BF); | ||
| } | ||
| } | ||
|
|
||
| // Check the new size to see if it's worth splitting the function. | ||
| if (BC.isX86() && LayoutUpdated) { | ||
|
|
@@ -933,6 +967,10 @@ void SplitFunctions::splitFunction(BinaryFunction &BF, SplitStrategy &S) { | |
| } | ||
| } | ||
|
|
||
| // Restore LP fragment for the main fragment if the splitting was undone. | ||
| if (BF.hasEHRanges() && !BF.isSplit()) | ||
| BF.setLPFragment(FragmentNum::main(), FragmentNum::main()); | ||
|
|
||
| // Fix branches if the splitting decision of the pass after function | ||
| // reordering is different from that of the pass before function reordering. | ||
| if (LayoutUpdated && BC.HasFinalizedFunctionOrder) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # REQUIRES: system-linux | ||
|
|
||
| # RUN: llvm-mc -filetype=obj -triple x86_64-unknown-linux %s -o %t.o | ||
| # RUN: link_fdata %s %t.o %t.fdata | ||
| # RUN: llvm-strip --strip-unneeded %t.o | ||
| # RUN: ld.lld --pie %t.o -o %t.exe -q | ||
| # RUN: llvm-bolt %t.exe -o %t.out --data %t.fdata --split-functions --split-eh \ | ||
| # RUN: --split-all-cold --print-after-lowering --print-only=_start 2>&1 \ | ||
| # RUN: | FileCheck %s | ||
|
|
||
| ## _start has two landing pads: one hot and one cold. Hence, BOLT will introduce | ||
| ## a landing pad trampoline. However, the trampoline code will make the main | ||
| ## split fragment larger than the whole function before split. Then BOLT will | ||
| ## undo the splitting and remove the trampoline. | ||
|
|
||
| # CHECK: Binary Function "_start" | ||
| # CHECK: IsSplit : | ||
| # CHECK-SAME: 0 | ||
|
|
||
| ## Check that a landing pad trampoline was created, but contains no instructions | ||
| ## and falls though to the real landing pad. | ||
|
|
||
| # CHECK: {{^[^[:space:]]+}} (0 instructions | ||
| # CHECK-NEXT: Landing Pad{{$}} | ||
| # CHECK: Exec Count | ||
| # CHECK-SAME: : 0 | ||
| # CHECK: Successors: | ||
| # CHECK-SAME: [[LP:[^[:space:]]+]] | ||
| # CHECK-EMPTY: | ||
| # CHECK-NEXT: [[LP]] | ||
|
|
||
| .text | ||
| .global foo | ||
| .type foo, %function | ||
| foo: | ||
| .cfi_startproc | ||
| ret | ||
| .cfi_endproc | ||
| .size foo, .-foo | ||
|
|
||
| .globl _start | ||
| .type _start, %function | ||
| _start: | ||
| # FDATA: 0 [unknown] 0 1 _start 0 1 100 | ||
| .Lfunc_begin0: | ||
| .cfi_startproc | ||
| .cfi_lsda 27, .Lexception0 | ||
| call foo | ||
| .Ltmp0: | ||
| call foo | ||
| .Ltmp1: | ||
| ret | ||
|
|
||
| ## Cold landing pad. | ||
| .LLP1: | ||
| ret | ||
|
|
||
| ## Hot landing pad. | ||
| LLP0: | ||
| # FDATA: 0 [unknown] 0 1 _start #LLP0# 1 100 | ||
| ret | ||
|
|
||
| .cfi_endproc | ||
| .Lfunc_end0: | ||
| .size _start, .-_start | ||
|
|
||
| ## EH table. | ||
| .section .gcc_except_table,"a",@progbits | ||
| .p2align 2 | ||
| GCC_except_table0: | ||
| .Lexception0: | ||
| .byte 255 # @LPStart Encoding = omit | ||
| .byte 255 # @TType Encoding = omit | ||
| .byte 1 # Call site Encoding = uleb128 | ||
| .uleb128 .Lcst_end0-.Lcst_begin0 | ||
| .Lcst_begin0: | ||
| .uleb128 .Lfunc_begin0-.Lfunc_begin0 # >> Call Site 1 << | ||
| .uleb128 .Ltmp0-.Lfunc_begin0 # Call between .Lfunc_begin0 and .Ltmp0 | ||
| .uleb128 LLP0-.Lfunc_begin0 # jumps to LLP0 | ||
| .byte 0 # On action: cleanup | ||
| .uleb128 .Ltmp0-.Lfunc_begin0 # >> Call Site 2 << | ||
| .uleb128 .Ltmp1-.Ltmp0 # Call between .Ltmp0 and .Ltmp1 | ||
| .uleb128 .LLP1-.Lfunc_begin0 # jumps to .LLP1 | ||
| .byte 0 # On action: cleanup | ||
| .Lcst_end0: | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably I'm missing something here -- but why resize + [] instead of insert?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we are growing the vector on demand? Now I'm thinking if I'm missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SG, nevermind.