1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[DWARFYAML] Add helper function getDWARFEmitterByName(). NFC.

In this patch, we add a helper function getDWARFEmitterByName(). This
function returns the proper DWARF section emitting method by the name.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D84952
This commit is contained in:
Xing GUO 2020-07-31 20:06:30 +08:00
parent 65b9283070
commit 3bfc8f644d
3 changed files with 40 additions and 52 deletions

View File

@ -43,6 +43,8 @@ Error emitDebugAddr(raw_ostream &OS, const Data &DI);
Error emitDebugStrOffsets(raw_ostream &OS, const Data &DI); Error emitDebugStrOffsets(raw_ostream &OS, const Data &DI);
Error emitDebugRnglists(raw_ostream &OS, const Data &DI); Error emitDebugRnglists(raw_ostream &OS, const Data &DI);
std::function<Error(raw_ostream &, const Data &)>
getDWARFEmitterByName(StringRef SecName);
Expected<StringMap<std::unique_ptr<MemoryBuffer>>> Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
emitDebugSections(StringRef YAMLString, emitDebugSections(StringRef YAMLString,
bool IsLittleEndian = sys::IsLittleEndianHost); bool IsLittleEndian = sys::IsLittleEndianHost);

View File

@ -15,6 +15,7 @@
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/BinaryFormat/Dwarf.h" #include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/ObjectYAML/DWARFYAML.h" #include "llvm/ObjectYAML/DWARFYAML.h"
#include "llvm/Support/Errc.h" #include "llvm/Support/Errc.h"
@ -762,14 +763,40 @@ Error DWARFYAML::emitDebugRnglists(raw_ostream &OS, const Data &DI) {
OS, *DI.DebugRnglists, DI.IsLittleEndian, DI.Is64BitAddrSize); OS, *DI.DebugRnglists, DI.IsLittleEndian, DI.Is64BitAddrSize);
} }
using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &); std::function<Error(raw_ostream &, const DWARFYAML::Data &)>
DWARFYAML::getDWARFEmitterByName(StringRef SecName) {
auto EmitFunc =
StringSwitch<
std::function<Error(raw_ostream &, const DWARFYAML::Data &)>>(SecName)
.Case("debug_abbrev", DWARFYAML::emitDebugAbbrev)
.Case("debug_addr", DWARFYAML::emitDebugAddr)
.Case("debug_aranges", DWARFYAML::emitDebugAranges)
.Case("debug_gnu_pubnames", DWARFYAML::emitDebugGNUPubnames)
.Case("debug_gnu_pubtypes", DWARFYAML::emitDebugGNUPubtypes)
.Case("debug_info", DWARFYAML::emitDebugInfo)
.Case("debug_line", DWARFYAML::emitDebugLine)
.Case("debug_pubnames", DWARFYAML::emitDebugPubnames)
.Case("debug_pubtypes", DWARFYAML::emitDebugPubtypes)
.Case("debug_ranges", DWARFYAML::emitDebugRanges)
.Case("debug_rnglists", DWARFYAML::emitDebugRnglists)
.Case("debug_str", DWARFYAML::emitDebugStr)
.Case("debug_str_offsets", DWARFYAML::emitDebugStrOffsets)
.Default([&](raw_ostream &, const DWARFYAML::Data &) {
return createStringError(errc::not_supported,
SecName + " is not supported");
});
return EmitFunc;
}
static Error static Error
emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, emitDebugSectionImpl(const DWARFYAML::Data &DI, StringRef Sec,
StringRef Sec,
StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) { StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
std::string Data; std::string Data;
raw_string_ostream DebugInfoStream(Data); raw_string_ostream DebugInfoStream(Data);
auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Sec);
if (Error Err = EmitFunc(DebugInfoStream, DI)) if (Error Err = EmitFunc(DebugInfoStream, DI))
return Err; return Err;
DebugInfoStream.flush(); DebugInfoStream.flush();
@ -796,23 +823,12 @@ DWARFYAML::emitDebugSections(StringRef YAMLString, bool IsLittleEndian) {
return createStringError(YIn.error(), GeneratedDiag.getMessage()); return createStringError(YIn.error(), GeneratedDiag.getMessage());
StringMap<std::unique_ptr<MemoryBuffer>> DebugSections; StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info", Error Err = Error::success();
DebugSections); cantFail(std::move(Err));
Err = joinErrors(std::move(Err),
emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine, for (StringRef SecName : DI.getNonEmptySectionNames())
"debug_line", DebugSections)); Err = joinErrors(std::move(Err),
Err = joinErrors(std::move(Err), emitDebugSectionImpl(DI, SecName, DebugSections));
emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr,
"debug_str", DebugSections));
Err = joinErrors(std::move(Err),
emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev,
"debug_abbrev", DebugSections));
Err = joinErrors(std::move(Err),
emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges,
"debug_aranges", DebugSections));
Err = joinErrors(std::move(Err),
emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges,
"debug_ranges", DebugSections));
if (Err) if (Err)
return std::move(Err); return std::move(Err);

View File

@ -949,39 +949,9 @@ Expected<uint64_t> emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
return 0; return 0;
uint64_t BeginOffset = CBA.tell(); uint64_t BeginOffset = CBA.tell();
Error Err = Error::success();
cantFail(std::move(Err));
if (Name == ".debug_str") auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Name.substr(1));
Err = DWARFYAML::emitDebugStr(*OS, DWARF); if (Error Err = EmitFunc(*OS, DWARF))
else if (Name == ".debug_aranges")
Err = DWARFYAML::emitDebugAranges(*OS, DWARF);
else if (Name == ".debug_ranges")
Err = DWARFYAML::emitDebugRanges(*OS, DWARF);
else if (Name == ".debug_line")
Err = DWARFYAML::emitDebugLine(*OS, DWARF);
else if (Name == ".debug_addr")
Err = DWARFYAML::emitDebugAddr(*OS, DWARF);
else if (Name == ".debug_abbrev")
Err = DWARFYAML::emitDebugAbbrev(*OS, DWARF);
else if (Name == ".debug_info")
Err = DWARFYAML::emitDebugInfo(*OS, DWARF);
else if (Name == ".debug_pubnames")
Err = DWARFYAML::emitDebugPubnames(*OS, DWARF);
else if (Name == ".debug_pubtypes")
Err = DWARFYAML::emitDebugPubtypes(*OS, DWARF);
else if (Name == ".debug_gnu_pubnames")
Err = DWARFYAML::emitDebugGNUPubnames(*OS, DWARF);
else if (Name == ".debug_gnu_pubtypes")
Err = DWARFYAML::emitDebugGNUPubtypes(*OS, DWARF);
else if (Name == ".debug_str_offsets")
Err = DWARFYAML::emitDebugStrOffsets(*OS, DWARF);
else if (Name == ".debug_rnglists")
Err = DWARFYAML::emitDebugRnglists(*OS, DWARF);
else
llvm_unreachable("unexpected emitDWARF() call");
if (Err)
return std::move(Err); return std::move(Err);
return CBA.tell() - BeginOffset; return CBA.tell() - BeginOffset;