1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeView.h"
16 #include "llvm/MC/MCDwarf.h"
17 #include "llvm/MC/MCLabel.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSectionCOFF.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbolCOFF.h"
25 #include "llvm/MC/MCSymbolELF.h"
26 #include "llvm/MC/MCSymbolMachO.h"
27 #include "llvm/Support/COFF.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ELF.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/SourceMgr.h"
34 
35 using namespace llvm;
36 
37 static cl::opt<char*>
38 AsSecureLogFileName("as-secure-log-file-name",
39         cl::desc("As secure log file name (initialized from "
40                  "AS_SECURE_LOG_FILE env variable)"),
41         cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
42 
43 
44 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
45                      const MCObjectFileInfo *mofi, const SourceMgr *mgr,
46                      bool DoAutoReset)
47     : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
48       Symbols(Allocator), UsedNames(Allocator),
49       CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
50       GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
51       AllowTemporaryLabels(true), DwarfCompileUnitID(0),
52       AutoReset(DoAutoReset), HadError(false) {
53   SecureLogFile = AsSecureLogFileName;
54   SecureLog = nullptr;
55   SecureLogUsed = false;
56 
57   if (SrcMgr && SrcMgr->getNumBuffers())
58     MainFileName =
59         SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
60 }
61 
62 MCContext::~MCContext() {
63   if (AutoReset)
64     reset();
65 
66   // NOTE: The symbols are all allocated out of a bump pointer allocator,
67   // we don't need to free them here.
68 }
69 
70 //===----------------------------------------------------------------------===//
71 // Module Lifetime Management
72 //===----------------------------------------------------------------------===//
73 
74 void MCContext::reset() {
75   // Call the destructors so the fragments are freed
76   COFFAllocator.DestroyAll();
77   ELFAllocator.DestroyAll();
78   MachOAllocator.DestroyAll();
79 
80   MCSubtargetAllocator.DestroyAll();
81   UsedNames.clear();
82   Symbols.clear();
83   SectionSymbols.clear();
84   Allocator.Reset();
85   Instances.clear();
86   CompilationDir.clear();
87   MainFileName.clear();
88   MCDwarfLineTablesCUMap.clear();
89   SectionsForRanges.clear();
90   MCGenDwarfLabelEntries.clear();
91   DwarfDebugFlags = StringRef();
92   DwarfCompileUnitID = 0;
93   CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
94 
95   CVContext.reset();
96 
97   MachOUniquingMap.clear();
98   ELFUniquingMap.clear();
99   COFFUniquingMap.clear();
100 
101   NextID.clear();
102   AllowTemporaryLabels = true;
103   DwarfLocSeen = false;
104   GenDwarfForAssembly = false;
105   GenDwarfFileNumber = 0;
106 
107   HadError = false;
108 }
109 
110 //===----------------------------------------------------------------------===//
111 // Symbol Manipulation
112 //===----------------------------------------------------------------------===//
113 
114 MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
115   SmallString<128> NameSV;
116   StringRef NameRef = Name.toStringRef(NameSV);
117 
118   assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
119 
120   MCSymbol *&Sym = Symbols[NameRef];
121   if (!Sym)
122     Sym = createSymbol(NameRef, false, false);
123 
124   return Sym;
125 }
126 
127 MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
128   MCSymbol *&Sym = SectionSymbols[&Section];
129   if (Sym)
130     return cast<MCSymbolELF>(Sym);
131 
132   StringRef Name = Section.getSectionName();
133   auto NameIter = UsedNames.insert(std::make_pair(Name, false)).first;
134   Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
135 
136   return cast<MCSymbolELF>(Sym);
137 }
138 
139 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
140                                                  unsigned Idx) {
141   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
142                            "$frame_escape_" + Twine(Idx));
143 }
144 
145 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
146   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
147                            "$parent_frame_offset");
148 }
149 
150 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
151   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
152                            FuncName);
153 }
154 
155 MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
156                                       bool IsTemporary) {
157   if (MOFI) {
158     switch (MOFI->getObjectFileType()) {
159     case MCObjectFileInfo::IsCOFF:
160       return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
161     case MCObjectFileInfo::IsELF:
162       return new (Name, *this) MCSymbolELF(Name, IsTemporary);
163     case MCObjectFileInfo::IsMachO:
164       return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
165     }
166   }
167   return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
168                                     IsTemporary);
169 }
170 
171 MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
172                                   bool CanBeUnnamed) {
173   if (CanBeUnnamed && !UseNamesOnTempLabels)
174     return createSymbolImpl(nullptr, true);
175 
176   // Determine whether this is a user written assembler temporary or normal
177   // label, if used.
178   bool IsTemporary = CanBeUnnamed;
179   if (AllowTemporaryLabels && !IsTemporary)
180     IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
181 
182   SmallString<128> NewName = Name;
183   bool AddSuffix = AlwaysAddSuffix;
184   unsigned &NextUniqueID = NextID[Name];
185   for (;;) {
186     if (AddSuffix) {
187       NewName.resize(Name.size());
188       raw_svector_ostream(NewName) << NextUniqueID++;
189     }
190     auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
191     if (NameEntry.second || !NameEntry.first->second) {
192       // Ok, we found a name.
193       // Mark it as used for a non-section symbol.
194       NameEntry.first->second = true;
195       // Have the MCSymbol object itself refer to the copy of the string that is
196       // embedded in the UsedNames entry.
197       return createSymbolImpl(&*NameEntry.first, IsTemporary);
198     }
199     assert(IsTemporary && "Cannot rename non-temporary symbols");
200     AddSuffix = true;
201   }
202   llvm_unreachable("Infinite loop");
203 }
204 
205 MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
206                                       bool CanBeUnnamed) {
207   SmallString<128> NameSV;
208   raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
209   return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
210 }
211 
212 MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
213   SmallString<128> NameSV;
214   raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
215   return createSymbol(NameSV, true, false);
216 }
217 
218 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
219   return createTempSymbol("tmp", true, CanBeUnnamed);
220 }
221 
222 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
223   MCLabel *&Label = Instances[LocalLabelVal];
224   if (!Label)
225     Label = new (*this) MCLabel(0);
226   return Label->incInstance();
227 }
228 
229 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
230   MCLabel *&Label = Instances[LocalLabelVal];
231   if (!Label)
232     Label = new (*this) MCLabel(0);
233   return Label->getInstance();
234 }
235 
236 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
237                                                        unsigned Instance) {
238   MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
239   if (!Sym)
240     Sym = createTempSymbol(false);
241   return Sym;
242 }
243 
244 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
245   unsigned Instance = NextInstance(LocalLabelVal);
246   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
247 }
248 
249 MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
250                                                bool Before) {
251   unsigned Instance = GetInstance(LocalLabelVal);
252   if (!Before)
253     ++Instance;
254   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
255 }
256 
257 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
258   SmallString<128> NameSV;
259   StringRef NameRef = Name.toStringRef(NameSV);
260   return Symbols.lookup(NameRef);
261 }
262 
263 void MCContext::setSymbolValue(MCStreamer &Streamer,
264                               StringRef Sym,
265                               uint64_t Val) {
266   auto Symbol = getOrCreateSymbol(Sym);
267   Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
268 }
269 
270 //===----------------------------------------------------------------------===//
271 // Section Management
272 //===----------------------------------------------------------------------===//
273 
274 MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
275                                            unsigned TypeAndAttributes,
276                                            unsigned Reserved2, SectionKind Kind,
277                                            const char *BeginSymName) {
278 
279   // We unique sections by their segment/section pair.  The returned section
280   // may not have the same flags as the requested section, if so this should be
281   // diagnosed by the client as an error.
282 
283   // Form the name to look up.
284   SmallString<64> Name;
285   Name += Segment;
286   Name.push_back(',');
287   Name += Section;
288 
289   // Do the lookup, if we have a hit, return it.
290   MCSectionMachO *&Entry = MachOUniquingMap[Name];
291   if (Entry)
292     return Entry;
293 
294   MCSymbol *Begin = nullptr;
295   if (BeginSymName)
296     Begin = createTempSymbol(BeginSymName, false);
297 
298   // Otherwise, return a new section.
299   return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
300              Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
301 }
302 
303 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
304   StringRef GroupName;
305   if (const MCSymbol *Group = Section->getGroup())
306     GroupName = Group->getName();
307 
308   unsigned UniqueID = Section->getUniqueID();
309   ELFUniquingMap.erase(
310       ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
311   auto I = ELFUniquingMap.insert(std::make_pair(
312                                      ELFSectionKey{Name, GroupName, UniqueID},
313                                      Section))
314                .first;
315   StringRef CachedName = I->first.SectionName;
316   const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
317 }
318 
319 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
320                                              unsigned Flags, unsigned EntrySize,
321                                              const MCSymbolELF *Group,
322                                              const MCSectionELF *Associated) {
323   StringMap<bool>::iterator I;
324   bool Inserted;
325   std::tie(I, Inserted) =
326       ELFRelSecNames.insert(std::make_pair(Name.str(), true));
327 
328   return new (ELFAllocator.Allocate())
329       MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
330                    EntrySize, Group, true, nullptr, Associated);
331 }
332 
333 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
334                                             const Twine &Suffix, unsigned Type,
335                                             unsigned Flags,
336                                             unsigned EntrySize) {
337   return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
338 }
339 
340 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
341                                        unsigned Flags, unsigned EntrySize,
342                                        const Twine &Group, unsigned UniqueID,
343                                        const char *BeginSymName) {
344   MCSymbolELF *GroupSym = nullptr;
345   if (!Group.isTriviallyEmpty() && !Group.str().empty())
346     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
347 
348   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
349                        BeginSymName, nullptr);
350 }
351 
352 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
353                                        unsigned Flags, unsigned EntrySize,
354                                        const MCSymbolELF *GroupSym,
355                                        unsigned UniqueID,
356                                        const char *BeginSymName,
357                                        const MCSectionELF *Associated) {
358   StringRef Group = "";
359   if (GroupSym)
360     Group = GroupSym->getName();
361   // Do the lookup, if we have a hit, return it.
362   auto IterBool = ELFUniquingMap.insert(
363       std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
364   auto &Entry = *IterBool.first;
365   if (!IterBool.second)
366     return Entry.second;
367 
368   StringRef CachedName = Entry.first.SectionName;
369 
370   SectionKind Kind;
371   if (Flags & ELF::SHF_ARM_PURECODE)
372     Kind = SectionKind::getExecuteOnly();
373   else if (Flags & ELF::SHF_EXECINSTR)
374     Kind = SectionKind::getText();
375   else
376     Kind = SectionKind::getReadOnly();
377 
378   MCSymbol *Begin = nullptr;
379   if (BeginSymName)
380     Begin = createTempSymbol(BeginSymName, false);
381 
382   MCSectionELF *Result = new (ELFAllocator.Allocate())
383       MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID,
384                    Begin, Associated);
385   Entry.second = Result;
386   return Result;
387 }
388 
389 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
390   MCSectionELF *Result = new (ELFAllocator.Allocate())
391       MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
392                    Group, ~0, nullptr, nullptr);
393   return Result;
394 }
395 
396 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
397                                          unsigned Characteristics,
398                                          SectionKind Kind,
399                                          StringRef COMDATSymName, int Selection,
400                                          unsigned UniqueID,
401                                          const char *BeginSymName) {
402   MCSymbol *COMDATSymbol = nullptr;
403   if (!COMDATSymName.empty()) {
404     COMDATSymbol = getOrCreateSymbol(COMDATSymName);
405     COMDATSymName = COMDATSymbol->getName();
406   }
407 
408 
409   // Do the lookup, if we have a hit, return it.
410   COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
411   auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
412   auto Iter = IterBool.first;
413   if (!IterBool.second)
414     return Iter->second;
415 
416   MCSymbol *Begin = nullptr;
417   if (BeginSymName)
418     Begin = createTempSymbol(BeginSymName, false);
419 
420   StringRef CachedName = Iter->first.SectionName;
421   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
422       CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
423 
424   Iter->second = Result;
425   return Result;
426 }
427 
428 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
429                                          unsigned Characteristics,
430                                          SectionKind Kind,
431                                          const char *BeginSymName) {
432   return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
433                         BeginSymName);
434 }
435 
436 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
437   COFFSectionKey T{Section, "", 0, GenericSectionID};
438   auto Iter = COFFUniquingMap.find(T);
439   if (Iter == COFFUniquingMap.end())
440     return nullptr;
441   return Iter->second;
442 }
443 
444 MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
445                                                     const MCSymbol *KeySym,
446                                                     unsigned UniqueID) {
447   // Return the normal section if we don't have to be associative or unique.
448   if (!KeySym && UniqueID == GenericSectionID)
449     return Sec;
450 
451   // If we have a key symbol, make an associative section with the same name and
452   // kind as the normal section.
453   unsigned Characteristics = Sec->getCharacteristics();
454   if (KeySym) {
455     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
456     return getCOFFSection(Sec->getSectionName(), Characteristics,
457                           Sec->getKind(), KeySym->getName(),
458                           COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
459   }
460 
461   return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
462                         "", 0, UniqueID);
463 }
464 
465 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
466   return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
467 }
468 
469 //===----------------------------------------------------------------------===//
470 // Dwarf Management
471 //===----------------------------------------------------------------------===//
472 
473 /// getDwarfFile - takes a file name an number to place in the dwarf file and
474 /// directory tables.  If the file number has already been allocated it is an
475 /// error and zero is returned and the client reports the error, else the
476 /// allocated file number is returned.  The file numbers may be in any order.
477 unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
478                                  unsigned FileNumber, unsigned CUID) {
479   MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
480   return Table.getFile(Directory, FileName, FileNumber);
481 }
482 
483 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
484 /// currently is assigned and false otherwise.
485 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
486   const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
487   if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
488     return false;
489 
490   return !MCDwarfFiles[FileNumber].Name.empty();
491 }
492 
493 /// Remove empty sections from SectionStartEndSyms, to avoid generating
494 /// useless debug info for them.
495 void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
496   SectionsForRanges.remove_if(
497       [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
498 }
499 
500 CodeViewContext &MCContext::getCVContext() {
501   if (!CVContext.get())
502     CVContext.reset(new CodeViewContext);
503   return *CVContext.get();
504 }
505 
506 //===----------------------------------------------------------------------===//
507 // Error Reporting
508 //===----------------------------------------------------------------------===//
509 
510 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
511   HadError = true;
512 
513   // If we have a source manager use it. Otherwise just use the generic
514   // report_fatal_error().
515   if (!SrcMgr)
516     report_fatal_error(Msg, false);
517 
518   // Use the source manager to print the message.
519   SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
520 }
521 
522 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
523   reportError(Loc, Msg);
524 
525   // If we reached here, we are failing ungracefully. Run the interrupt handlers
526   // to make sure any special cleanups get done, in particular that we remove
527   // files registered with RemoveFileOnSignal.
528   sys::RunInterruptHandlers();
529   exit(1);
530 }
531