10
10
#include " llvm/ExecutionEngine/Orc/MachOPlatform.h"
11
11
#include " llvm/IR/Constants.h"
12
12
#include " llvm/IR/Mangler.h"
13
+ #include " llvm/Object/ELFObjectFile.h"
13
14
#include " llvm/Object/MachO.h"
14
15
#include " llvm/Object/ObjectFile.h"
15
16
#include " llvm/Support/Debug.h"
@@ -83,17 +84,12 @@ void IRSymbolMapper::add(ExecutionSession &ES, const ManglingOptions &MO,
83
84
}
84
85
}
85
86
86
- Expected<std::pair<SymbolFlagsMap, SymbolStringPtr>>
87
- getObjectSymbolInfo (ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
88
- auto Obj = object::ObjectFile::createObjectFile (ObjBuffer);
89
-
90
- if (!Obj)
91
- return Obj.takeError ();
92
-
93
- bool IsMachO = isa<object::MachOObjectFile>(Obj->get ());
94
-
87
+ static Expected<std::pair<SymbolFlagsMap, SymbolStringPtr>>
88
+ getMachOObjectFileSymbolInfo (ExecutionSession &ES,
89
+ const object::MachOObjectFile &Obj) {
95
90
SymbolFlagsMap SymbolFlags;
96
- for (auto &Sym : (*Obj)->symbols ()) {
91
+
92
+ for (auto &Sym : Obj.symbols ()) {
97
93
Expected<uint32_t > SymFlagsOrErr = Sym.getFlags ();
98
94
if (!SymFlagsOrErr)
99
95
// TODO: Test this error.
@@ -123,21 +119,19 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
123
119
return SymFlags.takeError ();
124
120
125
121
// Strip the 'exported' flag from MachO linker-private symbols.
126
- if (IsMachO && Name->startswith (" l" ))
122
+ if (Name->startswith (" l" ))
127
123
*SymFlags &= ~JITSymbolFlags::Exported;
128
124
129
125
SymbolFlags[InternedName] = std::move (*SymFlags);
130
126
}
131
127
132
128
SymbolStringPtr InitSymbol;
133
-
134
129
size_t Counter = 0 ;
135
130
auto AddInitSymbol = [&]() {
136
131
while (true ) {
137
132
std::string InitSymString;
138
133
raw_string_ostream (InitSymString)
139
- << " $." << ObjBuffer.getBufferIdentifier () << " .__inits."
140
- << Counter++;
134
+ << " $." << Obj.getFileName () << " .__inits." << Counter++;
141
135
InitSymbol = ES.intern (InitSymString);
142
136
if (SymbolFlags.count (InitSymbol))
143
137
continue ;
@@ -146,26 +140,119 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
146
140
}
147
141
};
148
142
149
- if (IsMachO) {
150
- auto &MachOObj = cast<object::MachOObjectFile>(*Obj->get ());
151
- for (auto &Sec : MachOObj.sections ()) {
152
- auto SecType = MachOObj.getSectionType (Sec);
153
- if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
154
- AddInitSymbol ();
155
- break ;
156
- }
157
- auto SegName =
158
- MachOObj.getSectionFinalSegmentName (Sec.getRawDataRefImpl ());
159
- auto SecName = cantFail (MachOObj.getSectionName (Sec.getRawDataRefImpl ()));
160
- if (MachOPlatform::isInitializerSection (SegName, SecName)) {
161
- AddInitSymbol ();
162
- break ;
163
- }
143
+ for (auto &Sec : Obj.sections ()) {
144
+ auto SecType = Obj.getSectionType (Sec);
145
+ if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
146
+ AddInitSymbol ();
147
+ break ;
148
+ }
149
+ auto SegName = Obj.getSectionFinalSegmentName (Sec.getRawDataRefImpl ());
150
+ auto SecName = cantFail (Obj.getSectionName (Sec.getRawDataRefImpl ()));
151
+ if (MachOPlatform::isInitializerSection (SegName, SecName)) {
152
+ AddInitSymbol ();
153
+ break ;
164
154
}
165
155
}
166
156
167
157
return std::make_pair (std::move (SymbolFlags), std::move (InitSymbol));
168
158
}
169
159
160
+ static Expected<std::pair<SymbolFlagsMap, SymbolStringPtr>>
161
+ getELFObjectFileSymbolInfo (ExecutionSession &ES,
162
+ const object::ELFObjectFileBase &Obj) {
163
+ SymbolFlagsMap SymbolFlags;
164
+ for (auto &Sym : Obj.symbols ()) {
165
+ Expected<uint32_t > SymFlagsOrErr = Sym.getFlags ();
166
+ if (!SymFlagsOrErr)
167
+ // TODO: Test this error.
168
+ return SymFlagsOrErr.takeError ();
169
+
170
+ // Skip symbols not defined in this object file.
171
+ if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
172
+ continue ;
173
+
174
+ // Skip symbols that are not global.
175
+ if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
176
+ continue ;
177
+
178
+ // Skip symbols that have type SF_File.
179
+ if (auto SymType = Sym.getType ()) {
180
+ if (*SymType == object::SymbolRef::ST_File)
181
+ continue ;
182
+ } else
183
+ return SymType.takeError ();
184
+
185
+ auto Name = Sym.getName ();
186
+ if (!Name)
187
+ return Name.takeError ();
188
+ auto InternedName = ES.intern (*Name);
189
+ auto SymFlags = JITSymbolFlags::fromObjectSymbol (Sym);
190
+ if (!SymFlags)
191
+ return SymFlags.takeError ();
192
+
193
+ // ELF STB_GNU_UNIQUE should map to Weak for ORC.
194
+ if (Sym.getBinding () == ELF::STB_GNU_UNIQUE)
195
+ *SymFlags |= JITSymbolFlags::Weak;
196
+
197
+ SymbolFlags[InternedName] = std::move (*SymFlags);
198
+ }
199
+
200
+ return std::make_pair (std::move (SymbolFlags), nullptr );
201
+ }
202
+
203
+ Expected<std::pair<SymbolFlagsMap, SymbolStringPtr>>
204
+ getGenericObjectFileSymbolInfo (ExecutionSession &ES,
205
+ const object::ObjectFile &Obj) {
206
+ SymbolFlagsMap SymbolFlags;
207
+ for (auto &Sym : Obj.symbols ()) {
208
+ Expected<uint32_t > SymFlagsOrErr = Sym.getFlags ();
209
+ if (!SymFlagsOrErr)
210
+ // TODO: Test this error.
211
+ return SymFlagsOrErr.takeError ();
212
+
213
+ // Skip symbols not defined in this object file.
214
+ if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
215
+ continue ;
216
+
217
+ // Skip symbols that are not global.
218
+ if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
219
+ continue ;
220
+
221
+ // Skip symbols that have type SF_File.
222
+ if (auto SymType = Sym.getType ()) {
223
+ if (*SymType == object::SymbolRef::ST_File)
224
+ continue ;
225
+ } else
226
+ return SymType.takeError ();
227
+
228
+ auto Name = Sym.getName ();
229
+ if (!Name)
230
+ return Name.takeError ();
231
+ auto InternedName = ES.intern (*Name);
232
+ auto SymFlags = JITSymbolFlags::fromObjectSymbol (Sym);
233
+ if (!SymFlags)
234
+ return SymFlags.takeError ();
235
+
236
+ SymbolFlags[InternedName] = std::move (*SymFlags);
237
+ }
238
+
239
+ return std::make_pair (std::move (SymbolFlags), nullptr );
240
+ }
241
+
242
+ Expected<std::pair<SymbolFlagsMap, SymbolStringPtr>>
243
+ getObjectSymbolInfo (ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
244
+ auto Obj = object::ObjectFile::createObjectFile (ObjBuffer);
245
+
246
+ if (!Obj)
247
+ return Obj.takeError ();
248
+
249
+ if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get ()))
250
+ return getMachOObjectFileSymbolInfo (ES, *MachOObj);
251
+ else if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj->get ()))
252
+ return getELFObjectFileSymbolInfo (ES, *ELFObj);
253
+
254
+ return getGenericObjectFileSymbolInfo (ES, **Obj);
255
+ }
256
+
170
257
} // End namespace orc.
171
258
} // End namespace llvm.
0 commit comments