LLVM  15.0.0git
ObjectFileInterface.cpp
Go to the documentation of this file.
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 
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 
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 
39  const object::MachOObjectFile &Obj) {
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 SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
67  if (!SymFlags)
68  return SymFlags.takeError();
69 
70  // Strip the 'exported' flag from MachO linker-private symbols.
71  if (Name->startswith("l"))
72  *SymFlags &= ~JITSymbolFlags::Exported;
73 
74  I.SymbolFlags[ES.intern(*Name)] = std::move(*SymFlags);
75  }
76 
77  for (auto &Sec : Obj.sections()) {
78  auto SecType = Obj.getSectionType(Sec);
80  addInitSymbol(I, ES, Obj.getFileName());
81  break;
82  }
83  auto SegName = Obj.getSectionFinalSegmentName(Sec.getRawDataRefImpl());
84  auto SecName = cantFail(Obj.getSectionName(Sec.getRawDataRefImpl()));
85  if (MachOPlatform::isInitializerSection(SegName, SecName)) {
86  addInitSymbol(I, ES, Obj.getFileName());
87  break;
88  }
89  }
90 
91  return I;
92 }
93 
96  const object::ELFObjectFileBase &Obj) {
98 
99  for (auto &Sym : Obj.symbols()) {
100  Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
101  if (!SymFlagsOrErr)
102  // TODO: Test this error.
103  return SymFlagsOrErr.takeError();
104 
105  // Skip symbols not defined in this object file.
106  if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
107  continue;
108 
109  // Skip symbols that are not global.
110  if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
111  continue;
112 
113  // Skip symbols that have type SF_File.
114  if (auto SymType = Sym.getType()) {
115  if (*SymType == object::SymbolRef::ST_File)
116  continue;
117  } else
118  return SymType.takeError();
119 
120  auto Name = Sym.getName();
121  if (!Name)
122  return Name.takeError();
123 
124  auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
125  if (!SymFlags)
126  return SymFlags.takeError();
127 
128  // ELF STB_GNU_UNIQUE should map to Weak for ORC.
129  if (Sym.getBinding() == ELF::STB_GNU_UNIQUE)
130  *SymFlags |= JITSymbolFlags::Weak;
131 
132  I.SymbolFlags[ES.intern(*Name)] = std::move(*SymFlags);
133  }
134 
135  SymbolStringPtr InitSymbol;
136  for (auto &Sec : Obj.sections()) {
137  if (auto SecName = Sec.getName()) {
138  if (ELFNixPlatform::isInitializerSection(*SecName)) {
139  addInitSymbol(I, ES, Obj.getFileName());
140  break;
141  }
142  }
143  }
144 
145  return I;
146 }
147 
150  const object::ObjectFile &Obj) {
152 
153  for (auto &Sym : Obj.symbols()) {
154  Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
155  if (!SymFlagsOrErr)
156  // TODO: Test this error.
157  return SymFlagsOrErr.takeError();
158 
159  // Skip symbols not defined in this object file.
160  if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
161  continue;
162 
163  // Skip symbols that are not global.
164  if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
165  continue;
166 
167  // Skip symbols that have type SF_File.
168  if (auto SymType = Sym.getType()) {
169  if (*SymType == object::SymbolRef::ST_File)
170  continue;
171  } else
172  return SymType.takeError();
173 
174  auto Name = Sym.getName();
175  if (!Name)
176  return Name.takeError();
177 
178  auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
179  if (!SymFlags)
180  return SymFlags.takeError();
181 
182  I.SymbolFlags[ES.intern(*Name)] = std::move(*SymFlags);
183  }
184 
185  return I;
186 }
187 
190  auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
191 
192  if (!Obj)
193  return Obj.takeError();
194 
195  if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get()))
196  return getMachOObjectFileSymbolInfo(ES, *MachOObj);
197  else if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj->get()))
198  return getELFObjectFileSymbolInfo(ES, *ELFObj);
199 
200  return getGenericObjectFileSymbolInfo(ES, **Obj);
201 }
202 
203 } // End namespace orc.
204 } // End namespace llvm.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
ELFNixPlatform.h
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
llvm::orc::getGenericObjectFileSymbolInfo
Expected< MaterializationUnit::Interface > getGenericObjectFileSymbolInfo(ExecutionSession &ES, const object::ObjectFile &Obj)
Definition: ObjectFileInterface.cpp:149
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::object::SymbolRef::ST_File
@ ST_File
Definition: ObjectFile.h:176
llvm::JITSymbolFlags
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::ObjectFile::symbols
symbol_iterator_range symbols() const
Definition: ObjectFile.h:319
MachO.h
llvm::object::ELFObjectFileBase
Definition: ELFObjectFile.h:51
llvm::JITSymbolFlags::MaterializationSideEffectsOnly
@ MaterializationSideEffectsOnly
Definition: JITSymbol.h:87
llvm::orc::ExecutionSession::intern
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1413
llvm::orc::MaterializationUnit::Interface
Definition: Core.h:676
llvm::orc::MachOPlatform::isInitializerSection
static bool isInitializerSection(StringRef SegName, StringRef SectName)
Returns true if the given section name is an initializer section.
Definition: MachOPlatform.cpp:311
llvm::object::MachOObjectFile
Definition: MachO.h:381
llvm::object::MachOObjectFile::getSectionType
unsigned getSectionType(SectionRef Sec) const
Definition: MachOObjectFile.cpp:1777
llvm::orc::ELFNixPlatform::isInitializerSection
static bool isInitializerSection(StringRef SecName)
Returns true if the given section name is an initializer section.
Definition: ELFNixPlatform.cpp:266
llvm::MachO::S_MOD_INIT_FUNC_POINTERS
@ S_MOD_INIT_FUNC_POINTERS
S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for initialization.
Definition: MachO.h:145
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::getELFObjectFileSymbolInfo
static Expected< MaterializationUnit::Interface > getELFObjectFileSymbolInfo(ExecutionSession &ES, const object::ELFObjectFileBase &Obj)
Definition: ObjectFileInterface.cpp:95
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:110
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:187
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
llvm::JITSymbolFlags::Weak
@ Weak
Definition: JITSymbol.h:82
llvm::object::ELFObjectFileBase::symbols
elf_symbol_iterator_range symbols() const
Definition: ELFObjectFile.h:236
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
MachOPlatform.h
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
ELFObjectFile.h
llvm::ELF::STB_GNU_UNIQUE
@ STB_GNU_UNIQUE
Definition: ELF.h:1191
llvm::MachO::SECTION_TYPE
@ SECTION_TYPE
Definition: MachO.h:112
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1362
llvm::orc::getMachOObjectFileSymbolInfo
static Expected< MaterializationUnit::Interface > getMachOObjectFileSymbolInfo(ExecutionSession &ES, const object::MachOObjectFile &Obj)
Definition: ObjectFileInterface.cpp:38
llvm::sampleprof::SecType
SecType
Definition: SampleProf.h:120
llvm::orc::addInitSymbol
void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES, StringRef ObjFileName)
Adds an initializer symbol to the given MU interface.
Definition: ObjectFileInterface.cpp:22
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:40
llvm::object::MachOObjectFile::getSectionFinalSegmentName
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const
Definition: MachOObjectFile.cpp:4327
llvm::orc::getObjectFileInterface
Expected< MaterializationUnit::Interface > getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer)
Returns a MaterializationUnit::Interface for the object file contained in the given buffer,...
Definition: ObjectFileInterface.cpp:189
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:111
llvm::JITSymbolFlags::fromObjectSymbol
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:69
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition: ObjectFile.h:327
llvm::object::MachOObjectFile::getSectionName
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
Definition: MachOObjectFile.cpp:1920
Debug.h
ObjectFileInterface.h