xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaELFObjectWriter.cpp (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===-- XtensaMCObjectWriter.cpp - Xtensa ELF writer ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 // See https://llvm.org/LICENSE.txt for license information.
7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "MCTargetDesc/XtensaMCTargetDesc.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include <cassert>
21 #include <cstdint>
22 
23 using namespace llvm;
24 
25 namespace {
26 class XtensaObjectWriter : public MCELFObjectTargetWriter {
27 public:
28   XtensaObjectWriter(uint8_t OSABI);
29 
30   virtual ~XtensaObjectWriter();
31 
32 protected:
33   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34                         const MCFixup &Fixup, bool IsPCRel) const override;
35   bool needsRelocateWithSymbol(const MCSymbol &Sym,
36                                unsigned Type) const override;
37 };
38 } // namespace
39 
40 XtensaObjectWriter::XtensaObjectWriter(uint8_t OSABI)
41     : MCELFObjectTargetWriter(false, OSABI, ELF::EM_XTENSA,
42                               /*HasRelocationAddend=*/true) {}
43 
44 XtensaObjectWriter::~XtensaObjectWriter() {}
45 
46 unsigned XtensaObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
47                                           const MCFixup &Fixup,
48                                           bool IsPCRel) const {
49 
50   switch ((unsigned)Fixup.getKind()) {
51   case FK_Data_4:
52     return ELF::R_XTENSA_32;
53   default:
54     return ELF::R_XTENSA_SLOT0_OP;
55   }
56 }
57 
58 std::unique_ptr<MCObjectTargetWriter>
59 llvm::createXtensaObjectWriter(uint8_t OSABI, bool IsLittleEndian) {
60   return std::make_unique<XtensaObjectWriter>(OSABI);
61 }
62 
63 bool XtensaObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
64                                                  unsigned Type) const {
65   return false;
66 }
67