xref: /freebsd/contrib/llvm-project/lld/MachO/Dwarf.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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 from the debug info, so we
24   // initialize DwarfObject with just the sections necessary to get that path.
25   // The debugger will locate the debug info via the object file paths that we
26   // emit in our STABS symbols, so we don't need to process & emit them
27   // 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::debugAbbrev, &dObj->abbrevSection)
33                 .Case(section_names::debugStr, &dObj->strSection)
34                 .Default(nullptr)) {
35       *s = toStringRef(isec->data);
36       hasDwarfInfo = true;
37     }
38   }
39 
40   if (hasDwarfInfo)
41     return dObj;
42   return nullptr;
43 }
44