DwarfLinker.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  1. //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
  2. //
  3. // The LLVM Linker
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "DebugMap.h"
  10. #include "BinaryHolder.h"
  11. #include "DebugMap.h"
  12. #include "dsymutil.h"
  13. #include "llvm/ADT/IntervalMap.h"
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/CodeGen/AsmPrinter.h"
  17. #include "llvm/CodeGen/DIE.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  21. #include "llvm/MC/MCAsmBackend.h"
  22. #include "llvm/MC/MCAsmInfo.h"
  23. #include "llvm/MC/MCContext.h"
  24. #include "llvm/MC/MCCodeEmitter.h"
  25. #include "llvm/MC/MCDwarf.h"
  26. #include "llvm/MC/MCInstrInfo.h"
  27. #include "llvm/MC/MCObjectFileInfo.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/MC/MCStreamer.h"
  30. #include "llvm/MC/MCSubtargetInfo.h"
  31. #include "llvm/Object/MachO.h"
  32. #include "llvm/Support/Dwarf.h"
  33. #include "llvm/Support/LEB128.h"
  34. #include "llvm/Support/TargetRegistry.h"
  35. #include "llvm/Target/TargetMachine.h"
  36. #include "llvm/Target/TargetOptions.h"
  37. #include <string>
  38. #include <tuple>
  39. // //
  40. ///////////////////////////////////////////////////////////////////////////////
  41. namespace llvm {
  42. namespace dsymutil {
  43. namespace {
  44. void warn(const Twine &Warning, const Twine &Context) {
  45. errs() << Twine("while processing ") + Context + ":\n";
  46. errs() << Twine("warning: ") + Warning + "\n";
  47. }
  48. bool error(const Twine &Error, const Twine &Context) {
  49. errs() << Twine("while processing ") + Context + ":\n";
  50. errs() << Twine("error: ") + Error + "\n";
  51. return false;
  52. }
  53. template <typename KeyT, typename ValT>
  54. using HalfOpenIntervalMap =
  55. IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
  56. IntervalMapHalfOpenInfo<KeyT>>;
  57. typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals;
  58. // FIXME: Delete this structure.
  59. struct PatchLocation {
  60. DIE::value_iterator I;
  61. PatchLocation() = default;
  62. PatchLocation(DIE::value_iterator I) : I(I) {}
  63. void set(uint64_t New) const {
  64. assert(I);
  65. const auto &Old = *I;
  66. assert(Old.getType() == DIEValue::isInteger);
  67. *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
  68. }
  69. uint64_t get() const {
  70. assert(I);
  71. return I->getDIEInteger().getValue();
  72. }
  73. };
  74. /// \brief Stores all information relating to a compile unit, be it in
  75. /// its original instance in the object file to its brand new cloned
  76. /// and linked DIE tree.
  77. class CompileUnit {
  78. public:
  79. /// \brief Information gathered about a DIE in the object file.
  80. struct DIEInfo {
  81. int64_t AddrAdjust; ///< Address offset to apply to the described entity.
  82. DIE *Clone; ///< Cloned version of that DIE.
  83. uint32_t ParentIdx; ///< The index of this DIE's parent.
  84. bool Keep; ///< Is the DIE part of the linked output?
  85. bool InDebugMap; ///< Was this DIE's entity found in the map?
  86. };
  87. CompileUnit(DWARFUnit &OrigUnit, unsigned ID)
  88. : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
  89. Ranges(RangeAlloc) {
  90. Info.resize(OrigUnit.getNumDIEs());
  91. }
  92. CompileUnit(CompileUnit &&RHS)
  93. : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
  94. CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
  95. NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) {
  96. // The CompileUnit container has been 'reserve()'d with the right
  97. // size. We cannot move the IntervalMap anyway.
  98. llvm_unreachable("CompileUnits should not be moved.");
  99. }
  100. DWARFUnit &getOrigUnit() const { return OrigUnit; }
  101. unsigned getUniqueID() const { return ID; }
  102. DIE *getOutputUnitDIE() const { return CUDie; }
  103. void setOutputUnitDIE(DIE *Die) { CUDie = Die; }
  104. DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
  105. const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
  106. uint64_t getStartOffset() const { return StartOffset; }
  107. uint64_t getNextUnitOffset() const { return NextUnitOffset; }
  108. void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
  109. uint64_t getLowPc() const { return LowPc; }
  110. uint64_t getHighPc() const { return HighPc; }
  111. Optional<PatchLocation> getUnitRangesAttribute() const {
  112. return UnitRangeAttribute;
  113. }
  114. const FunctionIntervals &getFunctionRanges() const { return Ranges; }
  115. const std::vector<PatchLocation> &getRangesAttributes() const {
  116. return RangeAttributes;
  117. }
  118. const std::vector<std::pair<PatchLocation, int64_t>> &
  119. getLocationAttributes() const {
  120. return LocationAttributes;
  121. }
  122. /// \brief Compute the end offset for this unit. Must be
  123. /// called after the CU's DIEs have been cloned.
  124. /// \returns the next unit offset (which is also the current
  125. /// debug_info section size).
  126. uint64_t computeNextUnitOffset();
  127. /// \brief Keep track of a forward reference to DIE \p Die in \p
  128. /// RefUnit by \p Attr. The attribute should be fixed up later to
  129. /// point to the absolute offset of \p Die in the debug_info section.
  130. void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
  131. PatchLocation Attr);
  132. /// \brief Apply all fixups recored by noteForwardReference().
  133. void fixupForwardReferences();
  134. /// \brief Add a function range [\p LowPC, \p HighPC) that is
  135. /// relocatad by applying offset \p PCOffset.
  136. void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
  137. /// \brief Keep track of a DW_AT_range attribute that we will need to
  138. /// patch up later.
  139. void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
  140. /// \brief Keep track of a location attribute pointing to a location
  141. /// list in the debug_loc section.
  142. void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
  143. /// \brief Add a name accelerator entry for \p Die with \p Name
  144. /// which is stored in the string table at \p Offset.
  145. void addNameAccelerator(const DIE *Die, const char *Name, uint32_t Offset,
  146. bool SkipPubnamesSection = false);
  147. /// \brief Add a type accelerator entry for \p Die with \p Name
  148. /// which is stored in the string table at \p Offset.
  149. void addTypeAccelerator(const DIE *Die, const char *Name, uint32_t Offset);
  150. struct AccelInfo {
  151. StringRef Name; ///< Name of the entry.
  152. const DIE *Die; ///< DIE this entry describes.
  153. uint32_t NameOffset; ///< Offset of Name in the string pool.
  154. bool SkipPubSection; ///< Emit this entry only in the apple_* sections.
  155. AccelInfo(StringRef Name, const DIE *Die, uint32_t NameOffset,
  156. bool SkipPubSection = false)
  157. : Name(Name), Die(Die), NameOffset(NameOffset),
  158. SkipPubSection(SkipPubSection) {}
  159. };
  160. const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
  161. const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
  162. private:
  163. DWARFUnit &OrigUnit;
  164. unsigned ID;
  165. std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
  166. DIE *CUDie; ///< Root of the linked DIE tree.
  167. uint64_t StartOffset;
  168. uint64_t NextUnitOffset;
  169. uint64_t LowPc;
  170. uint64_t HighPc;
  171. /// \brief A list of attributes to fixup with the absolute offset of
  172. /// a DIE in the debug_info section.
  173. ///
  174. /// The offsets for the attributes in this array couldn't be set while
  175. /// cloning because for cross-cu forward refences the target DIE's
  176. /// offset isn't known you emit the reference attribute.
  177. std::vector<std::tuple<DIE *, const CompileUnit *, PatchLocation>>
  178. ForwardDIEReferences;
  179. FunctionIntervals::Allocator RangeAlloc;
  180. /// \brief The ranges in that interval map are the PC ranges for
  181. /// functions in this unit, associated with the PC offset to apply
  182. /// to the addresses to get the linked address.
  183. FunctionIntervals Ranges;
  184. /// \brief DW_AT_ranges attributes to patch after we have gathered
  185. /// all the unit's function addresses.
  186. /// @{
  187. std::vector<PatchLocation> RangeAttributes;
  188. Optional<PatchLocation> UnitRangeAttribute;
  189. /// @}
  190. /// \brief Location attributes that need to be transfered from th
  191. /// original debug_loc section to the liked one. They are stored
  192. /// along with the PC offset that is to be applied to their
  193. /// function's address.
  194. std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
  195. /// \brief Accelerator entries for the unit, both for the pub*
  196. /// sections and the apple* ones.
  197. /// @{
  198. std::vector<AccelInfo> Pubnames;
  199. std::vector<AccelInfo> Pubtypes;
  200. /// @}
  201. };
  202. uint64_t CompileUnit::computeNextUnitOffset() {
  203. NextUnitOffset = StartOffset + 11 /* Header size */;
  204. // The root DIE might be null, meaning that the Unit had nothing to
  205. // contribute to the linked output. In that case, we will emit the
  206. // unit header without any actual DIE.
  207. if (CUDie)
  208. NextUnitOffset += CUDie->getSize();
  209. return NextUnitOffset;
  210. }
  211. /// \brief Keep track of a forward cross-cu reference from this unit
  212. /// to \p Die that lives in \p RefUnit.
  213. void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
  214. PatchLocation Attr) {
  215. ForwardDIEReferences.emplace_back(Die, RefUnit, Attr);
  216. }
  217. /// \brief Apply all fixups recorded by noteForwardReference().
  218. void CompileUnit::fixupForwardReferences() {
  219. for (const auto &Ref : ForwardDIEReferences) {
  220. DIE *RefDie;
  221. const CompileUnit *RefUnit;
  222. PatchLocation Attr;
  223. std::tie(RefDie, RefUnit, Attr) = Ref;
  224. Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
  225. }
  226. }
  227. void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
  228. int64_t PcOffset) {
  229. Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
  230. this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
  231. this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
  232. }
  233. void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
  234. if (Die.getTag() != dwarf::DW_TAG_compile_unit)
  235. RangeAttributes.push_back(Attr);
  236. else
  237. UnitRangeAttribute = Attr;
  238. }
  239. void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
  240. LocationAttributes.emplace_back(Attr, PcOffset);
  241. }
  242. /// \brief Add a name accelerator entry for \p Die with \p Name
  243. /// which is stored in the string table at \p Offset.
  244. void CompileUnit::addNameAccelerator(const DIE *Die, const char *Name,
  245. uint32_t Offset, bool SkipPubSection) {
  246. Pubnames.emplace_back(Name, Die, Offset, SkipPubSection);
  247. }
  248. /// \brief Add a type accelerator entry for \p Die with \p Name
  249. /// which is stored in the string table at \p Offset.
  250. void CompileUnit::addTypeAccelerator(const DIE *Die, const char *Name,
  251. uint32_t Offset) {
  252. Pubtypes.emplace_back(Name, Die, Offset, false);
  253. }
  254. /// \brief A string table that doesn't need relocations.
  255. ///
  256. /// We are doing a final link, no need for a string table that
  257. /// has relocation entries for every reference to it. This class
  258. /// provides this ablitity by just associating offsets with
  259. /// strings.
  260. class NonRelocatableStringpool {
  261. public:
  262. /// \brief Entries are stored into the StringMap and simply linked
  263. /// together through the second element of this pair in order to
  264. /// keep track of insertion order.
  265. typedef StringMap<std::pair<uint32_t, StringMapEntryBase *>, BumpPtrAllocator>
  266. MapTy;
  267. NonRelocatableStringpool()
  268. : CurrentEndOffset(0), Sentinel(0), Last(&Sentinel) {
  269. // Legacy dsymutil puts an empty string at the start of the line
  270. // table.
  271. getStringOffset("");
  272. }
  273. /// \brief Get the offset of string \p S in the string table. This
  274. /// can insert a new element or return the offset of a preexisitng
  275. /// one.
  276. uint32_t getStringOffset(StringRef S);
  277. /// \brief Get permanent storage for \p S (but do not necessarily
  278. /// emit \p S in the output section).
  279. /// \returns The StringRef that points to permanent storage to use
  280. /// in place of \p S.
  281. StringRef internString(StringRef S);
  282. // \brief Return the first entry of the string table.
  283. const MapTy::MapEntryTy *getFirstEntry() const {
  284. return getNextEntry(&Sentinel);
  285. }
  286. // \brief Get the entry following \p E in the string table or null
  287. // if \p E was the last entry.
  288. const MapTy::MapEntryTy *getNextEntry(const MapTy::MapEntryTy *E) const {
  289. return static_cast<const MapTy::MapEntryTy *>(E->getValue().second);
  290. }
  291. uint64_t getSize() { return CurrentEndOffset; }
  292. private:
  293. MapTy Strings;
  294. uint32_t CurrentEndOffset;
  295. MapTy::MapEntryTy Sentinel, *Last;
  296. };
  297. /// \brief Get the offset of string \p S in the string table. This
  298. /// can insert a new element or return the offset of a preexisitng
  299. /// one.
  300. uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
  301. if (S.empty() && !Strings.empty())
  302. return 0;
  303. std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
  304. MapTy::iterator It;
  305. bool Inserted;
  306. // A non-empty string can't be at offset 0, so if we have an entry
  307. // with a 0 offset, it must be a previously interned string.
  308. std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
  309. if (Inserted || It->getValue().first == 0) {
  310. // Set offset and chain at the end of the entries list.
  311. It->getValue().first = CurrentEndOffset;
  312. CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
  313. Last->getValue().second = &*It;
  314. Last = &*It;
  315. }
  316. return It->getValue().first;
  317. }
  318. /// \brief Put \p S into the StringMap so that it gets permanent
  319. /// storage, but do not actually link it in the chain of elements
  320. /// that go into the output section. A latter call to
  321. /// getStringOffset() with the same string will chain it though.
  322. StringRef NonRelocatableStringpool::internString(StringRef S) {
  323. std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
  324. auto InsertResult = Strings.insert(std::make_pair(S, Entry));
  325. return InsertResult.first->getKey();
  326. }
  327. /// \brief The Dwarf streaming logic
  328. ///
  329. /// All interactions with the MC layer that is used to build the debug
  330. /// information binary representation are handled in this class.
  331. class DwarfStreamer {
  332. /// \defgroup MCObjects MC layer objects constructed by the streamer
  333. /// @{
  334. std::unique_ptr<MCRegisterInfo> MRI;
  335. std::unique_ptr<MCAsmInfo> MAI;
  336. std::unique_ptr<MCObjectFileInfo> MOFI;
  337. std::unique_ptr<MCContext> MC;
  338. MCAsmBackend *MAB; // Owned by MCStreamer
  339. std::unique_ptr<MCInstrInfo> MII;
  340. std::unique_ptr<MCSubtargetInfo> MSTI;
  341. MCCodeEmitter *MCE; // Owned by MCStreamer
  342. MCStreamer *MS; // Owned by AsmPrinter
  343. std::unique_ptr<TargetMachine> TM;
  344. std::unique_ptr<AsmPrinter> Asm;
  345. /// @}
  346. /// \brief the file we stream the linked Dwarf to.
  347. std::unique_ptr<raw_fd_ostream> OutFile;
  348. uint32_t RangesSectionSize;
  349. uint32_t LocSectionSize;
  350. uint32_t LineSectionSize;
  351. uint32_t FrameSectionSize;
  352. /// \brief Emit the pubnames or pubtypes section contribution for \p
  353. /// Unit into \p Sec. The data is provided in \p Names.
  354. void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
  355. const CompileUnit &Unit,
  356. const std::vector<CompileUnit::AccelInfo> &Names);
  357. public:
  358. /// \brief Actually create the streamer and the ouptut file.
  359. ///
  360. /// This could be done directly in the constructor, but it feels
  361. /// more natural to handle errors through return value.
  362. bool init(Triple TheTriple, StringRef OutputFilename);
  363. /// \brief Dump the file to the disk.
  364. bool finish();
  365. AsmPrinter &getAsmPrinter() const { return *Asm; }
  366. /// \brief Set the current output section to debug_info and change
  367. /// the MC Dwarf version to \p DwarfVersion.
  368. void switchToDebugInfoSection(unsigned DwarfVersion);
  369. /// \brief Emit the compilation unit header for \p Unit in the
  370. /// debug_info section.
  371. ///
  372. /// As a side effect, this also switches the current Dwarf version
  373. /// of the MC layer to the one of U.getOrigUnit().
  374. void emitCompileUnitHeader(CompileUnit &Unit);
  375. /// \brief Recursively emit the DIE tree rooted at \p Die.
  376. void emitDIE(DIE &Die);
  377. /// \brief Emit the abbreviation table \p Abbrevs to the
  378. /// debug_abbrev section.
  379. void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs);
  380. /// \brief Emit the string table described by \p Pool.
  381. void emitStrings(const NonRelocatableStringpool &Pool);
  382. /// \brief Emit debug_ranges for \p FuncRange by translating the
  383. /// original \p Entries.
  384. void emitRangesEntries(
  385. int64_t UnitPcOffset, uint64_t OrigLowPc,
  386. FunctionIntervals::const_iterator FuncRange,
  387. const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
  388. unsigned AddressSize);
  389. /// \brief Emit debug_aranges entries for \p Unit and if \p
  390. /// DoRangesSection is true, also emit the debug_ranges entries for
  391. /// the DW_TAG_compile_unit's DW_AT_ranges attribute.
  392. void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
  393. uint32_t getRangesSectionSize() const { return RangesSectionSize; }
  394. /// \brief Emit the debug_loc contribution for \p Unit by copying
  395. /// the entries from \p Dwarf and offseting them. Update the
  396. /// location attributes to point to the new entries.
  397. void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
  398. /// \brief Emit the line table described in \p Rows into the
  399. /// debug_line section.
  400. void emitLineTableForUnit(StringRef PrologueBytes, unsigned MinInstLength,
  401. std::vector<DWARFDebugLine::Row> &Rows,
  402. unsigned AdddressSize);
  403. uint32_t getLineSectionSize() const { return LineSectionSize; }
  404. /// \brief Emit the .debug_pubnames contribution for \p Unit.
  405. void emitPubNamesForUnit(const CompileUnit &Unit);
  406. /// \brief Emit the .debug_pubtypes contribution for \p Unit.
  407. void emitPubTypesForUnit(const CompileUnit &Unit);
  408. /// \brief Emit a CIE.
  409. void emitCIE(StringRef CIEBytes);
  410. /// \brief Emit an FDE with data \p Bytes.
  411. void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
  412. StringRef Bytes);
  413. uint32_t getFrameSectionSize() const { return FrameSectionSize; }
  414. };
  415. bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
  416. std::string ErrorStr;
  417. std::string TripleName;
  418. StringRef Context = "dwarf streamer init";
  419. // Get the target.
  420. const Target *TheTarget =
  421. TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
  422. if (!TheTarget)
  423. return error(ErrorStr, Context);
  424. TripleName = TheTriple.getTriple();
  425. // Create all the MC Objects.
  426. MRI.reset(TheTarget->createMCRegInfo(TripleName));
  427. if (!MRI)
  428. return error(Twine("no register info for target ") + TripleName, Context);
  429. MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
  430. if (!MAI)
  431. return error("no asm info for target " + TripleName, Context);
  432. MOFI.reset(new MCObjectFileInfo);
  433. MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
  434. MOFI->InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
  435. *MC);
  436. MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
  437. if (!MAB)
  438. return error("no asm backend for target " + TripleName, Context);
  439. MII.reset(TheTarget->createMCInstrInfo());
  440. if (!MII)
  441. return error("no instr info info for target " + TripleName, Context);
  442. MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
  443. if (!MSTI)
  444. return error("no subtarget info for target " + TripleName, Context);
  445. MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
  446. if (!MCE)
  447. return error("no code emitter for target " + TripleName, Context);
  448. // Create the output file.
  449. std::error_code EC;
  450. OutFile =
  451. llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
  452. if (EC)
  453. return error(Twine(OutputFilename) + ": " + EC.message(), Context);
  454. MS = TheTarget->createMCObjectStreamer(TheTriple, *MC, *MAB, *OutFile, MCE,
  455. *MSTI, false,
  456. /*DWARFMustBeAtTheEnd*/ false);
  457. if (!MS)
  458. return error("no object streamer for target " + TripleName, Context);
  459. // Finally create the AsmPrinter we'll use to emit the DIEs.
  460. TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
  461. if (!TM)
  462. return error("no target machine for target " + TripleName, Context);
  463. Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
  464. if (!Asm)
  465. return error("no asm printer for target " + TripleName, Context);
  466. RangesSectionSize = 0;
  467. LocSectionSize = 0;
  468. LineSectionSize = 0;
  469. FrameSectionSize = 0;
  470. return true;
  471. }
  472. bool DwarfStreamer::finish() {
  473. MS->Finish();
  474. return true;
  475. }
  476. /// \brief Set the current output section to debug_info and change
  477. /// the MC Dwarf version to \p DwarfVersion.
  478. void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
  479. MS->SwitchSection(MOFI->getDwarfInfoSection());
  480. MC->setDwarfVersion(DwarfVersion);
  481. }
  482. /// \brief Emit the compilation unit header for \p Unit in the
  483. /// debug_info section.
  484. ///
  485. /// A Dwarf scetion header is encoded as:
  486. /// uint32_t Unit length (omiting this field)
  487. /// uint16_t Version
  488. /// uint32_t Abbreviation table offset
  489. /// uint8_t Address size
  490. ///
  491. /// Leading to a total of 11 bytes.
  492. void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
  493. unsigned Version = Unit.getOrigUnit().getVersion();
  494. switchToDebugInfoSection(Version);
  495. // Emit size of content not including length itself. The size has
  496. // already been computed in CompileUnit::computeOffsets(). Substract
  497. // 4 to that size to account for the length field.
  498. Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
  499. Asm->EmitInt16(Version);
  500. // We share one abbreviations table across all units so it's always at the
  501. // start of the section.
  502. Asm->EmitInt32(0);
  503. Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
  504. }
  505. /// \brief Emit the \p Abbrevs array as the shared abbreviation table
  506. /// for the linked Dwarf file.
  507. void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) {
  508. MS->SwitchSection(MOFI->getDwarfAbbrevSection());
  509. Asm->emitDwarfAbbrevs(Abbrevs);
  510. }
  511. /// \brief Recursively emit the DIE tree rooted at \p Die.
  512. void DwarfStreamer::emitDIE(DIE &Die) {
  513. MS->SwitchSection(MOFI->getDwarfInfoSection());
  514. Asm->emitDwarfDIE(Die);
  515. }
  516. /// \brief Emit the debug_str section stored in \p Pool.
  517. void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
  518. Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
  519. for (auto *Entry = Pool.getFirstEntry(); Entry;
  520. Entry = Pool.getNextEntry(Entry))
  521. Asm->OutStreamer->EmitBytes(
  522. StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
  523. }
  524. /// \brief Emit the debug_range section contents for \p FuncRange by
  525. /// translating the original \p Entries. The debug_range section
  526. /// format is totally trivial, consisting just of pairs of address
  527. /// sized addresses describing the ranges.
  528. void DwarfStreamer::emitRangesEntries(
  529. int64_t UnitPcOffset, uint64_t OrigLowPc,
  530. FunctionIntervals::const_iterator FuncRange,
  531. const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
  532. unsigned AddressSize) {
  533. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
  534. // Offset each range by the right amount.
  535. int64_t PcOffset = FuncRange.value() + UnitPcOffset;
  536. for (const auto &Range : Entries) {
  537. if (Range.isBaseAddressSelectionEntry(AddressSize)) {
  538. warn("unsupported base address selection operation",
  539. "emitting debug_ranges");
  540. break;
  541. }
  542. // Do not emit empty ranges.
  543. if (Range.StartAddress == Range.EndAddress)
  544. continue;
  545. // All range entries should lie in the function range.
  546. if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
  547. Range.EndAddress + OrigLowPc <= FuncRange.stop()))
  548. warn("inconsistent range data.", "emitting debug_ranges");
  549. MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
  550. MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
  551. RangesSectionSize += 2 * AddressSize;
  552. }
  553. // Add the terminator entry.
  554. MS->EmitIntValue(0, AddressSize);
  555. MS->EmitIntValue(0, AddressSize);
  556. RangesSectionSize += 2 * AddressSize;
  557. }
  558. /// \brief Emit the debug_aranges contribution of a unit and
  559. /// if \p DoDebugRanges is true the debug_range contents for a
  560. /// compile_unit level DW_AT_ranges attribute (Which are basically the
  561. /// same thing with a different base address).
  562. /// Just aggregate all the ranges gathered inside that unit.
  563. void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
  564. bool DoDebugRanges) {
  565. unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
  566. // Gather the ranges in a vector, so that we can simplify them. The
  567. // IntervalMap will have coalesced the non-linked ranges, but here
  568. // we want to coalesce the linked addresses.
  569. std::vector<std::pair<uint64_t, uint64_t>> Ranges;
  570. const auto &FunctionRanges = Unit.getFunctionRanges();
  571. for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
  572. Range != End; ++Range)
  573. Ranges.push_back(std::make_pair(Range.start() + Range.value(),
  574. Range.stop() + Range.value()));
  575. // The object addresses where sorted, but again, the linked
  576. // addresses might end up in a different order.
  577. std::sort(Ranges.begin(), Ranges.end());
  578. if (!Ranges.empty()) {
  579. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
  580. MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
  581. MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
  582. unsigned HeaderSize =
  583. sizeof(int32_t) + // Size of contents (w/o this field
  584. sizeof(int16_t) + // DWARF ARange version number
  585. sizeof(int32_t) + // Offset of CU in the .debug_info section
  586. sizeof(int8_t) + // Pointer Size (in bytes)
  587. sizeof(int8_t); // Segment Size (in bytes)
  588. unsigned TupleSize = AddressSize * 2;
  589. unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
  590. Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
  591. Asm->OutStreamer->EmitLabel(BeginLabel);
  592. Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
  593. Asm->EmitInt32(Unit.getStartOffset()); // Corresponding unit's offset
  594. Asm->EmitInt8(AddressSize); // Address size
  595. Asm->EmitInt8(0); // Segment size
  596. Asm->OutStreamer->EmitFill(Padding, 0x0);
  597. for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
  598. ++Range) {
  599. uint64_t RangeStart = Range->first;
  600. MS->EmitIntValue(RangeStart, AddressSize);
  601. while ((Range + 1) != End && Range->second == (Range + 1)->first)
  602. ++Range;
  603. MS->EmitIntValue(Range->second - RangeStart, AddressSize);
  604. }
  605. // Emit terminator
  606. Asm->OutStreamer->EmitIntValue(0, AddressSize);
  607. Asm->OutStreamer->EmitIntValue(0, AddressSize);
  608. Asm->OutStreamer->EmitLabel(EndLabel);
  609. }
  610. if (!DoDebugRanges)
  611. return;
  612. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
  613. // Offset each range by the right amount.
  614. int64_t PcOffset = -Unit.getLowPc();
  615. // Emit coalesced ranges.
  616. for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
  617. MS->EmitIntValue(Range->first + PcOffset, AddressSize);
  618. while (Range + 1 != End && Range->second == (Range + 1)->first)
  619. ++Range;
  620. MS->EmitIntValue(Range->second + PcOffset, AddressSize);
  621. RangesSectionSize += 2 * AddressSize;
  622. }
  623. // Add the terminator entry.
  624. MS->EmitIntValue(0, AddressSize);
  625. MS->EmitIntValue(0, AddressSize);
  626. RangesSectionSize += 2 * AddressSize;
  627. }
  628. /// \brief Emit location lists for \p Unit and update attribtues to
  629. /// point to the new entries.
  630. void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
  631. DWARFContext &Dwarf) {
  632. const auto &Attributes = Unit.getLocationAttributes();
  633. if (Attributes.empty())
  634. return;
  635. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
  636. unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
  637. const DWARFSection &InputSec = Dwarf.getLocSection();
  638. DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
  639. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  640. const auto *OrigUnitDie = OrigUnit.getUnitDIE(false);
  641. int64_t UnitPcOffset = 0;
  642. uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
  643. &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
  644. if (OrigLowPc != -1ULL)
  645. UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
  646. for (const auto &Attr : Attributes) {
  647. uint32_t Offset = Attr.first.get();
  648. Attr.first.set(LocSectionSize);
  649. // This is the quantity to add to the old location address to get
  650. // the correct address for the new one.
  651. int64_t LocPcOffset = Attr.second + UnitPcOffset;
  652. while (Data.isValidOffset(Offset)) {
  653. uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
  654. uint64_t High = Data.getUnsigned(&Offset, AddressSize);
  655. LocSectionSize += 2 * AddressSize;
  656. if (Low == 0 && High == 0) {
  657. Asm->OutStreamer->EmitIntValue(0, AddressSize);
  658. Asm->OutStreamer->EmitIntValue(0, AddressSize);
  659. break;
  660. }
  661. Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize);
  662. Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize);
  663. uint64_t Length = Data.getU16(&Offset);
  664. Asm->OutStreamer->EmitIntValue(Length, 2);
  665. // Just copy the bytes over.
  666. Asm->OutStreamer->EmitBytes(
  667. StringRef(InputSec.Data.substr(Offset, Length)));
  668. Offset += Length;
  669. LocSectionSize += Length + 2;
  670. }
  671. }
  672. }
  673. void DwarfStreamer::emitLineTableForUnit(StringRef PrologueBytes,
  674. unsigned MinInstLength,
  675. std::vector<DWARFDebugLine::Row> &Rows,
  676. unsigned PointerSize) {
  677. // Switch to the section where the table will be emitted into.
  678. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
  679. MCSymbol *LineStartSym = MC->createTempSymbol();
  680. MCSymbol *LineEndSym = MC->createTempSymbol();
  681. // The first 4 bytes is the total length of the information for this
  682. // compilation unit (not including these 4 bytes for the length).
  683. Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
  684. Asm->OutStreamer->EmitLabel(LineStartSym);
  685. // Copy Prologue.
  686. MS->EmitBytes(PrologueBytes);
  687. LineSectionSize += PrologueBytes.size() + 4;
  688. SmallString<128> EncodingBuffer;
  689. raw_svector_ostream EncodingOS(EncodingBuffer);
  690. if (Rows.empty()) {
  691. // We only have the dummy entry, dsymutil emits an entry with a 0
  692. // address in that case.
  693. MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
  694. MS->EmitBytes(EncodingOS.str());
  695. LineSectionSize += EncodingBuffer.size();
  696. MS->EmitLabel(LineEndSym);
  697. return;
  698. }
  699. // Line table state machine fields
  700. unsigned FileNum = 1;
  701. unsigned LastLine = 1;
  702. unsigned Column = 0;
  703. unsigned IsStatement = 1;
  704. unsigned Isa = 0;
  705. uint64_t Address = -1ULL;
  706. unsigned RowsSinceLastSequence = 0;
  707. for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
  708. auto &Row = Rows[Idx];
  709. int64_t AddressDelta;
  710. if (Address == -1ULL) {
  711. MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
  712. MS->EmitULEB128IntValue(PointerSize + 1);
  713. MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
  714. MS->EmitIntValue(Row.Address, PointerSize);
  715. LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
  716. AddressDelta = 0;
  717. } else {
  718. AddressDelta = (Row.Address - Address) / MinInstLength;
  719. }
  720. // FIXME: code copied and transfromed from
  721. // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share
  722. // this code, but the current compatibility requirement with
  723. // classic dsymutil makes it hard. Revisit that once this
  724. // requirement is dropped.
  725. if (FileNum != Row.File) {
  726. FileNum = Row.File;
  727. MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
  728. MS->EmitULEB128IntValue(FileNum);
  729. LineSectionSize += 1 + getULEB128Size(FileNum);
  730. }
  731. if (Column != Row.Column) {
  732. Column = Row.Column;
  733. MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
  734. MS->EmitULEB128IntValue(Column);
  735. LineSectionSize += 1 + getULEB128Size(Column);
  736. }
  737. // FIXME: We should handle the discriminator here, but dsymutil
  738. // doesn' consider it, thus ignore it for now.
  739. if (Isa != Row.Isa) {
  740. Isa = Row.Isa;
  741. MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
  742. MS->EmitULEB128IntValue(Isa);
  743. LineSectionSize += 1 + getULEB128Size(Isa);
  744. }
  745. if (IsStatement != Row.IsStmt) {
  746. IsStatement = Row.IsStmt;
  747. MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
  748. LineSectionSize += 1;
  749. }
  750. if (Row.BasicBlock) {
  751. MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
  752. LineSectionSize += 1;
  753. }
  754. if (Row.PrologueEnd) {
  755. MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
  756. LineSectionSize += 1;
  757. }
  758. if (Row.EpilogueBegin) {
  759. MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
  760. LineSectionSize += 1;
  761. }
  762. int64_t LineDelta = int64_t(Row.Line) - LastLine;
  763. if (!Row.EndSequence) {
  764. MCDwarfLineAddr::Encode(*MC, LineDelta, AddressDelta, EncodingOS);
  765. MS->EmitBytes(EncodingOS.str());
  766. LineSectionSize += EncodingBuffer.size();
  767. EncodingBuffer.resize(0);
  768. EncodingOS.resync();
  769. Address = Row.Address;
  770. LastLine = Row.Line;
  771. RowsSinceLastSequence++;
  772. } else {
  773. if (LineDelta) {
  774. MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
  775. MS->EmitSLEB128IntValue(LineDelta);
  776. LineSectionSize += 1 + getSLEB128Size(LineDelta);
  777. }
  778. if (AddressDelta) {
  779. MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
  780. MS->EmitULEB128IntValue(AddressDelta);
  781. LineSectionSize += 1 + getULEB128Size(AddressDelta);
  782. }
  783. MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
  784. MS->EmitBytes(EncodingOS.str());
  785. LineSectionSize += EncodingBuffer.size();
  786. EncodingBuffer.resize(0);
  787. EncodingOS.resync();
  788. Address = -1ULL;
  789. LastLine = FileNum = IsStatement = 1;
  790. RowsSinceLastSequence = Column = Isa = 0;
  791. }
  792. }
  793. if (RowsSinceLastSequence) {
  794. MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
  795. MS->EmitBytes(EncodingOS.str());
  796. LineSectionSize += EncodingBuffer.size();
  797. EncodingBuffer.resize(0);
  798. EncodingOS.resync();
  799. }
  800. MS->EmitLabel(LineEndSym);
  801. }
  802. /// \brief Emit the pubnames or pubtypes section contribution for \p
  803. /// Unit into \p Sec. The data is provided in \p Names.
  804. void DwarfStreamer::emitPubSectionForUnit(
  805. MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
  806. const std::vector<CompileUnit::AccelInfo> &Names) {
  807. if (Names.empty())
  808. return;
  809. // Start the dwarf pubnames section.
  810. Asm->OutStreamer->SwitchSection(Sec);
  811. MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
  812. MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
  813. bool HeaderEmitted = false;
  814. // Emit the pubnames for this compilation unit.
  815. for (const auto &Name : Names) {
  816. if (Name.SkipPubSection)
  817. continue;
  818. if (!HeaderEmitted) {
  819. // Emit the header.
  820. Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
  821. Asm->OutStreamer->EmitLabel(BeginLabel);
  822. Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
  823. Asm->EmitInt32(Unit.getStartOffset()); // Unit offset
  824. Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
  825. HeaderEmitted = true;
  826. }
  827. Asm->EmitInt32(Name.Die->getOffset());
  828. Asm->OutStreamer->EmitBytes(
  829. StringRef(Name.Name.data(), Name.Name.size() + 1));
  830. }
  831. if (!HeaderEmitted)
  832. return;
  833. Asm->EmitInt32(0); // End marker.
  834. Asm->OutStreamer->EmitLabel(EndLabel);
  835. }
  836. /// \brief Emit .debug_pubnames for \p Unit.
  837. void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
  838. emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
  839. "names", Unit, Unit.getPubnames());
  840. }
  841. /// \brief Emit .debug_pubtypes for \p Unit.
  842. void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
  843. emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
  844. "types", Unit, Unit.getPubtypes());
  845. }
  846. /// \brief Emit a CIE into the debug_frame section.
  847. void DwarfStreamer::emitCIE(StringRef CIEBytes) {
  848. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
  849. MS->EmitBytes(CIEBytes);
  850. FrameSectionSize += CIEBytes.size();
  851. }
  852. /// \brief Emit a FDE into the debug_frame section. \p FDEBytes
  853. /// contains the FDE data without the length, CIE offset and address
  854. /// which will be replaced with the paramter values.
  855. void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
  856. uint32_t Address, StringRef FDEBytes) {
  857. MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
  858. MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
  859. MS->EmitIntValue(CIEOffset, 4);
  860. MS->EmitIntValue(Address, AddrSize);
  861. MS->EmitBytes(FDEBytes);
  862. FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
  863. }
  864. /// \brief The core of the Dwarf linking logic.
  865. ///
  866. /// The link of the dwarf information from the object files will be
  867. /// driven by the selection of 'root DIEs', which are DIEs that
  868. /// describe variables or functions that are present in the linked
  869. /// binary (and thus have entries in the debug map). All the debug
  870. /// information that will be linked (the DIEs, but also the line
  871. /// tables, ranges, ...) is derived from that set of root DIEs.
  872. ///
  873. /// The root DIEs are identified because they contain relocations that
  874. /// correspond to a debug map entry at specific places (the low_pc for
  875. /// a function, the location for a variable). These relocations are
  876. /// called ValidRelocs in the DwarfLinker and are gathered as a very
  877. /// first step when we start processing a DebugMapObject.
  878. class DwarfLinker {
  879. public:
  880. DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
  881. : OutputFilename(OutputFilename), Options(Options),
  882. BinHolder(Options.Verbose), LastCIEOffset(0) {}
  883. ~DwarfLinker() {
  884. for (auto *Abbrev : Abbreviations)
  885. delete Abbrev;
  886. }
  887. /// \brief Link the contents of the DebugMap.
  888. bool link(const DebugMap &);
  889. private:
  890. /// \brief Called at the start of a debug object link.
  891. void startDebugObject(DWARFContext &, DebugMapObject &);
  892. /// \brief Called at the end of a debug object link.
  893. void endDebugObject();
  894. /// \defgroup FindValidRelocations Translate debug map into a list
  895. /// of relevant relocations
  896. ///
  897. /// @{
  898. struct ValidReloc {
  899. uint32_t Offset;
  900. uint32_t Size;
  901. uint64_t Addend;
  902. const DebugMapObject::DebugMapEntry *Mapping;
  903. ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
  904. const DebugMapObject::DebugMapEntry *Mapping)
  905. : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
  906. bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
  907. };
  908. /// \brief The valid relocations for the current DebugMapObject.
  909. /// This vector is sorted by relocation offset.
  910. std::vector<ValidReloc> ValidRelocs;
  911. /// \brief Index into ValidRelocs of the next relocation to
  912. /// consider. As we walk the DIEs in acsending file offset and as
  913. /// ValidRelocs is sorted by file offset, keeping this index
  914. /// uptodate is all we have to do to have a cheap lookup during the
  915. /// root DIE selection and during DIE cloning.
  916. unsigned NextValidReloc;
  917. bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
  918. const DebugMapObject &DMO);
  919. bool findValidRelocs(const object::SectionRef &Section,
  920. const object::ObjectFile &Obj,
  921. const DebugMapObject &DMO);
  922. void findValidRelocsMachO(const object::SectionRef &Section,
  923. const object::MachOObjectFile &Obj,
  924. const DebugMapObject &DMO);
  925. /// @}
  926. /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
  927. ///
  928. /// @{
  929. /// \brief Recursively walk the \p DIE tree and look for DIEs to
  930. /// keep. Store that information in \p CU's DIEInfo.
  931. void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
  932. const DebugMapObject &DMO, CompileUnit &CU,
  933. unsigned Flags);
  934. /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
  935. enum TravesalFlags {
  936. TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
  937. TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
  938. TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
  939. TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
  940. };
  941. /// \brief Mark the passed DIE as well as all the ones it depends on
  942. /// as kept.
  943. void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
  944. CompileUnit::DIEInfo &MyInfo,
  945. const DebugMapObject &DMO, CompileUnit &CU,
  946. unsigned Flags);
  947. unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
  948. CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
  949. unsigned Flags);
  950. unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
  951. CompileUnit &Unit,
  952. CompileUnit::DIEInfo &MyInfo, unsigned Flags);
  953. unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
  954. CompileUnit &Unit,
  955. CompileUnit::DIEInfo &MyInfo,
  956. unsigned Flags);
  957. bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
  958. CompileUnit::DIEInfo &Info);
  959. /// @}
  960. /// \defgroup Linking Methods used to link the debug information
  961. ///
  962. /// @{
  963. /// \brief Recursively clone \p InputDIE into an tree of DIE objects
  964. /// where useless (as decided by lookForDIEsToKeep()) bits have been
  965. /// stripped out and addresses have been rewritten according to the
  966. /// debug map.
  967. ///
  968. /// \param OutOffset is the offset the cloned DIE in the output
  969. /// compile unit.
  970. /// \param PCOffset (while cloning a function scope) is the offset
  971. /// applied to the entry point of the function to get the linked address.
  972. ///
  973. /// \returns the root of the cloned tree.
  974. DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
  975. int64_t PCOffset, uint32_t OutOffset);
  976. typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
  977. /// \brief Information gathered and exchanged between the various
  978. /// clone*Attributes helpers about the attributes of a particular DIE.
  979. struct AttributesInfo {
  980. const char *Name, *MangledName; ///< Names.
  981. uint32_t NameOffset, MangledNameOffset; ///< Offsets in the string pool.
  982. uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE
  983. int64_t PCOffset; ///< Offset to apply to PC addresses inside a function.
  984. bool HasLowPc; ///< Does the DIE have a low_pc attribute?
  985. bool IsDeclaration; ///< Is this DIE only a declaration?
  986. AttributesInfo()
  987. : Name(nullptr), MangledName(nullptr), NameOffset(0),
  988. MangledNameOffset(0), OrigHighPc(0), PCOffset(0), HasLowPc(false),
  989. IsDeclaration(false) {}
  990. };
  991. /// \brief Helper for cloneDIE.
  992. unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
  993. CompileUnit &U, const DWARFFormValue &Val,
  994. const AttributeSpec AttrSpec, unsigned AttrSize,
  995. AttributesInfo &AttrInfo);
  996. /// \brief Helper for cloneDIE.
  997. unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
  998. const DWARFFormValue &Val, const DWARFUnit &U);
  999. /// \brief Helper for cloneDIE.
  1000. unsigned
  1001. cloneDieReferenceAttribute(DIE &Die,
  1002. const DWARFDebugInfoEntryMinimal &InputDIE,
  1003. AttributeSpec AttrSpec, unsigned AttrSize,
  1004. const DWARFFormValue &Val, CompileUnit &Unit);
  1005. /// \brief Helper for cloneDIE.
  1006. unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
  1007. const DWARFFormValue &Val, unsigned AttrSize);
  1008. /// \brief Helper for cloneDIE.
  1009. unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
  1010. const DWARFFormValue &Val,
  1011. const CompileUnit &Unit, AttributesInfo &Info);
  1012. /// \brief Helper for cloneDIE.
  1013. unsigned cloneScalarAttribute(DIE &Die,
  1014. const DWARFDebugInfoEntryMinimal &InputDIE,
  1015. CompileUnit &U, AttributeSpec AttrSpec,
  1016. const DWARFFormValue &Val, unsigned AttrSize,
  1017. AttributesInfo &Info);
  1018. /// \brief Helper for cloneDIE.
  1019. bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
  1020. bool isLittleEndian);
  1021. /// \brief Assign an abbreviation number to \p Abbrev
  1022. void AssignAbbrev(DIEAbbrev &Abbrev);
  1023. /// \brief FoldingSet that uniques the abbreviations.
  1024. FoldingSet<DIEAbbrev> AbbreviationsSet;
  1025. /// \brief Storage for the unique Abbreviations.
  1026. /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
  1027. /// be changed to a vecot of unique_ptrs.
  1028. std::vector<DIEAbbrev *> Abbreviations;
  1029. /// \brief Compute and emit debug_ranges section for \p Unit, and
  1030. /// patch the attributes referencing it.
  1031. void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
  1032. /// \brief Generate and emit the DW_AT_ranges attribute for a
  1033. /// compile_unit if it had one.
  1034. void generateUnitRanges(CompileUnit &Unit) const;
  1035. /// \brief Extract the line tables fromt he original dwarf, extract
  1036. /// the relevant parts according to the linked function ranges and
  1037. /// emit the result in the debug_line section.
  1038. void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
  1039. /// \brief Emit the accelerator entries for \p Unit.
  1040. void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
  1041. /// \brief Patch the frame info for an object file and emit it.
  1042. void patchFrameInfoForObject(const DebugMapObject &, DWARFContext &,
  1043. unsigned AddressSize);
  1044. /// \brief DIELoc objects that need to be destructed (but not freed!).
  1045. std::vector<DIELoc *> DIELocs;
  1046. /// \brief DIEBlock objects that need to be destructed (but not freed!).
  1047. std::vector<DIEBlock *> DIEBlocks;
  1048. /// \brief Allocator used for all the DIEValue objects.
  1049. BumpPtrAllocator DIEAlloc;
  1050. /// @}
  1051. /// \defgroup Helpers Various helper methods.
  1052. ///
  1053. /// @{
  1054. const DWARFDebugInfoEntryMinimal *
  1055. resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
  1056. const DWARFDebugInfoEntryMinimal &DIE,
  1057. CompileUnit *&ReferencedCU);
  1058. CompileUnit *getUnitForOffset(unsigned Offset);
  1059. bool getDIENames(const DWARFDebugInfoEntryMinimal &Die, DWARFUnit &U,
  1060. AttributesInfo &Info);
  1061. void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
  1062. const DWARFDebugInfoEntryMinimal *DIE = nullptr) const;
  1063. bool createStreamer(Triple TheTriple, StringRef OutputFilename);
  1064. /// @}
  1065. private:
  1066. std::string OutputFilename;
  1067. LinkOptions Options;
  1068. BinaryHolder BinHolder;
  1069. std::unique_ptr<DwarfStreamer> Streamer;
  1070. /// The units of the current debug map object.
  1071. std::vector<CompileUnit> Units;
  1072. /// The debug map object curently under consideration.
  1073. DebugMapObject *CurrentDebugObject;
  1074. /// \brief The Dwarf string pool
  1075. NonRelocatableStringpool StringPool;
  1076. /// \brief This map is keyed by the entry PC of functions in that
  1077. /// debug object and the associated value is a pair storing the
  1078. /// corresponding end PC and the offset to apply to get the linked
  1079. /// address.
  1080. ///
  1081. /// See startDebugObject() for a more complete description of its use.
  1082. std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
  1083. /// \brief The CIEs that have been emitted in the output
  1084. /// section. The actual CIE data serves a the key to this StringMap,
  1085. /// this takes care of comparing the semantics of CIEs defined in
  1086. /// different object files.
  1087. StringMap<uint32_t> EmittedCIEs;
  1088. /// Offset of the last CIE that has been emitted in the output
  1089. /// debug_frame section.
  1090. uint32_t LastCIEOffset;
  1091. };
  1092. /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
  1093. /// returning our CompileUnit object instead.
  1094. CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
  1095. auto CU =
  1096. std::upper_bound(Units.begin(), Units.end(), Offset,
  1097. [](uint32_t LHS, const CompileUnit &RHS) {
  1098. return LHS < RHS.getOrigUnit().getNextUnitOffset();
  1099. });
  1100. return CU != Units.end() ? &*CU : nullptr;
  1101. }
  1102. /// \brief Resolve the DIE attribute reference that has been
  1103. /// extracted in \p RefValue. The resulting DIE migh be in another
  1104. /// CompileUnit which is stored into \p ReferencedCU.
  1105. /// \returns null if resolving fails for any reason.
  1106. const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
  1107. DWARFFormValue &RefValue, const DWARFUnit &Unit,
  1108. const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
  1109. assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
  1110. uint64_t RefOffset = *RefValue.getAsReference(&Unit);
  1111. if ((RefCU = getUnitForOffset(RefOffset)))
  1112. if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
  1113. return RefDie;
  1114. reportWarning("could not find referenced DIE", &Unit, &DIE);
  1115. return nullptr;
  1116. }
  1117. /// \brief Get the potential name and mangled name for the entity
  1118. /// described by \p Die and store them in \Info if they are not
  1119. /// already there.
  1120. /// \returns is a name was found.
  1121. bool DwarfLinker::getDIENames(const DWARFDebugInfoEntryMinimal &Die,
  1122. DWARFUnit &U, AttributesInfo &Info) {
  1123. // FIXME: a bit wastefull as the first getName might return the
  1124. // short name.
  1125. if (!Info.MangledName &&
  1126. (Info.MangledName = Die.getName(&U, DINameKind::LinkageName)))
  1127. Info.MangledNameOffset = StringPool.getStringOffset(Info.MangledName);
  1128. if (!Info.Name && (Info.Name = Die.getName(&U, DINameKind::ShortName)))
  1129. Info.NameOffset = StringPool.getStringOffset(Info.Name);
  1130. return Info.Name || Info.MangledName;
  1131. }
  1132. /// \brief Report a warning to the user, optionaly including
  1133. /// information about a specific \p DIE related to the warning.
  1134. void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
  1135. const DWARFDebugInfoEntryMinimal *DIE) const {
  1136. StringRef Context = "<debug map>";
  1137. if (CurrentDebugObject)
  1138. Context = CurrentDebugObject->getObjectFilename();
  1139. warn(Warning, Context);
  1140. if (!Options.Verbose || !DIE)
  1141. return;
  1142. errs() << " in DIE:\n";
  1143. DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
  1144. 6 /* Indent */);
  1145. }
  1146. bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
  1147. if (Options.NoOutput)
  1148. return true;
  1149. Streamer = llvm::make_unique<DwarfStreamer>();
  1150. return Streamer->init(TheTriple, OutputFilename);
  1151. }
  1152. /// \brief Recursive helper to gather the child->parent relationships in the
  1153. /// original compile unit.
  1154. static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
  1155. unsigned ParentIdx, CompileUnit &CU) {
  1156. unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
  1157. CU.getInfo(MyIdx).ParentIdx = ParentIdx;
  1158. if (DIE->hasChildren())
  1159. for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
  1160. Child = Child->getSibling())
  1161. gatherDIEParents(Child, MyIdx, CU);
  1162. }
  1163. static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
  1164. switch (Tag) {
  1165. default:
  1166. return false;
  1167. case dwarf::DW_TAG_subprogram:
  1168. case dwarf::DW_TAG_lexical_block:
  1169. case dwarf::DW_TAG_subroutine_type:
  1170. case dwarf::DW_TAG_structure_type:
  1171. case dwarf::DW_TAG_class_type:
  1172. case dwarf::DW_TAG_union_type:
  1173. return true;
  1174. }
  1175. llvm_unreachable("Invalid Tag");
  1176. }
  1177. void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
  1178. Units.reserve(Dwarf.getNumCompileUnits());
  1179. NextValidReloc = 0;
  1180. // Iterate over the debug map entries and put all the ones that are
  1181. // functions (because they have a size) into the Ranges map. This
  1182. // map is very similar to the FunctionRanges that are stored in each
  1183. // unit, with 2 notable differences:
  1184. // - obviously this one is global, while the other ones are per-unit.
  1185. // - this one contains not only the functions described in the DIE
  1186. // tree, but also the ones that are only in the debug map.
  1187. // The latter information is required to reproduce dsymutil's logic
  1188. // while linking line tables. The cases where this information
  1189. // matters look like bugs that need to be investigated, but for now
  1190. // we need to reproduce dsymutil's behavior.
  1191. // FIXME: Once we understood exactly if that information is needed,
  1192. // maybe totally remove this (or try to use it to do a real
  1193. // -gline-tables-only on Darwin.
  1194. for (const auto &Entry : Obj.symbols()) {
  1195. const auto &Mapping = Entry.getValue();
  1196. if (Mapping.Size)
  1197. Ranges[Mapping.ObjectAddress] = std::make_pair(
  1198. Mapping.ObjectAddress + Mapping.Size,
  1199. int64_t(Mapping.BinaryAddress) - Mapping.ObjectAddress);
  1200. }
  1201. }
  1202. void DwarfLinker::endDebugObject() {
  1203. Units.clear();
  1204. ValidRelocs.clear();
  1205. Ranges.clear();
  1206. for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
  1207. (*I)->~DIEBlock();
  1208. for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
  1209. (*I)->~DIELoc();
  1210. DIEBlocks.clear();
  1211. DIELocs.clear();
  1212. DIEAlloc.Reset();
  1213. }
  1214. /// \brief Iterate over the relocations of the given \p Section and
  1215. /// store the ones that correspond to debug map entries into the
  1216. /// ValidRelocs array.
  1217. void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
  1218. const object::MachOObjectFile &Obj,
  1219. const DebugMapObject &DMO) {
  1220. StringRef Contents;
  1221. Section.getContents(Contents);
  1222. DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
  1223. for (const object::RelocationRef &Reloc : Section.relocations()) {
  1224. object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
  1225. MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
  1226. unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
  1227. uint64_t Offset64 = Reloc.getOffset();
  1228. if ((RelocSize != 4 && RelocSize != 8)) {
  1229. reportWarning(" unsupported relocation in debug_info section.");
  1230. continue;
  1231. }
  1232. uint32_t Offset = Offset64;
  1233. // Mach-o uses REL relocations, the addend is at the relocation offset.
  1234. uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
  1235. auto Sym = Reloc.getSymbol();
  1236. if (Sym != Obj.symbol_end()) {
  1237. ErrorOr<StringRef> SymbolName = Sym->getName();
  1238. if (!SymbolName) {
  1239. reportWarning("error getting relocation symbol name.");
  1240. continue;
  1241. }
  1242. if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
  1243. ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
  1244. } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
  1245. // Do not store the addend. The addend was the address of the
  1246. // symbol in the object file, the address in the binary that is
  1247. // stored in the debug map doesn't need to be offseted.
  1248. ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
  1249. }
  1250. }
  1251. }
  1252. /// \brief Dispatch the valid relocation finding logic to the
  1253. /// appropriate handler depending on the object file format.
  1254. bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
  1255. const object::ObjectFile &Obj,
  1256. const DebugMapObject &DMO) {
  1257. // Dispatch to the right handler depending on the file type.
  1258. if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
  1259. findValidRelocsMachO(Section, *MachOObj, DMO);
  1260. else
  1261. reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
  1262. if (ValidRelocs.empty())
  1263. return false;
  1264. // Sort the relocations by offset. We will walk the DIEs linearly in
  1265. // the file, this allows us to just keep an index in the relocation
  1266. // array that we advance during our walk, rather than resorting to
  1267. // some associative container. See DwarfLinker::NextValidReloc.
  1268. std::sort(ValidRelocs.begin(), ValidRelocs.end());
  1269. return true;
  1270. }
  1271. /// \brief Look for relocations in the debug_info section that match
  1272. /// entries in the debug map. These relocations will drive the Dwarf
  1273. /// link by indicating which DIEs refer to symbols present in the
  1274. /// linked binary.
  1275. /// \returns wether there are any valid relocations in the debug info.
  1276. bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
  1277. const DebugMapObject &DMO) {
  1278. // Find the debug_info section.
  1279. for (const object::SectionRef &Section : Obj.sections()) {
  1280. StringRef SectionName;
  1281. Section.getName(SectionName);
  1282. SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
  1283. if (SectionName != "debug_info")
  1284. continue;
  1285. return findValidRelocs(Section, Obj, DMO);
  1286. }
  1287. return false;
  1288. }
  1289. /// \brief Checks that there is a relocation against an actual debug
  1290. /// map entry between \p StartOffset and \p NextOffset.
  1291. ///
  1292. /// This function must be called with offsets in strictly ascending
  1293. /// order because it never looks back at relocations it already 'went past'.
  1294. /// \returns true and sets Info.InDebugMap if it is the case.
  1295. bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
  1296. CompileUnit::DIEInfo &Info) {
  1297. assert(NextValidReloc == 0 ||
  1298. StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
  1299. if (NextValidReloc >= ValidRelocs.size())
  1300. return false;
  1301. uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
  1302. // We might need to skip some relocs that we didn't consider. For
  1303. // example the high_pc of a discarded DIE might contain a reloc that
  1304. // is in the list because it actually corresponds to the start of a
  1305. // function that is in the debug map.
  1306. while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
  1307. RelocOffset = ValidRelocs[++NextValidReloc].Offset;
  1308. if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
  1309. return false;
  1310. const auto &ValidReloc = ValidRelocs[NextValidReloc++];
  1311. const auto &Mapping = ValidReloc.Mapping->getValue();
  1312. if (Options.Verbose)
  1313. outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
  1314. << " " << format("\t%016" PRIx64 " => %016" PRIx64,
  1315. uint64_t(Mapping.ObjectAddress),
  1316. uint64_t(Mapping.BinaryAddress));
  1317. Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend -
  1318. Mapping.ObjectAddress;
  1319. Info.InDebugMap = true;
  1320. return true;
  1321. }
  1322. /// \brief Get the starting and ending (exclusive) offset for the
  1323. /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
  1324. /// supposed to point to the position of the first attribute described
  1325. /// by \p Abbrev.
  1326. /// \return [StartOffset, EndOffset) as a pair.
  1327. static std::pair<uint32_t, uint32_t>
  1328. getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
  1329. unsigned Offset, const DWARFUnit &Unit) {
  1330. DataExtractor Data = Unit.getDebugInfoExtractor();
  1331. for (unsigned i = 0; i < Idx; ++i)
  1332. DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
  1333. uint32_t End = Offset;
  1334. DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
  1335. return std::make_pair(Offset, End);
  1336. }
  1337. /// \brief Check if a variable describing DIE should be kept.
  1338. /// \returns updated TraversalFlags.
  1339. unsigned DwarfLinker::shouldKeepVariableDIE(
  1340. const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
  1341. CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
  1342. const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
  1343. // Global variables with constant value can always be kept.
  1344. if (!(Flags & TF_InFunctionScope) &&
  1345. Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
  1346. MyInfo.InDebugMap = true;
  1347. return Flags | TF_Keep;
  1348. }
  1349. uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
  1350. if (LocationIdx == -1U)
  1351. return Flags;
  1352. uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
  1353. const DWARFUnit &OrigUnit = Unit.getOrigUnit();
  1354. uint32_t LocationOffset, LocationEndOffset;
  1355. std::tie(LocationOffset, LocationEndOffset) =
  1356. getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
  1357. // See if there is a relocation to a valid debug map entry inside
  1358. // this variable's location. The order is important here. We want to
  1359. // always check in the variable has a valid relocation, so that the
  1360. // DIEInfo is filled. However, we don't want a static variable in a
  1361. // function to force us to keep the enclosing function.
  1362. if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
  1363. (Flags & TF_InFunctionScope))
  1364. return Flags;
  1365. if (Options.Verbose)
  1366. DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
  1367. return Flags | TF_Keep;
  1368. }
  1369. /// \brief Check if a function describing DIE should be kept.
  1370. /// \returns updated TraversalFlags.
  1371. unsigned DwarfLinker::shouldKeepSubprogramDIE(
  1372. const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
  1373. CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
  1374. const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
  1375. Flags |= TF_InFunctionScope;
  1376. uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
  1377. if (LowPcIdx == -1U)
  1378. return Flags;
  1379. uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
  1380. const DWARFUnit &OrigUnit = Unit.getOrigUnit();
  1381. uint32_t LowPcOffset, LowPcEndOffset;
  1382. std::tie(LowPcOffset, LowPcEndOffset) =
  1383. getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
  1384. uint64_t LowPc =
  1385. DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
  1386. assert(LowPc != -1ULL && "low_pc attribute is not an address.");
  1387. if (LowPc == -1ULL ||
  1388. !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
  1389. return Flags;
  1390. if (Options.Verbose)
  1391. DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
  1392. Flags |= TF_Keep;
  1393. DWARFFormValue HighPcValue;
  1394. if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) {
  1395. reportWarning("Function without high_pc. Range will be discarded.\n",
  1396. &OrigUnit, &DIE);
  1397. return Flags;
  1398. }
  1399. uint64_t HighPc;
  1400. if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) {
  1401. HighPc = *HighPcValue.getAsAddress(&OrigUnit);
  1402. } else {
  1403. assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant));
  1404. HighPc = LowPc + *HighPcValue.getAsUnsignedConstant();
  1405. }
  1406. // Replace the debug map range with a more accurate one.
  1407. Ranges[LowPc] = std::make_pair(HighPc, MyInfo.AddrAdjust);
  1408. Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust);
  1409. return Flags;
  1410. }
  1411. /// \brief Check if a DIE should be kept.
  1412. /// \returns updated TraversalFlags.
  1413. unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
  1414. CompileUnit &Unit,
  1415. CompileUnit::DIEInfo &MyInfo,
  1416. unsigned Flags) {
  1417. switch (DIE.getTag()) {
  1418. case dwarf::DW_TAG_constant:
  1419. case dwarf::DW_TAG_variable:
  1420. return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
  1421. case dwarf::DW_TAG_subprogram:
  1422. return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
  1423. case dwarf::DW_TAG_module:
  1424. case dwarf::DW_TAG_imported_module:
  1425. case dwarf::DW_TAG_imported_declaration:
  1426. case dwarf::DW_TAG_imported_unit:
  1427. // We always want to keep these.
  1428. return Flags | TF_Keep;
  1429. }
  1430. return Flags;
  1431. }
  1432. /// \brief Mark the passed DIE as well as all the ones it depends on
  1433. /// as kept.
  1434. ///
  1435. /// This function is called by lookForDIEsToKeep on DIEs that are
  1436. /// newly discovered to be needed in the link. It recursively calls
  1437. /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
  1438. /// TraversalFlags to inform it that it's not doing the primary DIE
  1439. /// tree walk.
  1440. void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
  1441. CompileUnit::DIEInfo &MyInfo,
  1442. const DebugMapObject &DMO,
  1443. CompileUnit &CU, unsigned Flags) {
  1444. const DWARFUnit &Unit = CU.getOrigUnit();
  1445. MyInfo.Keep = true;
  1446. // First mark all the parent chain as kept.
  1447. unsigned AncestorIdx = MyInfo.ParentIdx;
  1448. while (!CU.getInfo(AncestorIdx).Keep) {
  1449. lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
  1450. TF_ParentWalk | TF_Keep | TF_DependencyWalk);
  1451. AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
  1452. }
  1453. // Then we need to mark all the DIEs referenced by this DIE's
  1454. // attributes as kept.
  1455. DataExtractor Data = Unit.getDebugInfoExtractor();
  1456. const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
  1457. uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
  1458. // Mark all DIEs referenced through atttributes as kept.
  1459. for (const auto &AttrSpec : Abbrev->attributes()) {
  1460. DWARFFormValue Val(AttrSpec.Form);
  1461. if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
  1462. DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
  1463. continue;
  1464. }
  1465. Val.extractValue(Data, &Offset, &Unit);
  1466. CompileUnit *ReferencedCU;
  1467. if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
  1468. lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
  1469. TF_Keep | TF_DependencyWalk);
  1470. }
  1471. }
  1472. /// \brief Recursively walk the \p DIE tree and look for DIEs to
  1473. /// keep. Store that information in \p CU's DIEInfo.
  1474. ///
  1475. /// This function is the entry point of the DIE selection
  1476. /// algorithm. It is expected to walk the DIE tree in file order and
  1477. /// (though the mediation of its helper) call hasValidRelocation() on
  1478. /// each DIE that might be a 'root DIE' (See DwarfLinker class
  1479. /// comment).
  1480. /// While walking the dependencies of root DIEs, this function is
  1481. /// also called, but during these dependency walks the file order is
  1482. /// not respected. The TF_DependencyWalk flag tells us which kind of
  1483. /// traversal we are currently doing.
  1484. void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
  1485. const DebugMapObject &DMO, CompileUnit &CU,
  1486. unsigned Flags) {
  1487. unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
  1488. CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
  1489. bool AlreadyKept = MyInfo.Keep;
  1490. // If the Keep flag is set, we are marking a required DIE's
  1491. // dependencies. If our target is already marked as kept, we're all
  1492. // set.
  1493. if ((Flags & TF_DependencyWalk) && AlreadyKept)
  1494. return;
  1495. // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
  1496. // because it would screw up the relocation finding logic.
  1497. if (!(Flags & TF_DependencyWalk))
  1498. Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
  1499. // If it is a newly kept DIE mark it as well as all its dependencies as kept.
  1500. if (!AlreadyKept && (Flags & TF_Keep))
  1501. keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
  1502. // The TF_ParentWalk flag tells us that we are currently walking up
  1503. // the parent chain of a required DIE, and we don't want to mark all
  1504. // the children of the parents as kept (consider for example a
  1505. // DW_TAG_namespace node in the parent chain). There are however a
  1506. // set of DIE types for which we want to ignore that directive and still
  1507. // walk their children.
  1508. if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
  1509. Flags &= ~TF_ParentWalk;
  1510. if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
  1511. return;
  1512. for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
  1513. Child = Child->getSibling())
  1514. lookForDIEsToKeep(*Child, DMO, CU, Flags);
  1515. }
  1516. /// \brief Assign an abbreviation numer to \p Abbrev.
  1517. ///
  1518. /// Our DIEs get freed after every DebugMapObject has been processed,
  1519. /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
  1520. /// the instances hold by the DIEs. When we encounter an abbreviation
  1521. /// that we don't know, we create a permanent copy of it.
  1522. void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
  1523. // Check the set for priors.
  1524. FoldingSetNodeID ID;
  1525. Abbrev.Profile(ID);
  1526. void *InsertToken;
  1527. DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
  1528. // If it's newly added.
  1529. if (InSet) {
  1530. // Assign existing abbreviation number.
  1531. Abbrev.setNumber(InSet->getNumber());
  1532. } else {
  1533. // Add to abbreviation list.
  1534. Abbreviations.push_back(
  1535. new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren()));
  1536. for (const auto &Attr : Abbrev.getData())
  1537. Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
  1538. AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken);
  1539. // Assign the unique abbreviation number.
  1540. Abbrev.setNumber(Abbreviations.size());
  1541. Abbreviations.back()->setNumber(Abbreviations.size());
  1542. }
  1543. }
  1544. /// \brief Clone a string attribute described by \p AttrSpec and add
  1545. /// it to \p Die.
  1546. /// \returns the size of the new attribute.
  1547. unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
  1548. const DWARFFormValue &Val,
  1549. const DWARFUnit &U) {
  1550. // Switch everything to out of line strings.
  1551. const char *String = *Val.getAsCString(&U);
  1552. unsigned Offset = StringPool.getStringOffset(String);
  1553. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
  1554. DIEInteger(Offset));
  1555. return 4;
  1556. }
  1557. /// \brief Clone an attribute referencing another DIE and add
  1558. /// it to \p Die.
  1559. /// \returns the size of the new attribute.
  1560. unsigned DwarfLinker::cloneDieReferenceAttribute(
  1561. DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
  1562. AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val,
  1563. CompileUnit &Unit) {
  1564. uint32_t Ref = *Val.getAsReference(&Unit.getOrigUnit());
  1565. DIE *NewRefDie = nullptr;
  1566. CompileUnit *RefUnit = nullptr;
  1567. const DWARFDebugInfoEntryMinimal *RefDie = nullptr;
  1568. if (!(RefUnit = getUnitForOffset(Ref)) ||
  1569. !(RefDie = RefUnit->getOrigUnit().getDIEForOffset(Ref))) {
  1570. const char *AttributeString = dwarf::AttributeString(AttrSpec.Attr);
  1571. if (!AttributeString)
  1572. AttributeString = "DW_AT_???";
  1573. reportWarning(Twine("Missing DIE for ref in attribute ") + AttributeString +
  1574. ". Dropping.",
  1575. &Unit.getOrigUnit(), &InputDIE);
  1576. return 0;
  1577. }
  1578. unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
  1579. CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
  1580. if (!RefInfo.Clone) {
  1581. assert(Ref > InputDIE.getOffset());
  1582. // We haven't cloned this DIE yet. Just create an empty one and
  1583. // store it. It'll get really cloned when we process it.
  1584. RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie->getTag()));
  1585. }
  1586. NewRefDie = RefInfo.Clone;
  1587. if (AttrSpec.Form == dwarf::DW_FORM_ref_addr) {
  1588. // We cannot currently rely on a DIEEntry to emit ref_addr
  1589. // references, because the implementation calls back to DwarfDebug
  1590. // to find the unit offset. (We don't have a DwarfDebug)
  1591. // FIXME: we should be able to design DIEEntry reliance on
  1592. // DwarfDebug away.
  1593. uint64_t Attr;
  1594. if (Ref < InputDIE.getOffset()) {
  1595. // We must have already cloned that DIE.
  1596. uint32_t NewRefOffset =
  1597. RefUnit->getStartOffset() + NewRefDie->getOffset();
  1598. Attr = NewRefOffset;
  1599. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1600. dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
  1601. } else {
  1602. // A forward reference. Note and fixup later.
  1603. Attr = 0xBADDEF;
  1604. Unit.noteForwardReference(
  1605. NewRefDie, RefUnit,
  1606. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1607. dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
  1608. }
  1609. return AttrSize;
  1610. }
  1611. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1612. dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
  1613. return AttrSize;
  1614. }
  1615. /// \brief Clone an attribute of block form (locations, constants) and add
  1616. /// it to \p Die.
  1617. /// \returns the size of the new attribute.
  1618. unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
  1619. const DWARFFormValue &Val,
  1620. unsigned AttrSize) {
  1621. DIE *Attr;
  1622. DIEValue Value;
  1623. DIELoc *Loc = nullptr;
  1624. DIEBlock *Block = nullptr;
  1625. // Just copy the block data over.
  1626. if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
  1627. Loc = new (DIEAlloc) DIELoc;
  1628. DIELocs.push_back(Loc);
  1629. } else {
  1630. Block = new (DIEAlloc) DIEBlock;
  1631. DIEBlocks.push_back(Block);
  1632. }
  1633. Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block);
  1634. if (Loc)
  1635. Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
  1636. dwarf::Form(AttrSpec.Form), Loc);
  1637. else
  1638. Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
  1639. dwarf::Form(AttrSpec.Form), Block);
  1640. ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
  1641. for (auto Byte : Bytes)
  1642. Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
  1643. dwarf::DW_FORM_data1, DIEInteger(Byte));
  1644. // FIXME: If DIEBlock and DIELoc just reuses the Size field of
  1645. // the DIE class, this if could be replaced by
  1646. // Attr->setSize(Bytes.size()).
  1647. if (Streamer) {
  1648. if (Loc)
  1649. Loc->ComputeSize(&Streamer->getAsmPrinter());
  1650. else
  1651. Block->ComputeSize(&Streamer->getAsmPrinter());
  1652. }
  1653. Die.addValue(DIEAlloc, Value);
  1654. return AttrSize;
  1655. }
  1656. /// \brief Clone an address attribute and add it to \p Die.
  1657. /// \returns the size of the new attribute.
  1658. unsigned DwarfLinker::cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
  1659. const DWARFFormValue &Val,
  1660. const CompileUnit &Unit,
  1661. AttributesInfo &Info) {
  1662. uint64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit());
  1663. if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
  1664. if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
  1665. Die.getTag() == dwarf::DW_TAG_lexical_block)
  1666. Addr += Info.PCOffset;
  1667. else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1668. Addr = Unit.getLowPc();
  1669. if (Addr == UINT64_MAX)
  1670. return 0;
  1671. }
  1672. Info.HasLowPc = true;
  1673. } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
  1674. if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1675. if (uint64_t HighPc = Unit.getHighPc())
  1676. Addr = HighPc;
  1677. else
  1678. return 0;
  1679. } else
  1680. // If we have a high_pc recorded for the input DIE, use
  1681. // it. Otherwise (when no relocations where applied) just use the
  1682. // one we just decoded.
  1683. Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
  1684. }
  1685. Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
  1686. static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
  1687. return Unit.getOrigUnit().getAddressByteSize();
  1688. }
  1689. /// \brief Clone a scalar attribute and add it to \p Die.
  1690. /// \returns the size of the new attribute.
  1691. unsigned DwarfLinker::cloneScalarAttribute(
  1692. DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
  1693. AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
  1694. AttributesInfo &Info) {
  1695. uint64_t Value;
  1696. if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
  1697. Die.getTag() == dwarf::DW_TAG_compile_unit) {
  1698. if (Unit.getLowPc() == -1ULL)
  1699. return 0;
  1700. // Dwarf >= 4 high_pc is an size, not an address.
  1701. Value = Unit.getHighPc() - Unit.getLowPc();
  1702. } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
  1703. Value = *Val.getAsSectionOffset();
  1704. else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
  1705. Value = *Val.getAsSignedConstant();
  1706. else if (auto OptionalValue = Val.getAsUnsignedConstant())
  1707. Value = *OptionalValue;
  1708. else {
  1709. reportWarning("Unsupported scalar attribute form. Dropping attribute.",
  1710. &Unit.getOrigUnit(), &InputDIE);
  1711. return 0;
  1712. }
  1713. PatchLocation Patch =
  1714. Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
  1715. dwarf::Form(AttrSpec.Form), DIEInteger(Value));
  1716. if (AttrSpec.Attr == dwarf::DW_AT_ranges)
  1717. Unit.noteRangeAttribute(Die, Patch);
  1718. // A more generic way to check for location attributes would be
  1719. // nice, but it's very unlikely that any other attribute needs a
  1720. // location list.
  1721. else if (AttrSpec.Attr == dwarf::DW_AT_location ||
  1722. AttrSpec.Attr == dwarf::DW_AT_frame_base)
  1723. Unit.noteLocationAttribute(Patch, Info.PCOffset);
  1724. else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
  1725. Info.IsDeclaration = true;
  1726. return AttrSize;
  1727. }
  1728. /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with
  1729. /// value \p Val, and add it to \p Die.
  1730. /// \returns the size of the cloned attribute.
  1731. unsigned DwarfLinker::cloneAttribute(DIE &Die,
  1732. const DWARFDebugInfoEntryMinimal &InputDIE,
  1733. CompileUnit &Unit,
  1734. const DWARFFormValue &Val,
  1735. const AttributeSpec AttrSpec,
  1736. unsigned AttrSize, AttributesInfo &Info) {
  1737. const DWARFUnit &U = Unit.getOrigUnit();
  1738. switch (AttrSpec.Form) {
  1739. case dwarf::DW_FORM_strp:
  1740. case dwarf::DW_FORM_string:
  1741. return cloneStringAttribute(Die, AttrSpec, Val, U);
  1742. case dwarf::DW_FORM_ref_addr:
  1743. case dwarf::DW_FORM_ref1:
  1744. case dwarf::DW_FORM_ref2:
  1745. case dwarf::DW_FORM_ref4:
  1746. case dwarf::DW_FORM_ref8:
  1747. return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
  1748. Unit);
  1749. case dwarf::DW_FORM_block:
  1750. case dwarf::DW_FORM_block1:
  1751. case dwarf::DW_FORM_block2:
  1752. case dwarf::DW_FORM_block4:
  1753. case dwarf::DW_FORM_exprloc:
  1754. return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
  1755. case dwarf::DW_FORM_addr:
  1756. return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
  1757. case dwarf::DW_FORM_data1:
  1758. case dwarf::DW_FORM_data2:
  1759. case dwarf::DW_FORM_data4:
  1760. case dwarf::DW_FORM_data8:
  1761. case dwarf::DW_FORM_udata:
  1762. case dwarf::DW_FORM_sdata:
  1763. case dwarf::DW_FORM_sec_offset:
  1764. case dwarf::DW_FORM_flag:
  1765. case dwarf::DW_FORM_flag_present:
  1766. return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
  1767. Info);
  1768. default:
  1769. reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U,
  1770. &InputDIE);
  1771. }
  1772. return 0;
  1773. }
  1774. /// \brief Apply the valid relocations found by findValidRelocs() to
  1775. /// the buffer \p Data, taking into account that Data is at \p BaseOffset
  1776. /// in the debug_info section.
  1777. ///
  1778. /// Like for findValidRelocs(), this function must be called with
  1779. /// monotonic \p BaseOffset values.
  1780. ///
  1781. /// \returns wether any reloc has been applied.
  1782. bool DwarfLinker::applyValidRelocs(MutableArrayRef<char> Data,
  1783. uint32_t BaseOffset, bool isLittleEndian) {
  1784. assert((NextValidReloc == 0 ||
  1785. BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
  1786. "BaseOffset should only be increasing.");
  1787. if (NextValidReloc >= ValidRelocs.size())
  1788. return false;
  1789. // Skip relocs that haven't been applied.
  1790. while (NextValidReloc < ValidRelocs.size() &&
  1791. ValidRelocs[NextValidReloc].Offset < BaseOffset)
  1792. ++NextValidReloc;
  1793. bool Applied = false;
  1794. uint64_t EndOffset = BaseOffset + Data.size();
  1795. while (NextValidReloc < ValidRelocs.size() &&
  1796. ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
  1797. ValidRelocs[NextValidReloc].Offset < EndOffset) {
  1798. const auto &ValidReloc = ValidRelocs[NextValidReloc++];
  1799. assert(ValidReloc.Offset - BaseOffset < Data.size());
  1800. assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
  1801. char Buf[8];
  1802. uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
  1803. Value += ValidReloc.Addend;
  1804. for (unsigned i = 0; i != ValidReloc.Size; ++i) {
  1805. unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
  1806. Buf[i] = uint8_t(Value >> (Index * 8));
  1807. }
  1808. assert(ValidReloc.Size <= sizeof(Buf));
  1809. memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
  1810. Applied = true;
  1811. }
  1812. return Applied;
  1813. }
  1814. static bool isTypeTag(uint16_t Tag) {
  1815. switch (Tag) {
  1816. case dwarf::DW_TAG_array_type:
  1817. case dwarf::DW_TAG_class_type:
  1818. case dwarf::DW_TAG_enumeration_type:
  1819. case dwarf::DW_TAG_pointer_type:
  1820. case dwarf::DW_TAG_reference_type:
  1821. case dwarf::DW_TAG_string_type:
  1822. case dwarf::DW_TAG_structure_type:
  1823. case dwarf::DW_TAG_subroutine_type:
  1824. case dwarf::DW_TAG_typedef:
  1825. case dwarf::DW_TAG_union_type:
  1826. case dwarf::DW_TAG_ptr_to_member_type:
  1827. case dwarf::DW_TAG_set_type:
  1828. case dwarf::DW_TAG_subrange_type:
  1829. case dwarf::DW_TAG_base_type:
  1830. case dwarf::DW_TAG_const_type:
  1831. case dwarf::DW_TAG_constant:
  1832. case dwarf::DW_TAG_file_type:
  1833. case dwarf::DW_TAG_namelist:
  1834. case dwarf::DW_TAG_packed_type:
  1835. case dwarf::DW_TAG_volatile_type:
  1836. case dwarf::DW_TAG_restrict_type:
  1837. case dwarf::DW_TAG_interface_type:
  1838. case dwarf::DW_TAG_unspecified_type:
  1839. case dwarf::DW_TAG_shared_type:
  1840. return true;
  1841. default:
  1842. break;
  1843. }
  1844. return false;
  1845. }
  1846. /// \brief Recursively clone \p InputDIE's subtrees that have been
  1847. /// selected to appear in the linked output.
  1848. ///
  1849. /// \param OutOffset is the Offset where the newly created DIE will
  1850. /// lie in the linked compile unit.
  1851. ///
  1852. /// \returns the cloned DIE object or null if nothing was selected.
  1853. DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE,
  1854. CompileUnit &Unit, int64_t PCOffset,
  1855. uint32_t OutOffset) {
  1856. DWARFUnit &U = Unit.getOrigUnit();
  1857. unsigned Idx = U.getDIEIndex(&InputDIE);
  1858. CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
  1859. // Should the DIE appear in the output?
  1860. if (!Unit.getInfo(Idx).Keep)
  1861. return nullptr;
  1862. uint32_t Offset = InputDIE.getOffset();
  1863. // The DIE might have been already created by a forward reference
  1864. // (see cloneDieReferenceAttribute()).
  1865. DIE *Die = Info.Clone;
  1866. if (!Die)
  1867. Die = Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
  1868. assert(Die->getTag() == InputDIE.getTag());
  1869. Die->setOffset(OutOffset);
  1870. // Extract and clone every attribute.
  1871. DataExtractor Data = U.getDebugInfoExtractor();
  1872. uint32_t NextOffset = U.getDIEAtIndex(Idx + 1)->getOffset();
  1873. AttributesInfo AttrInfo;
  1874. // We could copy the data only if we need to aply a relocation to
  1875. // it. After testing, it seems there is no performance downside to
  1876. // doing the copy unconditionally, and it makes the code simpler.
  1877. SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
  1878. Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
  1879. // Modify the copy with relocated addresses.
  1880. if (applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
  1881. // If we applied relocations, we store the value of high_pc that was
  1882. // potentially stored in the input DIE. If high_pc is an address
  1883. // (Dwarf version == 2), then it might have been relocated to a
  1884. // totally unrelated value (because the end address in the object
  1885. // file might be start address of another function which got moved
  1886. // independantly by the linker). The computation of the actual
  1887. // high_pc value is done in cloneAddressAttribute().
  1888. AttrInfo.OrigHighPc =
  1889. InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0);
  1890. }
  1891. // Reset the Offset to 0 as we will be working on the local copy of
  1892. // the data.
  1893. Offset = 0;
  1894. const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
  1895. Offset += getULEB128Size(Abbrev->getCode());
  1896. // We are entering a subprogram. Get and propagate the PCOffset.
  1897. if (Die->getTag() == dwarf::DW_TAG_subprogram)
  1898. PCOffset = Info.AddrAdjust;
  1899. AttrInfo.PCOffset = PCOffset;
  1900. for (const auto &AttrSpec : Abbrev->attributes()) {
  1901. DWARFFormValue Val(AttrSpec.Form);
  1902. uint32_t AttrSize = Offset;
  1903. Val.extractValue(Data, &Offset, &U);
  1904. AttrSize = Offset - AttrSize;
  1905. OutOffset +=
  1906. cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
  1907. }
  1908. // Look for accelerator entries.
  1909. uint16_t Tag = InputDIE.getTag();
  1910. // FIXME: This is slightly wrong. An inline_subroutine without a
  1911. // low_pc, but with AT_ranges might be interesting to get into the
  1912. // accelerator tables too. For now stick with dsymutil's behavior.
  1913. if ((Info.InDebugMap || AttrInfo.HasLowPc) &&
  1914. Tag != dwarf::DW_TAG_compile_unit &&
  1915. getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
  1916. if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
  1917. Unit.addNameAccelerator(Die, AttrInfo.MangledName,
  1918. AttrInfo.MangledNameOffset,
  1919. Tag == dwarf::DW_TAG_inlined_subroutine);
  1920. if (AttrInfo.Name)
  1921. Unit.addNameAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset,
  1922. Tag == dwarf::DW_TAG_inlined_subroutine);
  1923. } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
  1924. getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
  1925. Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset);
  1926. }
  1927. DIEAbbrev NewAbbrev = Die->generateAbbrev();
  1928. // If a scope DIE is kept, we must have kept at least one child. If
  1929. // it's not the case, we'll just be emitting one wasteful end of
  1930. // children marker, but things won't break.
  1931. if (InputDIE.hasChildren())
  1932. NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
  1933. // Assign a permanent abbrev number
  1934. AssignAbbrev(NewAbbrev);
  1935. Die->setAbbrevNumber(NewAbbrev.getNumber());
  1936. // Add the size of the abbreviation number to the output offset.
  1937. OutOffset += getULEB128Size(Die->getAbbrevNumber());
  1938. if (!Abbrev->hasChildren()) {
  1939. // Update our size.
  1940. Die->setSize(OutOffset - Die->getOffset());
  1941. return Die;
  1942. }
  1943. // Recursively clone children.
  1944. for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
  1945. Child = Child->getSibling()) {
  1946. if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset)) {
  1947. Die->addChild(Clone);
  1948. OutOffset = Clone->getOffset() + Clone->getSize();
  1949. }
  1950. }
  1951. // Account for the end of children marker.
  1952. OutOffset += sizeof(int8_t);
  1953. // Update our size.
  1954. Die->setSize(OutOffset - Die->getOffset());
  1955. return Die;
  1956. }
  1957. /// \brief Patch the input object file relevant debug_ranges entries
  1958. /// and emit them in the output file. Update the relevant attributes
  1959. /// to point at the new entries.
  1960. void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
  1961. DWARFContext &OrigDwarf) const {
  1962. DWARFDebugRangeList RangeList;
  1963. const auto &FunctionRanges = Unit.getFunctionRanges();
  1964. unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
  1965. DataExtractor RangeExtractor(OrigDwarf.getRangeSection(),
  1966. OrigDwarf.isLittleEndian(), AddressSize);
  1967. auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
  1968. DWARFUnit &OrigUnit = Unit.getOrigUnit();
  1969. const auto *OrigUnitDie = OrigUnit.getUnitDIE(false);
  1970. uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
  1971. &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
  1972. // Ranges addresses are based on the unit's low_pc. Compute the
  1973. // offset we need to apply to adapt to the the new unit's low_pc.
  1974. int64_t UnitPcOffset = 0;
  1975. if (OrigLowPc != -1ULL)
  1976. UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
  1977. for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
  1978. uint32_t Offset = RangeAttribute.get();
  1979. RangeAttribute.set(Streamer->getRangesSectionSize());
  1980. RangeList.extract(RangeExtractor, &Offset);
  1981. const auto &Entries = RangeList.getEntries();
  1982. const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
  1983. if (CurrRange == InvalidRange || First.StartAddress < CurrRange.start() ||
  1984. First.StartAddress >= CurrRange.stop()) {
  1985. CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
  1986. if (CurrRange == InvalidRange ||
  1987. CurrRange.start() > First.StartAddress + OrigLowPc) {
  1988. reportWarning("no mapping for range.");
  1989. continue;
  1990. }
  1991. }
  1992. Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
  1993. AddressSize);
  1994. }
  1995. }
  1996. /// \brief Generate the debug_aranges entries for \p Unit and if the
  1997. /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
  1998. /// contribution for this attribute.
  1999. /// FIXME: this could actually be done right in patchRangesForUnit,
  2000. /// but for the sake of initial bit-for-bit compatibility with legacy
  2001. /// dsymutil, we have to do it in a delayed pass.
  2002. void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
  2003. auto Attr = Unit.getUnitRangesAttribute();
  2004. if (Attr)
  2005. Attr->set(Streamer->getRangesSectionSize());
  2006. Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
  2007. }
  2008. /// \brief Insert the new line info sequence \p Seq into the current
  2009. /// set of already linked line info \p Rows.
  2010. static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
  2011. std::vector<DWARFDebugLine::Row> &Rows) {
  2012. if (Seq.empty())
  2013. return;
  2014. if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
  2015. Rows.insert(Rows.end(), Seq.begin(), Seq.end());
  2016. Seq.clear();
  2017. return;
  2018. }
  2019. auto InsertPoint = std::lower_bound(
  2020. Rows.begin(), Rows.end(), Seq.front(),
  2021. [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
  2022. return LHS.Address < RHS.Address;
  2023. });
  2024. // FIXME: this only removes the unneeded end_sequence if the
  2025. // sequences have been inserted in order. using a global sort like
  2026. // described in patchLineTableForUnit() and delaying the end_sequene
  2027. // elimination to emitLineTableForUnit() we can get rid of all of them.
  2028. if (InsertPoint != Rows.end() &&
  2029. InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
  2030. *InsertPoint = Seq.front();
  2031. Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
  2032. } else {
  2033. Rows.insert(InsertPoint, Seq.begin(), Seq.end());
  2034. }
  2035. Seq.clear();
  2036. }
  2037. static void patchStmtList(DIE &Die, DIEInteger Offset) {
  2038. for (auto &V : Die.values())
  2039. if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
  2040. V = DIEValue(V.getAttribute(), V.getForm(), Offset);
  2041. return;
  2042. }
  2043. llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
  2044. }
  2045. /// \brief Extract the line table for \p Unit from \p OrigDwarf, and
  2046. /// recreate a relocated version of these for the address ranges that
  2047. /// are present in the binary.
  2048. void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
  2049. DWARFContext &OrigDwarf) {
  2050. const DWARFDebugInfoEntryMinimal *CUDie = Unit.getOrigUnit().getUnitDIE();
  2051. uint64_t StmtList = CUDie->getAttributeValueAsSectionOffset(
  2052. &Unit.getOrigUnit(), dwarf::DW_AT_stmt_list, -1ULL);
  2053. if (StmtList == -1ULL)
  2054. return;
  2055. // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
  2056. if (auto *OutputDIE = Unit.getOutputUnitDIE())
  2057. patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
  2058. // Parse the original line info for the unit.
  2059. DWARFDebugLine::LineTable LineTable;
  2060. uint32_t StmtOffset = StmtList;
  2061. StringRef LineData = OrigDwarf.getLineSection().Data;
  2062. DataExtractor LineExtractor(LineData, OrigDwarf.isLittleEndian(),
  2063. Unit.getOrigUnit().getAddressByteSize());
  2064. LineTable.parse(LineExtractor, &OrigDwarf.getLineSection().Relocs,
  2065. &StmtOffset);
  2066. // This vector is the output line table.
  2067. std::vector<DWARFDebugLine::Row> NewRows;
  2068. NewRows.reserve(LineTable.Rows.size());
  2069. // Current sequence of rows being extracted, before being inserted
  2070. // in NewRows.
  2071. std::vector<DWARFDebugLine::Row> Seq;
  2072. const auto &FunctionRanges = Unit.getFunctionRanges();
  2073. auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
  2074. // FIXME: This logic is meant to generate exactly the same output as
  2075. // Darwin's classic dsynutil. There is a nicer way to implement this
  2076. // by simply putting all the relocated line info in NewRows and simply
  2077. // sorting NewRows before passing it to emitLineTableForUnit. This
  2078. // should be correct as sequences for a function should stay
  2079. // together in the sorted output. There are a few corner cases that
  2080. // look suspicious though, and that required to implement the logic
  2081. // this way. Revisit that once initial validation is finished.
  2082. // Iterate over the object file line info and extract the sequences
  2083. // that correspond to linked functions.
  2084. for (auto &Row : LineTable.Rows) {
  2085. // Check wether we stepped out of the range. The range is
  2086. // half-open, but consider accept the end address of the range if
  2087. // it is marked as end_sequence in the input (because in that
  2088. // case, the relocation offset is accurate and that entry won't
  2089. // serve as the start of another function).
  2090. if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
  2091. Row.Address > CurrRange.stop() ||
  2092. (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
  2093. // We just stepped out of a known range. Insert a end_sequence
  2094. // corresponding to the end of the range.
  2095. uint64_t StopAddress = CurrRange != InvalidRange
  2096. ? CurrRange.stop() + CurrRange.value()
  2097. : -1ULL;
  2098. CurrRange = FunctionRanges.find(Row.Address);
  2099. bool CurrRangeValid =
  2100. CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
  2101. if (!CurrRangeValid) {
  2102. CurrRange = InvalidRange;
  2103. if (StopAddress != -1ULL) {
  2104. // Try harder by looking in the DebugMapObject function
  2105. // ranges map. There are corner cases where this finds a
  2106. // valid entry. It's unclear if this is right or wrong, but
  2107. // for now do as dsymutil.
  2108. // FIXME: Understand exactly what cases this addresses and
  2109. // potentially remove it along with the Ranges map.
  2110. auto Range = Ranges.lower_bound(Row.Address);
  2111. if (Range != Ranges.begin() && Range != Ranges.end())
  2112. --Range;
  2113. if (Range != Ranges.end() && Range->first <= Row.Address &&
  2114. Range->second.first >= Row.Address) {
  2115. StopAddress = Row.Address + Range->second.second;
  2116. }
  2117. }
  2118. }
  2119. if (StopAddress != -1ULL && !Seq.empty()) {
  2120. // Insert end sequence row with the computed end address, but
  2121. // the same line as the previous one.
  2122. Seq.emplace_back(Seq.back());
  2123. Seq.back().Address = StopAddress;
  2124. Seq.back().EndSequence = 1;
  2125. Seq.back().PrologueEnd = 0;
  2126. Seq.back().BasicBlock = 0;
  2127. Seq.back().EpilogueBegin = 0;
  2128. insertLineSequence(Seq, NewRows);
  2129. }
  2130. if (!CurrRangeValid)
  2131. continue;
  2132. }
  2133. // Ignore empty sequences.
  2134. if (Row.EndSequence && Seq.empty())
  2135. continue;
  2136. // Relocate row address and add it to the current sequence.
  2137. Row.Address += CurrRange.value();
  2138. Seq.emplace_back(Row);
  2139. if (Row.EndSequence)
  2140. insertLineSequence(Seq, NewRows);
  2141. }
  2142. // Finished extracting, now emit the line tables.
  2143. uint32_t PrologueEnd = StmtList + 10 + LineTable.Prologue.PrologueLength;
  2144. // FIXME: LLVM hardcodes it's prologue values. We just copy the
  2145. // prologue over and that works because we act as both producer and
  2146. // consumer. It would be nicer to have a real configurable line
  2147. // table emitter.
  2148. if (LineTable.Prologue.Version != 2 ||
  2149. LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
  2150. LineTable.Prologue.LineBase != -5 || LineTable.Prologue.LineRange != 14 ||
  2151. LineTable.Prologue.OpcodeBase != 13)
  2152. reportWarning("line table paramters mismatch. Cannot emit.");
  2153. else
  2154. Streamer->emitLineTableForUnit(LineData.slice(StmtList + 4, PrologueEnd),
  2155. LineTable.Prologue.MinInstLength, NewRows,
  2156. Unit.getOrigUnit().getAddressByteSize());
  2157. }
  2158. void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
  2159. Streamer->emitPubNamesForUnit(Unit);
  2160. Streamer->emitPubTypesForUnit(Unit);
  2161. }
  2162. /// \brief Read the frame info stored in the object, and emit the
  2163. /// patched frame descriptions for the linked binary.
  2164. ///
  2165. /// This is actually pretty easy as the data of the CIEs and FDEs can
  2166. /// be considered as black boxes and moved as is. The only thing to do
  2167. /// is to patch the addresses in the headers.
  2168. void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
  2169. DWARFContext &OrigDwarf,
  2170. unsigned AddrSize) {
  2171. StringRef FrameData = OrigDwarf.getDebugFrameSection();
  2172. if (FrameData.empty())
  2173. return;
  2174. DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
  2175. uint32_t InputOffset = 0;
  2176. // Store the data of the CIEs defined in this object, keyed by their
  2177. // offsets.
  2178. DenseMap<uint32_t, StringRef> LocalCIES;
  2179. while (Data.isValidOffset(InputOffset)) {
  2180. uint32_t EntryOffset = InputOffset;
  2181. uint32_t InitialLength = Data.getU32(&InputOffset);
  2182. if (InitialLength == 0xFFFFFFFF)
  2183. return reportWarning("Dwarf64 bits no supported");
  2184. uint32_t CIEId = Data.getU32(&InputOffset);
  2185. if (CIEId == 0xFFFFFFFF) {
  2186. // This is a CIE, store it.
  2187. StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
  2188. LocalCIES[EntryOffset] = CIEData;
  2189. // The -4 is to account for the CIEId we just read.
  2190. InputOffset += InitialLength - 4;
  2191. continue;
  2192. }
  2193. uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
  2194. // Some compilers seem to emit frame info that doesn't start at
  2195. // the function entry point, thus we can't just lookup the address
  2196. // in the debug map. Use the linker's range map to see if the FDE
  2197. // describes something that we can relocate.
  2198. auto Range = Ranges.upper_bound(Loc);
  2199. if (Range != Ranges.begin())
  2200. --Range;
  2201. if (Range == Ranges.end() || Range->first > Loc ||
  2202. Range->second.first <= Loc) {
  2203. // The +4 is to account for the size of the InitialLength field itself.
  2204. InputOffset = EntryOffset + InitialLength + 4;
  2205. continue;
  2206. }
  2207. // This is an FDE, and we have a mapping.
  2208. // Have we already emitted a corresponding CIE?
  2209. StringRef CIEData = LocalCIES[CIEId];
  2210. if (CIEData.empty())
  2211. return reportWarning("Inconsistent debug_frame content. Dropping.");
  2212. // Look if we already emitted a CIE that corresponds to the
  2213. // referenced one (the CIE data is the key of that lookup).
  2214. auto IteratorInserted = EmittedCIEs.insert(
  2215. std::make_pair(CIEData, Streamer->getFrameSectionSize()));
  2216. // If there is no CIE yet for this ID, emit it.
  2217. if (IteratorInserted.second ||
  2218. // FIXME: dsymutil-classic only caches the last used CIE for
  2219. // reuse. Mimic that behavior for now. Just removing that
  2220. // second half of the condition and the LastCIEOffset variable
  2221. // makes the code DTRT.
  2222. LastCIEOffset != IteratorInserted.first->getValue()) {
  2223. LastCIEOffset = Streamer->getFrameSectionSize();
  2224. IteratorInserted.first->getValue() = LastCIEOffset;
  2225. Streamer->emitCIE(CIEData);
  2226. }
  2227. // Emit the FDE with updated address and CIE pointer.
  2228. // (4 + AddrSize) is the size of the CIEId + initial_location
  2229. // fields that will get reconstructed by emitFDE().
  2230. unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
  2231. Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
  2232. Loc + Range->second.second,
  2233. FrameData.substr(InputOffset, FDERemainingBytes));
  2234. InputOffset += FDERemainingBytes;
  2235. }
  2236. }
  2237. bool DwarfLinker::link(const DebugMap &Map) {
  2238. if (Map.begin() == Map.end()) {
  2239. errs() << "Empty debug map.\n";
  2240. return false;
  2241. }
  2242. if (!createStreamer(Map.getTriple(), OutputFilename))
  2243. return false;
  2244. // Size of the DIEs (and headers) generated for the linked output.
  2245. uint64_t OutputDebugInfoSize = 0;
  2246. // A unique ID that identifies each compile unit.
  2247. unsigned UnitID = 0;
  2248. for (const auto &Obj : Map.objects()) {
  2249. CurrentDebugObject = Obj.get();
  2250. if (Options.Verbose)
  2251. outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
  2252. auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
  2253. if (std::error_code EC = ErrOrObj.getError()) {
  2254. reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
  2255. continue;
  2256. }
  2257. // Look for relocations that correspond to debug map entries.
  2258. if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
  2259. if (Options.Verbose)
  2260. outs() << "No valid relocations found. Skipping.\n";
  2261. continue;
  2262. }
  2263. // Setup access to the debug info.
  2264. DWARFContextInMemory DwarfContext(*ErrOrObj);
  2265. startDebugObject(DwarfContext, *Obj);
  2266. // In a first phase, just read in the debug info and store the DIE
  2267. // parent links that we will use during the next phase.
  2268. for (const auto &CU : DwarfContext.compile_units()) {
  2269. auto *CUDie = CU->getUnitDIE(false);
  2270. if (Options.Verbose) {
  2271. outs() << "Input compilation unit:";
  2272. CUDie->dump(outs(), CU.get(), 0);
  2273. }
  2274. Units.emplace_back(*CU, UnitID++);
  2275. gatherDIEParents(CUDie, 0, Units.back());
  2276. }
  2277. // Then mark all the DIEs that need to be present in the linked
  2278. // output and collect some information about them. Note that this
  2279. // loop can not be merged with the previous one becaue cross-cu
  2280. // references require the ParentIdx to be setup for every CU in
  2281. // the object file before calling this.
  2282. for (auto &CurrentUnit : Units)
  2283. lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getUnitDIE(), *Obj,
  2284. CurrentUnit, 0);
  2285. // The calls to applyValidRelocs inside cloneDIE will walk the
  2286. // reloc array again (in the same way findValidRelocsInDebugInfo()
  2287. // did). We need to reset the NextValidReloc index to the beginning.
  2288. NextValidReloc = 0;
  2289. // Construct the output DIE tree by cloning the DIEs we chose to
  2290. // keep above. If there are no valid relocs, then there's nothing
  2291. // to clone/emit.
  2292. if (!ValidRelocs.empty())
  2293. for (auto &CurrentUnit : Units) {
  2294. const auto *InputDIE = CurrentUnit.getOrigUnit().getUnitDIE();
  2295. CurrentUnit.setStartOffset(OutputDebugInfoSize);
  2296. DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PCOffset */,
  2297. 11 /* Unit Header size */);
  2298. CurrentUnit.setOutputUnitDIE(OutputDIE);
  2299. OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset();
  2300. if (Options.NoOutput)
  2301. continue;
  2302. // FIXME: for compatibility with the classic dsymutil, we emit
  2303. // an empty line table for the unit, even if the unit doesn't
  2304. // actually exist in the DIE tree.
  2305. patchLineTableForUnit(CurrentUnit, DwarfContext);
  2306. if (!OutputDIE)
  2307. continue;
  2308. patchRangesForUnit(CurrentUnit, DwarfContext);
  2309. Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext);
  2310. emitAcceleratorEntriesForUnit(CurrentUnit);
  2311. }
  2312. // Emit all the compile unit's debug information.
  2313. if (!ValidRelocs.empty() && !Options.NoOutput)
  2314. for (auto &CurrentUnit : Units) {
  2315. generateUnitRanges(CurrentUnit);
  2316. CurrentUnit.fixupForwardReferences();
  2317. Streamer->emitCompileUnitHeader(CurrentUnit);
  2318. if (!CurrentUnit.getOutputUnitDIE())
  2319. continue;
  2320. Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
  2321. }
  2322. if (!ValidRelocs.empty() && !Options.NoOutput && !Units.empty())
  2323. patchFrameInfoForObject(*Obj, DwarfContext,
  2324. Units[0].getOrigUnit().getAddressByteSize());
  2325. // Clean-up before starting working on the next object.
  2326. endDebugObject();
  2327. }
  2328. // Emit everything that's global.
  2329. if (!Options.NoOutput) {
  2330. Streamer->emitAbbrevs(Abbreviations);
  2331. Streamer->emitStrings(StringPool);
  2332. }
  2333. return Options.NoOutput ? true : Streamer->finish();
  2334. }
  2335. }
  2336. bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
  2337. const LinkOptions &Options) {
  2338. DwarfLinker Linker(OutputFilename, Options);
  2339. return Linker.link(DM);
  2340. }
  2341. }
  2342. }