Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/MC/MCContext.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
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/MC/MCContext.h"
10
#include "llvm/ADT/SmallString.h"
11
#include "llvm/ADT/SmallVector.h"
12
#include "llvm/ADT/StringMap.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/ADT/Twine.h"
15
#include "llvm/BinaryFormat/COFF.h"
16
#include "llvm/BinaryFormat/ELF.h"
17
#include "llvm/BinaryFormat/Wasm.h"
18
#include "llvm/BinaryFormat/XCOFF.h"
19
#include "llvm/MC/MCAsmInfo.h"
20
#include "llvm/MC/MCCodeView.h"
21
#include "llvm/MC/MCDwarf.h"
22
#include "llvm/MC/MCExpr.h"
23
#include "llvm/MC/MCFragment.h"
24
#include "llvm/MC/MCInst.h"
25
#include "llvm/MC/MCLabel.h"
26
#include "llvm/MC/MCSectionCOFF.h"
27
#include "llvm/MC/MCSectionDXContainer.h"
28
#include "llvm/MC/MCSectionELF.h"
29
#include "llvm/MC/MCSectionGOFF.h"
30
#include "llvm/MC/MCSectionMachO.h"
31
#include "llvm/MC/MCSectionSPIRV.h"
32
#include "llvm/MC/MCSectionWasm.h"
33
#include "llvm/MC/MCSectionXCOFF.h"
34
#include "llvm/MC/MCStreamer.h"
35
#include "llvm/MC/MCSubtargetInfo.h"
36
#include "llvm/MC/MCSymbol.h"
37
#include "llvm/MC/MCSymbolCOFF.h"
38
#include "llvm/MC/MCSymbolELF.h"
39
#include "llvm/MC/MCSymbolGOFF.h"
40
#include "llvm/MC/MCSymbolMachO.h"
41
#include "llvm/MC/MCSymbolWasm.h"
42
#include "llvm/MC/MCSymbolXCOFF.h"
43
#include "llvm/MC/MCTargetOptions.h"
44
#include "llvm/MC/SectionKind.h"
45
#include "llvm/Support/Casting.h"
46
#include "llvm/Support/CommandLine.h"
47
#include "llvm/Support/ErrorHandling.h"
48
#include "llvm/Support/MemoryBuffer.h"
49
#include "llvm/Support/Path.h"
50
#include "llvm/Support/SMLoc.h"
51
#include "llvm/Support/SourceMgr.h"
52
#include "llvm/Support/raw_ostream.h"
53
#include <cassert>
54
#include <cstdlib>
55
#include <optional>
56
#include <tuple>
57
#include <utility>
58
59
using namespace llvm;
60
61
static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &,
62
0
                               std::vector<const MDNode *> &) {
63
0
  SMD.print(nullptr, errs());
64
0
}
65
66
MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
67
                     const MCRegisterInfo *mri, const MCSubtargetInfo *msti,
68
                     const SourceMgr *mgr, MCTargetOptions const *TargetOpts,
69
                     bool DoAutoReset, StringRef Swift5ReflSegmentName)
70
    : Swift5ReflectionSegmentName(Swift5ReflSegmentName), TT(TheTriple),
71
      SrcMgr(mgr), InlineSrcMgr(nullptr), DiagHandler(defaultDiagHandler),
72
      MAI(mai), MRI(mri), MSTI(msti), Symbols(Allocator), UsedNames(Allocator),
73
      InlineAsmUsedLabelNames(Allocator),
74
      CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
75
34.4k
      AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
76
34.4k
  SecureLogFile = TargetOptions ? TargetOptions->AsSecureLogFile : "";
77
78
34.4k
  if (SrcMgr && SrcMgr->getNumBuffers())
79
0
    MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())
80
0
                                   ->getBufferIdentifier());
81
82
34.4k
  switch (TheTriple.getObjectFormat()) {
83
0
  case Triple::MachO:
84
0
    Env = IsMachO;
85
0
    break;
86
0
  case Triple::COFF:
87
0
    if (!TheTriple.isOSWindows() && !TheTriple.isUEFI())
88
0
      report_fatal_error(
89
0
          "Cannot initialize MC for non-Windows COFF object files.");
90
91
0
    Env = IsCOFF;
92
0
    break;
93
34.3k
  case Triple::ELF:
94
34.3k
    Env = IsELF;
95
34.3k
    break;
96
101
  case Triple::Wasm:
97
101
    Env = IsWasm;
98
101
    break;
99
0
  case Triple::XCOFF:
100
0
    Env = IsXCOFF;
101
0
    break;
102
0
  case Triple::GOFF:
103
0
    Env = IsGOFF;
104
0
    break;
105
0
  case Triple::DXContainer:
106
0
    Env = IsDXContainer;
107
0
    break;
108
0
  case Triple::SPIRV:
109
0
    Env = IsSPIRV;
110
0
    break;
111
0
  case Triple::UnknownObjectFormat:
112
0
    report_fatal_error("Cannot initialize MC for unknown object file format.");
113
0
    break;
114
34.4k
  }
115
34.4k
}
116
117
34.4k
MCContext::~MCContext() {
118
34.4k
  if (AutoReset)
119
0
    reset();
120
121
  // NOTE: The symbols are all allocated out of a bump pointer allocator,
122
  // we don't need to free them here.
123
34.4k
}
124
125
13
void MCContext::initInlineSourceManager() {
126
13
  if (!InlineSrcMgr)
127
13
    InlineSrcMgr.reset(new SourceMgr());
128
13
}
129
130
//===----------------------------------------------------------------------===//
131
// Module Lifetime Management
132
//===----------------------------------------------------------------------===//
133
134
68.8k
void MCContext::reset() {
135
68.8k
  SrcMgr = nullptr;
136
68.8k
  InlineSrcMgr.reset();
137
68.8k
  LocInfos.clear();
138
68.8k
  DiagHandler = defaultDiagHandler;
139
140
  // Call the destructors so the fragments are freed
141
68.8k
  COFFAllocator.DestroyAll();
142
68.8k
  DXCAllocator.DestroyAll();
143
68.8k
  ELFAllocator.DestroyAll();
144
68.8k
  GOFFAllocator.DestroyAll();
145
68.8k
  MachOAllocator.DestroyAll();
146
68.8k
  WasmAllocator.DestroyAll();
147
68.8k
  XCOFFAllocator.DestroyAll();
148
68.8k
  MCInstAllocator.DestroyAll();
149
68.8k
  SPIRVAllocator.DestroyAll();
150
151
68.8k
  MCSubtargetAllocator.DestroyAll();
152
68.8k
  InlineAsmUsedLabelNames.clear();
153
68.8k
  UsedNames.clear();
154
68.8k
  Symbols.clear();
155
68.8k
  Allocator.Reset();
156
68.8k
  Instances.clear();
157
68.8k
  CompilationDir.clear();
158
68.8k
  MainFileName.clear();
159
68.8k
  MCDwarfLineTablesCUMap.clear();
160
68.8k
  SectionsForRanges.clear();
161
68.8k
  MCGenDwarfLabelEntries.clear();
162
68.8k
  DwarfDebugFlags = StringRef();
163
68.8k
  DwarfCompileUnitID = 0;
164
68.8k
  CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
165
166
68.8k
  CVContext.reset();
167
168
68.8k
  MachOUniquingMap.clear();
169
68.8k
  ELFUniquingMap.clear();
170
68.8k
  GOFFUniquingMap.clear();
171
68.8k
  COFFUniquingMap.clear();
172
68.8k
  WasmUniquingMap.clear();
173
68.8k
  XCOFFUniquingMap.clear();
174
68.8k
  DXCUniquingMap.clear();
175
176
68.8k
  ELFEntrySizeMap.clear();
177
68.8k
  ELFSeenGenericMergeableSections.clear();
178
179
68.8k
  NextID.clear();
180
68.8k
  AllowTemporaryLabels = true;
181
68.8k
  DwarfLocSeen = false;
182
68.8k
  GenDwarfForAssembly = false;
183
68.8k
  GenDwarfFileNumber = 0;
184
185
68.8k
  HadError = false;
186
68.8k
}
187
188
//===----------------------------------------------------------------------===//
189
// MCInst Management
190
//===----------------------------------------------------------------------===//
191
192
671k
MCInst *MCContext::createMCInst() {
193
671k
  return new (MCInstAllocator.Allocate()) MCInst;
194
671k
}
195
196
//===----------------------------------------------------------------------===//
197
// Symbol Manipulation
198
//===----------------------------------------------------------------------===//
199
200
1.71M
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
201
1.71M
  SmallString<128> NameSV;
202
1.71M
  StringRef NameRef = Name.toStringRef(NameSV);
203
204
1.71M
  assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
205
206
0
  MCSymbol *&Sym = Symbols[NameRef];
207
1.71M
  if (!Sym)
208
605k
    Sym = createSymbol(NameRef, false, false);
209
210
1.71M
  return Sym;
211
1.71M
}
212
213
MCSymbol *MCContext::getOrCreateFrameAllocSymbol(const Twine &FuncName,
214
0
                                                 unsigned Idx) {
215
0
  return getOrCreateSymbol(MAI->getPrivateGlobalPrefix() + FuncName +
216
0
                           "$frame_escape_" + Twine(Idx));
217
0
}
218
219
0
MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(const Twine &FuncName) {
220
0
  return getOrCreateSymbol(MAI->getPrivateGlobalPrefix() + FuncName +
221
0
                           "$parent_frame_offset");
222
0
}
223
224
0
MCSymbol *MCContext::getOrCreateLSDASymbol(const Twine &FuncName) {
225
0
  return getOrCreateSymbol(MAI->getPrivateGlobalPrefix() + "__ehtable$" +
226
0
                           FuncName);
227
0
}
228
229
MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
230
820k
                                      bool IsTemporary) {
231
820k
  static_assert(std::is_trivially_destructible<MCSymbolCOFF>(),
232
820k
                "MCSymbol classes must be trivially destructible");
233
820k
  static_assert(std::is_trivially_destructible<MCSymbolELF>(),
234
820k
                "MCSymbol classes must be trivially destructible");
235
820k
  static_assert(std::is_trivially_destructible<MCSymbolMachO>(),
236
820k
                "MCSymbol classes must be trivially destructible");
237
820k
  static_assert(std::is_trivially_destructible<MCSymbolWasm>(),
238
820k
                "MCSymbol classes must be trivially destructible");
239
820k
  static_assert(std::is_trivially_destructible<MCSymbolXCOFF>(),
240
820k
                "MCSymbol classes must be trivially destructible");
241
242
820k
  switch (getObjectFileType()) {
243
0
  case MCContext::IsCOFF:
244
0
    return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
245
798k
  case MCContext::IsELF:
246
798k
    return new (Name, *this) MCSymbolELF(Name, IsTemporary);
247
0
  case MCContext::IsGOFF:
248
0
    return new (Name, *this) MCSymbolGOFF(Name, IsTemporary);
249
0
  case MCContext::IsMachO:
250
0
    return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
251
22.4k
  case MCContext::IsWasm:
252
22.4k
    return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
253
0
  case MCContext::IsXCOFF:
254
0
    return createXCOFFSymbolImpl(Name, IsTemporary);
255
0
  case MCContext::IsDXContainer:
256
0
    break;
257
0
  case MCContext::IsSPIRV:
258
0
    return new (Name, *this)
259
0
        MCSymbol(MCSymbol::SymbolKindUnset, Name, IsTemporary);
260
820k
  }
261
0
  return new (Name, *this)
262
0
      MCSymbol(MCSymbol::SymbolKindUnset, Name, IsTemporary);
263
820k
}
264
265
MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
266
820k
                                  bool CanBeUnnamed) {
267
820k
  if (CanBeUnnamed && !UseNamesOnTempLabels)
268
203k
    return createSymbolImpl(nullptr, true);
269
270
  // Determine whether this is a user written assembler temporary or normal
271
  // label, if used.
272
617k
  bool IsTemporary = CanBeUnnamed;
273
617k
  if (AllowTemporaryLabels && !IsTemporary)
274
617k
    IsTemporary = Name.starts_with(MAI->getPrivateGlobalPrefix());
275
276
617k
  SmallString<128> NewName = Name;
277
617k
  bool AddSuffix = AlwaysAddSuffix;
278
617k
  unsigned &NextUniqueID = NextID[Name];
279
617k
  while (true) {
280
617k
    if (AddSuffix) {
281
12.0k
      NewName.resize(Name.size());
282
12.0k
      raw_svector_ostream(NewName) << NextUniqueID++;
283
12.0k
    }
284
617k
    auto NameEntry = UsedNames.insert(std::make_pair(NewName.str(), true));
285
617k
    if (NameEntry.second || !NameEntry.first->second) {
286
      // Ok, we found a name.
287
      // Mark it as used for a non-section symbol.
288
617k
      NameEntry.first->second = true;
289
      // Have the MCSymbol object itself refer to the copy of the string that is
290
      // embedded in the UsedNames entry.
291
617k
      return createSymbolImpl(&*NameEntry.first, IsTemporary);
292
617k
    }
293
0
    assert(IsTemporary && "Cannot rename non-temporary symbols");
294
0
    AddSuffix = true;
295
0
  }
296
0
  llvm_unreachable("Infinite loop");
297
0
}
298
299
203k
MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix) {
300
203k
  SmallString<128> NameSV;
301
203k
  raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
302
203k
  return createSymbol(NameSV, AlwaysAddSuffix, true);
303
203k
}
304
305
1.34k
MCSymbol *MCContext::createNamedTempSymbol(const Twine &Name) {
306
1.34k
  SmallString<128> NameSV;
307
1.34k
  raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
308
1.34k
  return createSymbol(NameSV, true, false);
309
1.34k
}
310
311
0
MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
312
0
  return createLinkerPrivateSymbol("tmp");
313
0
}
314
315
0
MCSymbol *MCContext::createLinkerPrivateSymbol(const Twine &Name) {
316
0
  SmallString<128> NameSV;
317
0
  raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << Name;
318
0
  return createSymbol(NameSV, true, false);
319
0
}
320
321
14.7k
MCSymbol *MCContext::createTempSymbol() { return createTempSymbol("tmp"); }
322
323
1.34k
MCSymbol *MCContext::createNamedTempSymbol() {
324
1.34k
  return createNamedTempSymbol("tmp");
325
1.34k
}
326
327
0
unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
328
0
  MCLabel *&Label = Instances[LocalLabelVal];
329
0
  if (!Label)
330
0
    Label = new (*this) MCLabel(0);
331
0
  return Label->incInstance();
332
0
}
333
334
0
unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
335
0
  MCLabel *&Label = Instances[LocalLabelVal];
336
0
  if (!Label)
337
0
    Label = new (*this) MCLabel(0);
338
0
  return Label->getInstance();
339
0
}
340
341
MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
342
0
                                                       unsigned Instance) {
343
0
  MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
344
0
  if (!Sym)
345
0
    Sym = createNamedTempSymbol();
346
0
  return Sym;
347
0
}
348
349
0
MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
350
0
  unsigned Instance = NextInstance(LocalLabelVal);
351
0
  return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
352
0
}
353
354
MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
355
0
                                               bool Before) {
356
0
  unsigned Instance = GetInstance(LocalLabelVal);
357
0
  if (!Before)
358
0
    ++Instance;
359
0
  return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
360
0
}
361
362
13
MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
363
13
  SmallString<128> NameSV;
364
13
  StringRef NameRef = Name.toStringRef(NameSV);
365
13
  return Symbols.lookup(NameRef);
366
13
}
367
368
void MCContext::setSymbolValue(MCStreamer &Streamer, const Twine &Sym,
369
0
                               uint64_t Val) {
370
0
  auto Symbol = getOrCreateSymbol(Sym);
371
0
  Streamer.emitAssignment(Symbol, MCConstantExpr::create(Val, *this));
372
0
}
373
374
0
void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
375
0
  InlineAsmUsedLabelNames[Sym->getName()] = Sym;
376
0
}
377
378
MCSymbolXCOFF *
379
MCContext::createXCOFFSymbolImpl(const StringMapEntry<bool> *Name,
380
0
                                 bool IsTemporary) {
381
0
  if (!Name)
382
0
    return new (nullptr, *this) MCSymbolXCOFF(nullptr, IsTemporary);
383
384
0
  StringRef OriginalName = Name->first();
385
0
  if (OriginalName.starts_with("._Renamed..") ||
386
0
      OriginalName.starts_with("_Renamed.."))
387
0
    reportError(SMLoc(), "invalid symbol name from source");
388
389
0
  if (MAI->isValidUnquotedName(OriginalName))
390
0
    return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
391
392
  // Now we have a name that contains invalid character(s) for XCOFF symbol.
393
  // Let's replace with something valid, but save the original name so that
394
  // we could still use the original name in the symbol table.
395
0
  SmallString<128> InvalidName(OriginalName);
396
397
  // If it's an entry point symbol, we will keep the '.'
398
  // in front for the convention purpose. Otherwise, add "_Renamed.."
399
  // as prefix to signal this is an renamed symbol.
400
0
  const bool IsEntryPoint = !InvalidName.empty() && InvalidName[0] == '.';
401
0
  SmallString<128> ValidName =
402
0
      StringRef(IsEntryPoint ? "._Renamed.." : "_Renamed..");
403
404
  // Append the hex values of '_' and invalid characters with "_Renamed..";
405
  // at the same time replace invalid characters with '_'.
406
0
  for (size_t I = 0; I < InvalidName.size(); ++I) {
407
0
    if (!MAI->isAcceptableChar(InvalidName[I]) || InvalidName[I] == '_') {
408
0
      raw_svector_ostream(ValidName).write_hex(InvalidName[I]);
409
0
      InvalidName[I] = '_';
410
0
    }
411
0
  }
412
413
  // Skip entry point symbol's '.' as we already have a '.' in front of
414
  // "_Renamed".
415
0
  if (IsEntryPoint)
416
0
    ValidName.append(InvalidName.substr(1, InvalidName.size() - 1));
417
0
  else
418
0
    ValidName.append(InvalidName);
419
420
0
  auto NameEntry = UsedNames.insert(std::make_pair(ValidName.str(), true));
421
0
  assert((NameEntry.second || !NameEntry.first->second) &&
422
0
         "This name is used somewhere else.");
423
  // Mark the name as used for a non-section symbol.
424
0
  NameEntry.first->second = true;
425
  // Have the MCSymbol object itself refer to the copy of the string
426
  // that is embedded in the UsedNames entry.
427
0
  MCSymbolXCOFF *XSym = new (&*NameEntry.first, *this)
428
0
      MCSymbolXCOFF(&*NameEntry.first, IsTemporary);
429
0
  XSym->setSymbolTableName(MCSymbolXCOFF::getUnqualifiedName(OriginalName));
430
0
  return XSym;
431
0
}
432
433
//===----------------------------------------------------------------------===//
434
// Section Management
435
//===----------------------------------------------------------------------===//
436
437
MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
438
                                           unsigned TypeAndAttributes,
439
                                           unsigned Reserved2, SectionKind Kind,
440
0
                                           const char *BeginSymName) {
441
  // We unique sections by their segment/section pair.  The returned section
442
  // may not have the same flags as the requested section, if so this should be
443
  // diagnosed by the client as an error.
444
445
  // Form the name to look up.
446
0
  assert(Section.size() <= 16 && "section name is too long");
447
0
  assert(!memchr(Section.data(), '\0', Section.size()) &&
448
0
         "section name cannot contain NUL");
449
450
  // Do the lookup, if we have a hit, return it.
451
0
  auto R = MachOUniquingMap.try_emplace((Segment + Twine(',') + Section).str());
452
0
  if (!R.second)
453
0
    return R.first->second;
454
455
0
  MCSymbol *Begin = nullptr;
456
0
  if (BeginSymName)
457
0
    Begin = createTempSymbol(BeginSymName, false);
458
459
  // Otherwise, return a new section.
460
0
  StringRef Name = R.first->first();
461
0
  R.first->second = new (MachOAllocator.Allocate())
462
0
      MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()),
463
0
                     TypeAndAttributes, Reserved2, Kind, Begin);
464
0
  return R.first->second;
465
0
}
466
467
MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
468
                                              unsigned Flags, SectionKind K,
469
                                              unsigned EntrySize,
470
                                              const MCSymbolELF *Group,
471
                                              bool Comdat, unsigned UniqueID,
472
2.14M
                                              const MCSymbolELF *LinkedToSym) {
473
2.14M
  MCSymbolELF *R;
474
2.14M
  MCSymbol *&Sym = Symbols[Section];
475
  // A section symbol can not redefine regular symbols. There may be multiple
476
  // sections with the same name, in which case the first such section wins.
477
2.14M
  if (Sym && Sym->isDefined() &&
478
2.14M
      (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
479
0
    reportError(SMLoc(), "invalid symbol redefinition");
480
2.14M
  if (Sym && Sym->isUndefined()) {
481
0
    R = cast<MCSymbolELF>(Sym);
482
2.14M
  } else {
483
2.14M
    auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
484
2.14M
    R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
485
2.14M
    if (!Sym)
486
2.14M
      Sym = R;
487
2.14M
  }
488
2.14M
  R->setBinding(ELF::STB_LOCAL);
489
2.14M
  R->setType(ELF::STT_SECTION);
490
491
2.14M
  auto *Ret = new (ELFAllocator.Allocate())
492
2.14M
      MCSectionELF(Section, Type, Flags, K, EntrySize, Group, Comdat, UniqueID,
493
2.14M
                   R, LinkedToSym);
494
495
2.14M
  auto *F = new MCDataFragment();
496
2.14M
  Ret->getFragmentList().insert(Ret->begin(), F);
497
2.14M
  F->setParent(Ret);
498
2.14M
  R->setFragment(F);
499
500
2.14M
  return Ret;
501
2.14M
}
502
503
MCSectionELF *
504
MCContext::createELFRelSection(const Twine &Name, unsigned Type, unsigned Flags,
505
                               unsigned EntrySize, const MCSymbolELF *Group,
506
0
                               const MCSectionELF *RelInfoSection) {
507
0
  StringMap<bool>::iterator I;
508
0
  bool Inserted;
509
0
  std::tie(I, Inserted) = RelSecNames.insert(std::make_pair(Name.str(), true));
510
511
0
  return createELFSectionImpl(
512
0
      I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
513
0
      true, true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
514
0
}
515
516
MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
517
                                            const Twine &Suffix, unsigned Type,
518
                                            unsigned Flags,
519
89
                                            unsigned EntrySize) {
520
89
  return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix,
521
89
                       /*IsComdat=*/true);
522
89
}
523
524
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
525
                                       unsigned Flags, unsigned EntrySize,
526
                                       const Twine &Group, bool IsComdat,
527
                                       unsigned UniqueID,
528
2.42M
                                       const MCSymbolELF *LinkedToSym) {
529
2.42M
  MCSymbolELF *GroupSym = nullptr;
530
2.42M
  if (!Group.isTriviallyEmpty() && !Group.str().empty())
531
125
    GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
532
533
2.42M
  return getELFSection(Section, Type, Flags, EntrySize, GroupSym, IsComdat,
534
2.42M
                       UniqueID, LinkedToSym);
535
2.42M
}
536
537
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
538
                                       unsigned Flags, unsigned EntrySize,
539
                                       const MCSymbolELF *GroupSym,
540
                                       bool IsComdat, unsigned UniqueID,
541
2.42M
                                       const MCSymbolELF *LinkedToSym) {
542
2.42M
  StringRef Group = "";
543
2.42M
  if (GroupSym)
544
125
    Group = GroupSym->getName();
545
2.42M
  assert(!(LinkedToSym && LinkedToSym->getName().empty()));
546
  // Do the lookup, if we have a hit, return it.
547
0
  auto IterBool = ELFUniquingMap.insert(std::make_pair(
548
2.42M
      ELFSectionKey{Section.str(), Group,
549
2.42M
                    LinkedToSym ? LinkedToSym->getName() : "", UniqueID},
550
2.42M
      nullptr));
551
2.42M
  auto &Entry = *IterBool.first;
552
2.42M
  if (!IterBool.second)
553
280k
    return Entry.second;
554
555
2.14M
  StringRef CachedName = Entry.first.SectionName;
556
557
2.14M
  SectionKind Kind;
558
2.14M
  if (Flags & ELF::SHF_ARM_PURECODE)
559
0
    Kind = SectionKind::getExecuteOnly();
560
2.14M
  else if (Flags & ELF::SHF_EXECINSTR)
561
34.3k
    Kind = SectionKind::getText();
562
2.10M
  else if (~Flags & ELF::SHF_WRITE)
563
1.78M
    Kind = SectionKind::getReadOnly();
564
325k
  else if (Flags & ELF::SHF_TLS)
565
68.6k
    Kind = (Type & ELF::SHT_NOBITS) ? SectionKind::getThreadBSS()
566
68.6k
                                    : SectionKind::getThreadData();
567
256k
  else
568
    // Default to `SectionKind::getText()`. This is the default for gas as
569
    // well. The condition that falls into this case is where we do not have any
570
    // section flags and must infer a classification rather than where we have
571
    // section flags (i.e. this is not that SHF_EXECINSTR is unset bur rather it
572
    // is unknown).
573
256k
    Kind = llvm::StringSwitch<SectionKind>(CachedName)
574
256k
               .Case(".bss", SectionKind::getBSS())
575
256k
               .StartsWith(".bss.", SectionKind::getBSS())
576
256k
               .StartsWith(".gnu.linkonce.b.", SectionKind::getBSS())
577
256k
               .StartsWith(".llvm.linkonce.b.", SectionKind::getBSS())
578
256k
               .Case(".data", SectionKind::getData())
579
256k
               .Case(".data1", SectionKind::getData())
580
256k
               .Case(".data.rel.ro", SectionKind::getReadOnlyWithRel())
581
256k
               .StartsWith(".data.", SectionKind::getData())
582
256k
               .Case(".rodata", SectionKind::getReadOnly())
583
256k
               .Case(".rodata1", SectionKind::getReadOnly())
584
256k
               .StartsWith(".rodata.", SectionKind::getReadOnly())
585
256k
               .Case(".tbss", SectionKind::getThreadBSS())
586
256k
               .StartsWith(".tbss.", SectionKind::getThreadData())
587
256k
               .StartsWith(".gnu.linkonce.tb.", SectionKind::getThreadData())
588
256k
               .StartsWith(".llvm.linkonce.tb.", SectionKind::getThreadData())
589
256k
               .Case(".tdata", SectionKind::getThreadData())
590
256k
               .StartsWith(".tdata.", SectionKind::getThreadData())
591
256k
               .StartsWith(".gnu.linkonce.td.", SectionKind::getThreadData())
592
256k
               .StartsWith(".llvm.linkonce.td.", SectionKind::getThreadData())
593
256k
               .StartsWith(".debug_", SectionKind::getMetadata())
594
256k
               .Default(SectionKind::getReadOnly());
595
596
2.14M
  MCSectionELF *Result =
597
2.14M
      createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,
598
2.14M
                           IsComdat, UniqueID, LinkedToSym);
599
2.14M
  Entry.second = Result;
600
601
2.14M
  recordELFMergeableSectionInfo(Result->getName(), Result->getFlags(),
602
2.14M
                                Result->getUniqueID(), Result->getEntrySize());
603
604
2.14M
  return Result;
605
2.42M
}
606
607
MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group,
608
0
                                               bool IsComdat) {
609
0
  return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
610
0
                              SectionKind::getReadOnly(), 4, Group, IsComdat,
611
0
                              MCSection::NonUniqueID, nullptr);
612
0
}
613
614
void MCContext::recordELFMergeableSectionInfo(StringRef SectionName,
615
                                              unsigned Flags, unsigned UniqueID,
616
2.14M
                                              unsigned EntrySize) {
617
2.14M
  bool IsMergeable = Flags & ELF::SHF_MERGE;
618
2.14M
  if (UniqueID == GenericSectionID)
619
2.14M
    ELFSeenGenericMergeableSections.insert(SectionName);
620
621
  // For mergeable sections or non-mergeable sections with a generic mergeable
622
  // section name we enter their Unique ID into the ELFEntrySizeMap so that
623
  // compatible globals can be assigned to the same section.
624
2.14M
  if (IsMergeable || isELFGenericMergeableSection(SectionName)) {
625
2.14M
    ELFEntrySizeMap.insert(std::make_pair(
626
2.14M
        ELFEntrySizeKey{SectionName, Flags, EntrySize}, UniqueID));
627
2.14M
  }
628
2.14M
}
629
630
1.86M
bool MCContext::isELFImplicitMergeableSectionNamePrefix(StringRef SectionName) {
631
1.86M
  return SectionName.starts_with(".rodata.str") ||
632
1.86M
         SectionName.starts_with(".rodata.cst");
633
1.86M
}
634
635
1.86M
bool MCContext::isELFGenericMergeableSection(StringRef SectionName) {
636
1.86M
  return isELFImplicitMergeableSectionNamePrefix(SectionName) ||
637
1.86M
         ELFSeenGenericMergeableSections.count(SectionName);
638
1.86M
}
639
640
std::optional<unsigned>
641
MCContext::getELFUniqueIDForEntsize(StringRef SectionName, unsigned Flags,
642
0
                                    unsigned EntrySize) {
643
0
  auto I = ELFEntrySizeMap.find(
644
0
      MCContext::ELFEntrySizeKey{SectionName, Flags, EntrySize});
645
0
  return (I != ELFEntrySizeMap.end()) ? std::optional<unsigned>(I->second)
646
0
                                      : std::nullopt;
647
0
}
648
649
MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
650
                                         MCSection *Parent,
651
0
                                         const MCExpr *SubsectionId) {
652
  // Do the lookup. If we don't have a hit, return a new section.
653
0
  auto IterBool =
654
0
      GOFFUniquingMap.insert(std::make_pair(Section.str(), nullptr));
655
0
  auto Iter = IterBool.first;
656
0
  if (!IterBool.second)
657
0
    return Iter->second;
658
659
0
  StringRef CachedName = Iter->first;
660
0
  MCSectionGOFF *GOFFSection = new (GOFFAllocator.Allocate())
661
0
      MCSectionGOFF(CachedName, Kind, Parent, SubsectionId);
662
0
  Iter->second = GOFFSection;
663
664
0
  return GOFFSection;
665
0
}
666
667
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
668
                                         unsigned Characteristics,
669
                                         SectionKind Kind,
670
                                         StringRef COMDATSymName, int Selection,
671
                                         unsigned UniqueID,
672
0
                                         const char *BeginSymName) {
673
0
  MCSymbol *COMDATSymbol = nullptr;
674
0
  if (!COMDATSymName.empty()) {
675
0
    COMDATSymbol = getOrCreateSymbol(COMDATSymName);
676
0
    COMDATSymName = COMDATSymbol->getName();
677
0
  }
678
679
  // Do the lookup, if we have a hit, return it.
680
0
  COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
681
0
  auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
682
0
  auto Iter = IterBool.first;
683
0
  if (!IterBool.second)
684
0
    return Iter->second;
685
686
0
  MCSymbol *Begin = nullptr;
687
0
  if (BeginSymName)
688
0
    Begin = createTempSymbol(BeginSymName, false);
689
690
0
  StringRef CachedName = Iter->first.SectionName;
691
0
  MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
692
0
      CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
693
694
0
  Iter->second = Result;
695
0
  return Result;
696
0
}
697
698
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
699
                                         unsigned Characteristics,
700
                                         SectionKind Kind,
701
0
                                         const char *BeginSymName) {
702
0
  return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
703
0
                        BeginSymName);
704
0
}
705
706
MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
707
                                                    const MCSymbol *KeySym,
708
0
                                                    unsigned UniqueID) {
709
  // Return the normal section if we don't have to be associative or unique.
710
0
  if (!KeySym && UniqueID == GenericSectionID)
711
0
    return Sec;
712
713
  // If we have a key symbol, make an associative section with the same name and
714
  // kind as the normal section.
715
0
  unsigned Characteristics = Sec->getCharacteristics();
716
0
  if (KeySym) {
717
0
    Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
718
0
    return getCOFFSection(Sec->getName(), Characteristics, Sec->getKind(),
719
0
                          KeySym->getName(),
720
0
                          COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
721
0
  }
722
723
0
  return getCOFFSection(Sec->getName(), Characteristics, Sec->getKind(), "", 0,
724
0
                        UniqueID);
725
0
}
726
727
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
728
                                         unsigned Flags, const Twine &Group,
729
                                         unsigned UniqueID,
730
10.8k
                                         const char *BeginSymName) {
731
10.8k
  MCSymbolWasm *GroupSym = nullptr;
732
10.8k
  if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
733
0
    GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
734
0
    GroupSym->setComdat(true);
735
0
  }
736
737
10.8k
  return getWasmSection(Section, K, Flags, GroupSym, UniqueID, BeginSymName);
738
10.8k
}
739
740
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
741
                                         unsigned Flags,
742
                                         const MCSymbolWasm *GroupSym,
743
                                         unsigned UniqueID,
744
10.8k
                                         const char *BeginSymName) {
745
10.8k
  StringRef Group = "";
746
10.8k
  if (GroupSym)
747
0
    Group = GroupSym->getName();
748
  // Do the lookup, if we have a hit, return it.
749
10.8k
  auto IterBool = WasmUniquingMap.insert(
750
10.8k
      std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
751
10.8k
  auto &Entry = *IterBool.first;
752
10.8k
  if (!IterBool.second)
753
202
    return Entry.second;
754
755
10.6k
  StringRef CachedName = Entry.first.SectionName;
756
757
10.6k
  MCSymbol *Begin = createSymbol(CachedName, true, false);
758
10.6k
  Symbols[Begin->getName()] = Begin;
759
10.6k
  cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
760
761
10.6k
  MCSectionWasm *Result = new (WasmAllocator.Allocate())
762
10.6k
      MCSectionWasm(CachedName, Kind, Flags, GroupSym, UniqueID, Begin);
763
10.6k
  Entry.second = Result;
764
765
10.6k
  auto *F = new MCDataFragment();
766
10.6k
  Result->getFragmentList().insert(Result->begin(), F);
767
10.6k
  F->setParent(Result);
768
10.6k
  Begin->setFragment(F);
769
770
10.6k
  return Result;
771
10.8k
}
772
773
bool MCContext::hasXCOFFSection(StringRef Section,
774
0
                                XCOFF::CsectProperties CsectProp) const {
775
0
  return XCOFFUniquingMap.count(
776
0
             XCOFFSectionKey(Section.str(), CsectProp.MappingClass)) != 0;
777
0
}
778
779
MCSectionXCOFF *MCContext::getXCOFFSection(
780
    StringRef Section, SectionKind Kind,
781
    std::optional<XCOFF::CsectProperties> CsectProp, bool MultiSymbolsAllowed,
782
    const char *BeginSymName,
783
0
    std::optional<XCOFF::DwarfSectionSubtypeFlags> DwarfSectionSubtypeFlags) {
784
0
  bool IsDwarfSec = DwarfSectionSubtypeFlags.has_value();
785
0
  assert((IsDwarfSec != CsectProp.has_value()) && "Invalid XCOFF section!");
786
787
  // Do the lookup. If we have a hit, return it.
788
0
  auto IterBool = XCOFFUniquingMap.insert(std::make_pair(
789
0
      IsDwarfSec ? XCOFFSectionKey(Section.str(), *DwarfSectionSubtypeFlags)
790
0
                 : XCOFFSectionKey(Section.str(), CsectProp->MappingClass),
791
0
      nullptr));
792
0
  auto &Entry = *IterBool.first;
793
0
  if (!IterBool.second) {
794
0
    MCSectionXCOFF *ExistedEntry = Entry.second;
795
0
    if (ExistedEntry->isMultiSymbolsAllowed() != MultiSymbolsAllowed)
796
0
      report_fatal_error("section's multiply symbols policy does not match");
797
798
0
    return ExistedEntry;
799
0
  }
800
801
  // Otherwise, return a new section.
802
0
  StringRef CachedName = Entry.first.SectionName;
803
0
  MCSymbolXCOFF *QualName = nullptr;
804
  // Debug section don't have storage class attribute.
805
0
  if (IsDwarfSec)
806
0
    QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol(CachedName));
807
0
  else
808
0
    QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol(
809
0
        CachedName + "[" +
810
0
        XCOFF::getMappingClassString(CsectProp->MappingClass) + "]"));
811
812
0
  MCSymbol *Begin = nullptr;
813
0
  if (BeginSymName)
814
0
    Begin = createTempSymbol(BeginSymName, false);
815
816
  // QualName->getUnqualifiedName() and CachedName are the same except when
817
  // CachedName contains invalid character(s) such as '$' for an XCOFF symbol.
818
0
  MCSectionXCOFF *Result = nullptr;
819
0
  if (IsDwarfSec)
820
0
    Result = new (XCOFFAllocator.Allocate()) MCSectionXCOFF(
821
0
        QualName->getUnqualifiedName(), Kind, QualName,
822
0
        *DwarfSectionSubtypeFlags, Begin, CachedName, MultiSymbolsAllowed);
823
0
  else
824
0
    Result = new (XCOFFAllocator.Allocate())
825
0
        MCSectionXCOFF(QualName->getUnqualifiedName(), CsectProp->MappingClass,
826
0
                       CsectProp->Type, Kind, QualName, Begin, CachedName,
827
0
                       MultiSymbolsAllowed);
828
829
0
  Entry.second = Result;
830
831
0
  auto *F = new MCDataFragment();
832
0
  Result->getFragmentList().insert(Result->begin(), F);
833
0
  F->setParent(Result);
834
835
0
  if (Begin)
836
0
    Begin->setFragment(F);
837
838
  // We might miss calculating the symbols difference as absolute value before
839
  // adding fixups when symbol_A without the fragment set is the csect itself
840
  // and symbol_B is in it.
841
  // TODO: Currently we only set the fragment for XMC_PR csects because we don't
842
  // have other cases that hit this problem yet.
843
0
  if (!IsDwarfSec && CsectProp->MappingClass == XCOFF::XMC_PR)
844
0
    QualName->setFragment(F);
845
846
0
  return Result;
847
0
}
848
849
0
MCSectionSPIRV *MCContext::getSPIRVSection() {
850
0
  MCSymbol *Begin = nullptr;
851
0
  MCSectionSPIRV *Result = new (SPIRVAllocator.Allocate())
852
0
      MCSectionSPIRV(SectionKind::getText(), Begin);
853
854
0
  auto *F = new MCDataFragment();
855
0
  Result->getFragmentList().insert(Result->begin(), F);
856
0
  F->setParent(Result);
857
858
0
  return Result;
859
0
}
860
861
MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
862
0
                                                       SectionKind K) {
863
  // Do the lookup, if we have a hit, return it.
864
0
  auto ItInsertedPair = DXCUniquingMap.try_emplace(Section);
865
0
  if (!ItInsertedPair.second)
866
0
    return ItInsertedPair.first->second;
867
868
0
  auto MapIt = ItInsertedPair.first;
869
  // Grab the name from the StringMap. Since the Section is going to keep a
870
  // copy of this StringRef we need to make sure the underlying string stays
871
  // alive as long as we need it.
872
0
  StringRef Name = MapIt->first();
873
0
  MapIt->second =
874
0
      new (DXCAllocator.Allocate()) MCSectionDXContainer(Name, K, nullptr);
875
876
  // The first fragment will store the header
877
0
  auto *F = new MCDataFragment();
878
0
  MapIt->second->getFragmentList().insert(MapIt->second->begin(), F);
879
0
  F->setParent(MapIt->second);
880
881
0
  return MapIt->second;
882
0
}
883
884
0
MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
885
0
  return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
886
0
}
887
888
void MCContext::addDebugPrefixMapEntry(const std::string &From,
889
0
                                       const std::string &To) {
890
0
  DebugPrefixMap.emplace_back(From, To);
891
0
}
892
893
0
void MCContext::remapDebugPath(SmallVectorImpl<char> &Path) {
894
0
  for (const auto &[From, To] : llvm::reverse(DebugPrefixMap))
895
0
    if (llvm::sys::path::replace_path_prefix(Path, From, To))
896
0
      break;
897
0
}
898
899
0
void MCContext::RemapDebugPaths() {
900
0
  const auto &DebugPrefixMap = this->DebugPrefixMap;
901
0
  if (DebugPrefixMap.empty())
902
0
    return;
903
904
  // Remap compilation directory.
905
0
  remapDebugPath(CompilationDir);
906
907
  // Remap MCDwarfDirs and RootFile.Name in all compilation units.
908
0
  SmallString<256> P;
909
0
  for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) {
910
0
    for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) {
911
0
      P = Dir;
912
0
      remapDebugPath(P);
913
0
      Dir = std::string(P);
914
0
    }
915
916
    // Used by DW_TAG_compile_unit's DT_AT_name and DW_TAG_label's
917
    // DW_AT_decl_file for DWARF v5 generated for assembly source.
918
0
    P = CUIDTablePair.second.getRootFile().Name;
919
0
    remapDebugPath(P);
920
0
    CUIDTablePair.second.getRootFile().Name = std::string(P);
921
0
  }
922
0
}
923
924
//===----------------------------------------------------------------------===//
925
// Dwarf Management
926
//===----------------------------------------------------------------------===//
927
928
0
EmitDwarfUnwindType MCContext::emitDwarfUnwindInfo() const {
929
0
  if (!TargetOptions)
930
0
    return EmitDwarfUnwindType::Default;
931
0
  return TargetOptions->EmitDwarfUnwind;
932
0
}
933
934
0
bool MCContext::emitCompactUnwindNonCanonical() const {
935
0
  if (TargetOptions)
936
0
    return TargetOptions->EmitCompactUnwindNonCanonical;
937
0
  return false;
938
0
}
939
940
0
void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
941
  // MCDwarf needs the root file as well as the compilation directory.
942
  // If we find a '.file 0' directive that will supersede these values.
943
0
  std::optional<MD5::MD5Result> Cksum;
944
0
  if (getDwarfVersion() >= 5) {
945
0
    MD5 Hash;
946
0
    MD5::MD5Result Sum;
947
0
    Hash.update(Buffer);
948
0
    Hash.final(Sum);
949
0
    Cksum = Sum;
950
0
  }
951
  // Canonicalize the root filename. It cannot be empty, and should not
952
  // repeat the compilation dir.
953
  // The MCContext ctor initializes MainFileName to the name associated with
954
  // the SrcMgr's main file ID, which might be the same as InputFileName (and
955
  // possibly include directory components).
956
  // Or, MainFileName might have been overridden by a -main-file-name option,
957
  // which is supposed to be just a base filename with no directory component.
958
  // So, if the InputFileName and MainFileName are not equal, assume
959
  // MainFileName is a substitute basename and replace the last component.
960
0
  SmallString<1024> FileNameBuf = InputFileName;
961
0
  if (FileNameBuf.empty() || FileNameBuf == "-")
962
0
    FileNameBuf = "<stdin>";
963
0
  if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
964
0
    llvm::sys::path::remove_filename(FileNameBuf);
965
0
    llvm::sys::path::append(FileNameBuf, getMainFileName());
966
0
  }
967
0
  StringRef FileName = FileNameBuf;
968
0
  if (FileName.consume_front(getCompilationDir()))
969
0
    if (llvm::sys::path::is_separator(FileName.front()))
970
0
      FileName = FileName.drop_front();
971
0
  assert(!FileName.empty());
972
0
  setMCLineTableRootFile(
973
0
      /*CUID=*/0, getCompilationDir(), FileName, Cksum, std::nullopt);
974
0
}
975
976
/// getDwarfFile - takes a file name and number to place in the dwarf file and
977
/// directory tables.  If the file number has already been allocated it is an
978
/// error and zero is returned and the client reports the error, else the
979
/// allocated file number is returned.  The file numbers may be in any order.
980
Expected<unsigned>
981
MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
982
                        unsigned FileNumber,
983
                        std::optional<MD5::MD5Result> Checksum,
984
794
                        std::optional<StringRef> Source, unsigned CUID) {
985
794
  MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
986
794
  return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
987
794
                          FileNumber);
988
794
}
989
990
/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
991
/// currently is assigned and false otherwise.
992
0
bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
993
0
  const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
994
0
  if (FileNumber == 0)
995
0
    return getDwarfVersion() >= 5;
996
0
  if (FileNumber >= LineTable.getMCDwarfFiles().size())
997
0
    return false;
998
999
0
  return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
1000
0
}
1001
1002
/// Remove empty sections from SectionsForRanges, to avoid generating
1003
/// useless debug info for them.
1004
0
void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
1005
0
  SectionsForRanges.remove_if(
1006
0
      [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
1007
0
}
1008
1009
0
CodeViewContext &MCContext::getCVContext() {
1010
0
  if (!CVContext)
1011
0
    CVContext.reset(new CodeViewContext);
1012
0
  return *CVContext;
1013
0
}
1014
1015
//===----------------------------------------------------------------------===//
1016
// Error Reporting
1017
//===----------------------------------------------------------------------===//
1018
1019
0
void MCContext::diagnose(const SMDiagnostic &SMD) {
1020
0
  assert(DiagHandler && "MCContext::DiagHandler is not set");
1021
0
  bool UseInlineSrcMgr = false;
1022
0
  const SourceMgr *SMP = nullptr;
1023
0
  if (SrcMgr) {
1024
0
    SMP = SrcMgr;
1025
0
  } else if (InlineSrcMgr) {
1026
0
    SMP = InlineSrcMgr.get();
1027
0
    UseInlineSrcMgr = true;
1028
0
  } else
1029
0
    llvm_unreachable("Either SourceMgr should be available");
1030
0
  DiagHandler(SMD, UseInlineSrcMgr, *SMP, LocInfos);
1031
0
}
1032
1033
void MCContext::reportCommon(
1034
    SMLoc Loc,
1035
0
    std::function<void(SMDiagnostic &, const SourceMgr *)> GetMessage) {
1036
  // * MCContext::SrcMgr is null when the MC layer emits machine code for input
1037
  //   other than assembly file, say, for .c/.cpp/.ll/.bc.
1038
  // * MCContext::InlineSrcMgr is null when the inline asm is not used.
1039
  // * A default SourceMgr is needed for diagnosing when both MCContext::SrcMgr
1040
  //   and MCContext::InlineSrcMgr are null.
1041
0
  SourceMgr SM;
1042
0
  const SourceMgr *SMP = &SM;
1043
0
  bool UseInlineSrcMgr = false;
1044
1045
  // FIXME: Simplify these by combining InlineSrcMgr & SrcMgr.
1046
  //        For MC-only execution, only SrcMgr is used;
1047
  //        For non MC-only execution, InlineSrcMgr is only ctor'd if there is
1048
  //        inline asm in the IR.
1049
0
  if (Loc.isValid()) {
1050
0
    if (SrcMgr) {
1051
0
      SMP = SrcMgr;
1052
0
    } else if (InlineSrcMgr) {
1053
0
      SMP = InlineSrcMgr.get();
1054
0
      UseInlineSrcMgr = true;
1055
0
    } else
1056
0
      llvm_unreachable("Either SourceMgr should be available");
1057
0
  }
1058
1059
0
  SMDiagnostic D;
1060
0
  GetMessage(D, SMP);
1061
0
  DiagHandler(D, UseInlineSrcMgr, *SMP, LocInfos);
1062
0
}
1063
1064
0
void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
1065
0
  HadError = true;
1066
0
  reportCommon(Loc, [&](SMDiagnostic &D, const SourceMgr *SMP) {
1067
0
    D = SMP->GetMessage(Loc, SourceMgr::DK_Error, Msg);
1068
0
  });
1069
0
}
1070
1071
0
void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
1072
0
  if (TargetOptions && TargetOptions->MCNoWarn)
1073
0
    return;
1074
0
  if (TargetOptions && TargetOptions->MCFatalWarnings) {
1075
0
    reportError(Loc, Msg);
1076
0
  } else {
1077
0
    reportCommon(Loc, [&](SMDiagnostic &D, const SourceMgr *SMP) {
1078
0
      D = SMP->GetMessage(Loc, SourceMgr::DK_Warning, Msg);
1079
0
    });
1080
0
  }
1081
0
}