-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy path0080-RISCV-Encode-RISCV-specific-ELF-e_flags-to-RISCV-Bin.patch
219 lines (214 loc) · 8.09 KB
/
0080-RISCV-Encode-RISCV-specific-ELF-e_flags-to-RISCV-Bin.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb@lowrisc.org>
Subject: [RISCV] Encode RISCV specific ELF e_flags to RISCV Binary by
RISCVTargetStreamer
Differential Revision: https://reviews.llvm.org/D41658
Patch by Shiva Chen.
---
lib/Target/RISCV/MCTargetDesc/CMakeLists.txt | 2 ++
lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp | 39 ++++++++++++++++++++++
lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h | 24 +++++++++++++
.../RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp | 12 +++++++
.../RISCV/MCTargetDesc/RISCVTargetStreamer.cpp | 18 ++++++++++
.../RISCV/MCTargetDesc/RISCVTargetStreamer.h | 22 ++++++++++++
test/MC/RISCV/elf-flags.s | 13 ++++++++
7 files changed, 130 insertions(+)
create mode 100644 lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
create mode 100644 lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
create mode 100644 lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
create mode 100644 lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
create mode 100644 test/MC/RISCV/elf-flags.s
diff --git a/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt b/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt
index 60429647edd..d9f4188aa75 100644
--- a/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt
+++ b/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt
@@ -5,4 +5,6 @@ add_llvm_library(LLVMRISCVDesc
RISCVMCCodeEmitter.cpp
RISCVMCExpr.cpp
RISCVMCTargetDesc.cpp
+ RISCVTargetStreamer.cpp
+ RISCVELFStreamer.cpp
)
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
new file mode 100644
index 00000000000..243f8ed7fb5
--- /dev/null
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
@@ -0,0 +1,39 @@
+//===-- RISCVELFStreamer.cpp - RISCV ELF Target Streamer Methods ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides RISCV specific target streamer methods.
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCVELFStreamer.h"
+#include "RISCVMCTargetDesc.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+
+using namespace llvm;
+
+// This part is for ELF object output.
+RISCVTargetELFStreamer::RISCVTargetELFStreamer(MCStreamer &S,
+ const MCSubtargetInfo &STI)
+ : RISCVTargetStreamer(S) {
+ MCAssembler &MCA = getStreamer().getAssembler();
+
+ const FeatureBitset &Features = STI.getFeatureBits();
+
+ unsigned EFlags = MCA.getELFHeaderEFlags();
+
+ if (Features[RISCV::FeatureStdExtC])
+ EFlags |= ELF::EF_RISCV_RVC;
+
+ MCA.setELFHeaderEFlags(EFlags);
+}
+
+MCELFStreamer &RISCVTargetELFStreamer::getStreamer() {
+ return static_cast<MCELFStreamer &>(Streamer);
+}
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
new file mode 100644
index 00000000000..d3cfbe3e83c
--- /dev/null
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
@@ -0,0 +1,24 @@
+//===-- RISCVELFStreamer.h - RISCV ELF Target Streamer ---------*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_RISCV_RISCVELFSTREAMER_H
+#define LLVM_LIB_TARGET_RISCV_RISCVELFSTREAMER_H
+
+#include "RISCVTargetStreamer.h"
+#include "llvm/MC/MCELFStreamer.h"
+
+namespace llvm {
+
+class RISCVTargetELFStreamer : public RISCVTargetStreamer {
+public:
+ MCELFStreamer &getStreamer();
+ RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
+};
+}
+#endif
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
index f9a69826e10..ad35f8c4d9e 100644
--- a/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
@@ -13,7 +13,9 @@
#include "RISCVMCTargetDesc.h"
#include "InstPrinter/RISCVInstPrinter.h"
+#include "RISCVELFStreamer.h"
#include "RISCVMCAsmInfo.h"
+#include "RISCVTargetStreamer.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCInstrInfo.h"
@@ -71,6 +73,14 @@ static MCInstPrinter *createRISCVMCInstPrinter(const Triple &T,
return new RISCVInstPrinter(MAI, MII, MRI);
}
+static MCTargetStreamer *
+createRISCVObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
+ const Triple &TT = STI.getTargetTriple();
+ if (TT.isOSBinFormatELF())
+ return new RISCVTargetELFStreamer(S, STI);
+ return new RISCVTargetStreamer(S);
+}
+
extern "C" void LLVMInitializeRISCVTargetMC() {
for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) {
TargetRegistry::RegisterMCAsmInfo(*T, createRISCVMCAsmInfo);
@@ -80,5 +90,7 @@ extern "C" void LLVMInitializeRISCVTargetMC() {
TargetRegistry::RegisterMCCodeEmitter(*T, createRISCVMCCodeEmitter);
TargetRegistry::RegisterMCInstPrinter(*T, createRISCVMCInstPrinter);
TargetRegistry::RegisterMCSubtargetInfo(*T, createRISCVMCSubtargetInfo);
+ TargetRegistry::RegisterObjectTargetStreamer(
+ *T, createRISCVObjectTargetStreamer);
}
}
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
new file mode 100644
index 00000000000..e72b0243b2e
--- /dev/null
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
@@ -0,0 +1,18 @@
+//===-- RISCVTargetStreamer.cpp - RISCV Target Streamer Methods -----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides RISCV specific target streamer methods.
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCVTargetStreamer.h"
+
+using namespace llvm;
+
+RISCVTargetStreamer::RISCVTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h b/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
new file mode 100644
index 00000000000..3f395405b59
--- /dev/null
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
@@ -0,0 +1,22 @@
+//===-- RISCVTargetStreamer.h - RISCV Target Streamer ----------*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_RISCV_RISCVTARGETSTREAMER_H
+#define LLVM_LIB_TARGET_RISCV_RISCVTARGETSTREAMER_H
+
+#include "llvm/MC/MCStreamer.h"
+
+namespace llvm {
+
+class RISCVTargetStreamer : public MCTargetStreamer {
+public:
+ RISCVTargetStreamer(MCStreamer &S);
+};
+}
+#endif
diff --git a/test/MC/RISCV/elf-flags.s b/test/MC/RISCV/elf-flags.s
new file mode 100644
index 00000000000..1d183aab339
--- /dev/null
+++ b/test/MC/RISCV/elf-flags.s
@@ -0,0 +1,13 @@
+# RUN: llvm-mc -triple=riscv32 -filetype=obj < %s | llvm-readobj -file-headers - | FileCheck -check-prefixes=CHECK-RVI %s
+# RUN: llvm-mc -triple=riscv64 -filetype=obj < %s | llvm-readobj -file-headers - | FileCheck -check-prefixes=CHECK-RVI %s
+# RUN: llvm-mc -triple=riscv32 -mattr=+c -filetype=obj < %s | llvm-readobj -file-headers - | FileCheck -check-prefixes=CHECK-RVIC %s
+# RUN: llvm-mc -triple=riscv64 -mattr=+c -filetype=obj < %s | llvm-readobj -file-headers - | FileCheck -check-prefixes=CHECK-RVIC %s
+
+# CHECK-RVI: Flags [ (0x0)
+# CHECK-RVI-NEXT: ]
+
+# CHECK-RVIC: Flags [ (0x1)
+# CHECK-RVIC-NEXT: EF_RISCV_RVC (0x1)
+# CHECK-RVIC-NEXT: ]
+
+nop
--
2.16.2