summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2018-10-12 17:01:46 +0000
committerYonghong Song <yhs@fb.com>2018-10-12 17:01:46 +0000
commit6c2327a09e2820302a3a2be0f0dbe24530ca5e7c (patch)
treedb78847cffe39e7e33fa9d72d663cb79524d2542 /llvm/lib/MC
parente67b68f341e655b2fd4beff337fe261fd12cb52a (diff)
downloadbcm5719-llvm-6c2327a09e2820302a3a2be0f0dbe24530ca5e7c.tar.gz
bcm5719-llvm-6c2327a09e2820302a3a2be0f0dbe24530ca5e7c.zip
[BPF] Add BTF generation for BPF target
BTF is the debug format for BPF, a kernel virtual machine and widely used for tracing, networking and security, etc ([1]). Currently only instruction streams are passed to kernel, the kernel verifier verifies them before execution. In order to provide better visibility of bpf programs to user space tools, some debug information, e.g., function names and debug line information are desirable for kernel so tools can get such information with better annotation for jited instructions for performance or other reasons. The dwarf is too complicated in kernel and for BPF. Hence, BTF is designed to be the debug format for BPF ([2]). Right now, pahole supports BTF for types, which are generated based on dwarf sections in the ELF file. In order to annotate performance metrics for jited bpf insns, it is necessary to pass debug line info to the kernel. Furthermore, we want to pass the actual code to the kernel because of the following reasons: . bpf program typically is small so storage overhead should be small. . in bpf land, it is totally possible that an application loads the bpf program into the kernel and then that application quits, so holding debug info by the user space application is not practical. . having source codes directly kept by kernel would ease deployment since the original source code does not need ship on every hosts and kernel-devel package does not need to be deployed even if kernel headers are used. The only reliable time to get the source code is during compilation time. This will result in both more accurate information and easier deployment as stated in the above. Another consideration is for JIT. The project like bcc use MCJIT to compile a C program into bpf insns and load them to the kernel ([3]). The generated BTF sections will be readily available for such cases as well. This patch implemented generation of BTF info in llvm compiler. The BTF related sections will be generated when both -target bpf and -g are specified. Two sections are generated: .BTF contains all the type and string information, and .BTF.ext contains the func_info and line_info. The separation is related to how two sections are used differently in bpf loader, e.g., linux libbpf ([4]). The .BTF section can be loaded into the kernel directly while .BTF.ext needs loader manipulation before loading to the kernel. The format of the each section is roughly defined in llvm:include/llvm/MC/MCBTFContext.h and from the implementation in llvm:lib/MC/MCBTFContext.cpp. A later example also shows the contents in each section. The type and func_info are gathered during CodeGen/AsmPrinter by traversing dwarf debug_info. The line_info is gathered in MCObjectStreamer before writing to the object file. After all the information is gathered, the two sections are emitted in MCObjectStreamer::finishImpl. With cmake CMAKE_BUILD_TYPE=Debug, the compiler can dump out all the tables except insn offset, which will be resolved later as relocation records. The debug type "btf" is used for BTFContext dump. Dwarf tests the debug info generation with llvm-dwarfdump to decode the binary sections and check whether the result is expected. Currently we do not have such a tool yet. We will implement btf dump functionality in bpftool ([5]) as the bpftool is considered the recommended tool for bpf introspection. The implementation for type and func_info is tested with linux kernel test cases. The line_info is visually checked with dump from linux kernel libbpf ([4]) and checked with readelf dumping section raw data. Note that the .BTF and .BTF.ext information will not be emitted to assembly code and there is no assembler support for BTF either. In the below, with a clang/llvm built with CMAKE_BUILD_TYPE=Debug, Each table contents are shown for a simple C program. -bash-4.2$ cat -n test.c 1 struct A { 2 int a; 3 char b; 4 }; 5 6 int test(struct A *t) { 7 return t->a; 8 } -bash-4.2$ clang -O2 -target bpf -g -mllvm -debug-only=btf -c test.c Type Table: [1] FUNC name_off=1 info=0x0c000001 size/type=2 param_type=3 [2] INT name_off=12 info=0x01000000 size/type=4 desc=0x01000020 [3] PTR name_off=0 info=0x02000000 size/type=4 [4] STRUCT name_off=16 info=0x04000002 size/type=8 name_off=18 type=2 bit_offset=0 name_off=20 type=5 bit_offset=32 [5] INT name_off=22 info=0x01000000 size/type=1 desc=0x02000008 String Table: 0 : 1 : test 6 : .text 12 : int 16 : A 18 : a 20 : b 22 : char 27 : test.c 34 : int test(struct A *t) { 58 : return t->a; FuncInfo Table: sec_name_off=6 insn_offset=<Omitted> type_id=1 LineInfo Table: sec_name_off=6 insn_offset=<Omitted> file_name_off=27 line_off=34 line_num=6 column_num=0 insn_offset=<Omitted> file_name_off=27 line_off=58 line_num=7 column_num=3 -bash-4.2$ readelf -S test.o ...... [12] .BTF PROGBITS 0000000000000000 0000028d 00000000000000c1 0000000000000000 0 0 1 [13] .BTF.ext PROGBITS 0000000000000000 0000034e 0000000000000050 0000000000000000 0 0 1 [14] .rel.BTF.ext REL 0000000000000000 00000648 0000000000000030 0000000000000010 16 13 8 ...... -bash-4.2$ The latest linux kernel ([6]) can already support .BTF with type information. The [7] has the reference implementation in linux kernel side to support .BTF.ext func_info. The .BTF.ext line_info support is not implemented yet. If you have difficulty accessing [6], you can manually do the following to access the code: git clone https://github.com/yonghong-song/bpf-next-linux.git cd bpf-next-linux git checkout btf The change will push to linux kernel soon once this patch is landed. References: [1]. https://www.kernel.org/doc/Documentation/networking/filter.txt [2]. https://lwn.net/Articles/750695/ [3]. https://github.com/iovisor/bcc [4]. https://github.com/torvalds/linux/tree/master/tools/lib/bpf [5]. https://github.com/torvalds/linux/tree/master/tools/bpf/bpftool [6]. https://github.com/torvalds/linux [7]. https://github.com/yonghong-song/bpf-next-linux/tree/btf Signed-off-by: Song Liu <songliubraving@fb.com> Signed-off-by: Yonghong Song <yhs@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Differential Revision: https://reviews.llvm.org/D52950 llvm-svn: 344366
Diffstat (limited to 'llvm/lib/MC')
-rw-r--r--llvm/lib/MC/CMakeLists.txt2
-rw-r--r--llvm/lib/MC/MCBTFContext.cpp235
-rw-r--r--llvm/lib/MC/MCContext.cpp11
-rw-r--r--llvm/lib/MC/MCDwarf2BTF.cpp99
-rw-r--r--llvm/lib/MC/MCDwarf2BTF.h29
-rw-r--r--llvm/lib/MC/MCObjectFileInfo.cpp3
-rw-r--r--llvm/lib/MC/MCObjectStreamer.cpp34
7 files changed, 412 insertions, 1 deletions
diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
index ba36d99e8f7..85bf1616fd6 100644
--- a/llvm/lib/MC/CMakeLists.txt
+++ b/llvm/lib/MC/CMakeLists.txt
@@ -10,11 +10,13 @@ add_llvm_library(LLVMMC
MCAsmMacro.cpp
MCAsmStreamer.cpp
MCAssembler.cpp
+ MCBTFContext.cpp
MCCodeEmitter.cpp
MCCodePadder.cpp
MCCodeView.cpp
MCContext.cpp
MCDwarf.cpp
+ MCDwarf2BTF.cpp
MCELFObjectTargetWriter.cpp
MCELFStreamer.cpp
MCExpr.cpp
diff --git a/llvm/lib/MC/MCBTFContext.cpp b/llvm/lib/MC/MCBTFContext.cpp
new file mode 100644
index 00000000000..cb846ee5e51
--- /dev/null
+++ b/llvm/lib/MC/MCBTFContext.cpp
@@ -0,0 +1,235 @@
+//===- lib/MC/MCBTFContext.cpp - Machine Code BTF Context -----------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCBTFContext.h"
+#include "llvm/MC/MCObjectFileInfo.h"
+#include "llvm/MC/MCObjectStreamer.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cstdlib>
+#include <tuple>
+#include <utility>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "btf"
+
+void MCBTFContext::addTypeEntry(std::unique_ptr<BTFTypeEntry> Entry) {
+ TypeEntries.push_back(std::move(Entry));
+}
+
+void MCBTFContext::dump(raw_ostream &OS) {
+ OS << "Type Table:\n";
+ for (size_t i = 0; i < TypeEntries.size(); i++) {
+ auto TypeEntry = TypeEntries[i].get();
+ TypeEntry->print(OS, *this);
+ }
+
+ OS << "\nString Table:\n";
+ StringTable.showTable(OS);
+
+ OS << "\nFuncInfo Table:\n";
+ for (auto &FuncSec : FuncInfoTable) {
+ OS << "sec_name_off=" << FuncSec.first << "\n";
+ for (auto &FuncInfo : FuncSec.second) {
+ OS << "\tinsn_offset=<Omitted> type_id="
+ << FuncInfo.TypeId << "\n";
+ }
+ }
+
+ OS << "\nLineInfo Table:\n";
+ for (auto &LineSec : LineInfoTable) {
+ OS << "sec_name_off=" << LineSec.first << "\n";
+ for (auto &LineInfo : LineSec.second) {
+ OS << "\tinsn_offset=<Omitted> file_name_off="
+ << LineInfo.FileNameOff
+ << " line_off=" << LineInfo.LineOff
+ << " line_num=" << LineInfo.LineNum
+ << " column_num=" << LineInfo.ColumnNum
+ << "\n";
+ }
+ }
+}
+
+void MCBTFContext::emitCommonHeader(MCObjectStreamer *MCOS) {
+ MCOS->EmitIntValue(BTF_MAGIC, 2);
+ MCOS->EmitIntValue(BTF_VERSION, 1);
+ MCOS->EmitIntValue(0, 1);
+}
+
+void MCBTFContext::emitBTFSection(MCObjectStreamer *MCOS) {
+ MCContext &context = MCOS->getContext();
+ MCOS->SwitchSection(context.getObjectFileInfo()->getBTFSection());
+
+ // emit header
+ emitCommonHeader(MCOS);
+ MCOS->EmitIntValue(sizeof(struct btf_header), 4);
+
+ uint32_t type_len = 0, str_len;
+ for (auto &TypeEntry : TypeEntries)
+ type_len += TypeEntry->getSize();
+ str_len = StringTable.getSize();
+
+ MCOS->EmitIntValue(0, 4);
+ MCOS->EmitIntValue(type_len, 4);
+ MCOS->EmitIntValue(type_len, 4);
+ MCOS->EmitIntValue(str_len, 4);
+
+ // emit type table
+ for (auto &TypeEntry: TypeEntries)
+ TypeEntry->emitData(MCOS);
+
+ // emit string table
+ for (auto &S : StringTable.getTable()) {
+ for (auto C : S)
+ MCOS->EmitIntValue(C, 1);
+ MCOS->EmitIntValue('\0', 1);
+ }
+}
+
+void MCBTFContext::emitBTFExtSection(MCObjectStreamer *MCOS) {
+ MCContext &context = MCOS->getContext();
+ MCOS->SwitchSection(context.getObjectFileInfo()->getBTFExtSection());
+
+ // emit header
+ emitCommonHeader(MCOS);
+ MCOS->EmitIntValue(sizeof(struct btf_ext_header), 4);
+
+ uint32_t func_len = 0, line_len = 0;
+ for (auto &FuncSec : FuncInfoTable) {
+ func_len += sizeof(struct btf_sec_func_info);
+ func_len += FuncSec.second.size() * sizeof(struct bpf_func_info);
+ }
+ for (auto &LineSec : LineInfoTable) {
+ line_len += sizeof(struct btf_sec_line_info);
+ line_len += LineSec.second.size() * sizeof(struct bpf_line_info);
+ }
+
+ MCOS->EmitIntValue(0, 4);
+ MCOS->EmitIntValue(func_len, 4);
+ MCOS->EmitIntValue(func_len, 4);
+ MCOS->EmitIntValue(line_len, 4);
+
+ // emit func_info table
+ for (const auto &FuncSec : FuncInfoTable) {
+ MCOS->EmitIntValue(FuncSec.first, 4);
+ MCOS->EmitIntValue(FuncSec.second.size(), 4);
+ for (const auto &FuncInfo : FuncSec.second) {
+ MCOS->EmitBTFAdvanceLineAddr(FuncInfo.Label, 4);
+ MCOS->EmitIntValue(FuncInfo.TypeId, 4);
+ }
+ }
+
+ // emit line_info table
+ for (const auto &LineSec : LineInfoTable) {
+ MCOS->EmitIntValue(LineSec.first, 4);
+ MCOS->EmitIntValue(LineSec.second.size(), 4);
+ for (const auto &LineInfo : LineSec.second) {
+ MCOS->EmitBTFAdvanceLineAddr(LineInfo.Label, 4);
+ MCOS->EmitIntValue(LineInfo.FileNameOff, 4);
+ MCOS->EmitIntValue(LineInfo.LineOff, 4);
+ MCOS->EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
+ }
+ }
+}
+
+void MCBTFContext::emitAll(MCObjectStreamer *MCOS) {
+ LLVM_DEBUG(dump(dbgs()));
+ emitBTFSection(MCOS);
+ emitBTFExtSection(MCOS);
+}
+
+void BTFTypeEntry::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ OS << "[" << Id << "] "
+ << btf_kind_str[BTF_INFO_KIND(BTFType.info)]
+ << " name_off=" << BTFType.name_off
+ << " info=" << format("0x%08lx", BTFType.info)
+ << " size/type=" << BTFType.size << "\n";
+}
+
+void BTFTypeEntry::emitData(MCObjectStreamer *MCOS) {
+ MCOS->EmitIntValue(BTFType.name_off, 4);
+ MCOS->EmitIntValue(BTFType.info, 4);
+ MCOS->EmitIntValue(BTFType.size, 4);
+}
+
+void BTFTypeEntryInt::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ BTFTypeEntry::print(OS, MCBTFContext);
+ OS << "\tdesc=" << format("0x%08lx", IntVal) << "\n";
+}
+
+void BTFTypeEntryInt::emitData(MCObjectStreamer *MCOS) {
+ BTFTypeEntry::emitData(MCOS);
+ MCOS->EmitIntValue(IntVal, 4);
+}
+
+void BTFTypeEntryEnum::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ BTFTypeEntry::print(OS, MCBTFContext);
+ for (size_t i = 0; i < BTF_INFO_VLEN(BTFType.info); i++) {
+ auto &EnumValue = EnumValues[i];
+ OS << "\tname_off=" << EnumValue.name_off
+ << " value=" << EnumValue.val << "\n";
+ }
+}
+
+void BTFTypeEntryEnum::emitData(MCObjectStreamer *MCOS) {
+ BTFTypeEntry::emitData(MCOS);
+ for (auto &EnumValue : EnumValues) {
+ MCOS->EmitIntValue(EnumValue.name_off, 4);
+ MCOS->EmitIntValue(EnumValue.val, 4);
+ }
+}
+
+void BTFTypeEntryArray::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ BTFTypeEntry::print(OS, MCBTFContext);
+ OS << "\telem_type=" << format("0x%08lx", ArrayInfo.type)
+ << " index_type=" << format("0x%08lx", ArrayInfo.index_type)
+ << " num_element=" << ArrayInfo.nelems << "\n";
+}
+
+void BTFTypeEntryArray::emitData(MCObjectStreamer *MCOS) {
+ BTFTypeEntry::emitData(MCOS);
+ MCOS->EmitIntValue(ArrayInfo.type, 4);
+ MCOS->EmitIntValue(ArrayInfo.index_type, 4);
+ MCOS->EmitIntValue(ArrayInfo.nelems, 4);
+}
+
+void BTFTypeEntryStruct::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ BTFTypeEntry::print(OS, MCBTFContext);
+ for (size_t i = 0; i < BTF_INFO_VLEN(BTFType.info); i++) {
+ auto &Member = Members[i];
+ OS << "\tname_off=" << Member.name_off
+ << " type=" << Member.type
+ << " bit_offset=" << Member.offset << "\n";
+ }
+}
+
+void BTFTypeEntryStruct::emitData(MCObjectStreamer *MCOS) {
+ BTFTypeEntry::emitData(MCOS);
+ for (auto &Member : Members) {
+ MCOS->EmitIntValue(Member.name_off, 4);
+ MCOS->EmitIntValue(Member.type, 4);
+ MCOS->EmitIntValue(Member.offset, 4);
+ }
+}
+
+void BTFTypeEntryFunc::print(raw_ostream &OS, MCBTFContext& MCBTFContext) {
+ BTFTypeEntry::print(OS, MCBTFContext);
+ for (size_t i = 0; i < BTF_INFO_VLEN(BTFType.info); i++) {
+ auto Parameter = Parameters[i];
+ OS << "\tparam_type=" << Parameter << "\n";
+ }
+}
+
+void BTFTypeEntryFunc::emitData(MCObjectStreamer *MCOS) {
+ BTFTypeEntry::emitData(MCOS);
+ for (auto &Parameter: Parameters)
+ MCOS->EmitIntValue(Parameter, 4);
+}
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index fab517075c5..18250a474b7 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -17,6 +17,7 @@
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCBTFContext.h"
#include "llvm/MC/MCCodeView.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCExpr.h"
@@ -60,7 +61,7 @@ MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
: SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
Symbols(Allocator), UsedNames(Allocator),
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
- AutoReset(DoAutoReset) {
+ AutoReset(DoAutoReset), BTFCtx(nullptr) {
SecureLogFile = AsSecureLogFileName;
if (SrcMgr && SrcMgr->getNumBuffers())
@@ -114,6 +115,14 @@ void MCContext::reset() {
GenDwarfFileNumber = 0;
HadError = false;
+ BTFCtx.reset();
+}
+
+//===----------------------------------------------------------------------===//
+// BTFCtx Manipulation
+//===----------------------------------------------------------------------===//
+void MCContext::setBTFContext(std::unique_ptr<MCBTFContext> Ctx) {
+ BTFCtx = std::move(Ctx);
}
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/MC/MCDwarf2BTF.cpp b/llvm/lib/MC/MCDwarf2BTF.cpp
new file mode 100644
index 00000000000..08a70e6f318
--- /dev/null
+++ b/llvm/lib/MC/MCDwarf2BTF.cpp
@@ -0,0 +1,99 @@
+//===- MCDwarf2BTF.cpp ---------------------------------------- *- C++ --*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCDwarf2BTF.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCObjectStreamer.h"
+#include "llvm/MC/MCSection.h"
+#include "llvm/MC/MCSectionELF.h"
+#include "llvm/MC/MCBTFContext.h"
+#include "llvm/Support/Endian.h"
+#include "llvm/Support/EndianStream.h"
+#include <fstream>
+
+using namespace llvm;
+
+void MCDwarf2BTF::addFiles(MCObjectStreamer *MCOS, std::string &FileName,
+ std::vector<FileContent> &Files) {
+ std::vector<std::string> Content;
+
+ std::ifstream Inputfile(FileName);
+ std::string Line;
+ Content.push_back(Line); // line 0 for empty string
+ while (std::getline(Inputfile, Line))
+ Content.push_back(Line);
+
+ Files.push_back(FileContent(FileName, Content));
+}
+
+void MCDwarf2BTF::addLines(MCObjectStreamer *MCOS, StringRef &SectionName,
+ std::vector<FileContent> &Files,
+ const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
+ MCContext &Context = MCOS->getContext();
+ auto &BTFCxt = Context.getBTFContext();
+
+ unsigned SecNameOff = BTFCxt->addString(SectionName.str());
+ for (const MCDwarfLineEntry &LineEntry : LineEntries) {
+ BTFLineInfo LineInfo;
+ unsigned FileNum = LineEntry.getFileNum();
+ unsigned Line = LineEntry.getLine();
+
+ LineInfo.Label = LineEntry.getLabel();
+ if (FileNum < Files.size()) {
+ LineInfo.FileNameOff = BTFCxt->addString(Files[FileNum].first);
+ if (Line < Files[FileNum].second.size())
+ LineInfo.LineOff = BTFCxt->addString(Files[FileNum].second[Line]);
+ else
+ LineInfo.LineOff = 0;
+ } else {
+ LineInfo.FileNameOff = 0;
+ LineInfo.LineOff = 0;
+ }
+ LineInfo.LineNum = Line;
+ LineInfo.ColumnNum = LineEntry.getColumn();
+ BTFCxt->addLineInfo(SecNameOff, LineInfo);
+ }
+}
+
+void MCDwarf2BTF::addDwarfLineInfo(MCObjectStreamer *MCOS) {
+ MCContext &Context = MCOS->getContext();
+
+ auto &LineTables = Context.getMCDwarfLineTables();
+ if (LineTables.empty())
+ return;
+
+ for (const auto &CUIDTablePair : LineTables) {
+ std::vector<std::string> Dirs;
+ std::vector<FileContent> Files;
+
+ for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
+ Dirs.push_back(Dir);
+ for (auto &File : CUIDTablePair.second.getMCDwarfFiles()) {
+ std::string FileName;
+ if (File.DirIndex == 0)
+ FileName = File.Name;
+ else
+ FileName = Dirs[File.DirIndex - 1] + "/" + File.Name;
+ MCDwarf2BTF::addFiles(MCOS, FileName, Files);
+ }
+ for (const auto &LineSec: CUIDTablePair.second.getMCLineSections().getMCLineEntries()) {
+ MCSection *Section = LineSec.first;
+ const MCLineSection::MCDwarfLineEntryCollection &LineEntries = LineSec.second;
+
+ StringRef SectionName;
+ if (MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(Section))
+ SectionName = SectionELF->getSectionName();
+ else
+ return;
+ MCDwarf2BTF::addLines(MCOS, SectionName, Files, LineEntries);
+ }
+ }
+}
diff --git a/llvm/lib/MC/MCDwarf2BTF.h b/llvm/lib/MC/MCDwarf2BTF.h
new file mode 100644
index 00000000000..22d1b7741a5
--- /dev/null
+++ b/llvm/lib/MC/MCDwarf2BTF.h
@@ -0,0 +1,29 @@
+//===- MCDwarf2BTF.h ------------------------------------------ *- C++ --*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIB_MC_MCDWARF2BTF_H
+#define LLVM_LIB_MC_MCDWARF2BTF_H
+
+#include "llvm/MC/MCDwarf.h"
+
+namespace llvm {
+
+using FileContent = std::pair<std::string, std::vector<std::string>>;
+
+class MCDwarf2BTF {
+public:
+ static void addFiles(MCObjectStreamer *MCOS, std::string &FileName,
+ std::vector<FileContent> &Files);
+ static void addLines(MCObjectStreamer *MCOS, StringRef &SectionName,
+ std::vector<FileContent> &Files,
+ const MCLineSection::MCDwarfLineEntryCollection &LineEntries);
+ static void addDwarfLineInfo(MCObjectStreamer *MCOS);
+};
+
+}
+#endif
diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp
index edfccfcb9ed..bddcf459ac0 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -468,6 +468,9 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
+
+ BTFSection = Ctx->getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
+ BTFExtSection = Ctx->getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
}
void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 8c88db009bd..4f74f4101c8 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -14,6 +14,7 @@
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCCodeView.h"
#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCBTFContext.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCObjectWriter.h"
@@ -21,6 +22,7 @@
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SourceMgr.h"
+#include "MCDwarf2BTF.h"
using namespace llvm;
MCObjectStreamer::MCObjectStreamer(MCContext &Context,
@@ -439,6 +441,31 @@ void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
insert(new MCDwarfCallFrameFragment(*AddrDelta));
}
+void MCObjectStreamer::EmitBTFAdvanceLineAddr(const MCSymbol *Label,
+ unsigned Size) {
+ const MCExpr *Value = MCSymbolRefExpr::create(Label, getContext());
+ MCDataFragment *DF = getOrCreateDataFragment();
+
+ // Avoid fixups when possible.
+ int64_t AbsValue;
+ SMLoc Loc;
+
+ if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
+ if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
+ getContext().reportError(
+ Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
+ return;
+ }
+ EmitIntValue(AbsValue, Size);
+ return;
+ }
+
+ DF->getFixups().push_back(
+ MCFixup::create(DF->getContents().size(), Value,
+ MCFixup::getKindForSize(Size, false), Loc));
+ DF->getContents().resize(DF->getContents().size() + Size, 0);
+}
+
void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
unsigned Line, unsigned Column,
bool PrologueEnd, bool IsStmt,
@@ -688,6 +715,13 @@ void MCObjectStreamer::FinishImpl() {
// Dump out the dwarf file & directory tables and line tables.
MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
+ auto &BTFCtx = getContext().getBTFContext();
+ if (BTFCtx) {
+ MCDwarf2BTF::addDwarfLineInfo(this);
+ BTFCtx->emitAll(this);
+ BTFCtx.reset();
+ }
+
flushPendingLabels();
getAssembler().Finish();
}
OpenPOWER on IntegriCloud