Skip to content

Commit 323451a

Browse files
authored
[X86] Set SHF_X86_64_LARGE for globals with explicit well-known large section name (llvm#74381)
Globals marked with the .lbss/.ldata/.lrodata should automatically be treated as large. Do this regardless of the code model for consistency when mixing object files compiled with different code models. Basically the other half of llvm#70748. Example in the wild: https://codebrowser.dev/qt5/qtbase/src/testlib/qtestcase.cpp.html#1664
1 parent f1db578 commit 323451a

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

llvm/lib/Target/TargetMachine.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ bool TargetMachine::isLargeGlobalObject(const GlobalObject *GO) const {
4343
if (getTargetTriple().getArch() != Triple::x86_64)
4444
return false;
4545

46-
if (getCodeModel() != CodeModel::Medium && getCodeModel() != CodeModel::Large)
47-
return false;
48-
4946
if (isa<Function>(GO))
5047
return getCodeModel() == CodeModel::Large;
5148

@@ -54,22 +51,29 @@ bool TargetMachine::isLargeGlobalObject(const GlobalObject *GO) const {
5451
if (GV->isThreadLocal())
5552
return false;
5653

57-
// Allowing large metadata sections in the presence of an explicit section is
58-
// useful, even if GCC does not allow them. However, we should not mark
59-
// certain well-known prefixes as large, because it would make the whole
60-
// output section large and cause the linker to move it, which is almost
61-
// always undesired.
54+
// We should properly mark well-known section name prefixes as small/large,
55+
// because otherwise the output section may have the wrong section flags and
56+
// the linker will lay it out in an unexpected way.
6257
StringRef Name = GV->getSection();
63-
auto IsPrefix = [&](StringRef Prefix) {
64-
StringRef S = Name;
65-
return S.consume_front(Prefix) && (S.empty() || S[0] == '.');
66-
};
67-
if (IsPrefix(".bss") || IsPrefix(".data") || IsPrefix(".rodata"))
68-
return false;
58+
if (!Name.empty()) {
59+
auto IsPrefix = [&](StringRef Prefix) {
60+
StringRef S = Name;
61+
return S.consume_front(Prefix) && (S.empty() || S[0] == '.');
62+
};
63+
if (IsPrefix(".bss") || IsPrefix(".data") || IsPrefix(".rodata"))
64+
return false;
65+
if (IsPrefix(".lbss") || IsPrefix(".ldata") || IsPrefix(".lrodata"))
66+
return true;
67+
}
68+
69+
if (getCodeModel() == CodeModel::Medium ||
70+
getCodeModel() == CodeModel::Large) {
71+
const DataLayout &DL = GV->getParent()->getDataLayout();
72+
uint64_t Size = DL.getTypeSizeInBits(GV->getValueType()) / 8;
73+
return Size == 0 || Size > LargeDataThreshold;
74+
}
6975

70-
const DataLayout &DL = GV->getParent()->getDataLayout();
71-
uint64_t Size = DL.getTypeSizeInBits(GV->getValueType()) / 8;
72-
return Size == 0 || Size > LargeDataThreshold;
76+
return false;
7377
}
7478

7579
bool TargetMachine::isPositionIndependent() const {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,29 @@
2121
; SMALL: .data {{.*}} WA {{.*}}
2222
; SMALL: .data.x {{.*}} WA {{.*}}
2323
; SMALL: .data0 {{.*}} WA {{.*}}
24+
; SMALL: .ldata {{.*}} WAl {{.*}}
25+
; SMALL: .ldata.x {{.*}} WAl {{.*}}
26+
; SMALL: .ldata0 {{.*}} WA {{.*}}
2427
; SMALL: foo {{.*}} WA {{.*}}
2528
; SMALL: .bss {{.*}} WA {{.*}}
29+
; SMALL: .lbss {{.*}} WAl {{.*}}
2630
; SMALL: .rodata {{.*}} A {{.*}}
31+
; SMALL: .lrodata {{.*}} Al {{.*}}
2732
; SMALL: .data.rel.ro {{.*}} WA {{.*}}
2833
; SMALL: .tbss {{.*}} WAT {{.*}}
2934
; SMALL: .tdata {{.*}} WAT {{.*}}
3035

3136
; SMALL-DS: .data {{.*}} WA {{.*}}
3237
; SMALL-DS: .data.x {{.*}} WA {{.*}}
3338
; SMALL-DS: .data0 {{.*}} WA {{.*}}
39+
; SMALL-DS: .ldata {{.*}} WAl {{.*}}
40+
; SMALL-DS: .ldata.x {{.*}} WAl {{.*}}
41+
; SMALL-DS: .ldata0 {{.*}} WA {{.*}}
3442
; SMALL-DS: .data.data {{.*}} WA {{.*}}
3543
; SMALL-DS: foo {{.*}} WA {{.*}}
44+
; SMALL-DS: .lbss {{.*}} WAl {{.*}}
3645
; SMALL-DS: .bss.bss {{.*}} WA {{.*}}
46+
; SMALL-DS: .lrodata {{.*}} Al {{.*}}
3747
; SMALL-DS: .rodata.rodata {{.*}} A {{.*}}
3848
; SMALL-DS: .data.rel.ro.relro {{.*}} WA {{.*}}
3949
; SMALL-DS: .tbss.tbss {{.*}} WAT {{.*}}
@@ -43,6 +53,8 @@
4353
; LARGE: .data.x {{.*}} WA {{.*}}
4454
; LARGE: .data0 {{.*}} WAl {{.*}}
4555
; LARGE: .ldata {{.*}} WAl {{.*}}
56+
; LARGE: .ldata.x {{.*}} WAl {{.*}}
57+
; LARGE: .ldata0 {{.*}} WAl {{.*}}
4658
; LARGE: foo {{.*}} WAl {{.*}}
4759
; LARGE: .bss {{.*}} WA {{.*}}
4860
; LARGE: .lbss {{.*}} WAl {{.*}}
@@ -55,6 +67,9 @@
5567
; LARGE-DS: .data {{.*}} WA {{.*}}
5668
; LARGE-DS: .data.x {{.*}} WA {{.*}}
5769
; LARGE-DS: .data0 {{.*}} WAl {{.*}}
70+
; LARGE-DS: .ldata {{.*}} WAl {{.*}}
71+
; LARGE-DS: .ldata.x {{.*}} WAl {{.*}}
72+
; LARGE-DS: .ldata0 {{.*}} WAl {{.*}}
5873
; LARGE-DS: .ldata.data {{.*}} WAl {{.*}}
5974
; LARGE-DS: foo {{.*}} WAl {{.*}}
6075
; LARGE-DS: .bss {{.*}} WA {{.*}}
@@ -71,11 +86,16 @@ target triple = "x86_64--linux"
7186
@data_with_explicit_section = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".data"
7287
@data_with_explicit_section2 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".data.x"
7388
@data_with_explicit_section0 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".data0"
89+
@ldata_with_explicit_section = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".ldata"
90+
@ldata_with_explicit_section2 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".ldata.x"
91+
@ldata_with_explicit_section0 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".ldata0"
7492
@data = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0]
7593
@foo_with_explicit_section = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section "foo"
7694
@bss_with_explicit_section = internal global [10 x i64] zeroinitializer, section ".bss"
95+
@lbss_with_explicit_section = internal global [10 x i64] zeroinitializer, section ".lbss"
7796
@bss = internal global [10 x i64] zeroinitializer
7897
@rodata_with_explicit_section = internal constant [10 x i64] zeroinitializer, section ".rodata"
98+
@lrodata_with_explicit_section = internal constant [10 x i64] zeroinitializer, section ".lrodata"
7999
@rodata = internal constant [10 x i64] zeroinitializer
80100
@relro = internal constant [10 x ptr] [ptr @func, ptr @func, ptr @func, ptr @func, ptr @func, ptr @func, ptr @func, ptr @func, ptr @func, ptr @func]
81101
@tbss = internal thread_local global [10 x i64] zeroinitializer

0 commit comments

Comments
 (0)