Skip to content

Commit f3ea731

Browse files
authored
[ELF] Set large section flag for globals with an explicit section (#69396)
An oversight in https://reviews.llvm.org/D148836 since this is a different code path.
1 parent 7aa2491 commit f3ea731

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,25 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
763763
return NextUniqueID++;
764764
}
765765

766+
static std::tuple<StringRef, bool, unsigned>
767+
getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
768+
StringRef Group = "";
769+
bool IsComdat = false;
770+
unsigned Flags = 0;
771+
if (const Comdat *C = getELFComdat(GO)) {
772+
Flags |= ELF::SHF_GROUP;
773+
Group = C->getName();
774+
IsComdat = C->getSelectionKind() == Comdat::Any;
775+
}
776+
if (auto *GV = dyn_cast<GlobalVariable>(GO)) {
777+
if (TM.isLargeData(GV)) {
778+
assert(TM.getTargetTriple().getArch() == Triple::x86_64);
779+
Flags |= ELF::SHF_X86_64_LARGE;
780+
}
781+
}
782+
return {Group, IsComdat, Flags};
783+
}
784+
766785
static MCSection *selectExplicitSectionGlobal(
767786
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
768787
MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
@@ -793,14 +812,9 @@ static MCSection *selectExplicitSectionGlobal(
793812
// Infer section flags from the section name if we can.
794813
Kind = getELFKindForNamedSection(SectionName, Kind);
795814

796-
StringRef Group = "";
797-
bool IsComdat = false;
798815
unsigned Flags = getELFSectionFlags(Kind);
799-
if (const Comdat *C = getELFComdat(GO)) {
800-
Group = C->getName();
801-
IsComdat = C->getSelectionKind() == Comdat::Any;
802-
Flags |= ELF::SHF_GROUP;
803-
}
816+
auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
817+
Flags |= ExtraFlags;
804818

805819
unsigned EntrySize = getEntrySizeForKind(Kind);
806820
const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize(
@@ -848,19 +862,8 @@ static MCSectionELF *selectELFSectionForGlobal(
848862
const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
849863
unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
850864

851-
StringRef Group = "";
852-
bool IsComdat = false;
853-
if (const Comdat *C = getELFComdat(GO)) {
854-
Flags |= ELF::SHF_GROUP;
855-
Group = C->getName();
856-
IsComdat = C->getSelectionKind() == Comdat::Any;
857-
}
858-
if (auto *GV = dyn_cast<GlobalVariable>(GO)) {
859-
if (TM.isLargeData(GV)) {
860-
assert(TM.getTargetTriple().getArch() == Triple::x86_64);
861-
Flags |= ELF::SHF_X86_64_LARGE;
862-
}
863-
}
865+
auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
866+
Flags |= ExtraFlags;
864867

865868
// Get the section entry size based on the kind.
866869
unsigned EntrySize = getEntrySizeForKind(Kind);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,31 @@
1717
; RUN: llvm-readelf -S %t | FileCheck %s --check-prefix=SMALL-DS
1818

1919
; SMALL: .data {{.*}} WA {{.*}}
20+
; SMALL: foo {{.*}} WA {{.*}}
2021
; SMALL: .bss {{.*}} WA {{.*}}
2122
; SMALL: .rodata {{.*}} A {{.*}}
2223
; SMALL: .data.rel.ro {{.*}} WA {{.*}}
2324
; SMALL: .tbss {{.*}} WAT {{.*}}
2425
; SMALL: .tdata {{.*}} WAT {{.*}}
2526

2627
; SMALL-DS: .data.data {{.*}} WA {{.*}}
28+
; SMALL-DS: foo {{.*}} WA {{.*}}
2729
; SMALL-DS: .bss.bss {{.*}} WA {{.*}}
2830
; SMALL-DS: .rodata.rodata {{.*}} A {{.*}}
2931
; SMALL-DS: .data.rel.ro.relro {{.*}} WA {{.*}}
3032
; SMALL-DS: .tbss.tbss {{.*}} WAT {{.*}}
3133
; SMALL-DS: .tdata.tdata {{.*}} WAT {{.*}}
3234

3335
; LARGE: .ldata {{.*}} WAl {{.*}}
36+
; LARGE: foo {{.*}} WAl {{.*}}
3437
; LARGE: .lbss {{.*}} WAl {{.*}}
3538
; LARGE: .lrodata {{.*}} Al {{.*}}
3639
; LARGE: .ldata.rel.ro {{.*}} WAl {{.*}}
3740
; LARGE: .tbss {{.*}} WAT {{.*}}
3841
; LARGE: .tdata {{.*}} WAT {{.*}}
3942

4043
; LARGE-DS: .ldata.data {{.*}} WAl {{.*}}
44+
; LARGE-DS: foo {{.*}} WAl {{.*}}
4145
; LARGE-DS: .lbss.bss {{.*}} WAl {{.*}}
4246
; LARGE-DS: .lrodata.rodata {{.*}} Al {{.*}}
4347
; LARGE-DS: .ldata.rel.ro.relro {{.*}} WAl {{.*}}
@@ -48,6 +52,7 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4852
target triple = "x86_64--linux"
4953

5054
@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]
55+
@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 "foo"
5156
@bss = internal global [10 x i64] zeroinitializer
5257
@rodata = internal constant [10 x i64] zeroinitializer
5358
@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]

0 commit comments

Comments
 (0)