llvm/lld/MachO/Dwarf.h
Jez Ng 3fcb0eeb15 [lld-macho] Emit STABS symbols for debugging, and drop debug sections
Debug sections contain a large amount of data. In order not to bloat the size
of the final binary, we remove them and instead emit STABS symbols for
`dsymutil` and the debugger to locate their contents in the object files.

With this diff, `dsymutil` is able to locate the debug info. However, we need
a few more features before `lldb` is able to work well with our binaries --
e.g. having `LC_DYSYMTAB` accurately reflect the number of local symbols,
emitting `LC_UUID`, and more. Those will be handled in follow-up diffs.

Note also that the STABS we emit differ slightly from what ld64 does. First, we
emit the path to the source file as one `N_SO` symbol instead of two. (`ld64`
emits one `N_SO` for the dirname and one of the basename.) Second, we do not
emit `N_BNSYM` and `N_ENSYM` STABS to mark the start and end of functions,
because the `N_FUN` STABS already serve that purpose. @clayborg recommended
these changes based on his knowledge of what the debugging tools look for.

Additionally, this current implementation doesn't accurately reflect the size
of function symbols. It uses the size of their containing sectioins as a proxy,
but that is only accurate if `.subsections_with_symbols` is set, and if there
isn't an `N_ALT_ENTRY` in that particular subsection. I think we have two
options to solve this:

1. We can split up subsections by symbol even if `.subsections_with_symbols`
   is not set, but include constraints to ensure those subsections retain
   their order in the final output. This is `ld64`'s approach.
2. We could just add a `size` field to our `Symbol` class. This seems simpler,
   and I'm more inclined toward it, but I'm not sure if there are use cases
   that it doesn't handle well. As such I'm punting on the decision for now.

Reviewed By: clayborg

Differential Revision: https://reviews.llvm.org/D89257
2020-12-01 15:05:20 -08:00

54 lines
1.5 KiB
C++

//===- DWARF.h -----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-------------------------------------------------------------------===//
#ifndef LLD_MACHO_DWARF_H
#define LLD_MACHO_DWARF_H
#include "llvm/ADT/StringRef.h"
#include "llvm/DebugInfo/DWARF/DWARFObject.h"
namespace lld {
namespace macho {
class ObjFile;
// Implements the interface between LLVM's DWARF-parsing utilities and LLD's
// InputSection structures.
class DwarfObject final : public llvm::DWARFObject {
public:
bool isLittleEndian() const override { return true; }
llvm::Optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override {
// TODO: implement this
return llvm::None;
}
void forEachInfoSections(
llvm::function_ref<void(const llvm::DWARFSection &)> f) const override {
f(infoSection);
}
llvm::StringRef getAbbrevSection() const override { return abbrevSection; }
llvm::StringRef getStrSection() const override { return strSection; }
// Returns an instance of DwarfObject if the given object file has the
// relevant DWARF debug sections.
static std::unique_ptr<DwarfObject> create(ObjFile *);
private:
llvm::DWARFSection infoSection;
llvm::StringRef abbrevSection;
llvm::StringRef strSection;
};
} // namespace macho
} // namespace lld
#endif