Skip to content

Commit c88b537

Browse files
authored
Target: Stop assigning RELRO sections to .ldata.rel.ro.
Linkers do not currently support PT_GNU_RELRO for SHF_X86_64_LARGE sections; that would require the linker to emit more than one PT_GNU_RELRO because large sections are discontiguous by design, and most ELF dynamic loaders do not support that (bionic appears to support it but glibc/musl/FreeBSD/NetBSD/OpenBSD appear not to). With current linkers these sections will end up in .ldata which results in silently disabling RELRO. Therefore, disable SHF_X86_64_LARGE for RELRO sections. If this ever gets supported by downstream components in the future we could add an opt-in flag for moving these sections to .ldata.rel.ro which would trigger the creation of a second PT_GNU_RELRO. Reviewers: MaskRay, aeubanks Reviewed By: aeubanks Pull Request: llvm#137742
1 parent f784fa7 commit c88b537

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

llvm/lib/Target/TargetMachine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
119119
GV->getName().starts_with("__start_") ||
120120
GV->getName().starts_with("__stop_")))
121121
return true;
122+
// Linkers do not currently support PT_GNU_RELRO for SHF_X86_64_LARGE
123+
// sections; that would require the linker to emit more than one
124+
// PT_GNU_RELRO because large sections are discontiguous by design, and most
125+
// ELF dynamic loaders do not support that (bionic appears to support it but
126+
// glibc/musl/FreeBSD/NetBSD/OpenBSD appear not to). With current linkers
127+
// these sections will end up in .ldata which results in silently disabling
128+
// RELRO. If this ever gets supported by downstream components in the future
129+
// we could add an opt-in flag for moving these sections to .ldata.rel.ro
130+
// which would trigger the creation of a second PT_GNU_RELRO.
131+
if (!GV->isDeclarationForLinker() &&
132+
TargetLoweringObjectFile::getKindForGlobal(GV, *this)
133+
.isReadOnlyWithRel())
134+
return false;
122135
const DataLayout &DL = GV->getDataLayout();
123136
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
124137
return Size == 0 || Size > LargeDataThreshold;

llvm/test/CodeGen/X86/code-model-elf-sections.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
; LARGE: .lbss {{.*}} WAl {{.*}}
6767
; LARGE: .rodata {{.*}} A {{.*}}
6868
; LARGE: .lrodata {{.*}} Al {{.*}}
69-
; LARGE: .ldata.rel.ro {{.*}} WAl {{.*}}
69+
; LARGE: .data.rel.ro {{.*}} WA {{.*}}
7070
; LARGE: .tbss {{.*}} WAT {{.*}}
7171
; LARGE: .tdata {{.*}} WAT {{.*}}
7272

@@ -84,7 +84,7 @@
8484
; LARGE-DS: .lbss.bss {{.*}} WAl {{.*}}
8585
; LARGE-DS: .rodata {{.*}} A {{.*}}
8686
; LARGE-DS: .lrodata.rodata {{.*}} Al {{.*}}
87-
; LARGE-DS: .ldata.rel.ro.relro {{.*}} WAl {{.*}}
87+
; LARGE-DS: .data.rel.ro.relro {{.*}} WA {{.*}}
8888
; LARGE-DS: .tbss.tbss {{.*}} WAT {{.*}}
8989
; LARGE-DS: .tdata.tdata {{.*}} WAT {{.*}}
9090

llvm/test/CodeGen/X86/code-model-elf.ll

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ target triple = "x86_64--linux"
5050

5151
@global_data = dso_local global [10 x i32] [i32 1, i32 2, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0], align 16
5252
@static_data = internal global [10 x i32] zeroinitializer, align 16
53-
@static_data_alias = internal constant ptr getelementptr inbounds ([10 x i32], ptr @static_data, i64 0, i64 2), align 8
53+
@static_data_relro = internal constant ptr getelementptr inbounds ([10 x i32], ptr @static_data, i64 0, i64 2), align 8
5454
@extern_data = external global [10 x i32], align 16
5555
@thread_data = external thread_local global i32, align 4
5656
@unknown_size_data = dso_local global [0 x i32] zeroinitializer, align 16
@@ -117,59 +117,57 @@ define dso_local ptr @lea_static_data() #0 {
117117
ret ptr @static_data
118118
}
119119

120-
define dso_local ptr @lea_static_data_alias() #0 {
121-
; SMALL-STATIC-LABEL: lea_static_data_alias:
120+
define dso_local ptr @lea_static_data_relro() #0 {
121+
; SMALL-STATIC-LABEL: lea_static_data_relro:
122122
; SMALL-STATIC: # %bb.0:
123-
; SMALL-STATIC-NEXT: movl $static_data_alias, %eax
123+
; SMALL-STATIC-NEXT: movl $static_data_relro, %eax
124124
; SMALL-STATIC-NEXT: retq
125125
;
126-
; MEDIUM-STATIC-LABEL: lea_static_data_alias:
126+
; MEDIUM-STATIC-LABEL: lea_static_data_relro:
127127
; MEDIUM-STATIC: # %bb.0:
128-
; MEDIUM-STATIC-NEXT: movabsq $static_data_alias, %rax
128+
; MEDIUM-STATIC-NEXT: movabsq $static_data_relro, %rax
129129
; MEDIUM-STATIC-NEXT: retq
130130
;
131-
; LARGE-STATIC-LABEL: lea_static_data_alias:
131+
; LARGE-STATIC-LABEL: lea_static_data_relro:
132132
; LARGE-STATIC: # %bb.0:
133-
; LARGE-STATIC-NEXT: movabsq $static_data_alias, %rax
133+
; LARGE-STATIC-NEXT: movabsq $static_data_relro, %rax
134134
; LARGE-STATIC-NEXT: retq
135135
;
136-
; SMALL-PIC-LABEL: lea_static_data_alias:
136+
; SMALL-PIC-LABEL: lea_static_data_relro:
137137
; SMALL-PIC: # %bb.0:
138-
; SMALL-PIC-NEXT: leaq static_data_alias(%rip), %rax
138+
; SMALL-PIC-NEXT: leaq static_data_relro(%rip), %rax
139139
; SMALL-PIC-NEXT: retq
140140
;
141-
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_data_alias:
141+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_data_relro:
142142
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
143-
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_data_alias(%rip), %rax
143+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_data_relro(%rip), %rax
144144
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
145145
;
146-
; MEDIUM-PIC-LABEL: lea_static_data_alias:
146+
; MEDIUM-PIC-LABEL: lea_static_data_relro:
147147
; MEDIUM-PIC: # %bb.0:
148-
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
149-
; MEDIUM-PIC-NEXT: movabsq $static_data_alias@GOTOFF, %rax
150-
; MEDIUM-PIC-NEXT: addq %rcx, %rax
148+
; MEDIUM-PIC-NEXT: leaq static_data_relro(%rip), %rax
151149
; MEDIUM-PIC-NEXT: retq
152150
;
153-
; LARGE-PIC-LABEL: lea_static_data_alias:
151+
; LARGE-PIC-LABEL: lea_static_data_relro:
154152
; LARGE-PIC: # %bb.0:
155153
; LARGE-PIC-NEXT: .L1$pb:
156154
; LARGE-PIC-NEXT: leaq .L1$pb(%rip), %rax
157155
; LARGE-PIC-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
158156
; LARGE-PIC-NEXT: addq %rax, %rcx
159-
; LARGE-PIC-NEXT: movabsq $static_data_alias@GOTOFF, %rax
157+
; LARGE-PIC-NEXT: movabsq $static_data_relro@GOTOFF, %rax
160158
; LARGE-PIC-NEXT: addq %rcx, %rax
161159
; LARGE-PIC-NEXT: retq
162160
;
163-
; LARGE-SMALL-DATA-PIC-LABEL: lea_static_data_alias:
161+
; LARGE-SMALL-DATA-PIC-LABEL: lea_static_data_relro:
164162
; LARGE-SMALL-DATA-PIC: # %bb.0:
165163
; LARGE-SMALL-DATA-PIC-NEXT: .L1$pb:
166164
; LARGE-SMALL-DATA-PIC-NEXT: leaq .L1$pb(%rip), %rax
167165
; LARGE-SMALL-DATA-PIC-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
168166
; LARGE-SMALL-DATA-PIC-NEXT: addq %rax, %rcx
169-
; LARGE-SMALL-DATA-PIC-NEXT: movabsq $static_data_alias@GOTOFF, %rax
167+
; LARGE-SMALL-DATA-PIC-NEXT: movabsq $static_data_relro@GOTOFF, %rax
170168
; LARGE-SMALL-DATA-PIC-NEXT: addq %rcx, %rax
171169
; LARGE-SMALL-DATA-PIC-NEXT: retq
172-
ret ptr @static_data_alias
170+
ret ptr @static_data_relro
173171
}
174172

175173
define dso_local ptr @lea_global_data() #0 {

0 commit comments

Comments
 (0)