Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- BPFELFObjectWriter.cpp - BPF ELF Writer ---------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "MCTargetDesc/BPFMCTargetDesc.h"
10
#include "llvm/BinaryFormat/ELF.h"
11
#include "llvm/MC/MCELFObjectWriter.h"
12
#include "llvm/MC/MCFixup.h"
13
#include "llvm/MC/MCObjectWriter.h"
14
#include "llvm/MC/MCValue.h"
15
#include "llvm/Support/ErrorHandling.h"
16
#include <cstdint>
17
18
using namespace llvm;
19
20
namespace {
21
22
class BPFELFObjectWriter : public MCELFObjectTargetWriter {
23
public:
24
  BPFELFObjectWriter(uint8_t OSABI);
25
204
  ~BPFELFObjectWriter() override = default;
26
27
protected:
28
  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29
                        const MCFixup &Fixup, bool IsPCRel) const override;
30
};
31
32
} // end anonymous namespace
33
34
BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
35
    : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
36
209
                              /*HasRelocationAddend*/ false) {}
37
38
unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
39
                                          const MCFixup &Fixup,
40
195
                                          bool IsPCRel) const {
41
195
  // determine the type of the relocation
42
195
  switch ((unsigned)Fixup.getKind()) {
43
195
  default:
44
0
    llvm_unreachable("invalid fixup kind!");
45
195
  case FK_SecRel_8:
46
20
    return ELF::R_BPF_64_64;
47
195
  case FK_PCRel_4:
48
0
  case FK_SecRel_4:
49
0
    return ELF::R_BPF_64_32;
50
47
  case FK_Data_8:
51
47
    return ELF::R_BPF_64_64;
52
128
  case FK_Data_4:
53
128
    if (const MCSymbolRefExpr *A = Target.getSymA()) {
54
128
      const MCSymbol &Sym = A->getSymbol();
55
128
56
128
      if (Sym.isDefined()) {
57
128
        MCSection &Section = Sym.getSection();
58
128
        const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
59
128
        assert(SectionELF && "Null section for reloc symbol");
60
128
61
128
        unsigned Flags = SectionELF->getFlags();
62
128
63
128
        if (Sym.isTemporary()) {
64
109
          // .BTF.ext generates FK_Data_4 relocations for
65
109
          // insn offset by creating temporary labels.
66
109
          // The insn offset is within the code section and
67
109
          // already been fulfilled by applyFixup(). No
68
109
          // further relocation is needed.
69
109
          // The reloc symbol should be in text section.
70
109
          if ((Flags & ELF::SHF_ALLOC) && 
(Flags & ELF::SHF_EXECINSTR)32
)
71
32
            return ELF::R_BPF_NONE;
72
19
        } else {
73
19
          // .BTF generates FK_Data_4 relocations for variable
74
19
          // offset in DataSec kind. Similar to the above .BTF.ext
75
19
          // insn offset, no further relocation is needed.
76
19
          // The reloc symbol should be in data section.
77
19
          if ((Flags & ELF::SHF_ALLOC) && 
(Flags & ELF::SHF_WRITE)6
)
78
6
            return ELF::R_BPF_NONE;
79
90
        }
80
128
      }
81
128
    }
82
90
    return ELF::R_BPF_64_32;
83
195
  }
84
195
}
85
86
std::unique_ptr<MCObjectTargetWriter>
87
209
llvm::createBPFELFObjectWriter(uint8_t OSABI) {
88
209
  return llvm::make_unique<BPFELFObjectWriter>(OSABI);
89
209
}