2018-09-15 07:51:05 +08:00
|
|
|
//===- DWARF.cpp ----------------------------------------------------------===//
|
2016-10-20 17:19:48 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2016-10-20 17:19:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2021-10-26 03:52:06 +08:00
|
|
|
// The --gdb-index option instructs the linker to emit a .gdb_index section.
|
2017-03-29 08:09:40 +08:00
|
|
|
// The section contains information to make gdb startup faster.
|
|
|
|
// The format of the section is described at
|
|
|
|
// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.
|
2016-10-20 17:19:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-09-15 07:51:05 +08:00
|
|
|
#include "DWARF.h"
|
2017-12-10 00:56:18 +08:00
|
|
|
#include "Symbols.h"
|
2018-09-26 16:11:34 +08:00
|
|
|
#include "Target.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2016-12-17 18:18:05 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
|
2016-12-15 17:08:13 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2016-10-20 17:19:48 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-10-20 17:19:48 +08:00
|
|
|
|
2017-09-25 07:12:36 +08:00
|
|
|
template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
|
2020-08-13 11:50:59 +08:00
|
|
|
// Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.
|
|
|
|
ArrayRef<typename ELFT::Shdr> objSections =
|
|
|
|
CHECK(obj->getObj().sections(), obj);
|
|
|
|
assert(objSections.size() == obj->getSections().size());
|
|
|
|
for (auto it : llvm::enumerate(obj->getSections())) {
|
|
|
|
InputSectionBase *sec = it.value();
|
2017-07-20 06:27:35 +08:00
|
|
|
if (!sec)
|
|
|
|
continue;
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
|
2018-11-12 02:57:35 +08:00
|
|
|
if (LLDDWARFSection *m =
|
|
|
|
StringSwitch<LLDDWARFSection *>(sec->name)
|
2018-12-03 18:48:57 +08:00
|
|
|
.Case(".debug_addr", &addrSection)
|
2019-08-08 01:18:18 +08:00
|
|
|
.Case(".debug_gnu_pubnames", &gnuPubnamesSection)
|
|
|
|
.Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
|
2020-04-29 09:53:12 +08:00
|
|
|
.Case(".debug_loclists", &loclistsSection)
|
2019-08-08 01:18:18 +08:00
|
|
|
.Case(".debug_ranges", &rangesSection)
|
|
|
|
.Case(".debug_rnglists", &rnglistsSection)
|
2019-08-08 06:49:14 +08:00
|
|
|
.Case(".debug_str_offsets", &strOffsetsSection)
|
2018-11-12 02:57:35 +08:00
|
|
|
.Case(".debug_line", &lineSection)
|
|
|
|
.Default(nullptr)) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
m->Data = toStringRef(sec->data());
|
2017-07-20 06:27:35 +08:00
|
|
|
m->sec = sec;
|
|
|
|
continue;
|
|
|
|
}
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
|
2017-07-20 06:27:35 +08:00
|
|
|
if (sec->name == ".debug_abbrev")
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
abbrevSection = toStringRef(sec->data());
|
2017-11-17 19:57:47 +08:00
|
|
|
else if (sec->name == ".debug_str")
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
strSection = toStringRef(sec->data());
|
|
|
|
else if (sec->name == ".debug_line_str")
|
2019-08-08 01:18:18 +08:00
|
|
|
lineStrSection = toStringRef(sec->data());
|
2020-08-13 11:50:59 +08:00
|
|
|
else if (sec->name == ".debug_info" &&
|
|
|
|
!(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) {
|
|
|
|
// In DWARF v5, -fdebug-types-section places type units in .debug_info
|
|
|
|
// sections in COMDAT groups. They are not compile units and thus should
|
|
|
|
// be ignored for .gdb_index/diagnostics purposes.
|
|
|
|
//
|
|
|
|
// We use a simple heuristic: the compile unit does not have the SHF_GROUP
|
|
|
|
// flag. If we place compile units in COMDAT groups in the future, we may
|
|
|
|
// need to perform a lightweight parsing. We drop the SHF_GROUP flag when
|
|
|
|
// the InputSection was created, so we need to retrieve sh_flags from the
|
|
|
|
// associated ELF section header.
|
|
|
|
infoSection.Data = toStringRef(sec->data());
|
|
|
|
infoSection.sec = sec;
|
|
|
|
}
|
2017-07-20 06:27:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 10:43:17 +08:00
|
|
|
namespace {
|
|
|
|
template <class RelTy> struct LLDRelocationResolver {
|
|
|
|
// In the ELF ABIs, S sepresents the value of the symbol in the relocation
|
2020-11-12 20:16:57 +08:00
|
|
|
// entry. For Rela, the addend is stored as part of the relocation entry and
|
|
|
|
// is provided by the `findAux` method.
|
|
|
|
// In resolve() methods, the `type` and `offset` arguments would always be 0,
|
|
|
|
// because we don't set an owning object for the `RelocationRef` instance that
|
|
|
|
// we create in `findAux()`.
|
|
|
|
static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
|
|
|
|
uint64_t /*locData*/, int64_t addend) {
|
|
|
|
return s + addend;
|
2019-03-22 10:43:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
|
2020-11-12 20:16:57 +08:00
|
|
|
// For Rel, the addend is extracted from the relocated location and is
|
|
|
|
// supplied by the caller.
|
|
|
|
static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
|
|
|
|
uint64_t locData, int64_t /*addend*/) {
|
|
|
|
return s + locData;
|
2019-03-22 10:43:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2017-07-20 06:27:35 +08:00
|
|
|
// Find if there is a relocation at Pos in Sec. The code is a bit
|
|
|
|
// more complicated than usual because we need to pass a section index
|
|
|
|
// to llvm since it has no idea about InputSection.
|
|
|
|
template <class ELFT>
|
|
|
|
template <class RelTy>
|
|
|
|
Optional<RelocAddrEntry>
|
|
|
|
LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
|
|
|
|
ArrayRef<RelTy> rels) const {
|
2019-04-17 16:00:46 +08:00
|
|
|
auto it =
|
2019-06-30 19:19:56 +08:00
|
|
|
partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
|
2017-08-01 12:11:03 +08:00
|
|
|
if (it == rels.end() || it->r_offset != pos)
|
2017-07-20 06:27:35 +08:00
|
|
|
return None;
|
2017-08-01 12:11:03 +08:00
|
|
|
const RelTy &rel = *it;
|
|
|
|
|
2017-07-27 06:13:32 +08:00
|
|
|
const ObjFile<ELFT> *file = sec.getFile<ELFT>();
|
2017-07-20 06:27:35 +08:00
|
|
|
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
|
2019-04-06 04:16:26 +08:00
|
|
|
const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
|
2017-07-20 06:27:35 +08:00
|
|
|
uint32_t secIndex = file->getSectionIndex(sym);
|
2017-11-30 06:09:16 +08:00
|
|
|
|
2019-06-26 16:09:08 +08:00
|
|
|
// An undefined symbol may be a symbol defined in a discarded section. We
|
|
|
|
// shall still resolve it. This is important for --gdb-index: the end address
|
|
|
|
// offset of an entry in .debug_ranges is relocated. If it is not resolved,
|
|
|
|
// its zero value will terminate the decoding of .debug_ranges prematurely.
|
|
|
|
Symbol &s = file->getRelocTargetSym(rel);
|
|
|
|
uint64_t val = 0;
|
[LLD][NFC] Remove getOffsetInFile() workaround.
Summary:
LLD has workaround for the times when SectionIndex was not passed properly:
LT->getFileLineInfoForAddress(
S->getOffsetInFile() + Offset, nullptr,
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info));
S->getOffsetInFile() was added to differentiate offsets between
various sections. Now SectionIndex is properly specified.
Thus it is not necessary to use getOffsetInFile() workaround.
See https://reviews.llvm.org/D58194, https://reviews.llvm.org/D58357.
This patch removes getOffsetInFile() workaround.
Reviewers: ruiu, grimar, MaskRay, espindola
Reviewed By: grimar, MaskRay
Subscribers: emaste, arichardson, llvm-commits
Tags: #llvm, #lld
Differential Revision: https://reviews.llvm.org/D75636
2020-03-05 03:56:52 +08:00
|
|
|
if (auto *dr = dyn_cast<Defined>(&s))
|
2019-06-26 16:09:08 +08:00
|
|
|
val = dr->value;
|
2017-07-20 06:27:35 +08:00
|
|
|
|
2019-03-22 10:43:17 +08:00
|
|
|
DataRefImpl d;
|
|
|
|
d.p = getAddend<ELFT>(rel);
|
|
|
|
return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
|
2019-07-18 13:22:55 +08:00
|
|
|
val, Optional<object::RelocationRef>(),
|
|
|
|
0, LLDRelocationResolver<RelTy>::resolve};
|
2017-07-20 06:27:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
|
|
|
|
uint64_t pos) const {
|
|
|
|
auto &sec = static_cast<const LLDDWARFSection &>(s);
|
|
|
|
if (sec.sec->areRelocsRela)
|
|
|
|
return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
|
|
|
|
return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template class elf::LLDDwarfObj<ELF32LE>;
|
|
|
|
template class elf::LLDDwarfObj<ELF32BE>;
|
|
|
|
template class elf::LLDDwarfObj<ELF64LE>;
|
|
|
|
template class elf::LLDDwarfObj<ELF64BE>;
|