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