xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/ObjectFileInterface.cpp (revision 1165fc9a526630487a1feb63daef65c5aee1a583)
1 //===------ ObjectFileInterface.cpp - MU interface utils for objects ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
10 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
11 #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
12 #include "llvm/Object/ELFObjectFile.h"
13 #include "llvm/Object/MachO.h"
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/Support/Debug.h"
16 
17 #define DEBUG_TYPE "orc"
18 
19 namespace llvm {
20 namespace orc {
21 
22 void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES,
23                    StringRef ObjFileName) {
24   assert(!I.InitSymbol && "I already has an init symbol");
25   size_t Counter = 0;
26 
27   do {
28     std::string InitSymString;
29     raw_string_ostream(InitSymString)
30         << "$." << ObjFileName << ".__inits." << Counter++;
31     I.InitSymbol = ES.intern(InitSymString);
32   } while (I.SymbolFlags.count(I.InitSymbol));
33 
34   I.SymbolFlags[I.InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
35 }
36 
37 static Expected<MaterializationUnit::Interface>
38 getMachOObjectFileSymbolInfo(ExecutionSession &ES,
39                              const object::MachOObjectFile &Obj) {
40   MaterializationUnit::Interface I;
41 
42   for (auto &Sym : Obj.symbols()) {
43     Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
44     if (!SymFlagsOrErr)
45       // TODO: Test this error.
46       return SymFlagsOrErr.takeError();
47 
48     // Skip symbols not defined in this object file.
49     if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
50       continue;
51 
52     // Skip symbols that are not global.
53     if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
54       continue;
55 
56     // Skip symbols that have type SF_File.
57     if (auto SymType = Sym.getType()) {
58       if (*SymType == object::SymbolRef::ST_File)
59         continue;
60     } else
61       return SymType.takeError();
62 
63     auto Name = Sym.getName();
64     if (!Name)
65       return Name.takeError();
66     auto InternedName = ES.intern(*Name);
67     auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
68     if (!SymFlags)
69       return SymFlags.takeError();
70 
71     // Strip the 'exported' flag from MachO linker-private symbols.
72     if (Name->startswith("l"))
73       *SymFlags &= ~JITSymbolFlags::Exported;
74 
75     I.SymbolFlags[InternedName] = std::move(*SymFlags);
76   }
77 
78   for (auto &Sec : Obj.sections()) {
79     auto SecType = Obj.getSectionType(Sec);
80     if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
81       addInitSymbol(I, ES, Obj.getFileName());
82       break;
83     }
84     auto SegName = Obj.getSectionFinalSegmentName(Sec.getRawDataRefImpl());
85     auto SecName = cantFail(Obj.getSectionName(Sec.getRawDataRefImpl()));
86     if (MachOPlatform::isInitializerSection(SegName, SecName)) {
87       addInitSymbol(I, ES, Obj.getFileName());
88       break;
89     }
90   }
91 
92   return I;
93 }
94 
95 static Expected<MaterializationUnit::Interface>
96 getELFObjectFileSymbolInfo(ExecutionSession &ES,
97                            const object::ELFObjectFileBase &Obj) {
98   MaterializationUnit::Interface I;
99 
100   for (auto &Sym : Obj.symbols()) {
101     Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
102     if (!SymFlagsOrErr)
103       // TODO: Test this error.
104       return SymFlagsOrErr.takeError();
105 
106     // Skip symbols not defined in this object file.
107     if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
108       continue;
109 
110     // Skip symbols that are not global.
111     if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
112       continue;
113 
114     // Skip symbols that have type SF_File.
115     if (auto SymType = Sym.getType()) {
116       if (*SymType == object::SymbolRef::ST_File)
117         continue;
118     } else
119       return SymType.takeError();
120 
121     auto Name = Sym.getName();
122     if (!Name)
123       return Name.takeError();
124     auto InternedName = ES.intern(*Name);
125     auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
126     if (!SymFlags)
127       return SymFlags.takeError();
128 
129     // ELF STB_GNU_UNIQUE should map to Weak for ORC.
130     if (Sym.getBinding() == ELF::STB_GNU_UNIQUE)
131       *SymFlags |= JITSymbolFlags::Weak;
132 
133     I.SymbolFlags[InternedName] = std::move(*SymFlags);
134   }
135 
136   SymbolStringPtr InitSymbol;
137   for (auto &Sec : Obj.sections()) {
138     if (auto SecName = Sec.getName()) {
139       if (ELFNixPlatform::isInitializerSection(*SecName)) {
140         addInitSymbol(I, ES, Obj.getFileName());
141         break;
142       }
143     }
144   }
145 
146   return I;
147 }
148 
149 Expected<MaterializationUnit::Interface>
150 getGenericObjectFileSymbolInfo(ExecutionSession &ES,
151                                const object::ObjectFile &Obj) {
152   MaterializationUnit::Interface I;
153 
154   for (auto &Sym : Obj.symbols()) {
155     Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
156     if (!SymFlagsOrErr)
157       // TODO: Test this error.
158       return SymFlagsOrErr.takeError();
159 
160     // Skip symbols not defined in this object file.
161     if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
162       continue;
163 
164     // Skip symbols that are not global.
165     if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
166       continue;
167 
168     // Skip symbols that have type SF_File.
169     if (auto SymType = Sym.getType()) {
170       if (*SymType == object::SymbolRef::ST_File)
171         continue;
172     } else
173       return SymType.takeError();
174 
175     auto Name = Sym.getName();
176     if (!Name)
177       return Name.takeError();
178     auto InternedName = ES.intern(*Name);
179     auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
180     if (!SymFlags)
181       return SymFlags.takeError();
182 
183     I.SymbolFlags[InternedName] = std::move(*SymFlags);
184   }
185 
186   return I;
187 }
188 
189 Expected<MaterializationUnit::Interface>
190 getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
191   auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
192 
193   if (!Obj)
194     return Obj.takeError();
195 
196   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get()))
197     return getMachOObjectFileSymbolInfo(ES, *MachOObj);
198   else if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj->get()))
199     return getELFObjectFileSymbolInfo(ES, *ELFObj);
200 
201   return getGenericObjectFileSymbolInfo(ES, **Obj);
202 }
203 
204 } // End namespace orc.
205 } // End namespace llvm.
206