Skip to content

Commit dcfd960

Browse files
dpaoliellosivadeilra
authored andcommitted
Apply 0011-section_fixes.patch
1 parent b7d0781 commit dcfd960

File tree

18 files changed

+376
-38
lines changed

18 files changed

+376
-38
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,6 +3668,9 @@ OPT_LIST(V)
36683668
PSF_Execute = 0x4,
36693669
PSF_Implicit = 0x8,
36703670
PSF_ZeroInit = 0x10,
3671+
PSF_NoPage = 0x20,
3672+
PSF_Shared = 0x40,
3673+
PSF_Discard = 0x80,
36713674
PSF_Invalid = 0x80000000U,
36723675
};
36733676

clang/include/clang/Basic/Attr.td

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,14 @@ def Section : InheritableAttr {
32383238
let Documentation = [SectionDocs];
32393239
}
32403240

3241+
def SectionFlags : InheritableAttr {
3242+
let Spellings = [];
3243+
let Args = [IntArgument<"Flags">];
3244+
let Subjects =
3245+
SubjectList<[ GlobalVar ], ErrorDiag>;
3246+
let Documentation = [SectionDocs];
3247+
}
3248+
32413249
// This is used for `__declspec(code_seg("segname"))`, but not for
32423250
// `#pragma code_seg("segname")`.
32433251
def CodeSeg : InheritableAttr {
@@ -3279,6 +3287,30 @@ def PragmaClangRelroSection : InheritableAttr {
32793287
let Documentation = [InternalOnly];
32803288
}
32813289

3290+
def PragmaMSBSSSection : InheritableAttr {
3291+
// This attribute has no spellings as it is only ever created implicitly.
3292+
let Spellings = [];
3293+
let Args = [StringArgument<"Name">];
3294+
let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
3295+
let Documentation = [InternalOnly];
3296+
}
3297+
3298+
def PragmaMSDataSection : InheritableAttr {
3299+
// This attribute has no spellings as it is only ever created implicitly.
3300+
let Spellings = [];
3301+
let Args = [StringArgument<"Name">];
3302+
let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
3303+
let Documentation = [InternalOnly];
3304+
}
3305+
3306+
def PragmaMSConstSection : InheritableAttr {
3307+
// This attribute has no spellings as it is only ever created implicitly.
3308+
let Spellings = [];
3309+
let Args = [StringArgument<"Name">];
3310+
let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
3311+
let Documentation = [InternalOnly];
3312+
}
3313+
32823314
def StrictFP : InheritableAttr {
32833315
// This attribute has no spellings as it is only ever created implicitly.
32843316
// Function uses strict floating point operations.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ def warn_cxx_ms_struct :
11861186
def err_pragma_pack_identifer_not_supported : Error<
11871187
"specifying an identifier within `#pragma pack` is not supported on this target">;
11881188
def err_section_conflict : Error<"%0 causes a section type conflict with %1">;
1189+
def err_section_unknown : Error<"%0 assigned to unknown section %1">;
11891190
def warn_section_msvc_compat : Warning<"`#pragma const_seg` for section %1 will"
11901191
" not apply to %0 due to the presence of a %select{mutable field||non-trivial constructor|non-trivial destructor}2">,
11911192
InGroup<IncompatibleMSPragmaSection>;

clang/include/clang/Sema/Sema.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,10 +2020,10 @@ class Sema final : public SemaBase {
20202020
};
20212021
SmallVector<AlignPackIncludeState, 8> AlignPackIncludeStack;
20222022
// Segment #pragmas.
2023-
PragmaStack<StringLiteral *> DataSegStack;
2024-
PragmaStack<StringLiteral *> BSSSegStack;
2025-
PragmaStack<StringLiteral *> ConstSegStack;
2026-
PragmaStack<StringLiteral *> CodeSegStack;
2023+
PragmaStack<std::string> DataSegStack;
2024+
PragmaStack<std::string> BSSSegStack;
2025+
PragmaStack<std::string> ConstSegStack;
2026+
PragmaStack<std::string> CodeSegStack;
20272027

20282028
// #pragma strict_gs_check.
20292029
PragmaStack<bool> StrictGuardStackCheckStack;
@@ -2061,7 +2061,7 @@ class Sema final : public SemaBase {
20612061
};
20622062

20632063
/// Last section used with #pragma init_seg.
2064-
StringLiteral *CurInitSeg;
2064+
std::string CurInitSeg;
20652065
SourceLocation CurInitSegLoc;
20662066

20672067
/// Sections used with #pragma alloc_text.
@@ -2221,6 +2221,7 @@ class Sema final : public SemaBase {
22212221
NamedDecl *TheDecl);
22222222
bool UnifySection(StringRef SectionName, int SectionFlags,
22232223
SourceLocation PragmaSectionLocation);
2224+
void AttachSectionFlags(VarDecl *VarDecl, StringRef SectionName);
22242225

22252226
/// Called on well formed \#pragma bss_seg/data_seg/const_seg/code_seg.
22262227
void ActOnPragmaMSSeg(SourceLocation PragmaLocation,

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,15 @@ enum ASTRecordTypes {
742742
UPDATE_MODULE_LOCAL_VISIBLE = 76,
743743

744744
UPDATE_TU_LOCAL_VISIBLE = 77,
745+
746+
// Record for for current init section
747+
CURRENT_INIT_SECTION = 78,
748+
749+
// Record code for the SectionInfos table that records the known sections
750+
SECTION_INFOS = 79,
751+
752+
// Record code for the pragma segment stacks
753+
SEGMENT_PRAGMA_OPTIONS = 80,
745754
};
746755

747756
/// Record types used within a source manager block.

clang/include/clang/Serialization/ASTReader.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,37 @@ class ASTReader
10291029
llvm::SmallVector<PragmaAlignPackStackEntry, 2> PragmaAlignPackStack;
10301030
llvm::SmallVector<std::string, 2> PragmaAlignPackStrings;
10311031

1032+
// The pragma init_seg() state.
1033+
std::string CurInitSeg = "";
1034+
SourceLocation CurInitSegLoc;
1035+
1036+
// The section info state
1037+
struct SectionInfo {
1038+
std::string SectionName;
1039+
SourceLocation PragmaSectionLocation;
1040+
int SectionFlags;
1041+
};
1042+
llvm::SmallVector<SectionInfo, 2> SectionInfos;
1043+
1044+
struct PragmaSegmentStackEntry {
1045+
std::string Value;
1046+
SourceLocation Location;
1047+
SourceLocation PushLocation;
1048+
StringRef SlotLabel;
1049+
};
1050+
1051+
struct PragmaSegmentStackState {
1052+
std::optional<std::string> PragmaCurrentValue;
1053+
SourceLocation PragmaCurrentLocation;
1054+
llvm::SmallVector<PragmaSegmentStackEntry, 2> PragmaStack;
1055+
llvm::SmallVector<std::string, 2> PragmaStrings;
1056+
};
1057+
1058+
PragmaSegmentStackState PragmaDataSegmentStackState;
1059+
PragmaSegmentStackState PragmaBSSSegmentStackState;
1060+
PragmaSegmentStackState PragmaConstSegmentStackState;
1061+
PragmaSegmentStackState PragmaCodeSegmentStackState;
1062+
10321063
/// The OpenCL extension settings.
10331064
OpenCLOptions OpenCLExtensions;
10341065

@@ -1942,6 +1973,8 @@ class ASTReader
19421973
/// Initializes the ASTContext
19431974
void InitializeContext();
19441975

1976+
void UpdateSemaPragmaSegment(PragmaSegmentStackState &StackState, Sema::PragmaStack<std::string> &PragmaStack);
1977+
19451978
/// Update the state of Sema after loading some additional modules.
19461979
void UpdateSema();
19471980

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ class ASTWriter : public ASTDeserializationListener,
634634
void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
635635
void WritePackPragmaOptions(Sema &SemaRef);
636636
void WriteFloatControlPragmaOptions(Sema &SemaRef);
637+
void WriteInitSection(Sema &SemaRef);
638+
void WriteSectionInfos(Sema &SemaRef);
639+
void WriteSegmentStack(Sema::PragmaStack<std::string> &SegmentStack, StringRef SegmentName);
640+
void WriteSegmentStacks(Sema &SemaRef);
637641
void WriteDeclsWithEffectsToVerify(Sema &SemaRef);
638642
void WriteModuleFileExtension(Sema &SemaRef,
639643
ModuleFileExtensionWriter &Writer);

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
449449
if (auto *SA = D.getAttr<PragmaClangRelroSectionAttr>())
450450
var->addAttribute("relro-section", SA->getName());
451451

452+
if (auto *SA = D.getAttr<PragmaMSBSSSectionAttr>())
453+
var->addAttribute("bss-section", SA->getName());
454+
if (auto *SA = D.getAttr<PragmaMSDataSectionAttr>())
455+
var->addAttribute("data-section", SA->getName());
456+
if (auto *SA = D.getAttr<PragmaMSConstSectionAttr>())
457+
var->addAttribute("rodata-section", SA->getName());
458+
452459
if (const SectionAttr *SA = D.getAttr<SectionAttr>())
453460
var->setSection(SA->getName());
454461

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,6 +2934,13 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
29342934
GV->addAttribute("rodata-section", SA->getName());
29352935
if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
29362936
GV->addAttribute("relro-section", SA->getName());
2937+
2938+
if (auto *SA = D->getAttr<PragmaMSBSSSectionAttr>())
2939+
GV->addAttribute("bss-section", SA->getName());
2940+
if (auto *SA = D->getAttr<PragmaMSDataSectionAttr>())
2941+
GV->addAttribute("data-section", SA->getName());
2942+
if (auto *SA = D->getAttr<PragmaMSConstSectionAttr>())
2943+
GV->addAttribute("rodata-section", SA->getName());
29372944
}
29382945

29392946
if (auto *F = dyn_cast<llvm::Function>(GO)) {
@@ -5900,11 +5907,9 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
59005907
D->getType().isConstantStorage(getContext(), true, true)));
59015908

59025909
// If it is in a read-only section, mark it 'constant'.
5903-
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5904-
const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5905-
if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
5910+
if (const SectionFlagsAttr *SFA = D->getAttr<SectionFlagsAttr>())
5911+
if ((SFA->getFlags() & ASTContext::PSF_Write) == 0)
59065912
GV->setConstant(true);
5907-
}
59085913

59095914
CharUnits AlignVal = getContext().getDeclAlign(D);
59105915
// Check for alignment specifed in an 'omp allocate' directive.

clang/lib/Parse/ParsePragma.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,10 +1097,10 @@ bool Parser::HandlePragmaMSSection(StringRef PragmaName,
10971097
.Case("read", ASTContext::PSF_Read)
10981098
.Case("write", ASTContext::PSF_Write)
10991099
.Case("execute", ASTContext::PSF_Execute)
1100-
.Case("shared", ASTContext::PSF_Invalid)
1101-
.Case("nopage", ASTContext::PSF_Invalid)
1100+
.Case("shared", ASTContext::PSF_Shared)
1101+
.Case("nopage", ASTContext::PSF_NoPage)
11021102
.Case("nocache", ASTContext::PSF_Invalid)
1103-
.Case("discard", ASTContext::PSF_Invalid)
1103+
.Case("discard", ASTContext::PSF_Discard)
11041104
.Case("remove", ASTContext::PSF_Invalid)
11051105
.Default(ASTContext::PSF_None);
11061106
if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {

0 commit comments

Comments
 (0)