DWARFLinker.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  1. //=== DWARFLinker.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. #include "llvm/DWARFLinker/DWARFLinker.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/BitVector.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/CodeGen/NonRelocatableStringpool.h"
  13. #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
  15. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  17. #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  23. #include "llvm/DebugInfo/DWARF/DWARFSection.h"
  24. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  25. #include "llvm/MC/MCDwarf.h"
  26. #include "llvm/Support/DataExtractor.h"
  27. #include "llvm/Support/Error.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/ErrorOr.h"
  30. #include "llvm/Support/FormatVariadic.h"
  31. #include "llvm/Support/LEB128.h"
  32. #include "llvm/Support/Path.h"
  33. #include "llvm/Support/ThreadPool.h"
  34. #include <vector>
  35. namespace llvm {
  36. /// Hold the input and output of the debug info size in bytes.
  37. struct DebugInfoSize {
  38. uint64_t Input;
  39. uint64_t Output;
  40. };
  41. /// Compute the total size of the debug info.
  42. static uint64_t getDebugInfoSize(DWARFContext &Dwarf) {
  43. uint64_t Size = 0;
  44. for (auto &Unit : Dwarf.compile_units()) {
  45. Size += Unit->getLength();
  46. }
  47. return Size;
  48. }
  49. /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
  50. /// CompileUnit object instead.
  51. static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) {
  52. auto CU = llvm::upper_bound(
  53. Units, Offset, [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
  54. return LHS < RHS->getOrigUnit().getNextUnitOffset();
  55. });
  56. return CU != Units.end() ? CU->get() : nullptr;
  57. }
  58. /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
  59. /// The resulting DIE might be in another CompileUnit which is stored into \p
  60. /// ReferencedCU. \returns null if resolving fails for any reason.
  61. DWARFDie DWARFLinker::resolveDIEReference(const DWARFFile &File,
  62. const UnitListTy &Units,
  63. const DWARFFormValue &RefValue,
  64. const DWARFDie &DIE,
  65. CompileUnit *&RefCU) {
  66. assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
  67. uint64_t RefOffset = *RefValue.getAsReference();
  68. if ((RefCU = getUnitForOffset(Units, RefOffset)))
  69. if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
  70. // In a file with broken references, an attribute might point to a NULL
  71. // DIE.
  72. if (!RefDie.isNULL())
  73. return RefDie;
  74. }
  75. reportWarning("could not find referenced DIE", File, &DIE);
  76. return DWARFDie();
  77. }
  78. /// \returns whether the passed \a Attr type might contain a DIE reference
  79. /// suitable for ODR uniquing.
  80. static bool isODRAttribute(uint16_t Attr) {
  81. switch (Attr) {
  82. default:
  83. return false;
  84. case dwarf::DW_AT_type:
  85. case dwarf::DW_AT_containing_type:
  86. case dwarf::DW_AT_specification:
  87. case dwarf::DW_AT_abstract_origin:
  88. case dwarf::DW_AT_import:
  89. return true;
  90. }
  91. llvm_unreachable("Improper attribute.");
  92. }
  93. static bool isTypeTag(uint16_t Tag) {
  94. switch (Tag) {
  95. case dwarf::DW_TAG_array_type:
  96. case dwarf::DW_TAG_class_type:
  97. case dwarf::DW_TAG_enumeration_type:
  98. case dwarf::DW_TAG_pointer_type:
  99. case dwarf::DW_TAG_reference_type:
  100. case dwarf::DW_TAG_string_type:
  101. case dwarf::DW_TAG_structure_type:
  102. case dwarf::DW_TAG_subroutine_type:
  103. case dwarf::DW_TAG_typedef:
  104. case dwarf::DW_TAG_union_type:
  105. case dwarf::DW_TAG_ptr_to_member_type:
  106. case dwarf::DW_TAG_set_type:
  107. case dwarf::DW_TAG_subrange_type:
  108. case dwarf::DW_TAG_base_type:
  109. case dwarf::DW_TAG_const_type:
  110. case dwarf::DW_TAG_constant:
  111. case dwarf::DW_TAG_file_type:
  112. case dwarf::DW_TAG_namelist:
  113. case dwarf::DW_TAG_packed_type:
  114. case dwarf::DW_TAG_volatile_type:
  115. case dwarf::DW_TAG_restrict_type:
  116. case dwarf::DW_TAG_atomic_type:
  117. case dwarf::DW_TAG_interface_type:
  118. case dwarf::DW_TAG_unspecified_type:
  119. case dwarf::DW_TAG_shared_type:
  120. case dwarf::DW_TAG_immutable_type:
  121. return true;
  122. default:
  123. break;
  124. }
  125. return false;
  126. }
  127. AddressesMap::~AddressesMap() = default;
  128. DwarfEmitter::~DwarfEmitter() = default;
  129. static std::optional<StringRef> StripTemplateParameters(StringRef Name) {
  130. // We are looking for template parameters to strip from Name. e.g.
  131. //
  132. // operator<<B>
  133. //
  134. // We look for > at the end but if it does not contain any < then we
  135. // have something like operator>>. We check for the operator<=> case.
  136. if (!Name.endswith(">") || Name.count("<") == 0 || Name.endswith("<=>"))
  137. return {};
  138. // How many < until we have the start of the template parameters.
  139. size_t NumLeftAnglesToSkip = 1;
  140. // If we have operator<=> then we need to skip its < as well.
  141. NumLeftAnglesToSkip += Name.count("<=>");
  142. size_t RightAngleCount = Name.count('>');
  143. size_t LeftAngleCount = Name.count('<');
  144. // If we have more < than > we have operator< or operator<<
  145. // we to account for their < as well.
  146. if (LeftAngleCount > RightAngleCount)
  147. NumLeftAnglesToSkip += LeftAngleCount - RightAngleCount;
  148. size_t StartOfTemplate = 0;
  149. while (NumLeftAnglesToSkip--)
  150. StartOfTemplate = Name.find('<', StartOfTemplate) + 1;
  151. return Name.substr(0, StartOfTemplate - 1);
  152. }
  153. bool DWARFLinker::DIECloner::getDIENames(const DWARFDie &Die,
  154. AttributesInfo &Info,
  155. OffsetsStringPool &StringPool,
  156. bool StripTemplate) {
  157. // This function will be called on DIEs having low_pcs and
  158. // ranges. As getting the name might be more expansive, filter out
  159. // blocks directly.
  160. if (Die.getTag() == dwarf::DW_TAG_lexical_block)
  161. return false;
  162. if (!Info.MangledName)
  163. if (const char *MangledName = Die.getLinkageName())
  164. Info.MangledName = StringPool.getEntry(MangledName);
  165. if (!Info.Name)
  166. if (const char *Name = Die.getShortName())
  167. Info.Name = StringPool.getEntry(Name);
  168. if (!Info.MangledName)
  169. Info.MangledName = Info.Name;
  170. if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
  171. StringRef Name = Info.Name.getString();
  172. if (std::optional<StringRef> StrippedName = StripTemplateParameters(Name))
  173. Info.NameWithoutTemplate = StringPool.getEntry(*StrippedName);
  174. }
  175. return Info.Name || Info.MangledName;
  176. }
  177. /// Resolve the relative path to a build artifact referenced by DWARF by
  178. /// applying DW_AT_comp_dir.
  179. static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
  180. sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
  181. }
  182. /// Collect references to parseable Swift interfaces in imported
  183. /// DW_TAG_module blocks.
  184. static void analyzeImportedModule(
  185. const DWARFDie &DIE, CompileUnit &CU,
  186. swiftInterfacesMap *ParseableSwiftInterfaces,
  187. std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
  188. if (CU.getLanguage() != dwarf::DW_LANG_Swift)
  189. return;
  190. if (!ParseableSwiftInterfaces)
  191. return;
  192. StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
  193. if (!Path.endswith(".swiftinterface"))
  194. return;
  195. // Don't track interfaces that are part of the SDK.
  196. StringRef SysRoot = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_sysroot));
  197. if (SysRoot.empty())
  198. SysRoot = CU.getSysRoot();
  199. if (!SysRoot.empty() && Path.startswith(SysRoot))
  200. return;
  201. std::optional<const char *> Name =
  202. dwarf::toString(DIE.find(dwarf::DW_AT_name));
  203. if (!Name)
  204. return;
  205. auto &Entry = (*ParseableSwiftInterfaces)[*Name];
  206. // The prepend path is applied later when copying.
  207. DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
  208. SmallString<128> ResolvedPath;
  209. if (sys::path::is_relative(Path))
  210. resolveRelativeObjectPath(ResolvedPath, CUDie);
  211. sys::path::append(ResolvedPath, Path);
  212. if (!Entry.empty() && Entry != ResolvedPath)
  213. ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") +
  214. *Name + ": " + Entry + " and " + Path,
  215. DIE);
  216. Entry = std::string(ResolvedPath.str());
  217. }
  218. /// The distinct types of work performed by the work loop in
  219. /// analyzeContextInfo.
  220. enum class ContextWorklistItemType : uint8_t {
  221. AnalyzeContextInfo,
  222. UpdateChildPruning,
  223. UpdatePruning,
  224. };
  225. /// This class represents an item in the work list. The type defines what kind
  226. /// of work needs to be performed when processing the current item. Everything
  227. /// but the Type and Die fields are optional based on the type.
  228. struct ContextWorklistItem {
  229. DWARFDie Die;
  230. unsigned ParentIdx;
  231. union {
  232. CompileUnit::DIEInfo *OtherInfo;
  233. DeclContext *Context;
  234. };
  235. ContextWorklistItemType Type;
  236. bool InImportedModule;
  237. ContextWorklistItem(DWARFDie Die, ContextWorklistItemType T,
  238. CompileUnit::DIEInfo *OtherInfo = nullptr)
  239. : Die(Die), ParentIdx(0), OtherInfo(OtherInfo), Type(T),
  240. InImportedModule(false) {}
  241. ContextWorklistItem(DWARFDie Die, DeclContext *Context, unsigned ParentIdx,
  242. bool InImportedModule)
  243. : Die(Die), ParentIdx(ParentIdx), Context(Context),
  244. Type(ContextWorklistItemType::AnalyzeContextInfo),
  245. InImportedModule(InImportedModule) {}
  246. };
  247. static bool updatePruning(const DWARFDie &Die, CompileUnit &CU,
  248. uint64_t ModulesEndOffset) {
  249. CompileUnit::DIEInfo &Info = CU.getInfo(Die);
  250. // Prune this DIE if it is either a forward declaration inside a
  251. // DW_TAG_module or a DW_TAG_module that contains nothing but
  252. // forward declarations.
  253. Info.Prune &= (Die.getTag() == dwarf::DW_TAG_module) ||
  254. (isTypeTag(Die.getTag()) &&
  255. dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0));
  256. // Only prune forward declarations inside a DW_TAG_module for which a
  257. // definition exists elsewhere.
  258. if (ModulesEndOffset == 0)
  259. Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
  260. else
  261. Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
  262. Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
  263. return Info.Prune;
  264. }
  265. static void updateChildPruning(const DWARFDie &Die, CompileUnit &CU,
  266. CompileUnit::DIEInfo &ChildInfo) {
  267. CompileUnit::DIEInfo &Info = CU.getInfo(Die);
  268. Info.Prune &= ChildInfo.Prune;
  269. }
  270. /// Recursive helper to build the global DeclContext information and
  271. /// gather the child->parent relationships in the original compile unit.
  272. ///
  273. /// This function uses the same work list approach as lookForDIEsToKeep.
  274. ///
  275. /// \return true when this DIE and all of its children are only
  276. /// forward declarations to types defined in external clang modules
  277. /// (i.e., forward declarations that are children of a DW_TAG_module).
  278. static void analyzeContextInfo(
  279. const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU,
  280. DeclContext *CurrentDeclContext, DeclContextTree &Contexts,
  281. uint64_t ModulesEndOffset, swiftInterfacesMap *ParseableSwiftInterfaces,
  282. std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
  283. // LIFO work list.
  284. std::vector<ContextWorklistItem> Worklist;
  285. Worklist.emplace_back(DIE, CurrentDeclContext, ParentIdx, false);
  286. while (!Worklist.empty()) {
  287. ContextWorklistItem Current = Worklist.back();
  288. Worklist.pop_back();
  289. switch (Current.Type) {
  290. case ContextWorklistItemType::UpdatePruning:
  291. updatePruning(Current.Die, CU, ModulesEndOffset);
  292. continue;
  293. case ContextWorklistItemType::UpdateChildPruning:
  294. updateChildPruning(Current.Die, CU, *Current.OtherInfo);
  295. continue;
  296. case ContextWorklistItemType::AnalyzeContextInfo:
  297. break;
  298. }
  299. unsigned Idx = CU.getOrigUnit().getDIEIndex(Current.Die);
  300. CompileUnit::DIEInfo &Info = CU.getInfo(Idx);
  301. // Clang imposes an ODR on modules(!) regardless of the language:
  302. // "The module-id should consist of only a single identifier,
  303. // which provides the name of the module being defined. Each
  304. // module shall have a single definition."
  305. //
  306. // This does not extend to the types inside the modules:
  307. // "[I]n C, this implies that if two structs are defined in
  308. // different submodules with the same name, those two types are
  309. // distinct types (but may be compatible types if their
  310. // definitions match)."
  311. //
  312. // We treat non-C++ modules like namespaces for this reason.
  313. if (Current.Die.getTag() == dwarf::DW_TAG_module &&
  314. Current.ParentIdx == 0 &&
  315. dwarf::toString(Current.Die.find(dwarf::DW_AT_name), "") !=
  316. CU.getClangModuleName()) {
  317. Current.InImportedModule = true;
  318. analyzeImportedModule(Current.Die, CU, ParseableSwiftInterfaces,
  319. ReportWarning);
  320. }
  321. Info.ParentIdx = Current.ParentIdx;
  322. Info.InModuleScope = CU.isClangModule() || Current.InImportedModule;
  323. if (CU.hasODR() || Info.InModuleScope) {
  324. if (Current.Context) {
  325. auto PtrInvalidPair = Contexts.getChildDeclContext(
  326. *Current.Context, Current.Die, CU, Info.InModuleScope);
  327. Current.Context = PtrInvalidPair.getPointer();
  328. Info.Ctxt =
  329. PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
  330. if (Info.Ctxt)
  331. Info.Ctxt->setDefinedInClangModule(Info.InModuleScope);
  332. } else
  333. Info.Ctxt = Current.Context = nullptr;
  334. }
  335. Info.Prune = Current.InImportedModule;
  336. // Add children in reverse order to the worklist to effectively process
  337. // them in order.
  338. Worklist.emplace_back(Current.Die, ContextWorklistItemType::UpdatePruning);
  339. for (auto Child : reverse(Current.Die.children())) {
  340. CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child);
  341. Worklist.emplace_back(
  342. Current.Die, ContextWorklistItemType::UpdateChildPruning, &ChildInfo);
  343. Worklist.emplace_back(Child, Current.Context, Idx,
  344. Current.InImportedModule);
  345. }
  346. }
  347. }
  348. static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
  349. switch (Tag) {
  350. default:
  351. return false;
  352. case dwarf::DW_TAG_class_type:
  353. case dwarf::DW_TAG_common_block:
  354. case dwarf::DW_TAG_lexical_block:
  355. case dwarf::DW_TAG_structure_type:
  356. case dwarf::DW_TAG_subprogram:
  357. case dwarf::DW_TAG_subroutine_type:
  358. case dwarf::DW_TAG_union_type:
  359. return true;
  360. }
  361. llvm_unreachable("Invalid Tag");
  362. }
  363. void DWARFLinker::cleanupAuxiliarryData(LinkContext &Context) {
  364. Context.clear();
  365. for (DIEBlock *I : DIEBlocks)
  366. I->~DIEBlock();
  367. for (DIELoc *I : DIELocs)
  368. I->~DIELoc();
  369. DIEBlocks.clear();
  370. DIELocs.clear();
  371. DIEAlloc.Reset();
  372. }
  373. /// Check if a variable describing DIE should be kept.
  374. /// \returns updated TraversalFlags.
  375. unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap &RelocMgr,
  376. const DWARFDie &DIE,
  377. CompileUnit::DIEInfo &MyInfo,
  378. unsigned Flags) {
  379. const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
  380. // Global variables with constant value can always be kept.
  381. if (!(Flags & TF_InFunctionScope) &&
  382. Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
  383. MyInfo.InDebugMap = true;
  384. return Flags | TF_Keep;
  385. }
  386. // See if there is a relocation to a valid debug map entry inside this
  387. // variable's location. The order is important here. We want to always check
  388. // if the variable has a valid relocation, so that the DIEInfo is filled.
  389. // However, we don't want a static variable in a function to force us to keep
  390. // the enclosing function, unless requested explicitly.
  391. const bool HasLiveMemoryLocation = RelocMgr.isLiveVariable(DIE, MyInfo);
  392. if (!HasLiveMemoryLocation || ((Flags & TF_InFunctionScope) &&
  393. !LLVM_UNLIKELY(Options.KeepFunctionForStatic)))
  394. return Flags;
  395. if (Options.Verbose) {
  396. outs() << "Keeping variable DIE:";
  397. DIDumpOptions DumpOpts;
  398. DumpOpts.ChildRecurseDepth = 0;
  399. DumpOpts.Verbose = Options.Verbose;
  400. DIE.dump(outs(), 8 /* Indent */, DumpOpts);
  401. }
  402. return Flags | TF_Keep;
  403. }
  404. /// Check if a function describing DIE should be kept.
  405. /// \returns updated TraversalFlags.
  406. unsigned DWARFLinker::shouldKeepSubprogramDIE(
  407. AddressesMap &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
  408. const DWARFFile &File, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
  409. unsigned Flags) {
  410. Flags |= TF_InFunctionScope;
  411. auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
  412. if (!LowPc)
  413. return Flags;
  414. assert(LowPc && "low_pc attribute is not an address.");
  415. if (!RelocMgr.isLiveSubprogram(DIE, MyInfo))
  416. return Flags;
  417. if (Options.Verbose) {
  418. outs() << "Keeping subprogram DIE:";
  419. DIDumpOptions DumpOpts;
  420. DumpOpts.ChildRecurseDepth = 0;
  421. DumpOpts.Verbose = Options.Verbose;
  422. DIE.dump(outs(), 8 /* Indent */, DumpOpts);
  423. }
  424. if (DIE.getTag() == dwarf::DW_TAG_label) {
  425. if (Unit.hasLabelAt(*LowPc))
  426. return Flags;
  427. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  428. // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
  429. // that don't fall into the CU's aranges. This is wrong IMO. Debug info
  430. // generation bugs aside, this is really wrong in the case of labels, where
  431. // a label marking the end of a function will have a PC == CU's high_pc.
  432. if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
  433. .value_or(UINT64_MAX) <= LowPc)
  434. return Flags;
  435. Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
  436. return Flags | TF_Keep;
  437. }
  438. Flags |= TF_Keep;
  439. std::optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
  440. if (!HighPc) {
  441. reportWarning("Function without high_pc. Range will be discarded.\n", File,
  442. &DIE);
  443. return Flags;
  444. }
  445. if (*LowPc > *HighPc) {
  446. reportWarning("low_pc greater than high_pc. Range will be discarded.\n",
  447. File, &DIE);
  448. return Flags;
  449. }
  450. // Replace the debug map range with a more accurate one.
  451. Ranges.insert({*LowPc, *HighPc}, MyInfo.AddrAdjust);
  452. Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
  453. return Flags;
  454. }
  455. /// Check if a DIE should be kept.
  456. /// \returns updated TraversalFlags.
  457. unsigned DWARFLinker::shouldKeepDIE(AddressesMap &RelocMgr, RangesTy &Ranges,
  458. const DWARFDie &DIE, const DWARFFile &File,
  459. CompileUnit &Unit,
  460. CompileUnit::DIEInfo &MyInfo,
  461. unsigned Flags) {
  462. switch (DIE.getTag()) {
  463. case dwarf::DW_TAG_constant:
  464. case dwarf::DW_TAG_variable:
  465. return shouldKeepVariableDIE(RelocMgr, DIE, MyInfo, Flags);
  466. case dwarf::DW_TAG_subprogram:
  467. case dwarf::DW_TAG_label:
  468. return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, File, Unit, MyInfo,
  469. Flags);
  470. case dwarf::DW_TAG_base_type:
  471. // DWARF Expressions may reference basic types, but scanning them
  472. // is expensive. Basic types are tiny, so just keep all of them.
  473. case dwarf::DW_TAG_imported_module:
  474. case dwarf::DW_TAG_imported_declaration:
  475. case dwarf::DW_TAG_imported_unit:
  476. // We always want to keep these.
  477. return Flags | TF_Keep;
  478. default:
  479. break;
  480. }
  481. return Flags;
  482. }
  483. /// Helper that updates the completeness of the current DIE based on the
  484. /// completeness of one of its children. It depends on the incompleteness of
  485. /// the children already being computed.
  486. static void updateChildIncompleteness(const DWARFDie &Die, CompileUnit &CU,
  487. CompileUnit::DIEInfo &ChildInfo) {
  488. switch (Die.getTag()) {
  489. case dwarf::DW_TAG_structure_type:
  490. case dwarf::DW_TAG_class_type:
  491. case dwarf::DW_TAG_union_type:
  492. break;
  493. default:
  494. return;
  495. }
  496. CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die);
  497. if (ChildInfo.Incomplete || ChildInfo.Prune)
  498. MyInfo.Incomplete = true;
  499. }
  500. /// Helper that updates the completeness of the current DIE based on the
  501. /// completeness of the DIEs it references. It depends on the incompleteness of
  502. /// the referenced DIE already being computed.
  503. static void updateRefIncompleteness(const DWARFDie &Die, CompileUnit &CU,
  504. CompileUnit::DIEInfo &RefInfo) {
  505. switch (Die.getTag()) {
  506. case dwarf::DW_TAG_typedef:
  507. case dwarf::DW_TAG_member:
  508. case dwarf::DW_TAG_reference_type:
  509. case dwarf::DW_TAG_ptr_to_member_type:
  510. case dwarf::DW_TAG_pointer_type:
  511. break;
  512. default:
  513. return;
  514. }
  515. CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die);
  516. if (MyInfo.Incomplete)
  517. return;
  518. if (RefInfo.Incomplete)
  519. MyInfo.Incomplete = true;
  520. }
  521. /// Look at the children of the given DIE and decide whether they should be
  522. /// kept.
  523. void DWARFLinker::lookForChildDIEsToKeep(
  524. const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
  525. SmallVectorImpl<WorklistItem> &Worklist) {
  526. // The TF_ParentWalk flag tells us that we are currently walking up the
  527. // parent chain of a required DIE, and we don't want to mark all the children
  528. // of the parents as kept (consider for example a DW_TAG_namespace node in
  529. // the parent chain). There are however a set of DIE types for which we want
  530. // to ignore that directive and still walk their children.
  531. if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
  532. Flags &= ~DWARFLinker::TF_ParentWalk;
  533. // We're finished if this DIE has no children or we're walking the parent
  534. // chain.
  535. if (!Die.hasChildren() || (Flags & DWARFLinker::TF_ParentWalk))
  536. return;
  537. // Add children in reverse order to the worklist to effectively process them
  538. // in order.
  539. for (auto Child : reverse(Die.children())) {
  540. // Add a worklist item before every child to calculate incompleteness right
  541. // after the current child is processed.
  542. CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child);
  543. Worklist.emplace_back(Die, CU, WorklistItemType::UpdateChildIncompleteness,
  544. &ChildInfo);
  545. Worklist.emplace_back(Child, CU, Flags);
  546. }
  547. }
  548. static bool isODRCanonicalCandidate(const DWARFDie &Die, CompileUnit &CU) {
  549. CompileUnit::DIEInfo &Info = CU.getInfo(Die);
  550. if (!Info.Ctxt || (Die.getTag() == dwarf::DW_TAG_namespace))
  551. return false;
  552. if (!CU.hasODR() && !Info.InModuleScope)
  553. return false;
  554. return !Info.Incomplete && Info.Ctxt != CU.getInfo(Info.ParentIdx).Ctxt;
  555. }
  556. void DWARFLinker::markODRCanonicalDie(const DWARFDie &Die, CompileUnit &CU) {
  557. CompileUnit::DIEInfo &Info = CU.getInfo(Die);
  558. Info.ODRMarkingDone = true;
  559. if (Info.Keep && isODRCanonicalCandidate(Die, CU) &&
  560. !Info.Ctxt->hasCanonicalDIE())
  561. Info.Ctxt->setHasCanonicalDIE();
  562. }
  563. /// Look at DIEs referenced by the given DIE and decide whether they should be
  564. /// kept. All DIEs referenced though attributes should be kept.
  565. void DWARFLinker::lookForRefDIEsToKeep(
  566. const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
  567. const UnitListTy &Units, const DWARFFile &File,
  568. SmallVectorImpl<WorklistItem> &Worklist) {
  569. bool UseOdr = (Flags & DWARFLinker::TF_DependencyWalk)
  570. ? (Flags & DWARFLinker::TF_ODR)
  571. : CU.hasODR();
  572. DWARFUnit &Unit = CU.getOrigUnit();
  573. DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
  574. const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
  575. uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
  576. SmallVector<std::pair<DWARFDie, CompileUnit &>, 4> ReferencedDIEs;
  577. for (const auto &AttrSpec : Abbrev->attributes()) {
  578. DWARFFormValue Val(AttrSpec.Form);
  579. if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
  580. AttrSpec.Attr == dwarf::DW_AT_sibling) {
  581. DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
  582. Unit.getFormParams());
  583. continue;
  584. }
  585. Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
  586. CompileUnit *ReferencedCU;
  587. if (auto RefDie =
  588. resolveDIEReference(File, Units, Val, Die, ReferencedCU)) {
  589. CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefDie);
  590. // If the referenced DIE has a DeclContext that has already been
  591. // emitted, then do not keep the one in this CU. We'll link to
  592. // the canonical DIE in cloneDieReferenceAttribute.
  593. //
  594. // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
  595. // be necessary and could be advantageously replaced by
  596. // ReferencedCU->hasODR() && CU.hasODR().
  597. //
  598. // FIXME: compatibility with dsymutil-classic. There is no
  599. // reason not to unique ref_addr references.
  600. if (AttrSpec.Form != dwarf::DW_FORM_ref_addr &&
  601. isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
  602. Info.Ctxt->hasCanonicalDIE())
  603. continue;
  604. // Keep a module forward declaration if there is no definition.
  605. if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
  606. Info.Ctxt->hasCanonicalDIE()))
  607. Info.Prune = false;
  608. ReferencedDIEs.emplace_back(RefDie, *ReferencedCU);
  609. }
  610. }
  611. unsigned ODRFlag = UseOdr ? DWARFLinker::TF_ODR : 0;
  612. // Add referenced DIEs in reverse order to the worklist to effectively
  613. // process them in order.
  614. for (auto &P : reverse(ReferencedDIEs)) {
  615. // Add a worklist item before every child to calculate incompleteness right
  616. // after the current child is processed.
  617. CompileUnit::DIEInfo &Info = P.second.getInfo(P.first);
  618. Worklist.emplace_back(Die, CU, WorklistItemType::UpdateRefIncompleteness,
  619. &Info);
  620. Worklist.emplace_back(P.first, P.second,
  621. DWARFLinker::TF_Keep |
  622. DWARFLinker::TF_DependencyWalk | ODRFlag);
  623. }
  624. }
  625. /// Look at the parent of the given DIE and decide whether they should be kept.
  626. void DWARFLinker::lookForParentDIEsToKeep(
  627. unsigned AncestorIdx, CompileUnit &CU, unsigned Flags,
  628. SmallVectorImpl<WorklistItem> &Worklist) {
  629. // Stop if we encounter an ancestor that's already marked as kept.
  630. if (CU.getInfo(AncestorIdx).Keep)
  631. return;
  632. DWARFUnit &Unit = CU.getOrigUnit();
  633. DWARFDie ParentDIE = Unit.getDIEAtIndex(AncestorIdx);
  634. Worklist.emplace_back(CU.getInfo(AncestorIdx).ParentIdx, CU, Flags);
  635. Worklist.emplace_back(ParentDIE, CU, Flags);
  636. }
  637. /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that
  638. /// information in \p CU's DIEInfo.
  639. ///
  640. /// This function is the entry point of the DIE selection algorithm. It is
  641. /// expected to walk the DIE tree in file order and (though the mediation of
  642. /// its helper) call hasValidRelocation() on each DIE that might be a 'root
  643. /// DIE' (See DwarfLinker class comment).
  644. ///
  645. /// While walking the dependencies of root DIEs, this function is also called,
  646. /// but during these dependency walks the file order is not respected. The
  647. /// TF_DependencyWalk flag tells us which kind of traversal we are currently
  648. /// doing.
  649. ///
  650. /// The recursive algorithm is implemented iteratively as a work list because
  651. /// very deep recursion could exhaust the stack for large projects. The work
  652. /// list acts as a scheduler for different types of work that need to be
  653. /// performed.
  654. ///
  655. /// The recursive nature of the algorithm is simulated by running the "main"
  656. /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs
  657. /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or
  658. /// fixing up a computed property (UpdateChildIncompleteness,
  659. /// UpdateRefIncompleteness).
  660. ///
  661. /// The return value indicates whether the DIE is incomplete.
  662. void DWARFLinker::lookForDIEsToKeep(AddressesMap &AddressesMap,
  663. RangesTy &Ranges, const UnitListTy &Units,
  664. const DWARFDie &Die, const DWARFFile &File,
  665. CompileUnit &Cu, unsigned Flags) {
  666. // LIFO work list.
  667. SmallVector<WorklistItem, 4> Worklist;
  668. Worklist.emplace_back(Die, Cu, Flags);
  669. while (!Worklist.empty()) {
  670. WorklistItem Current = Worklist.pop_back_val();
  671. // Look at the worklist type to decide what kind of work to perform.
  672. switch (Current.Type) {
  673. case WorklistItemType::UpdateChildIncompleteness:
  674. updateChildIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
  675. continue;
  676. case WorklistItemType::UpdateRefIncompleteness:
  677. updateRefIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
  678. continue;
  679. case WorklistItemType::LookForChildDIEsToKeep:
  680. lookForChildDIEsToKeep(Current.Die, Current.CU, Current.Flags, Worklist);
  681. continue;
  682. case WorklistItemType::LookForRefDIEsToKeep:
  683. lookForRefDIEsToKeep(Current.Die, Current.CU, Current.Flags, Units, File,
  684. Worklist);
  685. continue;
  686. case WorklistItemType::LookForParentDIEsToKeep:
  687. lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags,
  688. Worklist);
  689. continue;
  690. case WorklistItemType::MarkODRCanonicalDie:
  691. markODRCanonicalDie(Current.Die, Current.CU);
  692. continue;
  693. case WorklistItemType::LookForDIEsToKeep:
  694. break;
  695. }
  696. unsigned Idx = Current.CU.getOrigUnit().getDIEIndex(Current.Die);
  697. CompileUnit::DIEInfo &MyInfo = Current.CU.getInfo(Idx);
  698. if (MyInfo.Prune) {
  699. // We're walking the dependencies of a module forward declaration that was
  700. // kept because there is no definition.
  701. if (Current.Flags & TF_DependencyWalk)
  702. MyInfo.Prune = false;
  703. else
  704. continue;
  705. }
  706. // If the Keep flag is set, we are marking a required DIE's dependencies.
  707. // If our target is already marked as kept, we're all set.
  708. bool AlreadyKept = MyInfo.Keep;
  709. if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
  710. continue;
  711. // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
  712. // because it would screw up the relocation finding logic.
  713. if (!(Current.Flags & TF_DependencyWalk))
  714. Current.Flags = shouldKeepDIE(AddressesMap, Ranges, Current.Die, File,
  715. Current.CU, MyInfo, Current.Flags);
  716. // We need to mark context for the canonical die in the end of normal
  717. // traversing(not TF_DependencyWalk) or after normal traversing if die
  718. // was not marked as kept.
  719. if (!(Current.Flags & TF_DependencyWalk) ||
  720. (MyInfo.ODRMarkingDone && !MyInfo.Keep)) {
  721. if (Current.CU.hasODR() || MyInfo.InModuleScope)
  722. Worklist.emplace_back(Current.Die, Current.CU,
  723. WorklistItemType::MarkODRCanonicalDie);
  724. }
  725. // Finish by looking for child DIEs. Because of the LIFO worklist we need
  726. // to schedule that work before any subsequent items are added to the
  727. // worklist.
  728. Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
  729. WorklistItemType::LookForChildDIEsToKeep);
  730. if (AlreadyKept || !(Current.Flags & TF_Keep))
  731. continue;
  732. // If it is a newly kept DIE mark it as well as all its dependencies as
  733. // kept.
  734. MyInfo.Keep = true;
  735. // We're looking for incomplete types.
  736. MyInfo.Incomplete =
  737. Current.Die.getTag() != dwarf::DW_TAG_subprogram &&
  738. Current.Die.getTag() != dwarf::DW_TAG_member &&
  739. dwarf::toUnsigned(Current.Die.find(dwarf::DW_AT_declaration), 0);
  740. // After looking at the parent chain, look for referenced DIEs. Because of
  741. // the LIFO worklist we need to schedule that work before any subsequent
  742. // items are added to the worklist.
  743. Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
  744. WorklistItemType::LookForRefDIEsToKeep);
  745. bool UseOdr = (Current.Flags & TF_DependencyWalk) ? (Current.Flags & TF_ODR)
  746. : Current.CU.hasODR();
  747. unsigned ODRFlag = UseOdr ? TF_ODR : 0;
  748. unsigned ParFlags = TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag;
  749. // Now schedule the parent walk.
  750. Worklist.emplace_back(MyInfo.ParentIdx, Current.CU, ParFlags);
  751. }
  752. }
  753. #ifndef NDEBUG
  754. /// A broken link in the keep chain. By recording both the parent and the child
  755. /// we can show only broken links for DIEs with multiple children.
  756. struct BrokenLink {
  757. BrokenLink(DWARFDie Parent, DWARFDie Child) : Parent(Parent), Child(Child) {}
  758. DWARFDie Parent;
  759. DWARFDie Child;
  760. };
  761. /// Verify the keep chain by looking for DIEs that are kept but who's parent
  762. /// isn't.
  763. static void verifyKeepChain(CompileUnit &CU) {
  764. std::vector<DWARFDie> Worklist;
  765. Worklist.push_back(CU.getOrigUnit().getUnitDIE());
  766. // List of broken links.
  767. std::vector<BrokenLink> BrokenLinks;
  768. while (!Worklist.empty()) {
  769. const DWARFDie Current = Worklist.back();
  770. Worklist.pop_back();
  771. const bool CurrentDieIsKept = CU.getInfo(Current).Keep;
  772. for (DWARFDie Child : reverse(Current.children())) {
  773. Worklist.push_back(Child);
  774. const bool ChildDieIsKept = CU.getInfo(Child).Keep;
  775. if (!CurrentDieIsKept && ChildDieIsKept)
  776. BrokenLinks.emplace_back(Current, Child);
  777. }
  778. }
  779. if (!BrokenLinks.empty()) {
  780. for (BrokenLink Link : BrokenLinks) {
  781. WithColor::error() << formatv(
  782. "Found invalid link in keep chain between {0:x} and {1:x}\n",
  783. Link.Parent.getOffset(), Link.Child.getOffset());
  784. errs() << "Parent:";
  785. Link.Parent.dump(errs(), 0, {});
  786. CU.getInfo(Link.Parent).dump();
  787. errs() << "Child:";
  788. Link.Child.dump(errs(), 2, {});
  789. CU.getInfo(Link.Child).dump();
  790. }
  791. report_fatal_error("invalid keep chain");
  792. }
  793. }
  794. #endif
  795. /// Assign an abbreviation number to \p Abbrev.
  796. ///
  797. /// Our DIEs get freed after every DebugMapObject has been processed,
  798. /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
  799. /// the instances hold by the DIEs. When we encounter an abbreviation
  800. /// that we don't know, we create a permanent copy of it.
  801. void DWARFLinker::assignAbbrev(DIEAbbrev &Abbrev) {
  802. // Check the set for priors.
  803. FoldingSetNodeID ID;
  804. Abbrev.Profile(ID);
  805. void *InsertToken;
  806. DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
  807. // If it's newly added.
  808. if (InSet) {
  809. // Assign existing abbreviation number.
  810. Abbrev.setNumber(InSet->getNumber());
  811. } else {
  812. // Add to abbreviation list.
  813. Abbreviations.push_back(
  814. std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
  815. for (const auto &Attr : Abbrev.getData())
  816. Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
  817. AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
  818. // Assign the unique abbreviation number.
  819. Abbrev.setNumber(Abbreviations.size());
  820. Abbreviations.back()->setNumber(Abbreviations.size());
  821. }
  822. }
  823. unsigned DWARFLinker::DIECloner::cloneStringAttribute(
  824. DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
  825. const DWARFUnit &, OffsetsStringPool &StringPool, AttributesInfo &Info) {
  826. std::optional<const char *> String = dwarf::toString(Val);
  827. if (!String)
  828. return 0;
  829. // Switch everything to out of line strings.
  830. auto StringEntry = StringPool.getEntry(*String);
  831. // Update attributes info.
  832. if (AttrSpec.Attr == dwarf::DW_AT_name)
  833. Info.Name = StringEntry;
  834. else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
  835. AttrSpec.Attr == dwarf::DW_AT_linkage_name)
  836. Info.MangledName = StringEntry;
  837. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
  838. DIEInteger(StringEntry.getOffset()));
  839. return 4;
  840. }
  841. unsigned DWARFLinker::DIECloner::cloneDieReferenceAttribute(
  842. DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
  843. unsigned AttrSize, const DWARFFormValue &Val, const DWARFFile &File,
  844. CompileUnit &Unit) {
  845. const DWARFUnit &U = Unit.getOrigUnit();
  846. uint64_t Ref = *Val.getAsReference();
  847. DIE *NewRefDie = nullptr;
  848. CompileUnit *RefUnit = nullptr;
  849. DWARFDie RefDie =
  850. Linker.resolveDIEReference(File, CompileUnits, Val, InputDIE, RefUnit);
  851. // If the referenced DIE is not found, drop the attribute.
  852. if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
  853. return 0;
  854. CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(RefDie);
  855. // If we already have emitted an equivalent DeclContext, just point
  856. // at it.
  857. if (isODRAttribute(AttrSpec.Attr) && RefInfo.Ctxt &&
  858. RefInfo.Ctxt->getCanonicalDIEOffset()) {
  859. assert(RefInfo.Ctxt->hasCanonicalDIE() &&
  860. "Offset to canonical die is set, but context is not marked");
  861. DIEInteger Attr(RefInfo.Ctxt->getCanonicalDIEOffset());
  862. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  863. dwarf::DW_FORM_ref_addr, Attr);
  864. return U.getRefAddrByteSize();
  865. }
  866. if (!RefInfo.Clone) {
  867. // We haven't cloned this DIE yet. Just create an empty one and
  868. // store it. It'll get really cloned when we process it.
  869. RefInfo.UnclonedReference = true;
  870. RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
  871. }
  872. NewRefDie = RefInfo.Clone;
  873. if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
  874. (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
  875. // We cannot currently rely on a DIEEntry to emit ref_addr
  876. // references, because the implementation calls back to DwarfDebug
  877. // to find the unit offset. (We don't have a DwarfDebug)
  878. // FIXME: we should be able to design DIEEntry reliance on
  879. // DwarfDebug away.
  880. uint64_t Attr;
  881. if (Ref < InputDIE.getOffset() && !RefInfo.UnclonedReference) {
  882. // We have already cloned that DIE.
  883. uint32_t NewRefOffset =
  884. RefUnit->getStartOffset() + NewRefDie->getOffset();
  885. Attr = NewRefOffset;
  886. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  887. dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
  888. } else {
  889. // A forward reference. Note and fixup later.
  890. Attr = 0xBADDEF;
  891. Unit.noteForwardReference(
  892. NewRefDie, RefUnit, RefInfo.Ctxt,
  893. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  894. dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
  895. }
  896. return U.getRefAddrByteSize();
  897. }
  898. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  899. dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
  900. return AttrSize;
  901. }
  902. void DWARFLinker::DIECloner::cloneExpression(
  903. DataExtractor &Data, DWARFExpression Expression, const DWARFFile &File,
  904. CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer) {
  905. using Encoding = DWARFExpression::Operation::Encoding;
  906. uint64_t OpOffset = 0;
  907. for (auto &Op : Expression) {
  908. auto Description = Op.getDescription();
  909. // DW_OP_const_type is variable-length and has 3
  910. // operands. DWARFExpression thus far only supports 2.
  911. auto Op0 = Description.Op[0];
  912. auto Op1 = Description.Op[1];
  913. if ((Op0 == Encoding::BaseTypeRef && Op1 != Encoding::SizeNA) ||
  914. (Op1 == Encoding::BaseTypeRef && Op0 != Encoding::Size1))
  915. Linker.reportWarning("Unsupported DW_OP encoding.", File);
  916. if ((Op0 == Encoding::BaseTypeRef && Op1 == Encoding::SizeNA) ||
  917. (Op1 == Encoding::BaseTypeRef && Op0 == Encoding::Size1)) {
  918. // This code assumes that the other non-typeref operand fits into 1 byte.
  919. assert(OpOffset < Op.getEndOffset());
  920. uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
  921. assert(ULEBsize <= 16);
  922. // Copy over the operation.
  923. OutputBuffer.push_back(Op.getCode());
  924. uint64_t RefOffset;
  925. if (Op1 == Encoding::SizeNA) {
  926. RefOffset = Op.getRawOperand(0);
  927. } else {
  928. OutputBuffer.push_back(Op.getRawOperand(0));
  929. RefOffset = Op.getRawOperand(1);
  930. }
  931. uint32_t Offset = 0;
  932. // Look up the base type. For DW_OP_convert, the operand may be 0 to
  933. // instead indicate the generic type. The same holds for
  934. // DW_OP_reinterpret, which is currently not supported.
  935. if (RefOffset > 0 || Op.getCode() != dwarf::DW_OP_convert) {
  936. RefOffset += Unit.getOrigUnit().getOffset();
  937. auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset);
  938. CompileUnit::DIEInfo &Info = Unit.getInfo(RefDie);
  939. if (DIE *Clone = Info.Clone)
  940. Offset = Clone->getOffset();
  941. else
  942. Linker.reportWarning(
  943. "base type ref doesn't point to DW_TAG_base_type.", File);
  944. }
  945. uint8_t ULEB[16];
  946. unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
  947. if (RealSize > ULEBsize) {
  948. // Emit the generic type as a fallback.
  949. RealSize = encodeULEB128(0, ULEB, ULEBsize);
  950. Linker.reportWarning("base type ref doesn't fit.", File);
  951. }
  952. assert(RealSize == ULEBsize && "padding failed");
  953. ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
  954. OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end());
  955. } else {
  956. // Copy over everything else unmodified.
  957. StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset());
  958. OutputBuffer.append(Bytes.begin(), Bytes.end());
  959. }
  960. OpOffset = Op.getEndOffset();
  961. }
  962. }
  963. unsigned DWARFLinker::DIECloner::cloneBlockAttribute(
  964. DIE &Die, const DWARFFile &File, CompileUnit &Unit, AttributeSpec AttrSpec,
  965. const DWARFFormValue &Val, unsigned AttrSize, bool IsLittleEndian) {
  966. DIEValueList *Attr;
  967. DIEValue Value;
  968. DIELoc *Loc = nullptr;
  969. DIEBlock *Block = nullptr;
  970. if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
  971. Loc = new (DIEAlloc) DIELoc;
  972. Linker.DIELocs.push_back(Loc);
  973. } else {
  974. Block = new (DIEAlloc) DIEBlock;
  975. Linker.DIEBlocks.push_back(Block);
  976. }
  977. Attr = Loc ? static_cast<DIEValueList *>(Loc)
  978. : static_cast<DIEValueList *>(Block);
  979. if (Loc)
  980. Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
  981. dwarf::Form(AttrSpec.Form), Loc);
  982. else
  983. Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
  984. dwarf::Form(AttrSpec.Form), Block);
  985. // If the block is a DWARF Expression, clone it into the temporary
  986. // buffer using cloneExpression(), otherwise copy the data directly.
  987. SmallVector<uint8_t, 32> Buffer;
  988. ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
  989. if (DWARFAttribute::mayHaveLocationExpr(AttrSpec.Attr) &&
  990. (Val.isFormClass(DWARFFormValue::FC_Block) ||
  991. Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
  992. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  993. DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
  994. IsLittleEndian, OrigUnit.getAddressByteSize());
  995. DWARFExpression Expr(Data, OrigUnit.getAddressByteSize(),
  996. OrigUnit.getFormParams().Format);
  997. cloneExpression(Data, Expr, File, Unit, Buffer);
  998. Bytes = Buffer;
  999. }
  1000. for (auto Byte : Bytes)
  1001. Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
  1002. dwarf::DW_FORM_data1, DIEInteger(Byte));
  1003. // FIXME: If DIEBlock and DIELoc just reuses the Size field of
  1004. // the DIE class, this "if" could be replaced by
  1005. // Attr->setSize(Bytes.size()).
  1006. if (Loc)
  1007. Loc->setSize(Bytes.size());
  1008. else
  1009. Block->setSize(Bytes.size());
  1010. Die.addValue(DIEAlloc, Value);
  1011. return AttrSize;
  1012. }
  1013. unsigned DWARFLinker::DIECloner::cloneAddressAttribute(
  1014. DIE &Die, AttributeSpec AttrSpec, unsigned AttrSize,
  1015. const DWARFFormValue &Val, const CompileUnit &Unit, AttributesInfo &Info) {
  1016. if (LLVM_UNLIKELY(Linker.Options.Update)) {
  1017. if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
  1018. Info.HasLowPc = true;
  1019. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1020. dwarf::Form(AttrSpec.Form), DIEInteger(Val.getRawUValue()));
  1021. return AttrSize;
  1022. }
  1023. dwarf::Form Form = AttrSpec.Form;
  1024. uint64_t Addr = 0;
  1025. if (Form == dwarf::DW_FORM_addrx) {
  1026. if (std::optional<uint64_t> AddrOffsetSectionBase =
  1027. Unit.getOrigUnit().getAddrOffsetSectionBase()) {
  1028. uint64_t StartOffset =
  1029. *AddrOffsetSectionBase +
  1030. Val.getRawUValue() * Unit.getOrigUnit().getAddressByteSize();
  1031. uint64_t EndOffset =
  1032. StartOffset + Unit.getOrigUnit().getAddressByteSize();
  1033. if (llvm::Expected<uint64_t> RelocAddr =
  1034. ObjFile.Addresses->relocateIndexedAddr(StartOffset, EndOffset))
  1035. Addr = *RelocAddr;
  1036. else
  1037. Linker.reportWarning(toString(RelocAddr.takeError()), ObjFile);
  1038. } else
  1039. Linker.reportWarning("no base offset for address table", ObjFile);
  1040. // Generation of DWARFv5 .debug_addr table is not supported yet.
  1041. // Convert attribute into the dwarf::DW_FORM_addr.
  1042. Form = dwarf::DW_FORM_addr;
  1043. } else
  1044. Addr = *Val.getAsAddress();
  1045. if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
  1046. if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
  1047. Die.getTag() == dwarf::DW_TAG_lexical_block ||
  1048. Die.getTag() == dwarf::DW_TAG_label) {
  1049. // The low_pc of a block or inline subroutine might get
  1050. // relocated because it happens to match the low_pc of the
  1051. // enclosing subprogram. To prevent issues with that, always use
  1052. // the low_pc from the input DIE if relocations have been applied.
  1053. Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
  1054. ? Info.OrigLowPc
  1055. : Addr) +
  1056. Info.PCOffset;
  1057. } else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1058. if (std::optional<uint64_t> LowPC = Unit.getLowPc())
  1059. Addr = *LowPC;
  1060. else
  1061. return 0;
  1062. }
  1063. Info.HasLowPc = true;
  1064. } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
  1065. if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1066. if (uint64_t HighPc = Unit.getHighPc())
  1067. Addr = HighPc;
  1068. else
  1069. return 0;
  1070. } else
  1071. // If we have a high_pc recorded for the input DIE, use
  1072. // it. Otherwise (when no relocations where applied) just use the
  1073. // one we just decoded.
  1074. Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
  1075. } else if (AttrSpec.Attr == dwarf::DW_AT_call_return_pc) {
  1076. // Relocate a return PC address within a call site entry.
  1077. if (Die.getTag() == dwarf::DW_TAG_call_site)
  1078. Addr = (Info.OrigCallReturnPc ? Info.OrigCallReturnPc : Addr) +
  1079. Info.PCOffset;
  1080. } else if (AttrSpec.Attr == dwarf::DW_AT_call_pc) {
  1081. // Relocate the address of a branch instruction within a call site entry.
  1082. if (Die.getTag() == dwarf::DW_TAG_call_site)
  1083. Addr = (Info.OrigCallPc ? Info.OrigCallPc : Addr) + Info.PCOffset;
  1084. }
  1085. Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
  1086. static_cast<dwarf::Form>(Form), DIEInteger(Addr));
  1087. return Unit.getOrigUnit().getAddressByteSize();
  1088. }
  1089. unsigned DWARFLinker::DIECloner::cloneScalarAttribute(
  1090. DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File,
  1091. CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
  1092. unsigned AttrSize, AttributesInfo &Info) {
  1093. uint64_t Value;
  1094. // Check for the offset to the macro table. If offset is incorrect then we
  1095. // need to remove the attribute.
  1096. if (AttrSpec.Attr == dwarf::DW_AT_macro_info) {
  1097. if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
  1098. const DWARFDebugMacro *Macro = File.Dwarf->getDebugMacinfo();
  1099. if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
  1100. return 0;
  1101. }
  1102. }
  1103. if (AttrSpec.Attr == dwarf::DW_AT_macros) {
  1104. if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
  1105. const DWARFDebugMacro *Macro = File.Dwarf->getDebugMacro();
  1106. if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
  1107. return 0;
  1108. }
  1109. }
  1110. if (LLVM_UNLIKELY(Linker.Options.Update)) {
  1111. if (auto OptionalValue = Val.getAsUnsignedConstant())
  1112. Value = *OptionalValue;
  1113. else if (auto OptionalValue = Val.getAsSignedConstant())
  1114. Value = *OptionalValue;
  1115. else if (auto OptionalValue = Val.getAsSectionOffset())
  1116. Value = *OptionalValue;
  1117. else {
  1118. Linker.reportWarning(
  1119. "Unsupported scalar attribute form. Dropping attribute.", File,
  1120. &InputDIE);
  1121. return 0;
  1122. }
  1123. if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
  1124. Info.IsDeclaration = true;
  1125. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1126. dwarf::Form(AttrSpec.Form), DIEInteger(Value));
  1127. return AttrSize;
  1128. }
  1129. if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
  1130. Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1131. std::optional<uint64_t> LowPC = Unit.getLowPc();
  1132. if (!LowPC)
  1133. return 0;
  1134. // Dwarf >= 4 high_pc is an size, not an address.
  1135. Value = Unit.getHighPc() - *LowPC;
  1136. } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
  1137. Value = *Val.getAsSectionOffset();
  1138. else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
  1139. Value = *Val.getAsSignedConstant();
  1140. else if (auto OptionalValue = Val.getAsUnsignedConstant())
  1141. Value = *OptionalValue;
  1142. else {
  1143. Linker.reportWarning(
  1144. "Unsupported scalar attribute form. Dropping attribute.", File,
  1145. &InputDIE);
  1146. return 0;
  1147. }
  1148. PatchLocation Patch =
  1149. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1150. dwarf::Form(AttrSpec.Form), DIEInteger(Value));
  1151. if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
  1152. Unit.noteRangeAttribute(Die, Patch);
  1153. Info.HasRanges = true;
  1154. }
  1155. // A more generic way to check for location attributes would be
  1156. // nice, but it's very unlikely that any other attribute needs a
  1157. // location list.
  1158. // FIXME: use DWARFAttribute::mayHaveLocationDescription().
  1159. else if (AttrSpec.Attr == dwarf::DW_AT_location ||
  1160. AttrSpec.Attr == dwarf::DW_AT_frame_base) {
  1161. Unit.noteLocationAttribute(Patch, Info.PCOffset);
  1162. } else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
  1163. Info.IsDeclaration = true;
  1164. return AttrSize;
  1165. }
  1166. /// Clone \p InputDIE's attribute described by \p AttrSpec with
  1167. /// value \p Val, and add it to \p Die.
  1168. /// \returns the size of the cloned attribute.
  1169. unsigned DWARFLinker::DIECloner::cloneAttribute(
  1170. DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File,
  1171. CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
  1172. const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info,
  1173. bool IsLittleEndian) {
  1174. const DWARFUnit &U = Unit.getOrigUnit();
  1175. switch (AttrSpec.Form) {
  1176. case dwarf::DW_FORM_strp:
  1177. case dwarf::DW_FORM_string:
  1178. case dwarf::DW_FORM_strx:
  1179. case dwarf::DW_FORM_strx1:
  1180. case dwarf::DW_FORM_strx2:
  1181. case dwarf::DW_FORM_strx3:
  1182. case dwarf::DW_FORM_strx4:
  1183. return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
  1184. case dwarf::DW_FORM_ref_addr:
  1185. case dwarf::DW_FORM_ref1:
  1186. case dwarf::DW_FORM_ref2:
  1187. case dwarf::DW_FORM_ref4:
  1188. case dwarf::DW_FORM_ref8:
  1189. return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
  1190. File, Unit);
  1191. case dwarf::DW_FORM_block:
  1192. case dwarf::DW_FORM_block1:
  1193. case dwarf::DW_FORM_block2:
  1194. case dwarf::DW_FORM_block4:
  1195. case dwarf::DW_FORM_exprloc:
  1196. return cloneBlockAttribute(Die, File, Unit, AttrSpec, Val, AttrSize,
  1197. IsLittleEndian);
  1198. case dwarf::DW_FORM_addr:
  1199. case dwarf::DW_FORM_addrx:
  1200. return cloneAddressAttribute(Die, AttrSpec, AttrSize, Val, Unit, Info);
  1201. case dwarf::DW_FORM_data1:
  1202. case dwarf::DW_FORM_data2:
  1203. case dwarf::DW_FORM_data4:
  1204. case dwarf::DW_FORM_data8:
  1205. case dwarf::DW_FORM_udata:
  1206. case dwarf::DW_FORM_sdata:
  1207. case dwarf::DW_FORM_sec_offset:
  1208. case dwarf::DW_FORM_flag:
  1209. case dwarf::DW_FORM_flag_present:
  1210. return cloneScalarAttribute(Die, InputDIE, File, Unit, AttrSpec, Val,
  1211. AttrSize, Info);
  1212. default:
  1213. Linker.reportWarning("Unsupported attribute form " +
  1214. dwarf::FormEncodingString(AttrSpec.Form) +
  1215. " in cloneAttribute. Dropping.",
  1216. File, &InputDIE);
  1217. }
  1218. return 0;
  1219. }
  1220. static bool isObjCSelector(StringRef Name) {
  1221. return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
  1222. (Name[1] == '[');
  1223. }
  1224. void DWARFLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
  1225. const DIE *Die,
  1226. DwarfStringPoolEntryRef Name,
  1227. OffsetsStringPool &StringPool,
  1228. bool SkipPubSection) {
  1229. assert(isObjCSelector(Name.getString()) && "not an objc selector");
  1230. // Objective C method or class function.
  1231. // "- [Class(Category) selector :withArg ...]"
  1232. StringRef ClassNameStart(Name.getString().drop_front(2));
  1233. size_t FirstSpace = ClassNameStart.find(' ');
  1234. if (FirstSpace == StringRef::npos)
  1235. return;
  1236. StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
  1237. if (!SelectorStart.size())
  1238. return;
  1239. StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
  1240. Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
  1241. // Add an entry for the class name that points to this
  1242. // method/class function.
  1243. StringRef ClassName(ClassNameStart.data(), FirstSpace);
  1244. Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
  1245. if (ClassName[ClassName.size() - 1] == ')') {
  1246. size_t OpenParens = ClassName.find('(');
  1247. if (OpenParens != StringRef::npos) {
  1248. StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
  1249. Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
  1250. SkipPubSection);
  1251. std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
  1252. // FIXME: The missing space here may be a bug, but
  1253. // dsymutil-classic also does it this way.
  1254. MethodNameNoCategory.append(std::string(SelectorStart));
  1255. Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
  1256. SkipPubSection);
  1257. }
  1258. }
  1259. }
  1260. static bool shouldSkipAttribute(
  1261. bool Update, DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
  1262. uint16_t Tag, bool InDebugMap, bool SkipPC, bool InFunctionScope) {
  1263. switch (AttrSpec.Attr) {
  1264. default:
  1265. return false;
  1266. case dwarf::DW_AT_low_pc:
  1267. case dwarf::DW_AT_high_pc:
  1268. case dwarf::DW_AT_ranges:
  1269. return !Update && SkipPC;
  1270. case dwarf::DW_AT_str_offsets_base:
  1271. // FIXME: Use the string offset table with Dwarf 5.
  1272. return true;
  1273. case dwarf::DW_AT_location:
  1274. case dwarf::DW_AT_frame_base:
  1275. // FIXME: for some reason dsymutil-classic keeps the location attributes
  1276. // when they are of block type (i.e. not location lists). This is totally
  1277. // wrong for globals where we will keep a wrong address. It is mostly
  1278. // harmless for locals, but there is no point in keeping these anyway when
  1279. // the function wasn't linked.
  1280. return !Update &&
  1281. (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
  1282. !InDebugMap)) &&
  1283. !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
  1284. }
  1285. }
  1286. DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
  1287. const DWARFFile &File, CompileUnit &Unit,
  1288. OffsetsStringPool &StringPool,
  1289. int64_t PCOffset, uint32_t OutOffset,
  1290. unsigned Flags, bool IsLittleEndian,
  1291. DIE *Die) {
  1292. DWARFUnit &U = Unit.getOrigUnit();
  1293. unsigned Idx = U.getDIEIndex(InputDIE);
  1294. CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
  1295. // Should the DIE appear in the output?
  1296. if (!Unit.getInfo(Idx).Keep)
  1297. return nullptr;
  1298. uint64_t Offset = InputDIE.getOffset();
  1299. assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
  1300. if (!Die) {
  1301. // The DIE might have been already created by a forward reference
  1302. // (see cloneDieReferenceAttribute()).
  1303. if (!Info.Clone)
  1304. Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
  1305. Die = Info.Clone;
  1306. }
  1307. assert(Die->getTag() == InputDIE.getTag());
  1308. Die->setOffset(OutOffset);
  1309. if (isODRCanonicalCandidate(InputDIE, Unit) && Info.Ctxt &&
  1310. (Info.Ctxt->getCanonicalDIEOffset() == 0)) {
  1311. if (!Info.Ctxt->hasCanonicalDIE())
  1312. Info.Ctxt->setHasCanonicalDIE();
  1313. // We are about to emit a DIE that is the root of its own valid
  1314. // DeclContext tree. Make the current offset the canonical offset
  1315. // for this context.
  1316. Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
  1317. }
  1318. // Extract and clone every attribute.
  1319. DWARFDataExtractor Data = U.getDebugInfoExtractor();
  1320. // Point to the next DIE (generally there is always at least a NULL
  1321. // entry after the current one). If this is a lone
  1322. // DW_TAG_compile_unit without any children, point to the next unit.
  1323. uint64_t NextOffset = (Idx + 1 < U.getNumDIEs())
  1324. ? U.getDIEAtIndex(Idx + 1).getOffset()
  1325. : U.getNextUnitOffset();
  1326. AttributesInfo AttrInfo;
  1327. // We could copy the data only if we need to apply a relocation to it. After
  1328. // testing, it seems there is no performance downside to doing the copy
  1329. // unconditionally, and it makes the code simpler.
  1330. SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
  1331. Data =
  1332. DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
  1333. // Modify the copy with relocated addresses.
  1334. if (ObjFile.Addresses->applyValidRelocs(DIECopy, Offset,
  1335. Data.isLittleEndian())) {
  1336. // If we applied relocations, we store the value of high_pc that was
  1337. // potentially stored in the input DIE. If high_pc is an address
  1338. // (Dwarf version == 2), then it might have been relocated to a
  1339. // totally unrelated value (because the end address in the object
  1340. // file might be start address of another function which got moved
  1341. // independently by the linker). The computation of the actual
  1342. // high_pc value is done in cloneAddressAttribute().
  1343. AttrInfo.OrigHighPc =
  1344. dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
  1345. // Also store the low_pc. It might get relocated in an
  1346. // inline_subprogram that happens at the beginning of its
  1347. // inlining function.
  1348. AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
  1349. std::numeric_limits<uint64_t>::max());
  1350. AttrInfo.OrigCallReturnPc =
  1351. dwarf::toAddress(InputDIE.find(dwarf::DW_AT_call_return_pc), 0);
  1352. AttrInfo.OrigCallPc =
  1353. dwarf::toAddress(InputDIE.find(dwarf::DW_AT_call_pc), 0);
  1354. }
  1355. // Reset the Offset to 0 as we will be working on the local copy of
  1356. // the data.
  1357. Offset = 0;
  1358. const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
  1359. Offset += getULEB128Size(Abbrev->getCode());
  1360. // We are entering a subprogram. Get and propagate the PCOffset.
  1361. if (Die->getTag() == dwarf::DW_TAG_subprogram)
  1362. PCOffset = Info.AddrAdjust;
  1363. AttrInfo.PCOffset = PCOffset;
  1364. if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
  1365. Flags |= TF_InFunctionScope;
  1366. if (!Info.InDebugMap && LLVM_LIKELY(!Update))
  1367. Flags |= TF_SkipPC;
  1368. } else if (Abbrev->getTag() == dwarf::DW_TAG_variable) {
  1369. // Function-local globals could be in the debug map even when the function
  1370. // is not, e.g., inlined functions.
  1371. if ((Flags & TF_InFunctionScope) && Info.InDebugMap)
  1372. Flags &= ~TF_SkipPC;
  1373. }
  1374. for (const auto &AttrSpec : Abbrev->attributes()) {
  1375. if (shouldSkipAttribute(Update, AttrSpec, Die->getTag(), Info.InDebugMap,
  1376. Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
  1377. DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
  1378. U.getFormParams());
  1379. continue;
  1380. }
  1381. DWARFFormValue Val(AttrSpec.Form);
  1382. uint64_t AttrSize = Offset;
  1383. Val.extractValue(Data, &Offset, U.getFormParams(), &U);
  1384. AttrSize = Offset - AttrSize;
  1385. OutOffset += cloneAttribute(*Die, InputDIE, File, Unit, StringPool, Val,
  1386. AttrSpec, AttrSize, AttrInfo, IsLittleEndian);
  1387. }
  1388. // Look for accelerator entries.
  1389. uint16_t Tag = InputDIE.getTag();
  1390. // FIXME: This is slightly wrong. An inline_subroutine without a
  1391. // low_pc, but with AT_ranges might be interesting to get into the
  1392. // accelerator tables too. For now stick with dsymutil's behavior.
  1393. if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
  1394. Tag != dwarf::DW_TAG_compile_unit &&
  1395. getDIENames(InputDIE, AttrInfo, StringPool,
  1396. Tag != dwarf::DW_TAG_inlined_subroutine)) {
  1397. if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
  1398. Unit.addNameAccelerator(Die, AttrInfo.MangledName,
  1399. Tag == dwarf::DW_TAG_inlined_subroutine);
  1400. if (AttrInfo.Name) {
  1401. if (AttrInfo.NameWithoutTemplate)
  1402. Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
  1403. /* SkipPubSection */ true);
  1404. Unit.addNameAccelerator(Die, AttrInfo.Name,
  1405. Tag == dwarf::DW_TAG_inlined_subroutine);
  1406. }
  1407. if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
  1408. addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
  1409. /* SkipPubSection =*/true);
  1410. } else if (Tag == dwarf::DW_TAG_namespace) {
  1411. if (!AttrInfo.Name)
  1412. AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
  1413. Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
  1414. } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
  1415. getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
  1416. AttrInfo.Name.getString()[0]) {
  1417. uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
  1418. uint64_t RuntimeLang =
  1419. dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
  1420. .value_or(0);
  1421. bool ObjCClassIsImplementation =
  1422. (RuntimeLang == dwarf::DW_LANG_ObjC ||
  1423. RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
  1424. dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
  1425. .value_or(0);
  1426. Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
  1427. Hash);
  1428. }
  1429. // Determine whether there are any children that we want to keep.
  1430. bool HasChildren = false;
  1431. for (auto Child : InputDIE.children()) {
  1432. unsigned Idx = U.getDIEIndex(Child);
  1433. if (Unit.getInfo(Idx).Keep) {
  1434. HasChildren = true;
  1435. break;
  1436. }
  1437. }
  1438. DIEAbbrev NewAbbrev = Die->generateAbbrev();
  1439. if (HasChildren)
  1440. NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
  1441. // Assign a permanent abbrev number
  1442. Linker.assignAbbrev(NewAbbrev);
  1443. Die->setAbbrevNumber(NewAbbrev.getNumber());
  1444. // Add the size of the abbreviation number to the output offset.
  1445. OutOffset += getULEB128Size(Die->getAbbrevNumber());
  1446. if (!HasChildren) {
  1447. // Update our size.
  1448. Die->setSize(OutOffset - Die->getOffset());
  1449. return Die;
  1450. }
  1451. // Recursively clone children.
  1452. for (auto Child : InputDIE.children()) {
  1453. if (DIE *Clone = cloneDIE(Child, File, Unit, StringPool, PCOffset,
  1454. OutOffset, Flags, IsLittleEndian)) {
  1455. Die->addChild(Clone);
  1456. OutOffset = Clone->getOffset() + Clone->getSize();
  1457. }
  1458. }
  1459. // Account for the end of children marker.
  1460. OutOffset += sizeof(int8_t);
  1461. // Update our size.
  1462. Die->setSize(OutOffset - Die->getOffset());
  1463. return Die;
  1464. }
  1465. /// Patch the input object file relevant debug_ranges entries
  1466. /// and emit them in the output file. Update the relevant attributes
  1467. /// to point at the new entries.
  1468. void DWARFLinker::patchRangesForUnit(const CompileUnit &Unit,
  1469. DWARFContext &OrigDwarf,
  1470. const DWARFFile &File) const {
  1471. DWARFDebugRangeList RangeList;
  1472. const auto &FunctionRanges = Unit.getFunctionRanges();
  1473. unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
  1474. DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
  1475. OrigDwarf.getDWARFObj().getRangesSection(),
  1476. OrigDwarf.isLittleEndian(), AddressSize);
  1477. std::optional<std::pair<AddressRange, int64_t>> CachedRange;
  1478. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  1479. auto OrigUnitDie = OrigUnit.getUnitDIE(false);
  1480. uint64_t UnitBaseAddress =
  1481. dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), 0);
  1482. for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
  1483. uint64_t Offset = RangeAttribute.get();
  1484. RangeAttribute.set(TheDwarfEmitter->getRangesSectionSize());
  1485. if (Error E = RangeList.extract(RangeExtractor, &Offset)) {
  1486. llvm::consumeError(std::move(E));
  1487. reportWarning("invalid range list ignored.", File);
  1488. RangeList.clear();
  1489. }
  1490. const auto &Entries = RangeList.getEntries();
  1491. uint64_t BaseAddress = UnitBaseAddress;
  1492. AddressRanges LinkedRanges;
  1493. if (!Entries.empty()) {
  1494. for (const auto &Range : Entries) {
  1495. if (Range.isBaseAddressSelectionEntry(
  1496. Unit.getOrigUnit().getAddressByteSize())) {
  1497. BaseAddress = Range.EndAddress;
  1498. continue;
  1499. }
  1500. if (!CachedRange ||
  1501. !CachedRange->first.contains(Range.StartAddress + BaseAddress))
  1502. CachedRange = FunctionRanges.getRangeValueThatContains(
  1503. Range.StartAddress + BaseAddress);
  1504. // All range entries should lie in the function range.
  1505. if (!CachedRange) {
  1506. reportWarning("inconsistent range data.", File);
  1507. continue;
  1508. }
  1509. LinkedRanges.insert(
  1510. {Range.StartAddress + BaseAddress + CachedRange->second,
  1511. Range.EndAddress + BaseAddress + CachedRange->second});
  1512. }
  1513. }
  1514. TheDwarfEmitter->emitDwarfDebugRangesTableFragment(Unit, LinkedRanges);
  1515. }
  1516. }
  1517. /// Generate the debug_aranges entries for \p Unit and if the
  1518. /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
  1519. /// contribution for this attribute.
  1520. /// FIXME: this could actually be done right in patchRangesForUnit,
  1521. /// but for the sake of initial bit-for-bit compatibility with legacy
  1522. /// dsymutil, we have to do it in a delayed pass.
  1523. void DWARFLinker::generateUnitRanges(CompileUnit &Unit) const {
  1524. auto Attr = Unit.getUnitRangesAttribute();
  1525. if (Attr)
  1526. Attr->set(TheDwarfEmitter->getRangesSectionSize());
  1527. TheDwarfEmitter->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
  1528. }
  1529. /// Insert the new line info sequence \p Seq into the current
  1530. /// set of already linked line info \p Rows.
  1531. static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
  1532. std::vector<DWARFDebugLine::Row> &Rows) {
  1533. if (Seq.empty())
  1534. return;
  1535. if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
  1536. llvm::append_range(Rows, Seq);
  1537. Seq.clear();
  1538. return;
  1539. }
  1540. object::SectionedAddress Front = Seq.front().Address;
  1541. auto InsertPoint = partition_point(
  1542. Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
  1543. // FIXME: this only removes the unneeded end_sequence if the
  1544. // sequences have been inserted in order. Using a global sort like
  1545. // described in patchLineTableForUnit() and delaying the end_sequene
  1546. // elimination to emitLineTableForUnit() we can get rid of all of them.
  1547. if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
  1548. InsertPoint->EndSequence) {
  1549. *InsertPoint = Seq.front();
  1550. Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
  1551. } else {
  1552. Rows.insert(InsertPoint, Seq.begin(), Seq.end());
  1553. }
  1554. Seq.clear();
  1555. }
  1556. static void patchStmtList(DIE &Die, DIEInteger Offset) {
  1557. for (auto &V : Die.values())
  1558. if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
  1559. V = DIEValue(V.getAttribute(), V.getForm(), Offset);
  1560. return;
  1561. }
  1562. llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
  1563. }
  1564. /// Extract the line table for \p Unit from \p OrigDwarf, and
  1565. /// recreate a relocated version of these for the address ranges that
  1566. /// are present in the binary.
  1567. void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
  1568. DWARFContext &OrigDwarf,
  1569. const DWARFFile &File) {
  1570. DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
  1571. auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
  1572. if (!StmtList)
  1573. return;
  1574. // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
  1575. if (auto *OutputDIE = Unit.getOutputUnitDIE())
  1576. patchStmtList(*OutputDIE,
  1577. DIEInteger(TheDwarfEmitter->getLineSectionSize()));
  1578. RangesTy &Ranges = File.Addresses->getValidAddressRanges();
  1579. // Parse the original line info for the unit.
  1580. DWARFDebugLine::LineTable LineTable;
  1581. uint64_t StmtOffset = *StmtList;
  1582. DWARFDataExtractor LineExtractor(
  1583. OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
  1584. OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
  1585. if (needToTranslateStrings())
  1586. return TheDwarfEmitter->translateLineTable(LineExtractor, StmtOffset);
  1587. if (Error Err =
  1588. LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf,
  1589. &Unit.getOrigUnit(), OrigDwarf.getWarningHandler()))
  1590. OrigDwarf.getWarningHandler()(std::move(Err));
  1591. // This vector is the output line table.
  1592. std::vector<DWARFDebugLine::Row> NewRows;
  1593. NewRows.reserve(LineTable.Rows.size());
  1594. // Current sequence of rows being extracted, before being inserted
  1595. // in NewRows.
  1596. std::vector<DWARFDebugLine::Row> Seq;
  1597. const auto &FunctionRanges = Unit.getFunctionRanges();
  1598. std::optional<std::pair<AddressRange, int64_t>> CurrRange;
  1599. // FIXME: This logic is meant to generate exactly the same output as
  1600. // Darwin's classic dsymutil. There is a nicer way to implement this
  1601. // by simply putting all the relocated line info in NewRows and simply
  1602. // sorting NewRows before passing it to emitLineTableForUnit. This
  1603. // should be correct as sequences for a function should stay
  1604. // together in the sorted output. There are a few corner cases that
  1605. // look suspicious though, and that required to implement the logic
  1606. // this way. Revisit that once initial validation is finished.
  1607. // Iterate over the object file line info and extract the sequences
  1608. // that correspond to linked functions.
  1609. for (auto &Row : LineTable.Rows) {
  1610. // Check whether we stepped out of the range. The range is
  1611. // half-open, but consider accept the end address of the range if
  1612. // it is marked as end_sequence in the input (because in that
  1613. // case, the relocation offset is accurate and that entry won't
  1614. // serve as the start of another function).
  1615. if (!CurrRange || !CurrRange->first.contains(Row.Address.Address) ||
  1616. (Row.Address.Address == CurrRange->first.end() && !Row.EndSequence)) {
  1617. // We just stepped out of a known range. Insert a end_sequence
  1618. // corresponding to the end of the range.
  1619. uint64_t StopAddress =
  1620. CurrRange ? CurrRange->first.end() + CurrRange->second : -1ULL;
  1621. CurrRange = FunctionRanges.getRangeValueThatContains(Row.Address.Address);
  1622. if (!CurrRange) {
  1623. if (StopAddress != -1ULL) {
  1624. // Try harder by looking in the Address ranges map.
  1625. // There are corner cases where this finds a
  1626. // valid entry. It's unclear if this is right or wrong, but
  1627. // for now do as dsymutil.
  1628. // FIXME: Understand exactly what cases this addresses and
  1629. // potentially remove it along with the Ranges map.
  1630. if (std::optional<std::pair<AddressRange, int64_t>> Range =
  1631. Ranges.getRangeValueThatContains(Row.Address.Address))
  1632. StopAddress = Row.Address.Address + (*Range).second;
  1633. }
  1634. }
  1635. if (StopAddress != -1ULL && !Seq.empty()) {
  1636. // Insert end sequence row with the computed end address, but
  1637. // the same line as the previous one.
  1638. auto NextLine = Seq.back();
  1639. NextLine.Address.Address = StopAddress;
  1640. NextLine.EndSequence = 1;
  1641. NextLine.PrologueEnd = 0;
  1642. NextLine.BasicBlock = 0;
  1643. NextLine.EpilogueBegin = 0;
  1644. Seq.push_back(NextLine);
  1645. insertLineSequence(Seq, NewRows);
  1646. }
  1647. if (!CurrRange)
  1648. continue;
  1649. }
  1650. // Ignore empty sequences.
  1651. if (Row.EndSequence && Seq.empty())
  1652. continue;
  1653. // Relocate row address and add it to the current sequence.
  1654. Row.Address.Address += CurrRange->second;
  1655. Seq.emplace_back(Row);
  1656. if (Row.EndSequence)
  1657. insertLineSequence(Seq, NewRows);
  1658. }
  1659. // Finished extracting, now emit the line tables.
  1660. // FIXME: LLVM hard-codes its prologue values. We just copy the
  1661. // prologue over and that works because we act as both producer and
  1662. // consumer. It would be nicer to have a real configurable line
  1663. // table emitter.
  1664. if (LineTable.Prologue.getVersion() < 2 ||
  1665. LineTable.Prologue.getVersion() > 5 ||
  1666. LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
  1667. LineTable.Prologue.OpcodeBase > 13)
  1668. reportWarning("line table parameters mismatch. Cannot emit.", File);
  1669. else {
  1670. uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
  1671. // DWARF v5 has an extra 2 bytes of information before the header_length
  1672. // field.
  1673. if (LineTable.Prologue.getVersion() == 5)
  1674. PrologueEnd += 2;
  1675. StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
  1676. MCDwarfLineTableParams Params;
  1677. Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
  1678. Params.DWARF2LineBase = LineTable.Prologue.LineBase;
  1679. Params.DWARF2LineRange = LineTable.Prologue.LineRange;
  1680. TheDwarfEmitter->emitLineTableForUnit(
  1681. Params, LineData.slice(*StmtList + 4, PrologueEnd),
  1682. LineTable.Prologue.MinInstLength, NewRows,
  1683. Unit.getOrigUnit().getAddressByteSize());
  1684. }
  1685. }
  1686. void DWARFLinker::DIECloner::rememberUnitForMacroOffset(CompileUnit &Unit) {
  1687. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  1688. DWARFDie OrigUnitDie = OrigUnit.getUnitDIE();
  1689. if (std::optional<uint64_t> MacroAttr =
  1690. dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macros))) {
  1691. UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit));
  1692. return;
  1693. }
  1694. if (std::optional<uint64_t> MacroAttr =
  1695. dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macro_info))) {
  1696. UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit));
  1697. return;
  1698. }
  1699. }
  1700. void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
  1701. for (DwarfLinkerAccelTableKind AccelTableKind : Options.AccelTables) {
  1702. switch (AccelTableKind) {
  1703. case DwarfLinkerAccelTableKind::Apple: {
  1704. // Add namespaces.
  1705. for (const auto &Namespace : Unit.getNamespaces())
  1706. AppleNamespaces.addName(Namespace.Name, Namespace.Die->getOffset() +
  1707. Unit.getStartOffset());
  1708. // Add names.
  1709. for (const auto &Pubname : Unit.getPubnames())
  1710. AppleNames.addName(Pubname.Name,
  1711. Pubname.Die->getOffset() + Unit.getStartOffset());
  1712. // Add types.
  1713. for (const auto &Pubtype : Unit.getPubtypes())
  1714. AppleTypes.addName(
  1715. Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
  1716. Pubtype.Die->getTag(),
  1717. Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
  1718. : 0,
  1719. Pubtype.QualifiedNameHash);
  1720. // Add ObjC names.
  1721. for (const auto &ObjC : Unit.getObjC())
  1722. AppleObjc.addName(ObjC.Name,
  1723. ObjC.Die->getOffset() + Unit.getStartOffset());
  1724. } break;
  1725. case DwarfLinkerAccelTableKind::Pub: {
  1726. TheDwarfEmitter->emitPubNamesForUnit(Unit);
  1727. TheDwarfEmitter->emitPubTypesForUnit(Unit);
  1728. } break;
  1729. case DwarfLinkerAccelTableKind::DebugNames: {
  1730. for (const auto &Namespace : Unit.getNamespaces())
  1731. DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
  1732. Namespace.Die->getTag(), Unit.getUniqueID());
  1733. for (const auto &Pubname : Unit.getPubnames())
  1734. DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
  1735. Pubname.Die->getTag(), Unit.getUniqueID());
  1736. for (const auto &Pubtype : Unit.getPubtypes())
  1737. DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
  1738. Pubtype.Die->getTag(), Unit.getUniqueID());
  1739. } break;
  1740. }
  1741. }
  1742. }
  1743. /// Read the frame info stored in the object, and emit the
  1744. /// patched frame descriptions for the resulting file.
  1745. ///
  1746. /// This is actually pretty easy as the data of the CIEs and FDEs can
  1747. /// be considered as black boxes and moved as is. The only thing to do
  1748. /// is to patch the addresses in the headers.
  1749. void DWARFLinker::patchFrameInfoForObject(const DWARFFile &File,
  1750. RangesTy &Ranges,
  1751. DWARFContext &OrigDwarf,
  1752. unsigned AddrSize) {
  1753. StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data;
  1754. if (FrameData.empty())
  1755. return;
  1756. DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
  1757. uint64_t InputOffset = 0;
  1758. // Store the data of the CIEs defined in this object, keyed by their
  1759. // offsets.
  1760. DenseMap<uint64_t, StringRef> LocalCIES;
  1761. while (Data.isValidOffset(InputOffset)) {
  1762. uint64_t EntryOffset = InputOffset;
  1763. uint32_t InitialLength = Data.getU32(&InputOffset);
  1764. if (InitialLength == 0xFFFFFFFF)
  1765. return reportWarning("Dwarf64 bits no supported", File);
  1766. uint32_t CIEId = Data.getU32(&InputOffset);
  1767. if (CIEId == 0xFFFFFFFF) {
  1768. // This is a CIE, store it.
  1769. StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
  1770. LocalCIES[EntryOffset] = CIEData;
  1771. // The -4 is to account for the CIEId we just read.
  1772. InputOffset += InitialLength - 4;
  1773. continue;
  1774. }
  1775. uint64_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
  1776. // Some compilers seem to emit frame info that doesn't start at
  1777. // the function entry point, thus we can't just lookup the address
  1778. // in the debug map. Use the AddressInfo's range map to see if the FDE
  1779. // describes something that we can relocate.
  1780. std::optional<std::pair<AddressRange, int64_t>> Range =
  1781. Ranges.getRangeValueThatContains(Loc);
  1782. if (!Range) {
  1783. // The +4 is to account for the size of the InitialLength field itself.
  1784. InputOffset = EntryOffset + InitialLength + 4;
  1785. continue;
  1786. }
  1787. // This is an FDE, and we have a mapping.
  1788. // Have we already emitted a corresponding CIE?
  1789. StringRef CIEData = LocalCIES[CIEId];
  1790. if (CIEData.empty())
  1791. return reportWarning("Inconsistent debug_frame content. Dropping.", File);
  1792. // Look if we already emitted a CIE that corresponds to the
  1793. // referenced one (the CIE data is the key of that lookup).
  1794. auto IteratorInserted = EmittedCIEs.insert(
  1795. std::make_pair(CIEData, TheDwarfEmitter->getFrameSectionSize()));
  1796. // If there is no CIE yet for this ID, emit it.
  1797. if (IteratorInserted.second) {
  1798. LastCIEOffset = TheDwarfEmitter->getFrameSectionSize();
  1799. IteratorInserted.first->getValue() = LastCIEOffset;
  1800. TheDwarfEmitter->emitCIE(CIEData);
  1801. }
  1802. // Emit the FDE with updated address and CIE pointer.
  1803. // (4 + AddrSize) is the size of the CIEId + initial_location
  1804. // fields that will get reconstructed by emitFDE().
  1805. unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
  1806. TheDwarfEmitter->emitFDE(IteratorInserted.first->getValue(), AddrSize,
  1807. Loc + Range->second,
  1808. FrameData.substr(InputOffset, FDERemainingBytes));
  1809. InputOffset += FDERemainingBytes;
  1810. }
  1811. }
  1812. uint32_t DWARFLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE,
  1813. CompileUnit &U,
  1814. const DWARFFile &File,
  1815. int ChildRecurseDepth) {
  1816. const char *Name = nullptr;
  1817. DWARFUnit *OrigUnit = &U.getOrigUnit();
  1818. CompileUnit *CU = &U;
  1819. std::optional<DWARFFormValue> Ref;
  1820. while (true) {
  1821. if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
  1822. Name = CurrentName;
  1823. if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
  1824. !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
  1825. break;
  1826. if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
  1827. break;
  1828. CompileUnit *RefCU;
  1829. if (auto RefDIE =
  1830. Linker.resolveDIEReference(File, CompileUnits, *Ref, DIE, RefCU)) {
  1831. CU = RefCU;
  1832. OrigUnit = &RefCU->getOrigUnit();
  1833. DIE = RefDIE;
  1834. }
  1835. }
  1836. unsigned Idx = OrigUnit->getDIEIndex(DIE);
  1837. if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
  1838. Name = "(anonymous namespace)";
  1839. if (CU->getInfo(Idx).ParentIdx == 0 ||
  1840. // FIXME: dsymutil-classic compatibility. Ignore modules.
  1841. CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
  1842. dwarf::DW_TAG_module)
  1843. return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
  1844. DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
  1845. return djbHash(
  1846. (Name ? Name : ""),
  1847. djbHash((Name ? "::" : ""),
  1848. hashFullyQualifiedName(Die, *CU, File, ++ChildRecurseDepth)));
  1849. }
  1850. static uint64_t getDwoId(const DWARFDie &CUDie) {
  1851. auto DwoId = dwarf::toUnsigned(
  1852. CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
  1853. if (DwoId)
  1854. return *DwoId;
  1855. return 0;
  1856. }
  1857. static std::string remapPath(StringRef Path,
  1858. const objectPrefixMap &ObjectPrefixMap) {
  1859. if (ObjectPrefixMap.empty())
  1860. return Path.str();
  1861. SmallString<256> p = Path;
  1862. for (const auto &Entry : ObjectPrefixMap)
  1863. if (llvm::sys::path::replace_path_prefix(p, Entry.first, Entry.second))
  1864. break;
  1865. return p.str().str();
  1866. }
  1867. static std::string getPCMFile(const DWARFDie &CUDie,
  1868. objectPrefixMap *ObjectPrefixMap) {
  1869. std::string PCMFile = dwarf::toString(
  1870. CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
  1871. if (PCMFile.empty())
  1872. return PCMFile;
  1873. if (ObjectPrefixMap)
  1874. PCMFile = remapPath(PCMFile, *ObjectPrefixMap);
  1875. return PCMFile;
  1876. }
  1877. std::pair<bool, bool> DWARFLinker::isClangModuleRef(const DWARFDie &CUDie,
  1878. std::string &PCMFile,
  1879. LinkContext &Context,
  1880. unsigned Indent,
  1881. bool Quiet) {
  1882. if (PCMFile.empty())
  1883. return std::make_pair(false, false);
  1884. // Clang module DWARF skeleton CUs abuse this for the path to the module.
  1885. uint64_t DwoId = getDwoId(CUDie);
  1886. std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
  1887. if (Name.empty()) {
  1888. if (!Quiet)
  1889. reportWarning("Anonymous module skeleton CU for " + PCMFile,
  1890. Context.File);
  1891. return std::make_pair(true, true);
  1892. }
  1893. if (!Quiet && Options.Verbose) {
  1894. outs().indent(Indent);
  1895. outs() << "Found clang module reference " << PCMFile;
  1896. }
  1897. auto Cached = ClangModules.find(PCMFile);
  1898. if (Cached != ClangModules.end()) {
  1899. // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
  1900. // fixed in clang, only warn about DWO_id mismatches in verbose mode.
  1901. // ASTFileSignatures will change randomly when a module is rebuilt.
  1902. if (!Quiet && Options.Verbose && (Cached->second != DwoId))
  1903. reportWarning(Twine("hash mismatch: this object file was built against a "
  1904. "different version of the module ") +
  1905. PCMFile,
  1906. Context.File);
  1907. if (!Quiet && Options.Verbose)
  1908. outs() << " [cached].\n";
  1909. return std::make_pair(true, true);
  1910. }
  1911. return std::make_pair(true, false);
  1912. }
  1913. bool DWARFLinker::registerModuleReference(const DWARFDie &CUDie,
  1914. LinkContext &Context,
  1915. objFileLoader Loader,
  1916. CompileUnitHandler OnCUDieLoaded,
  1917. unsigned Indent) {
  1918. std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
  1919. std::pair<bool, bool> IsClangModuleRef =
  1920. isClangModuleRef(CUDie, PCMFile, Context, Indent, false);
  1921. if (!IsClangModuleRef.first)
  1922. return false;
  1923. if (IsClangModuleRef.second)
  1924. return true;
  1925. if (Options.Verbose)
  1926. outs() << " ...\n";
  1927. // Cyclic dependencies are disallowed by Clang, but we still
  1928. // shouldn't run into an infinite loop, so mark it as processed now.
  1929. ClangModules.insert({PCMFile, getDwoId(CUDie)});
  1930. if (Error E = loadClangModule(Loader, CUDie, PCMFile, Context, OnCUDieLoaded,
  1931. Indent + 2)) {
  1932. consumeError(std::move(E));
  1933. return false;
  1934. }
  1935. return true;
  1936. }
  1937. Error DWARFLinker::loadClangModule(objFileLoader Loader, const DWARFDie &CUDie,
  1938. const std::string &PCMFile,
  1939. LinkContext &Context,
  1940. CompileUnitHandler OnCUDieLoaded,
  1941. unsigned Indent) {
  1942. uint64_t DwoId = getDwoId(CUDie);
  1943. std::string ModuleName = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
  1944. /// Using a SmallString<0> because loadClangModule() is recursive.
  1945. SmallString<0> Path(Options.PrependPath);
  1946. if (sys::path::is_relative(PCMFile))
  1947. resolveRelativeObjectPath(Path, CUDie);
  1948. sys::path::append(Path, PCMFile);
  1949. // Don't use the cached binary holder because we have no thread-safety
  1950. // guarantee and the lifetime is limited.
  1951. if (Loader == nullptr) {
  1952. reportError("Could not load clang module: loader is not specified.\n",
  1953. Context.File);
  1954. return Error::success();
  1955. }
  1956. auto ErrOrObj = Loader(Context.File.FileName, Path);
  1957. if (!ErrOrObj)
  1958. return Error::success();
  1959. std::unique_ptr<CompileUnit> Unit;
  1960. for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
  1961. OnCUDieLoaded(*CU);
  1962. // Recursively get all modules imported by this one.
  1963. auto ChildCUDie = CU->getUnitDIE();
  1964. if (!ChildCUDie)
  1965. continue;
  1966. if (!registerModuleReference(ChildCUDie, Context, Loader, OnCUDieLoaded,
  1967. Indent)) {
  1968. if (Unit) {
  1969. std::string Err =
  1970. (PCMFile +
  1971. ": Clang modules are expected to have exactly 1 compile unit.\n");
  1972. reportError(Err, Context.File);
  1973. return make_error<StringError>(Err, inconvertibleErrorCode());
  1974. }
  1975. // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
  1976. // fixed in clang, only warn about DWO_id mismatches in verbose mode.
  1977. // ASTFileSignatures will change randomly when a module is rebuilt.
  1978. uint64_t PCMDwoId = getDwoId(ChildCUDie);
  1979. if (PCMDwoId != DwoId) {
  1980. if (Options.Verbose)
  1981. reportWarning(
  1982. Twine("hash mismatch: this object file was built against a "
  1983. "different version of the module ") +
  1984. PCMFile,
  1985. Context.File);
  1986. // Update the cache entry with the DwoId of the module loaded from disk.
  1987. ClangModules[PCMFile] = PCMDwoId;
  1988. }
  1989. // Add this module.
  1990. Unit = std::make_unique<CompileUnit>(*CU, UniqueUnitID++, !Options.NoODR,
  1991. ModuleName);
  1992. }
  1993. }
  1994. if (Unit)
  1995. Context.ModuleUnits.emplace_back(RefModuleUnit{*ErrOrObj, std::move(Unit)});
  1996. return Error::success();
  1997. }
  1998. uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(
  1999. DWARFContext &DwarfContext, const DWARFFile &File,
  2000. OffsetsStringPool &StringPool, bool IsLittleEndian) {
  2001. uint64_t OutputDebugInfoSize =
  2002. Linker.Options.NoOutput ? 0 : Emitter->getDebugInfoSectionSize();
  2003. const uint64_t StartOutputDebugInfoSize = OutputDebugInfoSize;
  2004. for (auto &CurrentUnit : CompileUnits) {
  2005. const uint16_t DwarfVersion = CurrentUnit->getOrigUnit().getVersion();
  2006. const uint32_t UnitHeaderSize = DwarfVersion >= 5 ? 12 : 11;
  2007. auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
  2008. CurrentUnit->setStartOffset(OutputDebugInfoSize);
  2009. if (!InputDIE) {
  2010. OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion);
  2011. continue;
  2012. }
  2013. if (CurrentUnit->getInfo(0).Keep) {
  2014. // Clone the InputDIE into your Unit DIE in our compile unit since it
  2015. // already has a DIE inside of it.
  2016. CurrentUnit->createOutputDIE();
  2017. rememberUnitForMacroOffset(*CurrentUnit);
  2018. cloneDIE(InputDIE, File, *CurrentUnit, StringPool, 0 /* PC offset */,
  2019. UnitHeaderSize, 0, IsLittleEndian,
  2020. CurrentUnit->getOutputUnitDIE());
  2021. }
  2022. OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion);
  2023. if (!Linker.Options.NoOutput) {
  2024. assert(Emitter);
  2025. if (LLVM_LIKELY(!Linker.Options.Update) ||
  2026. Linker.needToTranslateStrings())
  2027. Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, File);
  2028. Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
  2029. if (LLVM_UNLIKELY(Linker.Options.Update))
  2030. continue;
  2031. Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, File);
  2032. auto ProcessExpr = [&](StringRef Bytes,
  2033. SmallVectorImpl<uint8_t> &Buffer) {
  2034. DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit();
  2035. DataExtractor Data(Bytes, IsLittleEndian,
  2036. OrigUnit.getAddressByteSize());
  2037. cloneExpression(Data,
  2038. DWARFExpression(Data, OrigUnit.getAddressByteSize(),
  2039. OrigUnit.getFormParams().Format),
  2040. File, *CurrentUnit, Buffer);
  2041. };
  2042. Emitter->emitLocationsForUnit(*CurrentUnit, DwarfContext, ProcessExpr);
  2043. }
  2044. }
  2045. if (!Linker.Options.NoOutput) {
  2046. assert(Emitter);
  2047. // Emit macro tables.
  2048. Emitter->emitMacroTables(File.Dwarf, UnitMacroMap, StringPool);
  2049. // Emit all the compile unit's debug information.
  2050. for (auto &CurrentUnit : CompileUnits) {
  2051. if (LLVM_LIKELY(!Linker.Options.Update))
  2052. Linker.generateUnitRanges(*CurrentUnit);
  2053. CurrentUnit->fixupForwardReferences();
  2054. if (!CurrentUnit->getOutputUnitDIE())
  2055. continue;
  2056. unsigned DwarfVersion = CurrentUnit->getOrigUnit().getVersion();
  2057. assert(Emitter->getDebugInfoSectionSize() ==
  2058. CurrentUnit->getStartOffset());
  2059. Emitter->emitCompileUnitHeader(*CurrentUnit, DwarfVersion);
  2060. Emitter->emitDIE(*CurrentUnit->getOutputUnitDIE());
  2061. assert(Emitter->getDebugInfoSectionSize() ==
  2062. CurrentUnit->computeNextUnitOffset(DwarfVersion));
  2063. }
  2064. }
  2065. return OutputDebugInfoSize - StartOutputDebugInfoSize;
  2066. }
  2067. bool DWARFLinker::emitPaperTrailWarnings(const DWARFFile &File,
  2068. OffsetsStringPool &StringPool) {
  2069. if (File.Warnings.empty())
  2070. return false;
  2071. DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
  2072. CUDie->setOffset(11);
  2073. StringRef Producer;
  2074. StringRef WarningHeader;
  2075. switch (DwarfLinkerClientID) {
  2076. case DwarfLinkerClient::Dsymutil:
  2077. Producer = StringPool.internString("dsymutil");
  2078. WarningHeader = "dsymutil_warning";
  2079. break;
  2080. default:
  2081. Producer = StringPool.internString("dwarfopt");
  2082. WarningHeader = "dwarfopt_warning";
  2083. break;
  2084. }
  2085. StringRef FileName = StringPool.internString(File.FileName);
  2086. CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
  2087. DIEInteger(StringPool.getStringOffset(Producer)));
  2088. DIEBlock *String = new (DIEAlloc) DIEBlock();
  2089. DIEBlocks.push_back(String);
  2090. for (auto &C : FileName)
  2091. String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
  2092. DIEInteger(C));
  2093. String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
  2094. DIEInteger(0));
  2095. CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
  2096. for (const auto &Warning : File.Warnings) {
  2097. DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
  2098. ConstDie.addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
  2099. DIEInteger(StringPool.getStringOffset(WarningHeader)));
  2100. ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
  2101. DIEInteger(1));
  2102. ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
  2103. DIEInteger(StringPool.getStringOffset(Warning)));
  2104. }
  2105. unsigned Size = 4 /* FORM_strp */ + FileName.size() + 1 +
  2106. File.Warnings.size() * (4 + 1 + 4) + 1 /* End of children */;
  2107. DIEAbbrev Abbrev = CUDie->generateAbbrev();
  2108. assignAbbrev(Abbrev);
  2109. CUDie->setAbbrevNumber(Abbrev.getNumber());
  2110. Size += getULEB128Size(Abbrev.getNumber());
  2111. // Abbreviation ordering needed for classic compatibility.
  2112. for (auto &Child : CUDie->children()) {
  2113. Abbrev = Child.generateAbbrev();
  2114. assignAbbrev(Abbrev);
  2115. Child.setAbbrevNumber(Abbrev.getNumber());
  2116. Size += getULEB128Size(Abbrev.getNumber());
  2117. }
  2118. CUDie->setSize(Size);
  2119. TheDwarfEmitter->emitPaperTrailWarningsDie(*CUDie);
  2120. return true;
  2121. }
  2122. void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) {
  2123. if (!needToTranslateStrings())
  2124. TheDwarfEmitter->emitSectionContents(
  2125. Dwarf.getDWARFObj().getLineSection().Data, "debug_line");
  2126. TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data,
  2127. "debug_loc");
  2128. TheDwarfEmitter->emitSectionContents(
  2129. Dwarf.getDWARFObj().getRangesSection().Data, "debug_ranges");
  2130. TheDwarfEmitter->emitSectionContents(
  2131. Dwarf.getDWARFObj().getFrameSection().Data, "debug_frame");
  2132. TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(),
  2133. "debug_aranges");
  2134. }
  2135. void DWARFLinker::addObjectFile(DWARFFile &File, objFileLoader Loader,
  2136. CompileUnitHandler OnCUDieLoaded) {
  2137. ObjectContexts.emplace_back(LinkContext(File));
  2138. if (ObjectContexts.back().File.Dwarf) {
  2139. for (const std::unique_ptr<DWARFUnit> &CU :
  2140. ObjectContexts.back().File.Dwarf->compile_units()) {
  2141. DWARFDie CUDie = CU->getUnitDIE();
  2142. if (!CUDie)
  2143. continue;
  2144. OnCUDieLoaded(*CU);
  2145. if (!LLVM_UNLIKELY(Options.Update))
  2146. registerModuleReference(CUDie, ObjectContexts.back(), Loader,
  2147. OnCUDieLoaded);
  2148. }
  2149. }
  2150. }
  2151. Error DWARFLinker::link() {
  2152. assert(Options.NoOutput || TheDwarfEmitter);
  2153. assert((Options.TargetDWARFVersion != 0) &&
  2154. "TargetDWARFVersion should be set");
  2155. // First populate the data structure we need for each iteration of the
  2156. // parallel loop.
  2157. unsigned NumObjects = ObjectContexts.size();
  2158. // This Dwarf string pool which is used for emission. It must be used
  2159. // serially as the order of calling getStringOffset matters for
  2160. // reproducibility.
  2161. OffsetsStringPool OffsetsStringPool(StringsTranslator, true);
  2162. // ODR Contexts for the optimize.
  2163. DeclContextTree ODRContexts;
  2164. for (LinkContext &OptContext : ObjectContexts) {
  2165. if (Options.Verbose) {
  2166. if (DwarfLinkerClientID == DwarfLinkerClient::Dsymutil)
  2167. outs() << "DEBUG MAP OBJECT: " << OptContext.File.FileName << "\n";
  2168. else
  2169. outs() << "OBJECT FILE: " << OptContext.File.FileName << "\n";
  2170. }
  2171. if (emitPaperTrailWarnings(OptContext.File, OffsetsStringPool))
  2172. continue;
  2173. if (!OptContext.File.Dwarf)
  2174. continue;
  2175. if (Options.VerifyInputDWARF)
  2176. verify(OptContext.File);
  2177. // Look for relocations that correspond to address map entries.
  2178. // there was findvalidrelocations previously ... probably we need to gather
  2179. // info here
  2180. if (LLVM_LIKELY(!Options.Update) &&
  2181. !OptContext.File.Addresses->hasValidRelocs()) {
  2182. if (Options.Verbose)
  2183. outs() << "No valid relocations found. Skipping.\n";
  2184. // Set "Skip" flag as a signal to other loops that we should not
  2185. // process this iteration.
  2186. OptContext.Skip = true;
  2187. continue;
  2188. }
  2189. // Setup access to the debug info.
  2190. if (!OptContext.File.Dwarf)
  2191. continue;
  2192. // Check whether type units are presented.
  2193. if (!OptContext.File.Dwarf->types_section_units().empty()) {
  2194. reportWarning("type units are not currently supported: file will "
  2195. "be skipped",
  2196. OptContext.File);
  2197. OptContext.Skip = true;
  2198. continue;
  2199. }
  2200. // Check for unsupported sections. Following sections can be referenced
  2201. // from .debug_info section. Current DWARFLinker implementation does not
  2202. // support or update references to these tables. Thus we report warning
  2203. // and skip corresponding object file.
  2204. if (!OptContext.File.Dwarf->getDWARFObj()
  2205. .getRnglistsSection()
  2206. .Data.empty()) {
  2207. reportWarning("'.debug_rnglists' is not currently supported: file "
  2208. "will be skipped",
  2209. OptContext.File);
  2210. OptContext.Skip = true;
  2211. continue;
  2212. }
  2213. if (!OptContext.File.Dwarf->getDWARFObj()
  2214. .getLoclistsSection()
  2215. .Data.empty()) {
  2216. reportWarning("'.debug_loclists' is not currently supported: file "
  2217. "will be skipped",
  2218. OptContext.File);
  2219. OptContext.Skip = true;
  2220. continue;
  2221. }
  2222. // In a first phase, just read in the debug info and load all clang modules.
  2223. OptContext.CompileUnits.reserve(
  2224. OptContext.File.Dwarf->getNumCompileUnits());
  2225. for (const auto &CU : OptContext.File.Dwarf->compile_units()) {
  2226. auto CUDie = CU->getUnitDIE(false);
  2227. if (Options.Verbose) {
  2228. outs() << "Input compilation unit:";
  2229. DIDumpOptions DumpOpts;
  2230. DumpOpts.ChildRecurseDepth = 0;
  2231. DumpOpts.Verbose = Options.Verbose;
  2232. CUDie.dump(outs(), 0, DumpOpts);
  2233. }
  2234. }
  2235. for (auto &CU : OptContext.ModuleUnits) {
  2236. if (Error Err =
  2237. cloneModuleUnit(OptContext, CU, ODRContexts, OffsetsStringPool))
  2238. reportWarning(toString(std::move(Err)), CU.File);
  2239. }
  2240. }
  2241. // At this point we know how much data we have emitted. We use this value to
  2242. // compare canonical DIE offsets in analyzeContextInfo to see if a definition
  2243. // is already emitted, without being affected by canonical die offsets set
  2244. // later. This prevents undeterminism when analyze and clone execute
  2245. // concurrently, as clone set the canonical DIE offset and analyze reads it.
  2246. const uint64_t ModulesEndOffset =
  2247. Options.NoOutput ? 0 : TheDwarfEmitter->getDebugInfoSectionSize();
  2248. // These variables manage the list of processed object files.
  2249. // The mutex and condition variable are to ensure that this is thread safe.
  2250. std::mutex ProcessedFilesMutex;
  2251. std::condition_variable ProcessedFilesConditionVariable;
  2252. BitVector ProcessedFiles(NumObjects, false);
  2253. // Analyzing the context info is particularly expensive so it is executed in
  2254. // parallel with emitting the previous compile unit.
  2255. auto AnalyzeLambda = [&](size_t I) {
  2256. auto &Context = ObjectContexts[I];
  2257. if (Context.Skip || !Context.File.Dwarf)
  2258. return;
  2259. for (const auto &CU : Context.File.Dwarf->compile_units()) {
  2260. // The !isClangModuleRef condition effectively skips over fully resolved
  2261. // skeleton units.
  2262. auto CUDie = CU->getUnitDIE();
  2263. std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
  2264. if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
  2265. !isClangModuleRef(CUDie, PCMFile, Context, 0, true).first) {
  2266. Context.CompileUnits.push_back(std::make_unique<CompileUnit>(
  2267. *CU, UniqueUnitID++, !Options.NoODR && !Options.Update, ""));
  2268. }
  2269. }
  2270. // Now build the DIE parent links that we will use during the next phase.
  2271. for (auto &CurrentUnit : Context.CompileUnits) {
  2272. auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
  2273. if (!CUDie)
  2274. continue;
  2275. analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
  2276. *CurrentUnit, &ODRContexts.getRoot(), ODRContexts,
  2277. ModulesEndOffset, Options.ParseableSwiftInterfaces,
  2278. [&](const Twine &Warning, const DWARFDie &DIE) {
  2279. reportWarning(Warning, Context.File, &DIE);
  2280. });
  2281. }
  2282. };
  2283. // For each object file map how many bytes were emitted.
  2284. StringMap<DebugInfoSize> SizeByObject;
  2285. // And then the remaining work in serial again.
  2286. // Note, although this loop runs in serial, it can run in parallel with
  2287. // the analyzeContextInfo loop so long as we process files with indices >=
  2288. // than those processed by analyzeContextInfo.
  2289. auto CloneLambda = [&](size_t I) {
  2290. auto &OptContext = ObjectContexts[I];
  2291. if (OptContext.Skip || !OptContext.File.Dwarf)
  2292. return;
  2293. // Then mark all the DIEs that need to be present in the generated output
  2294. // and collect some information about them.
  2295. // Note that this loop can not be merged with the previous one because
  2296. // cross-cu references require the ParentIdx to be setup for every CU in
  2297. // the object file before calling this.
  2298. if (LLVM_UNLIKELY(Options.Update)) {
  2299. for (auto &CurrentUnit : OptContext.CompileUnits)
  2300. CurrentUnit->markEverythingAsKept();
  2301. copyInvariantDebugSection(*OptContext.File.Dwarf);
  2302. } else {
  2303. for (auto &CurrentUnit : OptContext.CompileUnits) {
  2304. lookForDIEsToKeep(*OptContext.File.Addresses,
  2305. OptContext.File.Addresses->getValidAddressRanges(),
  2306. OptContext.CompileUnits,
  2307. CurrentUnit->getOrigUnit().getUnitDIE(),
  2308. OptContext.File, *CurrentUnit, 0);
  2309. #ifndef NDEBUG
  2310. verifyKeepChain(*CurrentUnit);
  2311. #endif
  2312. }
  2313. }
  2314. // The calls to applyValidRelocs inside cloneDIE will walk the reloc
  2315. // array again (in the same way findValidRelocsInDebugInfo() did). We
  2316. // need to reset the NextValidReloc index to the beginning.
  2317. if (OptContext.File.Addresses->hasValidRelocs() ||
  2318. LLVM_UNLIKELY(Options.Update)) {
  2319. SizeByObject[OptContext.File.FileName].Input =
  2320. getDebugInfoSize(*OptContext.File.Dwarf);
  2321. SizeByObject[OptContext.File.FileName].Output =
  2322. DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc,
  2323. OptContext.CompileUnits, Options.Update)
  2324. .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File,
  2325. OffsetsStringPool,
  2326. OptContext.File.Dwarf->isLittleEndian());
  2327. }
  2328. if (!Options.NoOutput && !OptContext.CompileUnits.empty() &&
  2329. LLVM_LIKELY(!Options.Update))
  2330. patchFrameInfoForObject(
  2331. OptContext.File, OptContext.File.Addresses->getValidAddressRanges(),
  2332. *OptContext.File.Dwarf,
  2333. OptContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
  2334. // Clean-up before starting working on the next object.
  2335. cleanupAuxiliarryData(OptContext);
  2336. };
  2337. auto EmitLambda = [&]() {
  2338. // Emit everything that's global.
  2339. if (!Options.NoOutput) {
  2340. TheDwarfEmitter->emitAbbrevs(Abbreviations, Options.TargetDWARFVersion);
  2341. TheDwarfEmitter->emitStrings(OffsetsStringPool);
  2342. for (DwarfLinkerAccelTableKind TableKind : Options.AccelTables) {
  2343. switch (TableKind) {
  2344. case DwarfLinkerAccelTableKind::Apple:
  2345. TheDwarfEmitter->emitAppleNamespaces(AppleNamespaces);
  2346. TheDwarfEmitter->emitAppleNames(AppleNames);
  2347. TheDwarfEmitter->emitAppleTypes(AppleTypes);
  2348. TheDwarfEmitter->emitAppleObjc(AppleObjc);
  2349. break;
  2350. case DwarfLinkerAccelTableKind::Pub:
  2351. // Already emitted by emitAcceleratorEntriesForUnit.
  2352. // Already emitted by emitAcceleratorEntriesForUnit.
  2353. break;
  2354. case DwarfLinkerAccelTableKind::DebugNames:
  2355. TheDwarfEmitter->emitDebugNames(DebugNames);
  2356. break;
  2357. }
  2358. }
  2359. }
  2360. };
  2361. auto AnalyzeAll = [&]() {
  2362. for (unsigned I = 0, E = NumObjects; I != E; ++I) {
  2363. AnalyzeLambda(I);
  2364. std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
  2365. ProcessedFiles.set(I);
  2366. ProcessedFilesConditionVariable.notify_one();
  2367. }
  2368. };
  2369. auto CloneAll = [&]() {
  2370. for (unsigned I = 0, E = NumObjects; I != E; ++I) {
  2371. {
  2372. std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
  2373. if (!ProcessedFiles[I]) {
  2374. ProcessedFilesConditionVariable.wait(
  2375. LockGuard, [&]() { return ProcessedFiles[I]; });
  2376. }
  2377. }
  2378. CloneLambda(I);
  2379. }
  2380. EmitLambda();
  2381. };
  2382. // To limit memory usage in the single threaded case, analyze and clone are
  2383. // run sequentially so the OptContext is freed after processing each object
  2384. // in endDebugObject.
  2385. if (Options.Threads == 1) {
  2386. for (unsigned I = 0, E = NumObjects; I != E; ++I) {
  2387. AnalyzeLambda(I);
  2388. CloneLambda(I);
  2389. }
  2390. EmitLambda();
  2391. } else {
  2392. ThreadPool Pool(hardware_concurrency(2));
  2393. Pool.async(AnalyzeAll);
  2394. Pool.async(CloneAll);
  2395. Pool.wait();
  2396. }
  2397. if (Options.Statistics) {
  2398. // Create a vector sorted in descending order by output size.
  2399. std::vector<std::pair<StringRef, DebugInfoSize>> Sorted;
  2400. for (auto &E : SizeByObject)
  2401. Sorted.emplace_back(E.first(), E.second);
  2402. llvm::sort(Sorted, [](auto &LHS, auto &RHS) {
  2403. return LHS.second.Output > RHS.second.Output;
  2404. });
  2405. auto ComputePercentange = [](int64_t Input, int64_t Output) -> float {
  2406. const float Difference = Output - Input;
  2407. const float Sum = Input + Output;
  2408. if (Sum == 0)
  2409. return 0;
  2410. return (Difference / (Sum / 2));
  2411. };
  2412. int64_t InputTotal = 0;
  2413. int64_t OutputTotal = 0;
  2414. const char *FormatStr = "{0,-45} {1,10}b {2,10}b {3,8:P}\n";
  2415. // Print header.
  2416. outs() << ".debug_info section size (in bytes)\n";
  2417. outs() << "----------------------------------------------------------------"
  2418. "---------------\n";
  2419. outs() << "Filename Object "
  2420. " dSYM Change\n";
  2421. outs() << "----------------------------------------------------------------"
  2422. "---------------\n";
  2423. // Print body.
  2424. for (auto &E : Sorted) {
  2425. InputTotal += E.second.Input;
  2426. OutputTotal += E.second.Output;
  2427. llvm::outs() << formatv(
  2428. FormatStr, sys::path::filename(E.first).take_back(45), E.second.Input,
  2429. E.second.Output, ComputePercentange(E.second.Input, E.second.Output));
  2430. }
  2431. // Print total and footer.
  2432. outs() << "----------------------------------------------------------------"
  2433. "---------------\n";
  2434. llvm::outs() << formatv(FormatStr, "Total", InputTotal, OutputTotal,
  2435. ComputePercentange(InputTotal, OutputTotal));
  2436. outs() << "----------------------------------------------------------------"
  2437. "---------------\n\n";
  2438. }
  2439. return Error::success();
  2440. }
  2441. Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
  2442. DeclContextTree &ODRContexts,
  2443. OffsetsStringPool &OffsetsStringPool,
  2444. unsigned Indent) {
  2445. assert(Unit.Unit.get() != nullptr);
  2446. if (!Unit.Unit->getOrigUnit().getUnitDIE().hasChildren())
  2447. return Error::success();
  2448. if (Options.Verbose) {
  2449. outs().indent(Indent);
  2450. outs() << "cloning .debug_info from " << Unit.File.FileName << "\n";
  2451. }
  2452. // Analyze context for the module.
  2453. analyzeContextInfo(Unit.Unit->getOrigUnit().getUnitDIE(), 0, *(Unit.Unit),
  2454. &ODRContexts.getRoot(), ODRContexts, 0,
  2455. Options.ParseableSwiftInterfaces,
  2456. [&](const Twine &Warning, const DWARFDie &DIE) {
  2457. reportWarning(Warning, Context.File, &DIE);
  2458. });
  2459. // Keep everything.
  2460. Unit.Unit->markEverythingAsKept();
  2461. // Clone unit.
  2462. UnitListTy CompileUnits;
  2463. CompileUnits.emplace_back(std::move(Unit.Unit));
  2464. assert(TheDwarfEmitter);
  2465. DIECloner(*this, TheDwarfEmitter, Unit.File, DIEAlloc, CompileUnits,
  2466. Options.Update)
  2467. .cloneAllCompileUnits(*Unit.File.Dwarf, Unit.File, OffsetsStringPool,
  2468. Unit.File.Dwarf->isLittleEndian());
  2469. return Error::success();
  2470. }
  2471. bool DWARFLinker::verify(const DWARFFile &File) {
  2472. assert(File.Dwarf);
  2473. DIDumpOptions DumpOpts;
  2474. if (!File.Dwarf->verify(llvm::outs(), DumpOpts.noImplicitRecursion())) {
  2475. reportWarning("input verification failed", File);
  2476. return false;
  2477. }
  2478. return true;
  2479. }
  2480. } // namespace llvm