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 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);
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 
97  const object::ELFObjectFileBase &Obj) {
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 
151  const object::ObjectFile &Obj) {
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 
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.
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:150
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::MachO::SECTION_TYPE
@ SECTION_TYPE
Definition: MachO.h:112
llvm::ELF::STB_GNU_UNIQUE
@ STB_GNU_UNIQUE
Definition: ELF.h:1182
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:1406
llvm::orc::MaterializationUnit::Interface
Definition: Core.h:673
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:96
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::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1359
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:190
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