CodeViewDebug.cpp 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403
  1. //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
  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. // This file contains support for writing Microsoft CodeView debug info.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CodeViewDebug.h"
  13. #include "DwarfExpression.h"
  14. #include "llvm/ADT/APSInt.h"
  15. #include "llvm/ADT/None.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/TinyPtrVector.h"
  21. #include "llvm/ADT/Triple.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/BinaryFormat/COFF.h"
  24. #include "llvm/BinaryFormat/Dwarf.h"
  25. #include "llvm/CodeGen/AsmPrinter.h"
  26. #include "llvm/CodeGen/LexicalScopes.h"
  27. #include "llvm/CodeGen/MachineFrameInfo.h"
  28. #include "llvm/CodeGen/MachineFunction.h"
  29. #include "llvm/CodeGen/MachineInstr.h"
  30. #include "llvm/CodeGen/MachineModuleInfo.h"
  31. #include "llvm/CodeGen/MachineOperand.h"
  32. #include "llvm/CodeGen/TargetFrameLowering.h"
  33. #include "llvm/CodeGen/TargetRegisterInfo.h"
  34. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  35. #include "llvm/Config/llvm-config.h"
  36. #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
  37. #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
  38. #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
  39. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  40. #include "llvm/DebugInfo/CodeView/EnumTables.h"
  41. #include "llvm/DebugInfo/CodeView/Line.h"
  42. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  43. #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
  44. #include "llvm/DebugInfo/CodeView/TypeRecord.h"
  45. #include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
  46. #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
  47. #include "llvm/IR/Constants.h"
  48. #include "llvm/IR/DataLayout.h"
  49. #include "llvm/IR/DebugInfoMetadata.h"
  50. #include "llvm/IR/Function.h"
  51. #include "llvm/IR/GlobalValue.h"
  52. #include "llvm/IR/GlobalVariable.h"
  53. #include "llvm/IR/Metadata.h"
  54. #include "llvm/IR/Module.h"
  55. #include "llvm/MC/MCAsmInfo.h"
  56. #include "llvm/MC/MCContext.h"
  57. #include "llvm/MC/MCSectionCOFF.h"
  58. #include "llvm/MC/MCStreamer.h"
  59. #include "llvm/MC/MCSymbol.h"
  60. #include "llvm/Support/BinaryByteStream.h"
  61. #include "llvm/Support/BinaryStreamReader.h"
  62. #include "llvm/Support/BinaryStreamWriter.h"
  63. #include "llvm/Support/Casting.h"
  64. #include "llvm/Support/CommandLine.h"
  65. #include "llvm/Support/Endian.h"
  66. #include "llvm/Support/Error.h"
  67. #include "llvm/Support/ErrorHandling.h"
  68. #include "llvm/Support/FormatVariadic.h"
  69. #include "llvm/Support/Path.h"
  70. #include "llvm/Support/Program.h"
  71. #include "llvm/Support/SMLoc.h"
  72. #include "llvm/Support/ScopedPrinter.h"
  73. #include "llvm/Target/TargetLoweringObjectFile.h"
  74. #include "llvm/Target/TargetMachine.h"
  75. #include <algorithm>
  76. #include <cassert>
  77. #include <cctype>
  78. #include <cstddef>
  79. #include <iterator>
  80. #include <limits>
  81. using namespace llvm;
  82. using namespace llvm::codeview;
  83. namespace {
  84. class CVMCAdapter : public CodeViewRecordStreamer {
  85. public:
  86. CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable)
  87. : OS(&OS), TypeTable(TypeTable) {}
  88. void emitBytes(StringRef Data) override { OS->emitBytes(Data); }
  89. void emitIntValue(uint64_t Value, unsigned Size) override {
  90. OS->emitIntValueInHex(Value, Size);
  91. }
  92. void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); }
  93. void AddComment(const Twine &T) override { OS->AddComment(T); }
  94. void AddRawComment(const Twine &T) override { OS->emitRawComment(T); }
  95. bool isVerboseAsm() override { return OS->isVerboseAsm(); }
  96. std::string getTypeName(TypeIndex TI) override {
  97. std::string TypeName;
  98. if (!TI.isNoneType()) {
  99. if (TI.isSimple())
  100. TypeName = std::string(TypeIndex::simpleTypeName(TI));
  101. else
  102. TypeName = std::string(TypeTable.getTypeName(TI));
  103. }
  104. return TypeName;
  105. }
  106. private:
  107. MCStreamer *OS = nullptr;
  108. TypeCollection &TypeTable;
  109. };
  110. } // namespace
  111. static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
  112. switch (Type) {
  113. case Triple::ArchType::x86:
  114. return CPUType::Pentium3;
  115. case Triple::ArchType::x86_64:
  116. return CPUType::X64;
  117. case Triple::ArchType::thumb:
  118. // LLVM currently doesn't support Windows CE and so thumb
  119. // here is indiscriminately mapped to ARMNT specifically.
  120. return CPUType::ARMNT;
  121. case Triple::ArchType::aarch64:
  122. return CPUType::ARM64;
  123. default:
  124. report_fatal_error("target architecture doesn't map to a CodeView CPUType");
  125. }
  126. }
  127. CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
  128. : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {}
  129. StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
  130. std::string &Filepath = FileToFilepathMap[File];
  131. if (!Filepath.empty())
  132. return Filepath;
  133. StringRef Dir = File->getDirectory(), Filename = File->getFilename();
  134. // If this is a Unix-style path, just use it as is. Don't try to canonicalize
  135. // it textually because one of the path components could be a symlink.
  136. if (Dir.startswith("/") || Filename.startswith("/")) {
  137. if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix))
  138. return Filename;
  139. Filepath = std::string(Dir);
  140. if (Dir.back() != '/')
  141. Filepath += '/';
  142. Filepath += Filename;
  143. return Filepath;
  144. }
  145. // Clang emits directory and relative filename info into the IR, but CodeView
  146. // operates on full paths. We could change Clang to emit full paths too, but
  147. // that would increase the IR size and probably not needed for other users.
  148. // For now, just concatenate and canonicalize the path here.
  149. if (Filename.find(':') == 1)
  150. Filepath = std::string(Filename);
  151. else
  152. Filepath = (Dir + "\\" + Filename).str();
  153. // Canonicalize the path. We have to do it textually because we may no longer
  154. // have access the file in the filesystem.
  155. // First, replace all slashes with backslashes.
  156. std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
  157. // Remove all "\.\" with "\".
  158. size_t Cursor = 0;
  159. while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
  160. Filepath.erase(Cursor, 2);
  161. // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
  162. // path should be well-formatted, e.g. start with a drive letter, etc.
  163. Cursor = 0;
  164. while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
  165. // Something's wrong if the path starts with "\..\", abort.
  166. if (Cursor == 0)
  167. break;
  168. size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
  169. if (PrevSlash == std::string::npos)
  170. // Something's wrong, abort.
  171. break;
  172. Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
  173. // The next ".." might be following the one we've just erased.
  174. Cursor = PrevSlash;
  175. }
  176. // Remove all duplicate backslashes.
  177. Cursor = 0;
  178. while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
  179. Filepath.erase(Cursor, 1);
  180. return Filepath;
  181. }
  182. unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
  183. StringRef FullPath = getFullFilepath(F);
  184. unsigned NextId = FileIdMap.size() + 1;
  185. auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
  186. if (Insertion.second) {
  187. // We have to compute the full filepath and emit a .cv_file directive.
  188. ArrayRef<uint8_t> ChecksumAsBytes;
  189. FileChecksumKind CSKind = FileChecksumKind::None;
  190. if (F->getChecksum()) {
  191. std::string Checksum = fromHex(F->getChecksum()->Value);
  192. void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
  193. memcpy(CKMem, Checksum.data(), Checksum.size());
  194. ChecksumAsBytes = ArrayRef<uint8_t>(
  195. reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
  196. switch (F->getChecksum()->Kind) {
  197. case DIFile::CSK_MD5:
  198. CSKind = FileChecksumKind::MD5;
  199. break;
  200. case DIFile::CSK_SHA1:
  201. CSKind = FileChecksumKind::SHA1;
  202. break;
  203. case DIFile::CSK_SHA256:
  204. CSKind = FileChecksumKind::SHA256;
  205. break;
  206. }
  207. }
  208. bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
  209. static_cast<unsigned>(CSKind));
  210. (void)Success;
  211. assert(Success && ".cv_file directive failed");
  212. }
  213. return Insertion.first->second;
  214. }
  215. CodeViewDebug::InlineSite &
  216. CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
  217. const DISubprogram *Inlinee) {
  218. auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
  219. InlineSite *Site = &SiteInsertion.first->second;
  220. if (SiteInsertion.second) {
  221. unsigned ParentFuncId = CurFn->FuncId;
  222. if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
  223. ParentFuncId =
  224. getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
  225. .SiteFuncId;
  226. Site->SiteFuncId = NextFuncId++;
  227. OS.EmitCVInlineSiteIdDirective(
  228. Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
  229. InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
  230. Site->Inlinee = Inlinee;
  231. InlinedSubprograms.insert(Inlinee);
  232. getFuncIdForSubprogram(Inlinee);
  233. }
  234. return *Site;
  235. }
  236. static StringRef getPrettyScopeName(const DIScope *Scope) {
  237. StringRef ScopeName = Scope->getName();
  238. if (!ScopeName.empty())
  239. return ScopeName;
  240. switch (Scope->getTag()) {
  241. case dwarf::DW_TAG_enumeration_type:
  242. case dwarf::DW_TAG_class_type:
  243. case dwarf::DW_TAG_structure_type:
  244. case dwarf::DW_TAG_union_type:
  245. return "<unnamed-tag>";
  246. case dwarf::DW_TAG_namespace:
  247. return "`anonymous namespace'";
  248. default:
  249. return StringRef();
  250. }
  251. }
  252. const DISubprogram *CodeViewDebug::collectParentScopeNames(
  253. const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
  254. const DISubprogram *ClosestSubprogram = nullptr;
  255. while (Scope != nullptr) {
  256. if (ClosestSubprogram == nullptr)
  257. ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
  258. // If a type appears in a scope chain, make sure it gets emitted. The
  259. // frontend will be responsible for deciding if this should be a forward
  260. // declaration or a complete type.
  261. if (const auto *Ty = dyn_cast<DICompositeType>(Scope))
  262. DeferredCompleteTypes.push_back(Ty);
  263. StringRef ScopeName = getPrettyScopeName(Scope);
  264. if (!ScopeName.empty())
  265. QualifiedNameComponents.push_back(ScopeName);
  266. Scope = Scope->getScope();
  267. }
  268. return ClosestSubprogram;
  269. }
  270. static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents,
  271. StringRef TypeName) {
  272. std::string FullyQualifiedName;
  273. for (StringRef QualifiedNameComponent :
  274. llvm::reverse(QualifiedNameComponents)) {
  275. FullyQualifiedName.append(std::string(QualifiedNameComponent));
  276. FullyQualifiedName.append("::");
  277. }
  278. FullyQualifiedName.append(std::string(TypeName));
  279. return FullyQualifiedName;
  280. }
  281. struct CodeViewDebug::TypeLoweringScope {
  282. TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
  283. ~TypeLoweringScope() {
  284. // Don't decrement TypeEmissionLevel until after emitting deferred types, so
  285. // inner TypeLoweringScopes don't attempt to emit deferred types.
  286. if (CVD.TypeEmissionLevel == 1)
  287. CVD.emitDeferredCompleteTypes();
  288. --CVD.TypeEmissionLevel;
  289. }
  290. CodeViewDebug &CVD;
  291. };
  292. std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope,
  293. StringRef Name) {
  294. // Ensure types in the scope chain are emitted as soon as possible.
  295. // This can create otherwise a situation where S_UDTs are emitted while
  296. // looping in emitDebugInfoForUDTs.
  297. TypeLoweringScope S(*this);
  298. SmallVector<StringRef, 5> QualifiedNameComponents;
  299. collectParentScopeNames(Scope, QualifiedNameComponents);
  300. return formatNestedName(QualifiedNameComponents, Name);
  301. }
  302. std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) {
  303. const DIScope *Scope = Ty->getScope();
  304. return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
  305. }
  306. TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
  307. // No scope means global scope and that uses the zero index.
  308. //
  309. // We also use zero index when the scope is a DISubprogram
  310. // to suppress the emission of LF_STRING_ID for the function,
  311. // which can trigger a link-time error with the linker in
  312. // VS2019 version 16.11.2 or newer.
  313. // Note, however, skipping the debug info emission for the DISubprogram
  314. // is a temporary fix. The root issue here is that we need to figure out
  315. // the proper way to encode a function nested in another function
  316. // (as introduced by the Fortran 'contains' keyword) in CodeView.
  317. if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope))
  318. return TypeIndex();
  319. assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
  320. // Check if we've already translated this scope.
  321. auto I = TypeIndices.find({Scope, nullptr});
  322. if (I != TypeIndices.end())
  323. return I->second;
  324. // Build the fully qualified name of the scope.
  325. std::string ScopeName = getFullyQualifiedName(Scope);
  326. StringIdRecord SID(TypeIndex(), ScopeName);
  327. auto TI = TypeTable.writeLeafType(SID);
  328. return recordTypeIndexForDINode(Scope, TI);
  329. }
  330. static StringRef removeTemplateArgs(StringRef Name) {
  331. // Remove template args from the display name. Assume that the template args
  332. // are the last thing in the name.
  333. if (Name.empty() || Name.back() != '>')
  334. return Name;
  335. int OpenBrackets = 0;
  336. for (int i = Name.size() - 1; i >= 0; --i) {
  337. if (Name[i] == '>')
  338. ++OpenBrackets;
  339. else if (Name[i] == '<') {
  340. --OpenBrackets;
  341. if (OpenBrackets == 0)
  342. return Name.substr(0, i);
  343. }
  344. }
  345. return Name;
  346. }
  347. TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
  348. assert(SP);
  349. // Check if we've already translated this subprogram.
  350. auto I = TypeIndices.find({SP, nullptr});
  351. if (I != TypeIndices.end())
  352. return I->second;
  353. // The display name includes function template arguments. Drop them to match
  354. // MSVC. We need to have the template arguments in the DISubprogram name
  355. // because they are used in other symbol records, such as S_GPROC32_IDs.
  356. StringRef DisplayName = removeTemplateArgs(SP->getName());
  357. const DIScope *Scope = SP->getScope();
  358. TypeIndex TI;
  359. if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
  360. // If the scope is a DICompositeType, then this must be a method. Member
  361. // function types take some special handling, and require access to the
  362. // subprogram.
  363. TypeIndex ClassType = getTypeIndex(Class);
  364. MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
  365. DisplayName);
  366. TI = TypeTable.writeLeafType(MFuncId);
  367. } else {
  368. // Otherwise, this must be a free function.
  369. TypeIndex ParentScope = getScopeIndex(Scope);
  370. FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
  371. TI = TypeTable.writeLeafType(FuncId);
  372. }
  373. return recordTypeIndexForDINode(SP, TI);
  374. }
  375. static bool isNonTrivial(const DICompositeType *DCTy) {
  376. return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial);
  377. }
  378. static FunctionOptions
  379. getFunctionOptions(const DISubroutineType *Ty,
  380. const DICompositeType *ClassTy = nullptr,
  381. StringRef SPName = StringRef("")) {
  382. FunctionOptions FO = FunctionOptions::None;
  383. const DIType *ReturnTy = nullptr;
  384. if (auto TypeArray = Ty->getTypeArray()) {
  385. if (TypeArray.size())
  386. ReturnTy = TypeArray[0];
  387. }
  388. // Add CxxReturnUdt option to functions that return nontrivial record types
  389. // or methods that return record types.
  390. if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy))
  391. if (isNonTrivial(ReturnDCTy) || ClassTy)
  392. FO |= FunctionOptions::CxxReturnUdt;
  393. // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.
  394. if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) {
  395. FO |= FunctionOptions::Constructor;
  396. // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.
  397. }
  398. return FO;
  399. }
  400. TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
  401. const DICompositeType *Class) {
  402. // Always use the method declaration as the key for the function type. The
  403. // method declaration contains the this adjustment.
  404. if (SP->getDeclaration())
  405. SP = SP->getDeclaration();
  406. assert(!SP->getDeclaration() && "should use declaration as key");
  407. // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
  408. // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
  409. auto I = TypeIndices.find({SP, Class});
  410. if (I != TypeIndices.end())
  411. return I->second;
  412. // Make sure complete type info for the class is emitted *after* the member
  413. // function type, as the complete class type is likely to reference this
  414. // member function type.
  415. TypeLoweringScope S(*this);
  416. const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
  417. FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());
  418. TypeIndex TI = lowerTypeMemberFunction(
  419. SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);
  420. return recordTypeIndexForDINode(SP, TI, Class);
  421. }
  422. TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
  423. TypeIndex TI,
  424. const DIType *ClassTy) {
  425. auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
  426. (void)InsertResult;
  427. assert(InsertResult.second && "DINode was already assigned a type index");
  428. return TI;
  429. }
  430. unsigned CodeViewDebug::getPointerSizeInBytes() {
  431. return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
  432. }
  433. void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
  434. const LexicalScope *LS) {
  435. if (const DILocation *InlinedAt = LS->getInlinedAt()) {
  436. // This variable was inlined. Associate it with the InlineSite.
  437. const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
  438. InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
  439. Site.InlinedLocals.emplace_back(Var);
  440. } else {
  441. // This variable goes into the corresponding lexical scope.
  442. ScopeVariables[LS].emplace_back(Var);
  443. }
  444. }
  445. static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
  446. const DILocation *Loc) {
  447. if (!llvm::is_contained(Locs, Loc))
  448. Locs.push_back(Loc);
  449. }
  450. void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
  451. const MachineFunction *MF) {
  452. // Skip this instruction if it has the same location as the previous one.
  453. if (!DL || DL == PrevInstLoc)
  454. return;
  455. const DIScope *Scope = DL.get()->getScope();
  456. if (!Scope)
  457. return;
  458. // Skip this line if it is longer than the maximum we can record.
  459. LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
  460. if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
  461. LI.isNeverStepInto())
  462. return;
  463. ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
  464. if (CI.getStartColumn() != DL.getCol())
  465. return;
  466. if (!CurFn->HaveLineInfo)
  467. CurFn->HaveLineInfo = true;
  468. unsigned FileId = 0;
  469. if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
  470. FileId = CurFn->LastFileId;
  471. else
  472. FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
  473. PrevInstLoc = DL;
  474. unsigned FuncId = CurFn->FuncId;
  475. if (const DILocation *SiteLoc = DL->getInlinedAt()) {
  476. const DILocation *Loc = DL.get();
  477. // If this location was actually inlined from somewhere else, give it the ID
  478. // of the inline call site.
  479. FuncId =
  480. getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
  481. // Ensure we have links in the tree of inline call sites.
  482. bool FirstLoc = true;
  483. while ((SiteLoc = Loc->getInlinedAt())) {
  484. InlineSite &Site =
  485. getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
  486. if (!FirstLoc)
  487. addLocIfNotPresent(Site.ChildSites, Loc);
  488. FirstLoc = false;
  489. Loc = SiteLoc;
  490. }
  491. addLocIfNotPresent(CurFn->ChildSites, Loc);
  492. }
  493. OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
  494. /*PrologueEnd=*/false, /*IsStmt=*/false,
  495. DL->getFilename(), SMLoc());
  496. }
  497. void CodeViewDebug::emitCodeViewMagicVersion() {
  498. OS.emitValueToAlignment(4);
  499. OS.AddComment("Debug section magic");
  500. OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);
  501. }
  502. static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
  503. switch (DWLang) {
  504. case dwarf::DW_LANG_C:
  505. case dwarf::DW_LANG_C89:
  506. case dwarf::DW_LANG_C99:
  507. case dwarf::DW_LANG_C11:
  508. case dwarf::DW_LANG_ObjC:
  509. return SourceLanguage::C;
  510. case dwarf::DW_LANG_C_plus_plus:
  511. case dwarf::DW_LANG_C_plus_plus_03:
  512. case dwarf::DW_LANG_C_plus_plus_11:
  513. case dwarf::DW_LANG_C_plus_plus_14:
  514. return SourceLanguage::Cpp;
  515. case dwarf::DW_LANG_Fortran77:
  516. case dwarf::DW_LANG_Fortran90:
  517. case dwarf::DW_LANG_Fortran95:
  518. case dwarf::DW_LANG_Fortran03:
  519. case dwarf::DW_LANG_Fortran08:
  520. return SourceLanguage::Fortran;
  521. case dwarf::DW_LANG_Pascal83:
  522. return SourceLanguage::Pascal;
  523. case dwarf::DW_LANG_Cobol74:
  524. case dwarf::DW_LANG_Cobol85:
  525. return SourceLanguage::Cobol;
  526. case dwarf::DW_LANG_Java:
  527. return SourceLanguage::Java;
  528. case dwarf::DW_LANG_D:
  529. return SourceLanguage::D;
  530. case dwarf::DW_LANG_Swift:
  531. return SourceLanguage::Swift;
  532. case dwarf::DW_LANG_Rust:
  533. return SourceLanguage::Rust;
  534. default:
  535. // There's no CodeView representation for this language, and CV doesn't
  536. // have an "unknown" option for the language field, so we'll use MASM,
  537. // as it's very low level.
  538. return SourceLanguage::Masm;
  539. }
  540. }
  541. void CodeViewDebug::beginModule(Module *M) {
  542. // If module doesn't have named metadata anchors or COFF debug section
  543. // is not available, skip any debug info related stuff.
  544. NamedMDNode *CUs = M->getNamedMetadata("llvm.dbg.cu");
  545. if (!CUs || !Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
  546. Asm = nullptr;
  547. return;
  548. }
  549. // Tell MMI that we have and need debug info.
  550. MMI->setDebugInfoAvailability(true);
  551. TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch());
  552. // Get the current source language.
  553. const MDNode *Node = *CUs->operands().begin();
  554. const auto *CU = cast<DICompileUnit>(Node);
  555. CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage());
  556. collectGlobalVariableInfo();
  557. // Check if we should emit type record hashes.
  558. ConstantInt *GH =
  559. mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash"));
  560. EmitDebugGlobalHashes = GH && !GH->isZero();
  561. }
  562. void CodeViewDebug::endModule() {
  563. if (!Asm || !MMI->hasDebugInfo())
  564. return;
  565. // The COFF .debug$S section consists of several subsections, each starting
  566. // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
  567. // of the payload followed by the payload itself. The subsections are 4-byte
  568. // aligned.
  569. // Use the generic .debug$S section, and make a subsection for all the inlined
  570. // subprograms.
  571. switchToDebugSectionForSymbol(nullptr);
  572. MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
  573. emitObjName();
  574. emitCompilerInformation();
  575. endCVSubsection(CompilerInfo);
  576. emitInlineeLinesSubsection();
  577. // Emit per-function debug information.
  578. for (auto &P : FnDebugInfo)
  579. if (!P.first->isDeclarationForLinker())
  580. emitDebugInfoForFunction(P.first, *P.second);
  581. // Get types used by globals without emitting anything.
  582. // This is meant to collect all static const data members so they can be
  583. // emitted as globals.
  584. collectDebugInfoForGlobals();
  585. // Emit retained types.
  586. emitDebugInfoForRetainedTypes();
  587. // Emit global variable debug information.
  588. setCurrentSubprogram(nullptr);
  589. emitDebugInfoForGlobals();
  590. // Switch back to the generic .debug$S section after potentially processing
  591. // comdat symbol sections.
  592. switchToDebugSectionForSymbol(nullptr);
  593. // Emit UDT records for any types used by global variables.
  594. if (!GlobalUDTs.empty()) {
  595. MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
  596. emitDebugInfoForUDTs(GlobalUDTs);
  597. endCVSubsection(SymbolsEnd);
  598. }
  599. // This subsection holds a file index to offset in string table table.
  600. OS.AddComment("File index to string table offset subsection");
  601. OS.emitCVFileChecksumsDirective();
  602. // This subsection holds the string table.
  603. OS.AddComment("String table");
  604. OS.emitCVStringTableDirective();
  605. // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol
  606. // subsection in the generic .debug$S section at the end. There is no
  607. // particular reason for this ordering other than to match MSVC.
  608. emitBuildInfo();
  609. // Emit type information and hashes last, so that any types we translate while
  610. // emitting function info are included.
  611. emitTypeInformation();
  612. if (EmitDebugGlobalHashes)
  613. emitTypeGlobalHashes();
  614. clear();
  615. }
  616. static void
  617. emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S,
  618. unsigned MaxFixedRecordLength = 0xF00) {
  619. // The maximum CV record length is 0xFF00. Most of the strings we emit appear
  620. // after a fixed length portion of the record. The fixed length portion should
  621. // always be less than 0xF00 (3840) bytes, so truncate the string so that the
  622. // overall record size is less than the maximum allowed.
  623. SmallString<32> NullTerminatedString(
  624. S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
  625. NullTerminatedString.push_back('\0');
  626. OS.emitBytes(NullTerminatedString);
  627. }
  628. void CodeViewDebug::emitTypeInformation() {
  629. if (TypeTable.empty())
  630. return;
  631. // Start the .debug$T or .debug$P section with 0x4.
  632. OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
  633. emitCodeViewMagicVersion();
  634. TypeTableCollection Table(TypeTable.records());
  635. TypeVisitorCallbackPipeline Pipeline;
  636. // To emit type record using Codeview MCStreamer adapter
  637. CVMCAdapter CVMCOS(OS, Table);
  638. TypeRecordMapping typeMapping(CVMCOS);
  639. Pipeline.addCallbackToPipeline(typeMapping);
  640. Optional<TypeIndex> B = Table.getFirst();
  641. while (B) {
  642. // This will fail if the record data is invalid.
  643. CVType Record = Table.getType(*B);
  644. Error E = codeview::visitTypeRecord(Record, *B, Pipeline);
  645. if (E) {
  646. logAllUnhandledErrors(std::move(E), errs(), "error: ");
  647. llvm_unreachable("produced malformed type record");
  648. }
  649. B = Table.getNext(*B);
  650. }
  651. }
  652. void CodeViewDebug::emitTypeGlobalHashes() {
  653. if (TypeTable.empty())
  654. return;
  655. // Start the .debug$H section with the version and hash algorithm, currently
  656. // hardcoded to version 0, SHA1.
  657. OS.SwitchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection());
  658. OS.emitValueToAlignment(4);
  659. OS.AddComment("Magic");
  660. OS.emitInt32(COFF::DEBUG_HASHES_SECTION_MAGIC);
  661. OS.AddComment("Section Version");
  662. OS.emitInt16(0);
  663. OS.AddComment("Hash Algorithm");
  664. OS.emitInt16(uint16_t(GlobalTypeHashAlg::SHA1_8));
  665. TypeIndex TI(TypeIndex::FirstNonSimpleIndex);
  666. for (const auto &GHR : TypeTable.hashes()) {
  667. if (OS.isVerboseAsm()) {
  668. // Emit an EOL-comment describing which TypeIndex this hash corresponds
  669. // to, as well as the stringified SHA1 hash.
  670. SmallString<32> Comment;
  671. raw_svector_ostream CommentOS(Comment);
  672. CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);
  673. OS.AddComment(Comment);
  674. ++TI;
  675. }
  676. assert(GHR.Hash.size() == 8);
  677. StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),
  678. GHR.Hash.size());
  679. OS.emitBinaryData(S);
  680. }
  681. }
  682. void CodeViewDebug::emitObjName() {
  683. MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME);
  684. StringRef PathRef(Asm->TM.Options.ObjectFilenameForDebug);
  685. llvm::SmallString<256> PathStore(PathRef);
  686. if (PathRef.empty() || PathRef == "-") {
  687. // Don't emit the filename if we're writing to stdout or to /dev/null.
  688. PathRef = {};
  689. } else {
  690. llvm::sys::path::remove_dots(PathStore, /*remove_dot_dot=*/true);
  691. PathRef = PathStore;
  692. }
  693. OS.AddComment("Signature");
  694. OS.emitIntValue(0, 4);
  695. OS.AddComment("Object name");
  696. emitNullTerminatedSymbolName(OS, PathRef);
  697. endSymbolRecord(CompilerEnd);
  698. }
  699. namespace {
  700. struct Version {
  701. int Part[4];
  702. };
  703. } // end anonymous namespace
  704. // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
  705. // the version number.
  706. static Version parseVersion(StringRef Name) {
  707. Version V = {{0}};
  708. int N = 0;
  709. for (const char C : Name) {
  710. if (isdigit(C)) {
  711. V.Part[N] *= 10;
  712. V.Part[N] += C - '0';
  713. } else if (C == '.') {
  714. ++N;
  715. if (N >= 4)
  716. return V;
  717. } else if (N > 0)
  718. return V;
  719. }
  720. return V;
  721. }
  722. void CodeViewDebug::emitCompilerInformation() {
  723. MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
  724. uint32_t Flags = 0;
  725. // The low byte of the flags indicates the source language.
  726. Flags = CurrentSourceLanguage;
  727. // TODO: Figure out which other flags need to be set.
  728. if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {
  729. Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);
  730. }
  731. using ArchType = llvm::Triple::ArchType;
  732. ArchType Arch = Triple(MMI->getModule()->getTargetTriple()).getArch();
  733. if (Asm->TM.Options.Hotpatch || Arch == ArchType::thumb ||
  734. Arch == ArchType::aarch64) {
  735. Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch);
  736. }
  737. OS.AddComment("Flags and language");
  738. OS.emitInt32(Flags);
  739. OS.AddComment("CPUType");
  740. OS.emitInt16(static_cast<uint64_t>(TheCPU));
  741. NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
  742. const MDNode *Node = *CUs->operands().begin();
  743. const auto *CU = cast<DICompileUnit>(Node);
  744. StringRef CompilerVersion = CU->getProducer();
  745. Version FrontVer = parseVersion(CompilerVersion);
  746. OS.AddComment("Frontend version");
  747. for (int N : FrontVer.Part) {
  748. N = std::min<int>(N, std::numeric_limits<uint16_t>::max());
  749. OS.emitInt16(N);
  750. }
  751. // Some Microsoft tools, like Binscope, expect a backend version number of at
  752. // least 8.something, so we'll coerce the LLVM version into a form that
  753. // guarantees it'll be big enough without really lying about the version.
  754. int Major = 1000 * LLVM_VERSION_MAJOR +
  755. 10 * LLVM_VERSION_MINOR +
  756. LLVM_VERSION_PATCH;
  757. // Clamp it for builds that use unusually large version numbers.
  758. Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
  759. Version BackVer = {{ Major, 0, 0, 0 }};
  760. OS.AddComment("Backend version");
  761. for (int N : BackVer.Part)
  762. OS.emitInt16(N);
  763. OS.AddComment("Null-terminated compiler version string");
  764. emitNullTerminatedSymbolName(OS, CompilerVersion);
  765. endSymbolRecord(CompilerEnd);
  766. }
  767. static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
  768. StringRef S) {
  769. StringIdRecord SIR(TypeIndex(0x0), S);
  770. return TypeTable.writeLeafType(SIR);
  771. }
  772. static std::string flattenCommandLine(ArrayRef<std::string> Args,
  773. StringRef MainFilename) {
  774. std::string FlatCmdLine;
  775. raw_string_ostream OS(FlatCmdLine);
  776. bool PrintedOneArg = false;
  777. if (!StringRef(Args[0]).contains("-cc1")) {
  778. llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
  779. PrintedOneArg = true;
  780. }
  781. for (unsigned i = 0; i < Args.size(); i++) {
  782. StringRef Arg = Args[i];
  783. if (Arg.empty())
  784. continue;
  785. if (Arg == "-main-file-name" || Arg == "-o") {
  786. i++; // Skip this argument and next one.
  787. continue;
  788. }
  789. if (Arg.startswith("-object-file-name") || Arg == MainFilename)
  790. continue;
  791. if (PrintedOneArg)
  792. OS << " ";
  793. llvm::sys::printArg(OS, Arg, /*Quote=*/true);
  794. PrintedOneArg = true;
  795. }
  796. OS.flush();
  797. return FlatCmdLine;
  798. }
  799. void CodeViewDebug::emitBuildInfo() {
  800. // First, make LF_BUILDINFO. It's a sequence of strings with various bits of
  801. // build info. The known prefix is:
  802. // - Absolute path of current directory
  803. // - Compiler path
  804. // - Main source file path, relative to CWD or absolute
  805. // - Type server PDB file
  806. // - Canonical compiler command line
  807. // If frontend and backend compilation are separated (think llc or LTO), it's
  808. // not clear if the compiler path should refer to the executable for the
  809. // frontend or the backend. Leave it blank for now.
  810. TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
  811. NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
  812. const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
  813. const auto *CU = cast<DICompileUnit>(Node);
  814. const DIFile *MainSourceFile = CU->getFile();
  815. BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
  816. getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());
  817. BuildInfoArgs[BuildInfoRecord::SourceFile] =
  818. getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());
  819. // FIXME: PDB is intentionally blank unless we implement /Zi type servers.
  820. BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
  821. getStringIdTypeIdx(TypeTable, "");
  822. if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
  823. BuildInfoArgs[BuildInfoRecord::BuildTool] =
  824. getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
  825. BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
  826. TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
  827. MainSourceFile->getFilename()));
  828. }
  829. BuildInfoRecord BIR(BuildInfoArgs);
  830. TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
  831. // Make a new .debug$S subsection for the S_BUILDINFO record, which points
  832. // from the module symbols into the type stream.
  833. MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
  834. MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);
  835. OS.AddComment("LF_BUILDINFO index");
  836. OS.emitInt32(BuildInfoIndex.getIndex());
  837. endSymbolRecord(BIEnd);
  838. endCVSubsection(BISubsecEnd);
  839. }
  840. void CodeViewDebug::emitInlineeLinesSubsection() {
  841. if (InlinedSubprograms.empty())
  842. return;
  843. OS.AddComment("Inlinee lines subsection");
  844. MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
  845. // We emit the checksum info for files. This is used by debuggers to
  846. // determine if a pdb matches the source before loading it. Visual Studio,
  847. // for instance, will display a warning that the breakpoints are not valid if
  848. // the pdb does not match the source.
  849. OS.AddComment("Inlinee lines signature");
  850. OS.emitInt32(unsigned(InlineeLinesSignature::Normal));
  851. for (const DISubprogram *SP : InlinedSubprograms) {
  852. assert(TypeIndices.count({SP, nullptr}));
  853. TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
  854. OS.AddBlankLine();
  855. unsigned FileId = maybeRecordFile(SP->getFile());
  856. OS.AddComment("Inlined function " + SP->getName() + " starts at " +
  857. SP->getFilename() + Twine(':') + Twine(SP->getLine()));
  858. OS.AddBlankLine();
  859. OS.AddComment("Type index of inlined function");
  860. OS.emitInt32(InlineeIdx.getIndex());
  861. OS.AddComment("Offset into filechecksum table");
  862. OS.emitCVFileChecksumOffsetDirective(FileId);
  863. OS.AddComment("Starting line number");
  864. OS.emitInt32(SP->getLine());
  865. }
  866. endCVSubsection(InlineEnd);
  867. }
  868. void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
  869. const DILocation *InlinedAt,
  870. const InlineSite &Site) {
  871. assert(TypeIndices.count({Site.Inlinee, nullptr}));
  872. TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
  873. // SymbolRecord
  874. MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);
  875. OS.AddComment("PtrParent");
  876. OS.emitInt32(0);
  877. OS.AddComment("PtrEnd");
  878. OS.emitInt32(0);
  879. OS.AddComment("Inlinee type index");
  880. OS.emitInt32(InlineeIdx.getIndex());
  881. unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
  882. unsigned StartLineNum = Site.Inlinee->getLine();
  883. OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
  884. FI.Begin, FI.End);
  885. endSymbolRecord(InlineEnd);
  886. emitLocalVariableList(FI, Site.InlinedLocals);
  887. // Recurse on child inlined call sites before closing the scope.
  888. for (const DILocation *ChildSite : Site.ChildSites) {
  889. auto I = FI.InlineSites.find(ChildSite);
  890. assert(I != FI.InlineSites.end() &&
  891. "child site not in function inline site map");
  892. emitInlinedCallSite(FI, ChildSite, I->second);
  893. }
  894. // Close the scope.
  895. emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);
  896. }
  897. void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
  898. // If we have a symbol, it may be in a section that is COMDAT. If so, find the
  899. // comdat key. A section may be comdat because of -ffunction-sections or
  900. // because it is comdat in the IR.
  901. MCSectionCOFF *GVSec =
  902. GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
  903. const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
  904. MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
  905. Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
  906. DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
  907. OS.SwitchSection(DebugSec);
  908. // Emit the magic version number if this is the first time we've switched to
  909. // this section.
  910. if (ComdatDebugSections.insert(DebugSec).second)
  911. emitCodeViewMagicVersion();
  912. }
  913. // Emit an S_THUNK32/S_END symbol pair for a thunk routine.
  914. // The only supported thunk ordinal is currently the standard type.
  915. void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,
  916. FunctionInfo &FI,
  917. const MCSymbol *Fn) {
  918. std::string FuncName =
  919. std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
  920. const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.
  921. OS.AddComment("Symbol subsection for " + Twine(FuncName));
  922. MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
  923. // Emit S_THUNK32
  924. MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);
  925. OS.AddComment("PtrParent");
  926. OS.emitInt32(0);
  927. OS.AddComment("PtrEnd");
  928. OS.emitInt32(0);
  929. OS.AddComment("PtrNext");
  930. OS.emitInt32(0);
  931. OS.AddComment("Thunk section relative address");
  932. OS.EmitCOFFSecRel32(Fn, /*Offset=*/0);
  933. OS.AddComment("Thunk section index");
  934. OS.EmitCOFFSectionIndex(Fn);
  935. OS.AddComment("Code size");
  936. OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);
  937. OS.AddComment("Ordinal");
  938. OS.emitInt8(unsigned(ordinal));
  939. OS.AddComment("Function name");
  940. emitNullTerminatedSymbolName(OS, FuncName);
  941. // Additional fields specific to the thunk ordinal would go here.
  942. endSymbolRecord(ThunkRecordEnd);
  943. // Local variables/inlined routines are purposely omitted here. The point of
  944. // marking this as a thunk is so Visual Studio will NOT stop in this routine.
  945. // Emit S_PROC_ID_END
  946. emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
  947. endCVSubsection(SymbolsEnd);
  948. }
  949. void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
  950. FunctionInfo &FI) {
  951. // For each function there is a separate subsection which holds the PC to
  952. // file:line table.
  953. const MCSymbol *Fn = Asm->getSymbol(GV);
  954. assert(Fn);
  955. // Switch to the to a comdat section, if appropriate.
  956. switchToDebugSectionForSymbol(Fn);
  957. std::string FuncName;
  958. auto *SP = GV->getSubprogram();
  959. assert(SP);
  960. setCurrentSubprogram(SP);
  961. if (SP->isThunk()) {
  962. emitDebugInfoForThunk(GV, FI, Fn);
  963. return;
  964. }
  965. // If we have a display name, build the fully qualified name by walking the
  966. // chain of scopes.
  967. if (!SP->getName().empty())
  968. FuncName = getFullyQualifiedName(SP->getScope(), SP->getName());
  969. // If our DISubprogram name is empty, use the mangled name.
  970. if (FuncName.empty())
  971. FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
  972. // Emit FPO data, but only on 32-bit x86. No other platforms use it.
  973. if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86)
  974. OS.EmitCVFPOData(Fn);
  975. // Emit a symbol subsection, required by VS2012+ to find function boundaries.
  976. OS.AddComment("Symbol subsection for " + Twine(FuncName));
  977. MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
  978. {
  979. SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID
  980. : SymbolKind::S_GPROC32_ID;
  981. MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);
  982. // These fields are filled in by tools like CVPACK which run after the fact.
  983. OS.AddComment("PtrParent");
  984. OS.emitInt32(0);
  985. OS.AddComment("PtrEnd");
  986. OS.emitInt32(0);
  987. OS.AddComment("PtrNext");
  988. OS.emitInt32(0);
  989. // This is the important bit that tells the debugger where the function
  990. // code is located and what's its size:
  991. OS.AddComment("Code size");
  992. OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
  993. OS.AddComment("Offset after prologue");
  994. OS.emitInt32(0);
  995. OS.AddComment("Offset before epilogue");
  996. OS.emitInt32(0);
  997. OS.AddComment("Function type index");
  998. OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex());
  999. OS.AddComment("Function section relative address");
  1000. OS.EmitCOFFSecRel32(Fn, /*Offset=*/0);
  1001. OS.AddComment("Function section index");
  1002. OS.EmitCOFFSectionIndex(Fn);
  1003. OS.AddComment("Flags");
  1004. OS.emitInt8(0);
  1005. // Emit the function display name as a null-terminated string.
  1006. OS.AddComment("Function name");
  1007. // Truncate the name so we won't overflow the record length field.
  1008. emitNullTerminatedSymbolName(OS, FuncName);
  1009. endSymbolRecord(ProcRecordEnd);
  1010. MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);
  1011. // Subtract out the CSR size since MSVC excludes that and we include it.
  1012. OS.AddComment("FrameSize");
  1013. OS.emitInt32(FI.FrameSize - FI.CSRSize);
  1014. OS.AddComment("Padding");
  1015. OS.emitInt32(0);
  1016. OS.AddComment("Offset of padding");
  1017. OS.emitInt32(0);
  1018. OS.AddComment("Bytes of callee saved registers");
  1019. OS.emitInt32(FI.CSRSize);
  1020. OS.AddComment("Exception handler offset");
  1021. OS.emitInt32(0);
  1022. OS.AddComment("Exception handler section");
  1023. OS.emitInt16(0);
  1024. OS.AddComment("Flags (defines frame register)");
  1025. OS.emitInt32(uint32_t(FI.FrameProcOpts));
  1026. endSymbolRecord(FrameProcEnd);
  1027. emitLocalVariableList(FI, FI.Locals);
  1028. emitGlobalVariableList(FI.Globals);
  1029. emitLexicalBlockList(FI.ChildBlocks, FI);
  1030. // Emit inlined call site information. Only emit functions inlined directly
  1031. // into the parent function. We'll emit the other sites recursively as part
  1032. // of their parent inline site.
  1033. for (const DILocation *InlinedAt : FI.ChildSites) {
  1034. auto I = FI.InlineSites.find(InlinedAt);
  1035. assert(I != FI.InlineSites.end() &&
  1036. "child site not in function inline site map");
  1037. emitInlinedCallSite(FI, InlinedAt, I->second);
  1038. }
  1039. for (auto Annot : FI.Annotations) {
  1040. MCSymbol *Label = Annot.first;
  1041. MDTuple *Strs = cast<MDTuple>(Annot.second);
  1042. MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);
  1043. OS.EmitCOFFSecRel32(Label, /*Offset=*/0);
  1044. // FIXME: Make sure we don't overflow the max record size.
  1045. OS.EmitCOFFSectionIndex(Label);
  1046. OS.emitInt16(Strs->getNumOperands());
  1047. for (Metadata *MD : Strs->operands()) {
  1048. // MDStrings are null terminated, so we can do EmitBytes and get the
  1049. // nice .asciz directive.
  1050. StringRef Str = cast<MDString>(MD)->getString();
  1051. assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
  1052. OS.emitBytes(StringRef(Str.data(), Str.size() + 1));
  1053. }
  1054. endSymbolRecord(AnnotEnd);
  1055. }
  1056. for (auto HeapAllocSite : FI.HeapAllocSites) {
  1057. const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
  1058. const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
  1059. const DIType *DITy = std::get<2>(HeapAllocSite);
  1060. MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);
  1061. OS.AddComment("Call site offset");
  1062. OS.EmitCOFFSecRel32(BeginLabel, /*Offset=*/0);
  1063. OS.AddComment("Call site section index");
  1064. OS.EmitCOFFSectionIndex(BeginLabel);
  1065. OS.AddComment("Call instruction length");
  1066. OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
  1067. OS.AddComment("Type index");
  1068. OS.emitInt32(getCompleteTypeIndex(DITy).getIndex());
  1069. endSymbolRecord(HeapAllocEnd);
  1070. }
  1071. if (SP != nullptr)
  1072. emitDebugInfoForUDTs(LocalUDTs);
  1073. // We're done with this function.
  1074. emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
  1075. }
  1076. endCVSubsection(SymbolsEnd);
  1077. // We have an assembler directive that takes care of the whole line table.
  1078. OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End);
  1079. }
  1080. CodeViewDebug::LocalVarDefRange
  1081. CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
  1082. LocalVarDefRange DR;
  1083. DR.InMemory = -1;
  1084. DR.DataOffset = Offset;
  1085. assert(DR.DataOffset == Offset && "truncation");
  1086. DR.IsSubfield = 0;
  1087. DR.StructOffset = 0;
  1088. DR.CVRegister = CVRegister;
  1089. return DR;
  1090. }
  1091. void CodeViewDebug::collectVariableInfoFromMFTable(
  1092. DenseSet<InlinedEntity> &Processed) {
  1093. const MachineFunction &MF = *Asm->MF;
  1094. const TargetSubtargetInfo &TSI = MF.getSubtarget();
  1095. const TargetFrameLowering *TFI = TSI.getFrameLowering();
  1096. const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
  1097. for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) {
  1098. if (!VI.Var)
  1099. continue;
  1100. assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
  1101. "Expected inlined-at fields to agree");
  1102. Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));
  1103. LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
  1104. // If variable scope is not found then skip this variable.
  1105. if (!Scope)
  1106. continue;
  1107. // If the variable has an attached offset expression, extract it.
  1108. // FIXME: Try to handle DW_OP_deref as well.
  1109. int64_t ExprOffset = 0;
  1110. bool Deref = false;
  1111. if (VI.Expr) {
  1112. // If there is one DW_OP_deref element, use offset of 0 and keep going.
  1113. if (VI.Expr->getNumElements() == 1 &&
  1114. VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref)
  1115. Deref = true;
  1116. else if (!VI.Expr->extractIfOffset(ExprOffset))
  1117. continue;
  1118. }
  1119. // Get the frame register used and the offset.
  1120. Register FrameReg;
  1121. StackOffset FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
  1122. uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
  1123. assert(!FrameOffset.getScalable() &&
  1124. "Frame offsets with a scalable component are not supported");
  1125. // Calculate the label ranges.
  1126. LocalVarDefRange DefRange =
  1127. createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset);
  1128. for (const InsnRange &Range : Scope->getRanges()) {
  1129. const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
  1130. const MCSymbol *End = getLabelAfterInsn(Range.second);
  1131. End = End ? End : Asm->getFunctionEnd();
  1132. DefRange.Ranges.emplace_back(Begin, End);
  1133. }
  1134. LocalVariable Var;
  1135. Var.DIVar = VI.Var;
  1136. Var.DefRanges.emplace_back(std::move(DefRange));
  1137. if (Deref)
  1138. Var.UseReferenceType = true;
  1139. recordLocalVariable(std::move(Var), Scope);
  1140. }
  1141. }
  1142. static bool canUseReferenceType(const DbgVariableLocation &Loc) {
  1143. return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
  1144. }
  1145. static bool needsReferenceType(const DbgVariableLocation &Loc) {
  1146. return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
  1147. }
  1148. void CodeViewDebug::calculateRanges(
  1149. LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) {
  1150. const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
  1151. // Calculate the definition ranges.
  1152. for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) {
  1153. const auto &Entry = *I;
  1154. if (!Entry.isDbgValue())
  1155. continue;
  1156. const MachineInstr *DVInst = Entry.getInstr();
  1157. assert(DVInst->isDebugValue() && "Invalid History entry");
  1158. // FIXME: Find a way to represent constant variables, since they are
  1159. // relatively common.
  1160. Optional<DbgVariableLocation> Location =
  1161. DbgVariableLocation::extractFromMachineInstruction(*DVInst);
  1162. if (!Location)
  1163. continue;
  1164. // CodeView can only express variables in register and variables in memory
  1165. // at a constant offset from a register. However, for variables passed
  1166. // indirectly by pointer, it is common for that pointer to be spilled to a
  1167. // stack location. For the special case of one offseted load followed by a
  1168. // zero offset load (a pointer spilled to the stack), we change the type of
  1169. // the local variable from a value type to a reference type. This tricks the
  1170. // debugger into doing the load for us.
  1171. if (Var.UseReferenceType) {
  1172. // We're using a reference type. Drop the last zero offset load.
  1173. if (canUseReferenceType(*Location))
  1174. Location->LoadChain.pop_back();
  1175. else
  1176. continue;
  1177. } else if (needsReferenceType(*Location)) {
  1178. // This location can't be expressed without switching to a reference type.
  1179. // Start over using that.
  1180. Var.UseReferenceType = true;
  1181. Var.DefRanges.clear();
  1182. calculateRanges(Var, Entries);
  1183. return;
  1184. }
  1185. // We can only handle a register or an offseted load of a register.
  1186. if (Location->Register == 0 || Location->LoadChain.size() > 1)
  1187. continue;
  1188. {
  1189. LocalVarDefRange DR;
  1190. DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
  1191. DR.InMemory = !Location->LoadChain.empty();
  1192. DR.DataOffset =
  1193. !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
  1194. if (Location->FragmentInfo) {
  1195. DR.IsSubfield = true;
  1196. DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
  1197. } else {
  1198. DR.IsSubfield = false;
  1199. DR.StructOffset = 0;
  1200. }
  1201. if (Var.DefRanges.empty() ||
  1202. Var.DefRanges.back().isDifferentLocation(DR)) {
  1203. Var.DefRanges.emplace_back(std::move(DR));
  1204. }
  1205. }
  1206. // Compute the label range.
  1207. const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr());
  1208. const MCSymbol *End;
  1209. if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) {
  1210. auto &EndingEntry = Entries[Entry.getEndIndex()];
  1211. End = EndingEntry.isDbgValue()
  1212. ? getLabelBeforeInsn(EndingEntry.getInstr())
  1213. : getLabelAfterInsn(EndingEntry.getInstr());
  1214. } else
  1215. End = Asm->getFunctionEnd();
  1216. // If the last range end is our begin, just extend the last range.
  1217. // Otherwise make a new range.
  1218. SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =
  1219. Var.DefRanges.back().Ranges;
  1220. if (!R.empty() && R.back().second == Begin)
  1221. R.back().second = End;
  1222. else
  1223. R.emplace_back(Begin, End);
  1224. // FIXME: Do more range combining.
  1225. }
  1226. }
  1227. void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
  1228. DenseSet<InlinedEntity> Processed;
  1229. // Grab the variable info that was squirreled away in the MMI side-table.
  1230. collectVariableInfoFromMFTable(Processed);
  1231. for (const auto &I : DbgValues) {
  1232. InlinedEntity IV = I.first;
  1233. if (Processed.count(IV))
  1234. continue;
  1235. const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);
  1236. const DILocation *InlinedAt = IV.second;
  1237. // Instruction ranges, specifying where IV is accessible.
  1238. const auto &Entries = I.second;
  1239. LexicalScope *Scope = nullptr;
  1240. if (InlinedAt)
  1241. Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
  1242. else
  1243. Scope = LScopes.findLexicalScope(DIVar->getScope());
  1244. // If variable scope is not found then skip this variable.
  1245. if (!Scope)
  1246. continue;
  1247. LocalVariable Var;
  1248. Var.DIVar = DIVar;
  1249. calculateRanges(Var, Entries);
  1250. recordLocalVariable(std::move(Var), Scope);
  1251. }
  1252. }
  1253. void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
  1254. const TargetSubtargetInfo &TSI = MF->getSubtarget();
  1255. const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
  1256. const MachineFrameInfo &MFI = MF->getFrameInfo();
  1257. const Function &GV = MF->getFunction();
  1258. auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()});
  1259. assert(Insertion.second && "function already has info");
  1260. CurFn = Insertion.first->second.get();
  1261. CurFn->FuncId = NextFuncId++;
  1262. CurFn->Begin = Asm->getFunctionBegin();
  1263. // The S_FRAMEPROC record reports the stack size, and how many bytes of
  1264. // callee-saved registers were used. For targets that don't use a PUSH
  1265. // instruction (AArch64), this will be zero.
  1266. CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();
  1267. CurFn->FrameSize = MFI.getStackSize();
  1268. CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();
  1269. CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF);
  1270. // For this function S_FRAMEPROC record, figure out which codeview register
  1271. // will be the frame pointer.
  1272. CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.
  1273. CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.
  1274. if (CurFn->FrameSize > 0) {
  1275. if (!TSI.getFrameLowering()->hasFP(*MF)) {
  1276. CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
  1277. CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;
  1278. } else {
  1279. // If there is an FP, parameters are always relative to it.
  1280. CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;
  1281. if (CurFn->HasStackRealignment) {
  1282. // If the stack needs realignment, locals are relative to SP or VFRAME.
  1283. CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
  1284. } else {
  1285. // Otherwise, locals are relative to EBP, and we probably have VLAs or
  1286. // other stack adjustments.
  1287. CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;
  1288. }
  1289. }
  1290. }
  1291. // Compute other frame procedure options.
  1292. FrameProcedureOptions FPO = FrameProcedureOptions::None;
  1293. if (MFI.hasVarSizedObjects())
  1294. FPO |= FrameProcedureOptions::HasAlloca;
  1295. if (MF->exposesReturnsTwice())
  1296. FPO |= FrameProcedureOptions::HasSetJmp;
  1297. // FIXME: Set HasLongJmp if we ever track that info.
  1298. if (MF->hasInlineAsm())
  1299. FPO |= FrameProcedureOptions::HasInlineAssembly;
  1300. if (GV.hasPersonalityFn()) {
  1301. if (isAsynchronousEHPersonality(
  1302. classifyEHPersonality(GV.getPersonalityFn())))
  1303. FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;
  1304. else
  1305. FPO |= FrameProcedureOptions::HasExceptionHandling;
  1306. }
  1307. if (GV.hasFnAttribute(Attribute::InlineHint))
  1308. FPO |= FrameProcedureOptions::MarkedInline;
  1309. if (GV.hasFnAttribute(Attribute::Naked))
  1310. FPO |= FrameProcedureOptions::Naked;
  1311. if (MFI.hasStackProtectorIndex())
  1312. FPO |= FrameProcedureOptions::SecurityChecks;
  1313. FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
  1314. FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
  1315. if (Asm->TM.getOptLevel() != CodeGenOpt::None &&
  1316. !GV.hasOptSize() && !GV.hasOptNone())
  1317. FPO |= FrameProcedureOptions::OptimizedForSpeed;
  1318. if (GV.hasProfileData()) {
  1319. FPO |= FrameProcedureOptions::ValidProfileCounts;
  1320. FPO |= FrameProcedureOptions::ProfileGuidedOptimization;
  1321. }
  1322. // FIXME: Set GuardCfg when it is implemented.
  1323. CurFn->FrameProcOpts = FPO;
  1324. OS.EmitCVFuncIdDirective(CurFn->FuncId);
  1325. // Find the end of the function prolog. First known non-DBG_VALUE and
  1326. // non-frame setup location marks the beginning of the function body.
  1327. // FIXME: is there a simpler a way to do this? Can we just search
  1328. // for the first instruction of the function, not the last of the prolog?
  1329. DebugLoc PrologEndLoc;
  1330. bool EmptyPrologue = true;
  1331. for (const auto &MBB : *MF) {
  1332. for (const auto &MI : MBB) {
  1333. if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
  1334. MI.getDebugLoc()) {
  1335. PrologEndLoc = MI.getDebugLoc();
  1336. break;
  1337. } else if (!MI.isMetaInstruction()) {
  1338. EmptyPrologue = false;
  1339. }
  1340. }
  1341. }
  1342. // Record beginning of function if we have a non-empty prologue.
  1343. if (PrologEndLoc && !EmptyPrologue) {
  1344. DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
  1345. maybeRecordLocation(FnStartDL, MF);
  1346. }
  1347. // Find heap alloc sites and emit labels around them.
  1348. for (const auto &MBB : *MF) {
  1349. for (const auto &MI : MBB) {
  1350. if (MI.getHeapAllocMarker()) {
  1351. requestLabelBeforeInsn(&MI);
  1352. requestLabelAfterInsn(&MI);
  1353. }
  1354. }
  1355. }
  1356. }
  1357. static bool shouldEmitUdt(const DIType *T) {
  1358. if (!T)
  1359. return false;
  1360. // MSVC does not emit UDTs for typedefs that are scoped to classes.
  1361. if (T->getTag() == dwarf::DW_TAG_typedef) {
  1362. if (DIScope *Scope = T->getScope()) {
  1363. switch (Scope->getTag()) {
  1364. case dwarf::DW_TAG_structure_type:
  1365. case dwarf::DW_TAG_class_type:
  1366. case dwarf::DW_TAG_union_type:
  1367. return false;
  1368. default:
  1369. // do nothing.
  1370. ;
  1371. }
  1372. }
  1373. }
  1374. while (true) {
  1375. if (!T || T->isForwardDecl())
  1376. return false;
  1377. const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
  1378. if (!DT)
  1379. return true;
  1380. T = DT->getBaseType();
  1381. }
  1382. return true;
  1383. }
  1384. void CodeViewDebug::addToUDTs(const DIType *Ty) {
  1385. // Don't record empty UDTs.
  1386. if (Ty->getName().empty())
  1387. return;
  1388. if (!shouldEmitUdt(Ty))
  1389. return;
  1390. SmallVector<StringRef, 5> ParentScopeNames;
  1391. const DISubprogram *ClosestSubprogram =
  1392. collectParentScopeNames(Ty->getScope(), ParentScopeNames);
  1393. std::string FullyQualifiedName =
  1394. formatNestedName(ParentScopeNames, getPrettyScopeName(Ty));
  1395. if (ClosestSubprogram == nullptr) {
  1396. GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
  1397. } else if (ClosestSubprogram == CurrentSubprogram) {
  1398. LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
  1399. }
  1400. // TODO: What if the ClosestSubprogram is neither null or the current
  1401. // subprogram? Currently, the UDT just gets dropped on the floor.
  1402. //
  1403. // The current behavior is not desirable. To get maximal fidelity, we would
  1404. // need to perform all type translation before beginning emission of .debug$S
  1405. // and then make LocalUDTs a member of FunctionInfo
  1406. }
  1407. TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
  1408. // Generic dispatch for lowering an unknown type.
  1409. switch (Ty->getTag()) {
  1410. case dwarf::DW_TAG_array_type:
  1411. return lowerTypeArray(cast<DICompositeType>(Ty));
  1412. case dwarf::DW_TAG_typedef:
  1413. return lowerTypeAlias(cast<DIDerivedType>(Ty));
  1414. case dwarf::DW_TAG_base_type:
  1415. return lowerTypeBasic(cast<DIBasicType>(Ty));
  1416. case dwarf::DW_TAG_pointer_type:
  1417. if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
  1418. return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
  1419. LLVM_FALLTHROUGH;
  1420. case dwarf::DW_TAG_reference_type:
  1421. case dwarf::DW_TAG_rvalue_reference_type:
  1422. return lowerTypePointer(cast<DIDerivedType>(Ty));
  1423. case dwarf::DW_TAG_ptr_to_member_type:
  1424. return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
  1425. case dwarf::DW_TAG_restrict_type:
  1426. case dwarf::DW_TAG_const_type:
  1427. case dwarf::DW_TAG_volatile_type:
  1428. // TODO: add support for DW_TAG_atomic_type here
  1429. return lowerTypeModifier(cast<DIDerivedType>(Ty));
  1430. case dwarf::DW_TAG_subroutine_type:
  1431. if (ClassTy) {
  1432. // The member function type of a member function pointer has no
  1433. // ThisAdjustment.
  1434. return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
  1435. /*ThisAdjustment=*/0,
  1436. /*IsStaticMethod=*/false);
  1437. }
  1438. return lowerTypeFunction(cast<DISubroutineType>(Ty));
  1439. case dwarf::DW_TAG_enumeration_type:
  1440. return lowerTypeEnum(cast<DICompositeType>(Ty));
  1441. case dwarf::DW_TAG_class_type:
  1442. case dwarf::DW_TAG_structure_type:
  1443. return lowerTypeClass(cast<DICompositeType>(Ty));
  1444. case dwarf::DW_TAG_union_type:
  1445. return lowerTypeUnion(cast<DICompositeType>(Ty));
  1446. case dwarf::DW_TAG_string_type:
  1447. return lowerTypeString(cast<DIStringType>(Ty));
  1448. case dwarf::DW_TAG_unspecified_type:
  1449. if (Ty->getName() == "decltype(nullptr)")
  1450. return TypeIndex::NullptrT();
  1451. return TypeIndex::None();
  1452. default:
  1453. // Use the null type index.
  1454. return TypeIndex();
  1455. }
  1456. }
  1457. TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
  1458. TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType());
  1459. StringRef TypeName = Ty->getName();
  1460. addToUDTs(Ty);
  1461. if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
  1462. TypeName == "HRESULT")
  1463. return TypeIndex(SimpleTypeKind::HResult);
  1464. if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
  1465. TypeName == "wchar_t")
  1466. return TypeIndex(SimpleTypeKind::WideCharacter);
  1467. return UnderlyingTypeIndex;
  1468. }
  1469. TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
  1470. const DIType *ElementType = Ty->getBaseType();
  1471. TypeIndex ElementTypeIndex = getTypeIndex(ElementType);
  1472. // IndexType is size_t, which depends on the bitness of the target.
  1473. TypeIndex IndexType = getPointerSizeInBytes() == 8
  1474. ? TypeIndex(SimpleTypeKind::UInt64Quad)
  1475. : TypeIndex(SimpleTypeKind::UInt32Long);
  1476. uint64_t ElementSize = getBaseTypeSize(ElementType) / 8;
  1477. // Add subranges to array type.
  1478. DINodeArray Elements = Ty->getElements();
  1479. for (int i = Elements.size() - 1; i >= 0; --i) {
  1480. const DINode *Element = Elements[i];
  1481. assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
  1482. const DISubrange *Subrange = cast<DISubrange>(Element);
  1483. int64_t Count = -1;
  1484. // If Subrange has a Count field, use it.
  1485. // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),
  1486. // where lowerbound is from the LowerBound field of the Subrange,
  1487. // or the language default lowerbound if that field is unspecified.
  1488. if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt *>())
  1489. Count = CI->getSExtValue();
  1490. else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt *>()) {
  1491. // Fortran uses 1 as the default lowerbound; other languages use 0.
  1492. int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;
  1493. auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>();
  1494. Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;
  1495. Count = UI->getSExtValue() - Lowerbound + 1;
  1496. }
  1497. // Forward declarations of arrays without a size and VLAs use a count of -1.
  1498. // Emit a count of zero in these cases to match what MSVC does for arrays
  1499. // without a size. MSVC doesn't support VLAs, so it's not clear what we
  1500. // should do for them even if we could distinguish them.
  1501. if (Count == -1)
  1502. Count = 0;
  1503. // Update the element size and element type index for subsequent subranges.
  1504. ElementSize *= Count;
  1505. // If this is the outermost array, use the size from the array. It will be
  1506. // more accurate if we had a VLA or an incomplete element type size.
  1507. uint64_t ArraySize =
  1508. (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
  1509. StringRef Name = (i == 0) ? Ty->getName() : "";
  1510. ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
  1511. ElementTypeIndex = TypeTable.writeLeafType(AR);
  1512. }
  1513. return ElementTypeIndex;
  1514. }
  1515. // This function lowers a Fortran character type (DIStringType).
  1516. // Note that it handles only the character*n variant (using SizeInBits
  1517. // field in DIString to describe the type size) at the moment.
  1518. // Other variants (leveraging the StringLength and StringLengthExp
  1519. // fields in DIStringType) remain TBD.
  1520. TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {
  1521. TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);
  1522. uint64_t ArraySize = Ty->getSizeInBits() >> 3;
  1523. StringRef Name = Ty->getName();
  1524. // IndexType is size_t, which depends on the bitness of the target.
  1525. TypeIndex IndexType = getPointerSizeInBytes() == 8
  1526. ? TypeIndex(SimpleTypeKind::UInt64Quad)
  1527. : TypeIndex(SimpleTypeKind::UInt32Long);
  1528. // Create a type of character array of ArraySize.
  1529. ArrayRecord AR(CharType, IndexType, ArraySize, Name);
  1530. return TypeTable.writeLeafType(AR);
  1531. }
  1532. TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
  1533. TypeIndex Index;
  1534. dwarf::TypeKind Kind;
  1535. uint32_t ByteSize;
  1536. Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
  1537. ByteSize = Ty->getSizeInBits() / 8;
  1538. SimpleTypeKind STK = SimpleTypeKind::None;
  1539. switch (Kind) {
  1540. case dwarf::DW_ATE_address:
  1541. // FIXME: Translate
  1542. break;
  1543. case dwarf::DW_ATE_boolean:
  1544. switch (ByteSize) {
  1545. case 1: STK = SimpleTypeKind::Boolean8; break;
  1546. case 2: STK = SimpleTypeKind::Boolean16; break;
  1547. case 4: STK = SimpleTypeKind::Boolean32; break;
  1548. case 8: STK = SimpleTypeKind::Boolean64; break;
  1549. case 16: STK = SimpleTypeKind::Boolean128; break;
  1550. }
  1551. break;
  1552. case dwarf::DW_ATE_complex_float:
  1553. switch (ByteSize) {
  1554. case 2: STK = SimpleTypeKind::Complex16; break;
  1555. case 4: STK = SimpleTypeKind::Complex32; break;
  1556. case 8: STK = SimpleTypeKind::Complex64; break;
  1557. case 10: STK = SimpleTypeKind::Complex80; break;
  1558. case 16: STK = SimpleTypeKind::Complex128; break;
  1559. }
  1560. break;
  1561. case dwarf::DW_ATE_float:
  1562. switch (ByteSize) {
  1563. case 2: STK = SimpleTypeKind::Float16; break;
  1564. case 4: STK = SimpleTypeKind::Float32; break;
  1565. case 6: STK = SimpleTypeKind::Float48; break;
  1566. case 8: STK = SimpleTypeKind::Float64; break;
  1567. case 10: STK = SimpleTypeKind::Float80; break;
  1568. case 16: STK = SimpleTypeKind::Float128; break;
  1569. }
  1570. break;
  1571. case dwarf::DW_ATE_signed:
  1572. switch (ByteSize) {
  1573. case 1: STK = SimpleTypeKind::SignedCharacter; break;
  1574. case 2: STK = SimpleTypeKind::Int16Short; break;
  1575. case 4: STK = SimpleTypeKind::Int32; break;
  1576. case 8: STK = SimpleTypeKind::Int64Quad; break;
  1577. case 16: STK = SimpleTypeKind::Int128Oct; break;
  1578. }
  1579. break;
  1580. case dwarf::DW_ATE_unsigned:
  1581. switch (ByteSize) {
  1582. case 1: STK = SimpleTypeKind::UnsignedCharacter; break;
  1583. case 2: STK = SimpleTypeKind::UInt16Short; break;
  1584. case 4: STK = SimpleTypeKind::UInt32; break;
  1585. case 8: STK = SimpleTypeKind::UInt64Quad; break;
  1586. case 16: STK = SimpleTypeKind::UInt128Oct; break;
  1587. }
  1588. break;
  1589. case dwarf::DW_ATE_UTF:
  1590. switch (ByteSize) {
  1591. case 2: STK = SimpleTypeKind::Character16; break;
  1592. case 4: STK = SimpleTypeKind::Character32; break;
  1593. }
  1594. break;
  1595. case dwarf::DW_ATE_signed_char:
  1596. if (ByteSize == 1)
  1597. STK = SimpleTypeKind::SignedCharacter;
  1598. break;
  1599. case dwarf::DW_ATE_unsigned_char:
  1600. if (ByteSize == 1)
  1601. STK = SimpleTypeKind::UnsignedCharacter;
  1602. break;
  1603. default:
  1604. break;
  1605. }
  1606. // Apply some fixups based on the source-level type name.
  1607. // Include some amount of canonicalization from an old naming scheme Clang
  1608. // used to use for integer types (in an outdated effort to be compatible with
  1609. // GCC's debug info/GDB's behavior, which has since been addressed).
  1610. if (STK == SimpleTypeKind::Int32 &&
  1611. (Ty->getName() == "long int" || Ty->getName() == "long"))
  1612. STK = SimpleTypeKind::Int32Long;
  1613. if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" ||
  1614. Ty->getName() == "unsigned long"))
  1615. STK = SimpleTypeKind::UInt32Long;
  1616. if (STK == SimpleTypeKind::UInt16Short &&
  1617. (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
  1618. STK = SimpleTypeKind::WideCharacter;
  1619. if ((STK == SimpleTypeKind::SignedCharacter ||
  1620. STK == SimpleTypeKind::UnsignedCharacter) &&
  1621. Ty->getName() == "char")
  1622. STK = SimpleTypeKind::NarrowCharacter;
  1623. return TypeIndex(STK);
  1624. }
  1625. TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,
  1626. PointerOptions PO) {
  1627. TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
  1628. // Pointers to simple types without any options can use SimpleTypeMode, rather
  1629. // than having a dedicated pointer type record.
  1630. if (PointeeTI.isSimple() && PO == PointerOptions::None &&
  1631. PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
  1632. Ty->getTag() == dwarf::DW_TAG_pointer_type) {
  1633. SimpleTypeMode Mode = Ty->getSizeInBits() == 64
  1634. ? SimpleTypeMode::NearPointer64
  1635. : SimpleTypeMode::NearPointer32;
  1636. return TypeIndex(PointeeTI.getSimpleKind(), Mode);
  1637. }
  1638. PointerKind PK =
  1639. Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
  1640. PointerMode PM = PointerMode::Pointer;
  1641. switch (Ty->getTag()) {
  1642. default: llvm_unreachable("not a pointer tag type");
  1643. case dwarf::DW_TAG_pointer_type:
  1644. PM = PointerMode::Pointer;
  1645. break;
  1646. case dwarf::DW_TAG_reference_type:
  1647. PM = PointerMode::LValueReference;
  1648. break;
  1649. case dwarf::DW_TAG_rvalue_reference_type:
  1650. PM = PointerMode::RValueReference;
  1651. break;
  1652. }
  1653. if (Ty->isObjectPointer())
  1654. PO |= PointerOptions::Const;
  1655. PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
  1656. return TypeTable.writeLeafType(PR);
  1657. }
  1658. static PointerToMemberRepresentation
  1659. translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
  1660. // SizeInBytes being zero generally implies that the member pointer type was
  1661. // incomplete, which can happen if it is part of a function prototype. In this
  1662. // case, use the unknown model instead of the general model.
  1663. if (IsPMF) {
  1664. switch (Flags & DINode::FlagPtrToMemberRep) {
  1665. case 0:
  1666. return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
  1667. : PointerToMemberRepresentation::GeneralFunction;
  1668. case DINode::FlagSingleInheritance:
  1669. return PointerToMemberRepresentation::SingleInheritanceFunction;
  1670. case DINode::FlagMultipleInheritance:
  1671. return PointerToMemberRepresentation::MultipleInheritanceFunction;
  1672. case DINode::FlagVirtualInheritance:
  1673. return PointerToMemberRepresentation::VirtualInheritanceFunction;
  1674. }
  1675. } else {
  1676. switch (Flags & DINode::FlagPtrToMemberRep) {
  1677. case 0:
  1678. return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
  1679. : PointerToMemberRepresentation::GeneralData;
  1680. case DINode::FlagSingleInheritance:
  1681. return PointerToMemberRepresentation::SingleInheritanceData;
  1682. case DINode::FlagMultipleInheritance:
  1683. return PointerToMemberRepresentation::MultipleInheritanceData;
  1684. case DINode::FlagVirtualInheritance:
  1685. return PointerToMemberRepresentation::VirtualInheritanceData;
  1686. }
  1687. }
  1688. llvm_unreachable("invalid ptr to member representation");
  1689. }
  1690. TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,
  1691. PointerOptions PO) {
  1692. assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
  1693. bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
  1694. TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
  1695. TypeIndex PointeeTI =
  1696. getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr);
  1697. PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
  1698. : PointerKind::Near32;
  1699. PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
  1700. : PointerMode::PointerToDataMember;
  1701. assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
  1702. uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
  1703. MemberPointerInfo MPI(
  1704. ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
  1705. PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
  1706. return TypeTable.writeLeafType(PR);
  1707. }
  1708. /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
  1709. /// have a translation, use the NearC convention.
  1710. static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
  1711. switch (DwarfCC) {
  1712. case dwarf::DW_CC_normal: return CallingConvention::NearC;
  1713. case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
  1714. case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall;
  1715. case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall;
  1716. case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal;
  1717. case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector;
  1718. }
  1719. return CallingConvention::NearC;
  1720. }
  1721. TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
  1722. ModifierOptions Mods = ModifierOptions::None;
  1723. PointerOptions PO = PointerOptions::None;
  1724. bool IsModifier = true;
  1725. const DIType *BaseTy = Ty;
  1726. while (IsModifier && BaseTy) {
  1727. // FIXME: Need to add DWARF tags for __unaligned and _Atomic
  1728. switch (BaseTy->getTag()) {
  1729. case dwarf::DW_TAG_const_type:
  1730. Mods |= ModifierOptions::Const;
  1731. PO |= PointerOptions::Const;
  1732. break;
  1733. case dwarf::DW_TAG_volatile_type:
  1734. Mods |= ModifierOptions::Volatile;
  1735. PO |= PointerOptions::Volatile;
  1736. break;
  1737. case dwarf::DW_TAG_restrict_type:
  1738. // Only pointer types be marked with __restrict. There is no known flag
  1739. // for __restrict in LF_MODIFIER records.
  1740. PO |= PointerOptions::Restrict;
  1741. break;
  1742. default:
  1743. IsModifier = false;
  1744. break;
  1745. }
  1746. if (IsModifier)
  1747. BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType();
  1748. }
  1749. // Check if the inner type will use an LF_POINTER record. If so, the
  1750. // qualifiers will go in the LF_POINTER record. This comes up for types like
  1751. // 'int *const' and 'int *__restrict', not the more common cases like 'const
  1752. // char *'.
  1753. if (BaseTy) {
  1754. switch (BaseTy->getTag()) {
  1755. case dwarf::DW_TAG_pointer_type:
  1756. case dwarf::DW_TAG_reference_type:
  1757. case dwarf::DW_TAG_rvalue_reference_type:
  1758. return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);
  1759. case dwarf::DW_TAG_ptr_to_member_type:
  1760. return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);
  1761. default:
  1762. break;
  1763. }
  1764. }
  1765. TypeIndex ModifiedTI = getTypeIndex(BaseTy);
  1766. // Return the base type index if there aren't any modifiers. For example, the
  1767. // metadata could contain restrict wrappers around non-pointer types.
  1768. if (Mods == ModifierOptions::None)
  1769. return ModifiedTI;
  1770. ModifierRecord MR(ModifiedTI, Mods);
  1771. return TypeTable.writeLeafType(MR);
  1772. }
  1773. TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
  1774. SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
  1775. for (const DIType *ArgType : Ty->getTypeArray())
  1776. ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType));
  1777. // MSVC uses type none for variadic argument.
  1778. if (ReturnAndArgTypeIndices.size() > 1 &&
  1779. ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {
  1780. ReturnAndArgTypeIndices.back() = TypeIndex::None();
  1781. }
  1782. TypeIndex ReturnTypeIndex = TypeIndex::Void();
  1783. ArrayRef<TypeIndex> ArgTypeIndices = None;
  1784. if (!ReturnAndArgTypeIndices.empty()) {
  1785. auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
  1786. ReturnTypeIndex = ReturnAndArgTypesRef.front();
  1787. ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
  1788. }
  1789. ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
  1790. TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
  1791. CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
  1792. FunctionOptions FO = getFunctionOptions(Ty);
  1793. ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),
  1794. ArgListIndex);
  1795. return TypeTable.writeLeafType(Procedure);
  1796. }
  1797. TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
  1798. const DIType *ClassTy,
  1799. int ThisAdjustment,
  1800. bool IsStaticMethod,
  1801. FunctionOptions FO) {
  1802. // Lower the containing class type.
  1803. TypeIndex ClassType = getTypeIndex(ClassTy);
  1804. DITypeRefArray ReturnAndArgs = Ty->getTypeArray();
  1805. unsigned Index = 0;
  1806. SmallVector<TypeIndex, 8> ArgTypeIndices;
  1807. TypeIndex ReturnTypeIndex = TypeIndex::Void();
  1808. if (ReturnAndArgs.size() > Index) {
  1809. ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);
  1810. }
  1811. // If the first argument is a pointer type and this isn't a static method,
  1812. // treat it as the special 'this' parameter, which is encoded separately from
  1813. // the arguments.
  1814. TypeIndex ThisTypeIndex;
  1815. if (!IsStaticMethod && ReturnAndArgs.size() > Index) {
  1816. if (const DIDerivedType *PtrTy =
  1817. dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) {
  1818. if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
  1819. ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);
  1820. Index++;
  1821. }
  1822. }
  1823. }
  1824. while (Index < ReturnAndArgs.size())
  1825. ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));
  1826. // MSVC uses type none for variadic argument.
  1827. if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())
  1828. ArgTypeIndices.back() = TypeIndex::None();
  1829. ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
  1830. TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
  1831. CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
  1832. MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,
  1833. ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);
  1834. return TypeTable.writeLeafType(MFR);
  1835. }
  1836. TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
  1837. unsigned VSlotCount =
  1838. Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
  1839. SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
  1840. VFTableShapeRecord VFTSR(Slots);
  1841. return TypeTable.writeLeafType(VFTSR);
  1842. }
  1843. static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
  1844. switch (Flags & DINode::FlagAccessibility) {
  1845. case DINode::FlagPrivate: return MemberAccess::Private;
  1846. case DINode::FlagPublic: return MemberAccess::Public;
  1847. case DINode::FlagProtected: return MemberAccess::Protected;
  1848. case 0:
  1849. // If there was no explicit access control, provide the default for the tag.
  1850. return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
  1851. : MemberAccess::Public;
  1852. }
  1853. llvm_unreachable("access flags are exclusive");
  1854. }
  1855. static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
  1856. if (SP->isArtificial())
  1857. return MethodOptions::CompilerGenerated;
  1858. // FIXME: Handle other MethodOptions.
  1859. return MethodOptions::None;
  1860. }
  1861. static MethodKind translateMethodKindFlags(const DISubprogram *SP,
  1862. bool Introduced) {
  1863. if (SP->getFlags() & DINode::FlagStaticMember)
  1864. return MethodKind::Static;
  1865. switch (SP->getVirtuality()) {
  1866. case dwarf::DW_VIRTUALITY_none:
  1867. break;
  1868. case dwarf::DW_VIRTUALITY_virtual:
  1869. return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
  1870. case dwarf::DW_VIRTUALITY_pure_virtual:
  1871. return Introduced ? MethodKind::PureIntroducingVirtual
  1872. : MethodKind::PureVirtual;
  1873. default:
  1874. llvm_unreachable("unhandled virtuality case");
  1875. }
  1876. return MethodKind::Vanilla;
  1877. }
  1878. static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
  1879. switch (Ty->getTag()) {
  1880. case dwarf::DW_TAG_class_type:
  1881. return TypeRecordKind::Class;
  1882. case dwarf::DW_TAG_structure_type:
  1883. return TypeRecordKind::Struct;
  1884. default:
  1885. llvm_unreachable("unexpected tag");
  1886. }
  1887. }
  1888. /// Return ClassOptions that should be present on both the forward declaration
  1889. /// and the defintion of a tag type.
  1890. static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
  1891. ClassOptions CO = ClassOptions::None;
  1892. // MSVC always sets this flag, even for local types. Clang doesn't always
  1893. // appear to give every type a linkage name, which may be problematic for us.
  1894. // FIXME: Investigate the consequences of not following them here.
  1895. if (!Ty->getIdentifier().empty())
  1896. CO |= ClassOptions::HasUniqueName;
  1897. // Put the Nested flag on a type if it appears immediately inside a tag type.
  1898. // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
  1899. // here. That flag is only set on definitions, and not forward declarations.
  1900. const DIScope *ImmediateScope = Ty->getScope();
  1901. if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
  1902. CO |= ClassOptions::Nested;
  1903. // Put the Scoped flag on function-local types. MSVC puts this flag for enum
  1904. // type only when it has an immediate function scope. Clang never puts enums
  1905. // inside DILexicalBlock scopes. Enum types, as generated by clang, are
  1906. // always in function, class, or file scopes.
  1907. if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {
  1908. if (ImmediateScope && isa<DISubprogram>(ImmediateScope))
  1909. CO |= ClassOptions::Scoped;
  1910. } else {
  1911. for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
  1912. Scope = Scope->getScope()) {
  1913. if (isa<DISubprogram>(Scope)) {
  1914. CO |= ClassOptions::Scoped;
  1915. break;
  1916. }
  1917. }
  1918. }
  1919. return CO;
  1920. }
  1921. void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {
  1922. switch (Ty->getTag()) {
  1923. case dwarf::DW_TAG_class_type:
  1924. case dwarf::DW_TAG_structure_type:
  1925. case dwarf::DW_TAG_union_type:
  1926. case dwarf::DW_TAG_enumeration_type:
  1927. break;
  1928. default:
  1929. return;
  1930. }
  1931. if (const auto *File = Ty->getFile()) {
  1932. StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
  1933. TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
  1934. UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());
  1935. TypeTable.writeLeafType(USLR);
  1936. }
  1937. }
  1938. TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
  1939. ClassOptions CO = getCommonClassOptions(Ty);
  1940. TypeIndex FTI;
  1941. unsigned EnumeratorCount = 0;
  1942. if (Ty->isForwardDecl()) {
  1943. CO |= ClassOptions::ForwardReference;
  1944. } else {
  1945. ContinuationRecordBuilder ContinuationBuilder;
  1946. ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
  1947. for (const DINode *Element : Ty->getElements()) {
  1948. // We assume that the frontend provides all members in source declaration
  1949. // order, which is what MSVC does.
  1950. if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
  1951. // FIXME: Is it correct to always emit these as unsigned here?
  1952. EnumeratorRecord ER(MemberAccess::Public,
  1953. APSInt(Enumerator->getValue(), true),
  1954. Enumerator->getName());
  1955. ContinuationBuilder.writeMemberType(ER);
  1956. EnumeratorCount++;
  1957. }
  1958. }
  1959. FTI = TypeTable.insertRecord(ContinuationBuilder);
  1960. }
  1961. std::string FullName = getFullyQualifiedName(Ty);
  1962. EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
  1963. getTypeIndex(Ty->getBaseType()));
  1964. TypeIndex EnumTI = TypeTable.writeLeafType(ER);
  1965. addUDTSrcLine(Ty, EnumTI);
  1966. return EnumTI;
  1967. }
  1968. //===----------------------------------------------------------------------===//
  1969. // ClassInfo
  1970. //===----------------------------------------------------------------------===//
  1971. struct llvm::ClassInfo {
  1972. struct MemberInfo {
  1973. const DIDerivedType *MemberTypeNode;
  1974. uint64_t BaseOffset;
  1975. };
  1976. // [MemberInfo]
  1977. using MemberList = std::vector<MemberInfo>;
  1978. using MethodsList = TinyPtrVector<const DISubprogram *>;
  1979. // MethodName -> MethodsList
  1980. using MethodsMap = MapVector<MDString *, MethodsList>;
  1981. /// Base classes.
  1982. std::vector<const DIDerivedType *> Inheritance;
  1983. /// Direct members.
  1984. MemberList Members;
  1985. // Direct overloaded methods gathered by name.
  1986. MethodsMap Methods;
  1987. TypeIndex VShapeTI;
  1988. std::vector<const DIType *> NestedTypes;
  1989. };
  1990. void CodeViewDebug::clear() {
  1991. assert(CurFn == nullptr);
  1992. FileIdMap.clear();
  1993. FnDebugInfo.clear();
  1994. FileToFilepathMap.clear();
  1995. LocalUDTs.clear();
  1996. GlobalUDTs.clear();
  1997. TypeIndices.clear();
  1998. CompleteTypeIndices.clear();
  1999. ScopeGlobals.clear();
  2000. CVGlobalVariableOffsets.clear();
  2001. }
  2002. void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
  2003. const DIDerivedType *DDTy) {
  2004. if (!DDTy->getName().empty()) {
  2005. Info.Members.push_back({DDTy, 0});
  2006. // Collect static const data members with values.
  2007. if ((DDTy->getFlags() & DINode::FlagStaticMember) ==
  2008. DINode::FlagStaticMember) {
  2009. if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) ||
  2010. isa<ConstantFP>(DDTy->getConstant())))
  2011. StaticConstMembers.push_back(DDTy);
  2012. }
  2013. return;
  2014. }
  2015. // An unnamed member may represent a nested struct or union. Attempt to
  2016. // interpret the unnamed member as a DICompositeType possibly wrapped in
  2017. // qualifier types. Add all the indirect fields to the current record if that
  2018. // succeeds, and drop the member if that fails.
  2019. assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
  2020. uint64_t Offset = DDTy->getOffsetInBits();
  2021. const DIType *Ty = DDTy->getBaseType();
  2022. bool FullyResolved = false;
  2023. while (!FullyResolved) {
  2024. switch (Ty->getTag()) {
  2025. case dwarf::DW_TAG_const_type:
  2026. case dwarf::DW_TAG_volatile_type:
  2027. // FIXME: we should apply the qualifier types to the indirect fields
  2028. // rather than dropping them.
  2029. Ty = cast<DIDerivedType>(Ty)->getBaseType();
  2030. break;
  2031. default:
  2032. FullyResolved = true;
  2033. break;
  2034. }
  2035. }
  2036. const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);
  2037. if (!DCTy)
  2038. return;
  2039. ClassInfo NestedInfo = collectClassInfo(DCTy);
  2040. for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
  2041. Info.Members.push_back(
  2042. {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
  2043. }
  2044. ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
  2045. ClassInfo Info;
  2046. // Add elements to structure type.
  2047. DINodeArray Elements = Ty->getElements();
  2048. for (auto *Element : Elements) {
  2049. // We assume that the frontend provides all members in source declaration
  2050. // order, which is what MSVC does.
  2051. if (!Element)
  2052. continue;
  2053. if (auto *SP = dyn_cast<DISubprogram>(Element)) {
  2054. Info.Methods[SP->getRawName()].push_back(SP);
  2055. } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
  2056. if (DDTy->getTag() == dwarf::DW_TAG_member) {
  2057. collectMemberInfo(Info, DDTy);
  2058. } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
  2059. Info.Inheritance.push_back(DDTy);
  2060. } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
  2061. DDTy->getName() == "__vtbl_ptr_type") {
  2062. Info.VShapeTI = getTypeIndex(DDTy);
  2063. } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
  2064. Info.NestedTypes.push_back(DDTy);
  2065. } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
  2066. // Ignore friend members. It appears that MSVC emitted info about
  2067. // friends in the past, but modern versions do not.
  2068. }
  2069. } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
  2070. Info.NestedTypes.push_back(Composite);
  2071. }
  2072. // Skip other unrecognized kinds of elements.
  2073. }
  2074. return Info;
  2075. }
  2076. static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) {
  2077. // This routine is used by lowerTypeClass and lowerTypeUnion to determine
  2078. // if a complete type should be emitted instead of a forward reference.
  2079. return Ty->getName().empty() && Ty->getIdentifier().empty() &&
  2080. !Ty->isForwardDecl();
  2081. }
  2082. TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
  2083. // Emit the complete type for unnamed structs. C++ classes with methods
  2084. // which have a circular reference back to the class type are expected to
  2085. // be named by the front-end and should not be "unnamed". C unnamed
  2086. // structs should not have circular references.
  2087. if (shouldAlwaysEmitCompleteClassType(Ty)) {
  2088. // If this unnamed complete type is already in the process of being defined
  2089. // then the description of the type is malformed and cannot be emitted
  2090. // into CodeView correctly so report a fatal error.
  2091. auto I = CompleteTypeIndices.find(Ty);
  2092. if (I != CompleteTypeIndices.end() && I->second == TypeIndex())
  2093. report_fatal_error("cannot debug circular reference to unnamed type");
  2094. return getCompleteTypeIndex(Ty);
  2095. }
  2096. // First, construct the forward decl. Don't look into Ty to compute the
  2097. // forward decl options, since it might not be available in all TUs.
  2098. TypeRecordKind Kind = getRecordKind(Ty);
  2099. ClassOptions CO =
  2100. ClassOptions::ForwardReference | getCommonClassOptions(Ty);
  2101. std::string FullName = getFullyQualifiedName(Ty);
  2102. ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
  2103. FullName, Ty->getIdentifier());
  2104. TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
  2105. if (!Ty->isForwardDecl())
  2106. DeferredCompleteTypes.push_back(Ty);
  2107. return FwdDeclTI;
  2108. }
  2109. TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
  2110. // Construct the field list and complete type record.
  2111. TypeRecordKind Kind = getRecordKind(Ty);
  2112. ClassOptions CO = getCommonClassOptions(Ty);
  2113. TypeIndex FieldTI;
  2114. TypeIndex VShapeTI;
  2115. unsigned FieldCount;
  2116. bool ContainsNestedClass;
  2117. std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
  2118. lowerRecordFieldList(Ty);
  2119. if (ContainsNestedClass)
  2120. CO |= ClassOptions::ContainsNestedClass;
  2121. // MSVC appears to set this flag by searching any destructor or method with
  2122. // FunctionOptions::Constructor among the emitted members. Clang AST has all
  2123. // the members, however special member functions are not yet emitted into
  2124. // debug information. For now checking a class's non-triviality seems enough.
  2125. // FIXME: not true for a nested unnamed struct.
  2126. if (isNonTrivial(Ty))
  2127. CO |= ClassOptions::HasConstructorOrDestructor;
  2128. std::string FullName = getFullyQualifiedName(Ty);
  2129. uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
  2130. ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
  2131. SizeInBytes, FullName, Ty->getIdentifier());
  2132. TypeIndex ClassTI = TypeTable.writeLeafType(CR);
  2133. addUDTSrcLine(Ty, ClassTI);
  2134. addToUDTs(Ty);
  2135. return ClassTI;
  2136. }
  2137. TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
  2138. // Emit the complete type for unnamed unions.
  2139. if (shouldAlwaysEmitCompleteClassType(Ty))
  2140. return getCompleteTypeIndex(Ty);
  2141. ClassOptions CO =
  2142. ClassOptions::ForwardReference | getCommonClassOptions(Ty);
  2143. std::string FullName = getFullyQualifiedName(Ty);
  2144. UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
  2145. TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
  2146. if (!Ty->isForwardDecl())
  2147. DeferredCompleteTypes.push_back(Ty);
  2148. return FwdDeclTI;
  2149. }
  2150. TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
  2151. ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
  2152. TypeIndex FieldTI;
  2153. unsigned FieldCount;
  2154. bool ContainsNestedClass;
  2155. std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
  2156. lowerRecordFieldList(Ty);
  2157. if (ContainsNestedClass)
  2158. CO |= ClassOptions::ContainsNestedClass;
  2159. uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
  2160. std::string FullName = getFullyQualifiedName(Ty);
  2161. UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
  2162. Ty->getIdentifier());
  2163. TypeIndex UnionTI = TypeTable.writeLeafType(UR);
  2164. addUDTSrcLine(Ty, UnionTI);
  2165. addToUDTs(Ty);
  2166. return UnionTI;
  2167. }
  2168. std::tuple<TypeIndex, TypeIndex, unsigned, bool>
  2169. CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
  2170. // Manually count members. MSVC appears to count everything that generates a
  2171. // field list record. Each individual overload in a method overload group
  2172. // contributes to this count, even though the overload group is a single field
  2173. // list record.
  2174. unsigned MemberCount = 0;
  2175. ClassInfo Info = collectClassInfo(Ty);
  2176. ContinuationRecordBuilder ContinuationBuilder;
  2177. ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
  2178. // Create base classes.
  2179. for (const DIDerivedType *I : Info.Inheritance) {
  2180. if (I->getFlags() & DINode::FlagVirtual) {
  2181. // Virtual base.
  2182. unsigned VBPtrOffset = I->getVBPtrOffset();
  2183. // FIXME: Despite the accessor name, the offset is really in bytes.
  2184. unsigned VBTableIndex = I->getOffsetInBits() / 4;
  2185. auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
  2186. ? TypeRecordKind::IndirectVirtualBaseClass
  2187. : TypeRecordKind::VirtualBaseClass;
  2188. VirtualBaseClassRecord VBCR(
  2189. RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
  2190. getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
  2191. VBTableIndex);
  2192. ContinuationBuilder.writeMemberType(VBCR);
  2193. MemberCount++;
  2194. } else {
  2195. assert(I->getOffsetInBits() % 8 == 0 &&
  2196. "bases must be on byte boundaries");
  2197. BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
  2198. getTypeIndex(I->getBaseType()),
  2199. I->getOffsetInBits() / 8);
  2200. ContinuationBuilder.writeMemberType(BCR);
  2201. MemberCount++;
  2202. }
  2203. }
  2204. // Create members.
  2205. for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
  2206. const DIDerivedType *Member = MemberInfo.MemberTypeNode;
  2207. TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
  2208. StringRef MemberName = Member->getName();
  2209. MemberAccess Access =
  2210. translateAccessFlags(Ty->getTag(), Member->getFlags());
  2211. if (Member->isStaticMember()) {
  2212. StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
  2213. ContinuationBuilder.writeMemberType(SDMR);
  2214. MemberCount++;
  2215. continue;
  2216. }
  2217. // Virtual function pointer member.
  2218. if ((Member->getFlags() & DINode::FlagArtificial) &&
  2219. Member->getName().startswith("_vptr$")) {
  2220. VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
  2221. ContinuationBuilder.writeMemberType(VFPR);
  2222. MemberCount++;
  2223. continue;
  2224. }
  2225. // Data member.
  2226. uint64_t MemberOffsetInBits =
  2227. Member->getOffsetInBits() + MemberInfo.BaseOffset;
  2228. if (Member->isBitField()) {
  2229. uint64_t StartBitOffset = MemberOffsetInBits;
  2230. if (const auto *CI =
  2231. dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
  2232. MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
  2233. }
  2234. StartBitOffset -= MemberOffsetInBits;
  2235. BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
  2236. StartBitOffset);
  2237. MemberBaseType = TypeTable.writeLeafType(BFR);
  2238. }
  2239. uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
  2240. DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
  2241. MemberName);
  2242. ContinuationBuilder.writeMemberType(DMR);
  2243. MemberCount++;
  2244. }
  2245. // Create methods
  2246. for (auto &MethodItr : Info.Methods) {
  2247. StringRef Name = MethodItr.first->getString();
  2248. std::vector<OneMethodRecord> Methods;
  2249. for (const DISubprogram *SP : MethodItr.second) {
  2250. TypeIndex MethodType = getMemberFunctionType(SP, Ty);
  2251. bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
  2252. unsigned VFTableOffset = -1;
  2253. if (Introduced)
  2254. VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
  2255. Methods.push_back(OneMethodRecord(
  2256. MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
  2257. translateMethodKindFlags(SP, Introduced),
  2258. translateMethodOptionFlags(SP), VFTableOffset, Name));
  2259. MemberCount++;
  2260. }
  2261. assert(!Methods.empty() && "Empty methods map entry");
  2262. if (Methods.size() == 1)
  2263. ContinuationBuilder.writeMemberType(Methods[0]);
  2264. else {
  2265. // FIXME: Make this use its own ContinuationBuilder so that
  2266. // MethodOverloadList can be split correctly.
  2267. MethodOverloadListRecord MOLR(Methods);
  2268. TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
  2269. OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
  2270. ContinuationBuilder.writeMemberType(OMR);
  2271. }
  2272. }
  2273. // Create nested classes.
  2274. for (const DIType *Nested : Info.NestedTypes) {
  2275. NestedTypeRecord R(getTypeIndex(Nested), Nested->getName());
  2276. ContinuationBuilder.writeMemberType(R);
  2277. MemberCount++;
  2278. }
  2279. TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
  2280. return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
  2281. !Info.NestedTypes.empty());
  2282. }
  2283. TypeIndex CodeViewDebug::getVBPTypeIndex() {
  2284. if (!VBPType.getIndex()) {
  2285. // Make a 'const int *' type.
  2286. ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
  2287. TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
  2288. PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
  2289. : PointerKind::Near32;
  2290. PointerMode PM = PointerMode::Pointer;
  2291. PointerOptions PO = PointerOptions::None;
  2292. PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
  2293. VBPType = TypeTable.writeLeafType(PR);
  2294. }
  2295. return VBPType;
  2296. }
  2297. TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) {
  2298. // The null DIType is the void type. Don't try to hash it.
  2299. if (!Ty)
  2300. return TypeIndex::Void();
  2301. // Check if we've already translated this type. Don't try to do a
  2302. // get-or-create style insertion that caches the hash lookup across the
  2303. // lowerType call. It will update the TypeIndices map.
  2304. auto I = TypeIndices.find({Ty, ClassTy});
  2305. if (I != TypeIndices.end())
  2306. return I->second;
  2307. TypeLoweringScope S(*this);
  2308. TypeIndex TI = lowerType(Ty, ClassTy);
  2309. return recordTypeIndexForDINode(Ty, TI, ClassTy);
  2310. }
  2311. codeview::TypeIndex
  2312. CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
  2313. const DISubroutineType *SubroutineTy) {
  2314. assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&
  2315. "this type must be a pointer type");
  2316. PointerOptions Options = PointerOptions::None;
  2317. if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)
  2318. Options = PointerOptions::LValueRefThisPointer;
  2319. else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)
  2320. Options = PointerOptions::RValueRefThisPointer;
  2321. // Check if we've already translated this type. If there is no ref qualifier
  2322. // on the function then we look up this pointer type with no associated class
  2323. // so that the TypeIndex for the this pointer can be shared with the type
  2324. // index for other pointers to this class type. If there is a ref qualifier
  2325. // then we lookup the pointer using the subroutine as the parent type.
  2326. auto I = TypeIndices.find({PtrTy, SubroutineTy});
  2327. if (I != TypeIndices.end())
  2328. return I->second;
  2329. TypeLoweringScope S(*this);
  2330. TypeIndex TI = lowerTypePointer(PtrTy, Options);
  2331. return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);
  2332. }
  2333. TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) {
  2334. PointerRecord PR(getTypeIndex(Ty),
  2335. getPointerSizeInBytes() == 8 ? PointerKind::Near64
  2336. : PointerKind::Near32,
  2337. PointerMode::LValueReference, PointerOptions::None,
  2338. Ty->getSizeInBits() / 8);
  2339. return TypeTable.writeLeafType(PR);
  2340. }
  2341. TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) {
  2342. // The null DIType is the void type. Don't try to hash it.
  2343. if (!Ty)
  2344. return TypeIndex::Void();
  2345. // Look through typedefs when getting the complete type index. Call
  2346. // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are
  2347. // emitted only once.
  2348. if (Ty->getTag() == dwarf::DW_TAG_typedef)
  2349. (void)getTypeIndex(Ty);
  2350. while (Ty->getTag() == dwarf::DW_TAG_typedef)
  2351. Ty = cast<DIDerivedType>(Ty)->getBaseType();
  2352. // If this is a non-record type, the complete type index is the same as the
  2353. // normal type index. Just call getTypeIndex.
  2354. switch (Ty->getTag()) {
  2355. case dwarf::DW_TAG_class_type:
  2356. case dwarf::DW_TAG_structure_type:
  2357. case dwarf::DW_TAG_union_type:
  2358. break;
  2359. default:
  2360. return getTypeIndex(Ty);
  2361. }
  2362. const auto *CTy = cast<DICompositeType>(Ty);
  2363. TypeLoweringScope S(*this);
  2364. // Make sure the forward declaration is emitted first. It's unclear if this
  2365. // is necessary, but MSVC does it, and we should follow suit until we can show
  2366. // otherwise.
  2367. // We only emit a forward declaration for named types.
  2368. if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {
  2369. TypeIndex FwdDeclTI = getTypeIndex(CTy);
  2370. // Just use the forward decl if we don't have complete type info. This
  2371. // might happen if the frontend is using modules and expects the complete
  2372. // definition to be emitted elsewhere.
  2373. if (CTy->isForwardDecl())
  2374. return FwdDeclTI;
  2375. }
  2376. // Check if we've already translated the complete record type.
  2377. // Insert the type with a null TypeIndex to signify that the type is currently
  2378. // being lowered.
  2379. auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
  2380. if (!InsertResult.second)
  2381. return InsertResult.first->second;
  2382. TypeIndex TI;
  2383. switch (CTy->getTag()) {
  2384. case dwarf::DW_TAG_class_type:
  2385. case dwarf::DW_TAG_structure_type:
  2386. TI = lowerCompleteTypeClass(CTy);
  2387. break;
  2388. case dwarf::DW_TAG_union_type:
  2389. TI = lowerCompleteTypeUnion(CTy);
  2390. break;
  2391. default:
  2392. llvm_unreachable("not a record");
  2393. }
  2394. // Update the type index associated with this CompositeType. This cannot
  2395. // use the 'InsertResult' iterator above because it is potentially
  2396. // invalidated by map insertions which can occur while lowering the class
  2397. // type above.
  2398. CompleteTypeIndices[CTy] = TI;
  2399. return TI;
  2400. }
  2401. /// Emit all the deferred complete record types. Try to do this in FIFO order,
  2402. /// and do this until fixpoint, as each complete record type typically
  2403. /// references
  2404. /// many other record types.
  2405. void CodeViewDebug::emitDeferredCompleteTypes() {
  2406. SmallVector<const DICompositeType *, 4> TypesToEmit;
  2407. while (!DeferredCompleteTypes.empty()) {
  2408. std::swap(DeferredCompleteTypes, TypesToEmit);
  2409. for (const DICompositeType *RecordTy : TypesToEmit)
  2410. getCompleteTypeIndex(RecordTy);
  2411. TypesToEmit.clear();
  2412. }
  2413. }
  2414. void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,
  2415. ArrayRef<LocalVariable> Locals) {
  2416. // Get the sorted list of parameters and emit them first.
  2417. SmallVector<const LocalVariable *, 6> Params;
  2418. for (const LocalVariable &L : Locals)
  2419. if (L.DIVar->isParameter())
  2420. Params.push_back(&L);
  2421. llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {
  2422. return L->DIVar->getArg() < R->DIVar->getArg();
  2423. });
  2424. for (const LocalVariable *L : Params)
  2425. emitLocalVariable(FI, *L);
  2426. // Next emit all non-parameters in the order that we found them.
  2427. for (const LocalVariable &L : Locals)
  2428. if (!L.DIVar->isParameter())
  2429. emitLocalVariable(FI, L);
  2430. }
  2431. void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,
  2432. const LocalVariable &Var) {
  2433. // LocalSym record, see SymbolRecord.h for more info.
  2434. MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);
  2435. LocalSymFlags Flags = LocalSymFlags::None;
  2436. if (Var.DIVar->isParameter())
  2437. Flags |= LocalSymFlags::IsParameter;
  2438. if (Var.DefRanges.empty())
  2439. Flags |= LocalSymFlags::IsOptimizedOut;
  2440. OS.AddComment("TypeIndex");
  2441. TypeIndex TI = Var.UseReferenceType
  2442. ? getTypeIndexForReferenceTo(Var.DIVar->getType())
  2443. : getCompleteTypeIndex(Var.DIVar->getType());
  2444. OS.emitInt32(TI.getIndex());
  2445. OS.AddComment("Flags");
  2446. OS.emitInt16(static_cast<uint16_t>(Flags));
  2447. // Truncate the name so we won't overflow the record length field.
  2448. emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
  2449. endSymbolRecord(LocalEnd);
  2450. // Calculate the on disk prefix of the appropriate def range record. The
  2451. // records and on disk formats are described in SymbolRecords.h. BytePrefix
  2452. // should be big enough to hold all forms without memory allocation.
  2453. SmallString<20> BytePrefix;
  2454. for (const LocalVarDefRange &DefRange : Var.DefRanges) {
  2455. BytePrefix.clear();
  2456. if (DefRange.InMemory) {
  2457. int Offset = DefRange.DataOffset;
  2458. unsigned Reg = DefRange.CVRegister;
  2459. // 32-bit x86 call sequences often use PUSH instructions, which disrupt
  2460. // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,
  2461. // instead. In frames without stack realignment, $T0 will be the CFA.
  2462. if (RegisterId(Reg) == RegisterId::ESP) {
  2463. Reg = unsigned(RegisterId::VFRAME);
  2464. Offset += FI.OffsetAdjustment;
  2465. }
  2466. // If we can use the chosen frame pointer for the frame and this isn't a
  2467. // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.
  2468. // Otherwise, use S_DEFRANGE_REGISTER_REL.
  2469. EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);
  2470. if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&
  2471. (bool(Flags & LocalSymFlags::IsParameter)
  2472. ? (EncFP == FI.EncodedParamFramePtrReg)
  2473. : (EncFP == FI.EncodedLocalFramePtrReg))) {
  2474. DefRangeFramePointerRelHeader DRHdr;
  2475. DRHdr.Offset = Offset;
  2476. OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr);
  2477. } else {
  2478. uint16_t RegRelFlags = 0;
  2479. if (DefRange.IsSubfield) {
  2480. RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |
  2481. (DefRange.StructOffset
  2482. << DefRangeRegisterRelSym::OffsetInParentShift);
  2483. }
  2484. DefRangeRegisterRelHeader DRHdr;
  2485. DRHdr.Register = Reg;
  2486. DRHdr.Flags = RegRelFlags;
  2487. DRHdr.BasePointerOffset = Offset;
  2488. OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr);
  2489. }
  2490. } else {
  2491. assert(DefRange.DataOffset == 0 && "unexpected offset into register");
  2492. if (DefRange.IsSubfield) {
  2493. DefRangeSubfieldRegisterHeader DRHdr;
  2494. DRHdr.Register = DefRange.CVRegister;
  2495. DRHdr.MayHaveNoName = 0;
  2496. DRHdr.OffsetInParent = DefRange.StructOffset;
  2497. OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr);
  2498. } else {
  2499. DefRangeRegisterHeader DRHdr;
  2500. DRHdr.Register = DefRange.CVRegister;
  2501. DRHdr.MayHaveNoName = 0;
  2502. OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr);
  2503. }
  2504. }
  2505. }
  2506. }
  2507. void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
  2508. const FunctionInfo& FI) {
  2509. for (LexicalBlock *Block : Blocks)
  2510. emitLexicalBlock(*Block, FI);
  2511. }
  2512. /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a
  2513. /// lexical block scope.
  2514. void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,
  2515. const FunctionInfo& FI) {
  2516. MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);
  2517. OS.AddComment("PtrParent");
  2518. OS.emitInt32(0); // PtrParent
  2519. OS.AddComment("PtrEnd");
  2520. OS.emitInt32(0); // PtrEnd
  2521. OS.AddComment("Code size");
  2522. OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4); // Code Size
  2523. OS.AddComment("Function section relative address");
  2524. OS.EmitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset
  2525. OS.AddComment("Function section index");
  2526. OS.EmitCOFFSectionIndex(FI.Begin); // Func Symbol
  2527. OS.AddComment("Lexical block name");
  2528. emitNullTerminatedSymbolName(OS, Block.Name); // Name
  2529. endSymbolRecord(RecordEnd);
  2530. // Emit variables local to this lexical block.
  2531. emitLocalVariableList(FI, Block.Locals);
  2532. emitGlobalVariableList(Block.Globals);
  2533. // Emit lexical blocks contained within this block.
  2534. emitLexicalBlockList(Block.Children, FI);
  2535. // Close the lexical block scope.
  2536. emitEndSymbolRecord(SymbolKind::S_END);
  2537. }
  2538. /// Convenience routine for collecting lexical block information for a list
  2539. /// of lexical scopes.
  2540. void CodeViewDebug::collectLexicalBlockInfo(
  2541. SmallVectorImpl<LexicalScope *> &Scopes,
  2542. SmallVectorImpl<LexicalBlock *> &Blocks,
  2543. SmallVectorImpl<LocalVariable> &Locals,
  2544. SmallVectorImpl<CVGlobalVariable> &Globals) {
  2545. for (LexicalScope *Scope : Scopes)
  2546. collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);
  2547. }
  2548. /// Populate the lexical blocks and local variable lists of the parent with
  2549. /// information about the specified lexical scope.
  2550. void CodeViewDebug::collectLexicalBlockInfo(
  2551. LexicalScope &Scope,
  2552. SmallVectorImpl<LexicalBlock *> &ParentBlocks,
  2553. SmallVectorImpl<LocalVariable> &ParentLocals,
  2554. SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {
  2555. if (Scope.isAbstractScope())
  2556. return;
  2557. // Gather information about the lexical scope including local variables,
  2558. // global variables, and address ranges.
  2559. bool IgnoreScope = false;
  2560. auto LI = ScopeVariables.find(&Scope);
  2561. SmallVectorImpl<LocalVariable> *Locals =
  2562. LI != ScopeVariables.end() ? &LI->second : nullptr;
  2563. auto GI = ScopeGlobals.find(Scope.getScopeNode());
  2564. SmallVectorImpl<CVGlobalVariable> *Globals =
  2565. GI != ScopeGlobals.end() ? GI->second.get() : nullptr;
  2566. const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());
  2567. const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();
  2568. // Ignore lexical scopes which do not contain variables.
  2569. if (!Locals && !Globals)
  2570. IgnoreScope = true;
  2571. // Ignore lexical scopes which are not lexical blocks.
  2572. if (!DILB)
  2573. IgnoreScope = true;
  2574. // Ignore scopes which have too many address ranges to represent in the
  2575. // current CodeView format or do not have a valid address range.
  2576. //
  2577. // For lexical scopes with multiple address ranges you may be tempted to
  2578. // construct a single range covering every instruction where the block is
  2579. // live and everything in between. Unfortunately, Visual Studio only
  2580. // displays variables from the first matching lexical block scope. If the
  2581. // first lexical block contains exception handling code or cold code which
  2582. // is moved to the bottom of the routine creating a single range covering
  2583. // nearly the entire routine, then it will hide all other lexical blocks
  2584. // and the variables they contain.
  2585. if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))
  2586. IgnoreScope = true;
  2587. if (IgnoreScope) {
  2588. // This scope can be safely ignored and eliminating it will reduce the
  2589. // size of the debug information. Be sure to collect any variable and scope
  2590. // information from the this scope or any of its children and collapse them
  2591. // into the parent scope.
  2592. if (Locals)
  2593. ParentLocals.append(Locals->begin(), Locals->end());
  2594. if (Globals)
  2595. ParentGlobals.append(Globals->begin(), Globals->end());
  2596. collectLexicalBlockInfo(Scope.getChildren(),
  2597. ParentBlocks,
  2598. ParentLocals,
  2599. ParentGlobals);
  2600. return;
  2601. }
  2602. // Create a new CodeView lexical block for this lexical scope. If we've
  2603. // seen this DILexicalBlock before then the scope tree is malformed and
  2604. // we can handle this gracefully by not processing it a second time.
  2605. auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()});
  2606. if (!BlockInsertion.second)
  2607. return;
  2608. // Create a lexical block containing the variables and collect the the
  2609. // lexical block information for the children.
  2610. const InsnRange &Range = Ranges.front();
  2611. assert(Range.first && Range.second);
  2612. LexicalBlock &Block = BlockInsertion.first->second;
  2613. Block.Begin = getLabelBeforeInsn(Range.first);
  2614. Block.End = getLabelAfterInsn(Range.second);
  2615. assert(Block.Begin && "missing label for scope begin");
  2616. assert(Block.End && "missing label for scope end");
  2617. Block.Name = DILB->getName();
  2618. if (Locals)
  2619. Block.Locals = std::move(*Locals);
  2620. if (Globals)
  2621. Block.Globals = std::move(*Globals);
  2622. ParentBlocks.push_back(&Block);
  2623. collectLexicalBlockInfo(Scope.getChildren(),
  2624. Block.Children,
  2625. Block.Locals,
  2626. Block.Globals);
  2627. }
  2628. void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
  2629. const Function &GV = MF->getFunction();
  2630. assert(FnDebugInfo.count(&GV));
  2631. assert(CurFn == FnDebugInfo[&GV].get());
  2632. collectVariableInfo(GV.getSubprogram());
  2633. // Build the lexical block structure to emit for this routine.
  2634. if (LexicalScope *CFS = LScopes.getCurrentFunctionScope())
  2635. collectLexicalBlockInfo(*CFS,
  2636. CurFn->ChildBlocks,
  2637. CurFn->Locals,
  2638. CurFn->Globals);
  2639. // Clear the scope and variable information from the map which will not be
  2640. // valid after we have finished processing this routine. This also prepares
  2641. // the map for the subsequent routine.
  2642. ScopeVariables.clear();
  2643. // Don't emit anything if we don't have any line tables.
  2644. // Thunks are compiler-generated and probably won't have source correlation.
  2645. if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {
  2646. FnDebugInfo.erase(&GV);
  2647. CurFn = nullptr;
  2648. return;
  2649. }
  2650. // Find heap alloc sites and add to list.
  2651. for (const auto &MBB : *MF) {
  2652. for (const auto &MI : MBB) {
  2653. if (MDNode *MD = MI.getHeapAllocMarker()) {
  2654. CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),
  2655. getLabelAfterInsn(&MI),
  2656. dyn_cast<DIType>(MD)));
  2657. }
  2658. }
  2659. }
  2660. CurFn->Annotations = MF->getCodeViewAnnotations();
  2661. CurFn->End = Asm->getFunctionEnd();
  2662. CurFn = nullptr;
  2663. }
  2664. // Usable locations are valid with non-zero line numbers. A line number of zero
  2665. // corresponds to optimized code that doesn't have a distinct source location.
  2666. // In this case, we try to use the previous or next source location depending on
  2667. // the context.
  2668. static bool isUsableDebugLoc(DebugLoc DL) {
  2669. return DL && DL.getLine() != 0;
  2670. }
  2671. void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
  2672. DebugHandlerBase::beginInstruction(MI);
  2673. // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
  2674. if (!Asm || !CurFn || MI->isDebugInstr() ||
  2675. MI->getFlag(MachineInstr::FrameSetup))
  2676. return;
  2677. // If the first instruction of a new MBB has no location, find the first
  2678. // instruction with a location and use that.
  2679. DebugLoc DL = MI->getDebugLoc();
  2680. if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) {
  2681. for (const auto &NextMI : *MI->getParent()) {
  2682. if (NextMI.isDebugInstr())
  2683. continue;
  2684. DL = NextMI.getDebugLoc();
  2685. if (isUsableDebugLoc(DL))
  2686. break;
  2687. }
  2688. // FIXME: Handle the case where the BB has no valid locations. This would
  2689. // probably require doing a real dataflow analysis.
  2690. }
  2691. PrevInstBB = MI->getParent();
  2692. // If we still don't have a debug location, don't record a location.
  2693. if (!isUsableDebugLoc(DL))
  2694. return;
  2695. maybeRecordLocation(DL, Asm->MF);
  2696. }
  2697. MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
  2698. MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
  2699. *EndLabel = MMI->getContext().createTempSymbol();
  2700. OS.emitInt32(unsigned(Kind));
  2701. OS.AddComment("Subsection size");
  2702. OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
  2703. OS.emitLabel(BeginLabel);
  2704. return EndLabel;
  2705. }
  2706. void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
  2707. OS.emitLabel(EndLabel);
  2708. // Every subsection must be aligned to a 4-byte boundary.
  2709. OS.emitValueToAlignment(4);
  2710. }
  2711. static StringRef getSymbolName(SymbolKind SymKind) {
  2712. for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())
  2713. if (EE.Value == SymKind)
  2714. return EE.Name;
  2715. return "";
  2716. }
  2717. MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {
  2718. MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
  2719. *EndLabel = MMI->getContext().createTempSymbol();
  2720. OS.AddComment("Record length");
  2721. OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
  2722. OS.emitLabel(BeginLabel);
  2723. if (OS.isVerboseAsm())
  2724. OS.AddComment("Record kind: " + getSymbolName(SymKind));
  2725. OS.emitInt16(unsigned(SymKind));
  2726. return EndLabel;
  2727. }
  2728. void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {
  2729. // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid
  2730. // an extra copy of every symbol record in LLD. This increases object file
  2731. // size by less than 1% in the clang build, and is compatible with the Visual
  2732. // C++ linker.
  2733. OS.emitValueToAlignment(4);
  2734. OS.emitLabel(SymEnd);
  2735. }
  2736. void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {
  2737. OS.AddComment("Record length");
  2738. OS.emitInt16(2);
  2739. if (OS.isVerboseAsm())
  2740. OS.AddComment("Record kind: " + getSymbolName(EndKind));
  2741. OS.emitInt16(uint16_t(EndKind)); // Record Kind
  2742. }
  2743. void CodeViewDebug::emitDebugInfoForUDTs(
  2744. const std::vector<std::pair<std::string, const DIType *>> &UDTs) {
  2745. #ifndef NDEBUG
  2746. size_t OriginalSize = UDTs.size();
  2747. #endif
  2748. for (const auto &UDT : UDTs) {
  2749. const DIType *T = UDT.second;
  2750. assert(shouldEmitUdt(T));
  2751. MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);
  2752. OS.AddComment("Type");
  2753. OS.emitInt32(getCompleteTypeIndex(T).getIndex());
  2754. assert(OriginalSize == UDTs.size() &&
  2755. "getCompleteTypeIndex found new UDTs!");
  2756. emitNullTerminatedSymbolName(OS, UDT.first);
  2757. endSymbolRecord(UDTRecordEnd);
  2758. }
  2759. }
  2760. void CodeViewDebug::collectGlobalVariableInfo() {
  2761. DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
  2762. GlobalMap;
  2763. for (const GlobalVariable &GV : MMI->getModule()->globals()) {
  2764. SmallVector<DIGlobalVariableExpression *, 1> GVEs;
  2765. GV.getDebugInfo(GVEs);
  2766. for (const auto *GVE : GVEs)
  2767. GlobalMap[GVE] = &GV;
  2768. }
  2769. NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
  2770. for (const MDNode *Node : CUs->operands()) {
  2771. const auto *CU = cast<DICompileUnit>(Node);
  2772. for (const auto *GVE : CU->getGlobalVariables()) {
  2773. const DIGlobalVariable *DIGV = GVE->getVariable();
  2774. const DIExpression *DIE = GVE->getExpression();
  2775. if ((DIE->getNumElements() == 2) &&
  2776. (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
  2777. // Record the constant offset for the variable.
  2778. //
  2779. // A Fortran common block uses this idiom to encode the offset
  2780. // of a variable from the common block's starting address.
  2781. CVGlobalVariableOffsets.insert(
  2782. std::make_pair(DIGV, DIE->getElement(1)));
  2783. // Emit constant global variables in a global symbol section.
  2784. if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
  2785. CVGlobalVariable CVGV = {DIGV, DIE};
  2786. GlobalVariables.emplace_back(std::move(CVGV));
  2787. }
  2788. const auto *GV = GlobalMap.lookup(GVE);
  2789. if (!GV || GV->isDeclarationForLinker())
  2790. continue;
  2791. DIScope *Scope = DIGV->getScope();
  2792. SmallVector<CVGlobalVariable, 1> *VariableList;
  2793. if (Scope && isa<DILocalScope>(Scope)) {
  2794. // Locate a global variable list for this scope, creating one if
  2795. // necessary.
  2796. auto Insertion = ScopeGlobals.insert(
  2797. {Scope, std::unique_ptr<GlobalVariableList>()});
  2798. if (Insertion.second)
  2799. Insertion.first->second = std::make_unique<GlobalVariableList>();
  2800. VariableList = Insertion.first->second.get();
  2801. } else if (GV->hasComdat())
  2802. // Emit this global variable into a COMDAT section.
  2803. VariableList = &ComdatVariables;
  2804. else
  2805. // Emit this global variable in a single global symbol section.
  2806. VariableList = &GlobalVariables;
  2807. CVGlobalVariable CVGV = {DIGV, GV};
  2808. VariableList->emplace_back(std::move(CVGV));
  2809. }
  2810. }
  2811. }
  2812. void CodeViewDebug::collectDebugInfoForGlobals() {
  2813. for (const CVGlobalVariable &CVGV : GlobalVariables) {
  2814. const DIGlobalVariable *DIGV = CVGV.DIGV;
  2815. const DIScope *Scope = DIGV->getScope();
  2816. getCompleteTypeIndex(DIGV->getType());
  2817. getFullyQualifiedName(Scope, DIGV->getName());
  2818. }
  2819. for (const CVGlobalVariable &CVGV : ComdatVariables) {
  2820. const DIGlobalVariable *DIGV = CVGV.DIGV;
  2821. const DIScope *Scope = DIGV->getScope();
  2822. getCompleteTypeIndex(DIGV->getType());
  2823. getFullyQualifiedName(Scope, DIGV->getName());
  2824. }
  2825. }
  2826. void CodeViewDebug::emitDebugInfoForGlobals() {
  2827. // First, emit all globals that are not in a comdat in a single symbol
  2828. // substream. MSVC doesn't like it if the substream is empty, so only open
  2829. // it if we have at least one global to emit.
  2830. switchToDebugSectionForSymbol(nullptr);
  2831. if (!GlobalVariables.empty() || !StaticConstMembers.empty()) {
  2832. OS.AddComment("Symbol subsection for globals");
  2833. MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
  2834. emitGlobalVariableList(GlobalVariables);
  2835. emitStaticConstMemberList();
  2836. endCVSubsection(EndLabel);
  2837. }
  2838. // Second, emit each global that is in a comdat into its own .debug$S
  2839. // section along with its own symbol substream.
  2840. for (const CVGlobalVariable &CVGV : ComdatVariables) {
  2841. const GlobalVariable *GV = CVGV.GVInfo.get<const GlobalVariable *>();
  2842. MCSymbol *GVSym = Asm->getSymbol(GV);
  2843. OS.AddComment("Symbol subsection for " +
  2844. Twine(GlobalValue::dropLLVMManglingEscape(GV->getName())));
  2845. switchToDebugSectionForSymbol(GVSym);
  2846. MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
  2847. // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
  2848. emitDebugInfoForGlobal(CVGV);
  2849. endCVSubsection(EndLabel);
  2850. }
  2851. }
  2852. void CodeViewDebug::emitDebugInfoForRetainedTypes() {
  2853. NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
  2854. for (const MDNode *Node : CUs->operands()) {
  2855. for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
  2856. if (DIType *RT = dyn_cast<DIType>(Ty)) {
  2857. getTypeIndex(RT);
  2858. // FIXME: Add to global/local DTU list.
  2859. }
  2860. }
  2861. }
  2862. }
  2863. // Emit each global variable in the specified array.
  2864. void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {
  2865. for (const CVGlobalVariable &CVGV : Globals) {
  2866. // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
  2867. emitDebugInfoForGlobal(CVGV);
  2868. }
  2869. }
  2870. void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
  2871. const std::string &QualifiedName) {
  2872. MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);
  2873. OS.AddComment("Type");
  2874. OS.emitInt32(getTypeIndex(DTy).getIndex());
  2875. OS.AddComment("Value");
  2876. // Encoded integers shouldn't need more than 10 bytes.
  2877. uint8_t Data[10];
  2878. BinaryStreamWriter Writer(Data, llvm::support::endianness::little);
  2879. CodeViewRecordIO IO(Writer);
  2880. cantFail(IO.mapEncodedInteger(Value));
  2881. StringRef SRef((char *)Data, Writer.getOffset());
  2882. OS.emitBinaryData(SRef);
  2883. OS.AddComment("Name");
  2884. emitNullTerminatedSymbolName(OS, QualifiedName);
  2885. endSymbolRecord(SConstantEnd);
  2886. }
  2887. void CodeViewDebug::emitStaticConstMemberList() {
  2888. for (const DIDerivedType *DTy : StaticConstMembers) {
  2889. const DIScope *Scope = DTy->getScope();
  2890. APSInt Value;
  2891. if (const ConstantInt *CI =
  2892. dyn_cast_or_null<ConstantInt>(DTy->getConstant()))
  2893. Value = APSInt(CI->getValue(),
  2894. DebugHandlerBase::isUnsignedDIType(DTy->getBaseType()));
  2895. else if (const ConstantFP *CFP =
  2896. dyn_cast_or_null<ConstantFP>(DTy->getConstant()))
  2897. Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true);
  2898. else
  2899. llvm_unreachable("cannot emit a constant without a value");
  2900. emitConstantSymbolRecord(DTy->getBaseType(), Value,
  2901. getFullyQualifiedName(Scope, DTy->getName()));
  2902. }
  2903. }
  2904. static bool isFloatDIType(const DIType *Ty) {
  2905. if (isa<DICompositeType>(Ty))
  2906. return false;
  2907. if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
  2908. dwarf::Tag T = (dwarf::Tag)Ty->getTag();
  2909. if (T == dwarf::DW_TAG_pointer_type ||
  2910. T == dwarf::DW_TAG_ptr_to_member_type ||
  2911. T == dwarf::DW_TAG_reference_type ||
  2912. T == dwarf::DW_TAG_rvalue_reference_type)
  2913. return false;
  2914. assert(DTy->getBaseType() && "Expected valid base type");
  2915. return isFloatDIType(DTy->getBaseType());
  2916. }
  2917. auto *BTy = cast<DIBasicType>(Ty);
  2918. return (BTy->getEncoding() == dwarf::DW_ATE_float);
  2919. }
  2920. void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
  2921. const DIGlobalVariable *DIGV = CVGV.DIGV;
  2922. const DIScope *Scope = DIGV->getScope();
  2923. // For static data members, get the scope from the declaration.
  2924. if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
  2925. DIGV->getRawStaticDataMemberDeclaration()))
  2926. Scope = MemberDecl->getScope();
  2927. // For Fortran, the scoping portion is elided in its name so that we can
  2928. // reference the variable in the command line of the VS debugger.
  2929. std::string QualifiedName =
  2930. (moduleIsInFortran()) ? std::string(DIGV->getName())
  2931. : getFullyQualifiedName(Scope, DIGV->getName());
  2932. if (const GlobalVariable *GV =
  2933. CVGV.GVInfo.dyn_cast<const GlobalVariable *>()) {
  2934. // DataSym record, see SymbolRecord.h for more info. Thread local data
  2935. // happens to have the same format as global data.
  2936. MCSymbol *GVSym = Asm->getSymbol(GV);
  2937. SymbolKind DataSym = GV->isThreadLocal()
  2938. ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32
  2939. : SymbolKind::S_GTHREAD32)
  2940. : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32
  2941. : SymbolKind::S_GDATA32);
  2942. MCSymbol *DataEnd = beginSymbolRecord(DataSym);
  2943. OS.AddComment("Type");
  2944. OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());
  2945. OS.AddComment("DataOffset");
  2946. uint64_t Offset = 0;
  2947. if (CVGlobalVariableOffsets.find(DIGV) != CVGlobalVariableOffsets.end())
  2948. // Use the offset seen while collecting info on globals.
  2949. Offset = CVGlobalVariableOffsets[DIGV];
  2950. OS.EmitCOFFSecRel32(GVSym, Offset);
  2951. OS.AddComment("Segment");
  2952. OS.EmitCOFFSectionIndex(GVSym);
  2953. OS.AddComment("Name");
  2954. const unsigned LengthOfDataRecord = 12;
  2955. emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord);
  2956. endSymbolRecord(DataEnd);
  2957. } else {
  2958. const DIExpression *DIE = CVGV.GVInfo.get<const DIExpression *>();
  2959. assert(DIE->isConstant() &&
  2960. "Global constant variables must contain a constant expression.");
  2961. // Use unsigned for floats.
  2962. bool isUnsigned = isFloatDIType(DIGV->getType())
  2963. ? true
  2964. : DebugHandlerBase::isUnsignedDIType(DIGV->getType());
  2965. APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);
  2966. emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName);
  2967. }
  2968. }