xref: /freebsd/contrib/llvm-project/lld/MachO/Dwarf.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1 //===- DWARF.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Dwarf.h"
10 #include "InputFiles.h"
11 #include "InputSection.h"
12 #include "OutputSegment.h"
13 
14 #include <memory>
15 
16 using namespace lld;
17 using namespace lld::macho;
18 using namespace llvm;
19 
20 std::unique_ptr<DwarfObject> DwarfObject::create(ObjFile *obj) {
21   auto dObj = std::make_unique<DwarfObject>();
22   bool hasDwarfInfo = false;
23   // LLD only needs to extract the source file path and line numbers from the
24   // debug info, so we initialize DwarfObject with just the sections necessary
25   // to get that path. The debugger will locate the debug info via the object
26   // file paths that we emit in our STABS symbols, so we don't need to process &
27   // emit them ourselves.
28   for (const InputSection *isec : obj->debugSections) {
29     if (StringRef *s =
30             StringSwitch<StringRef *>(isec->getName())
31                 .Case(section_names::debugInfo, &dObj->infoSection.Data)
32                 .Case(section_names::debugLine, &dObj->lineSection.Data)
33                 .Case(section_names::debugAbbrev, &dObj->abbrevSection)
34                 .Case(section_names::debugStr, &dObj->strSection)
35                 .Default(nullptr)) {
36       *s = toStringRef(isec->data);
37       hasDwarfInfo = true;
38     }
39   }
40 
41   if (hasDwarfInfo)
42     return dObj;
43   return nullptr;
44 }
45