xref: /freebsd/contrib/llvm-project/llvm/lib/Target/ARM/ARMTargetObjectFile.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
1 //===-- llvm/Target/ARMTargetObjectFile.cpp - ARM Object Info Impl --------===//
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 "ARMTargetObjectFile.h"
10 #include "ARMSubtarget.h"
11 #include "ARMTargetMachine.h"
12 #include "MCTargetDesc/ARMMCAsmInfo.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/MC/SectionKind.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include <cassert>
23 
24 using namespace llvm;
25 using namespace dwarf;
26 
27 //===----------------------------------------------------------------------===//
28 //                               ELF Target
29 //===----------------------------------------------------------------------===//
30 
31 ARMElfTargetObjectFile::ARMElfTargetObjectFile() {
32   PLTRelativeSpecifier = ARM::S_PREL31;
33   SupportIndirectSymViaGOTPCRel = true;
34 }
35 
36 void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
37                                         const TargetMachine &TM) {
38   const ARMBaseTargetMachine &ARM_TM = static_cast<const ARMBaseTargetMachine &>(TM);
39   bool isAAPCS_ABI = ARM_TM.TargetABI == ARM::ARMABI::ARM_ABI_AAPCS;
40   bool genExecuteOnly =
41       ARM_TM.getMCSubtargetInfo()->hasFeature(ARM::FeatureExecuteOnly);
42 
43   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
44   InitializeELF(isAAPCS_ABI);
45 
46   if (isAAPCS_ABI) {
47     LSDASection = nullptr;
48   }
49 
50   // Make code section unreadable when in execute-only mode
51   if (genExecuteOnly) {
52     unsigned Type = ELF::SHT_PROGBITS;
53     unsigned Flags =
54         ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_ARM_PURECODE;
55     // Since we cannot modify flags for an existing section, we create a new
56     // section with the right flags, and use 0 as the unique ID for
57     // execute-only text
58     TextSection =
59         Ctx.getELFSection(".text", Type, Flags, 0, "", false, 0U, nullptr);
60   }
61 }
62 
63 MCRegister ARMElfTargetObjectFile::getStaticBase() const { return ARM::R9; }
64 
65 const MCExpr *ARMElfTargetObjectFile::getIndirectSymViaGOTPCRel(
66     const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
67     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
68   int64_t FinalOffset = Offset + MV.getConstant();
69   const MCExpr *Res =
70       MCSymbolRefExpr::create(Sym, ARM::S_GOT_PREL, getContext());
71   const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
72   return MCBinaryExpr::createAdd(Res, Off, getContext());
73 }
74 
75 const MCExpr *ARMElfTargetObjectFile::
76 getIndirectSymViaRWPI(const MCSymbol *Sym) const {
77   return MCSymbolRefExpr::create(Sym, ARM::S_SBREL, getContext());
78 }
79 
80 const MCExpr *ARMElfTargetObjectFile::getTTypeGlobalReference(
81     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
82     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
83   if (TM.getMCAsmInfo()->getExceptionHandlingType() != ExceptionHandling::ARM)
84     return TargetLoweringObjectFileELF::getTTypeGlobalReference(
85         GV, Encoding, TM, MMI, Streamer);
86 
87   assert(Encoding == DW_EH_PE_absptr && "Can handle absptr encoding only");
88 
89   return MCSymbolRefExpr::create(TM.getSymbol(GV), ARM::S_TARGET2,
90                                  getContext());
91 }
92 
93 const MCExpr *ARMElfTargetObjectFile::
94 getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
95   return MCSymbolRefExpr::create(Sym, ARM::S_TLSLDO, getContext());
96 }
97 
98 static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind SK,
99                                   const TargetMachine &TM) {
100   if (const Function *F = dyn_cast<Function>(GO))
101     if (TM.getSubtarget<ARMSubtarget>(*F).genExecuteOnly() && SK.isText())
102       return true;
103   return false;
104 }
105 
106 MCSection *ARMElfTargetObjectFile::getExplicitSectionGlobal(
107     const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
108   // Set execute-only access for the explicit section
109   if (isExecuteOnlyFunction(GO, SK, TM))
110     SK = SectionKind::getExecuteOnly();
111 
112   return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, SK, TM);
113 }
114 
115 MCSection *ARMElfTargetObjectFile::SelectSectionForGlobal(
116     const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
117   // Place the global in the execute-only text section
118   if (isExecuteOnlyFunction(GO, SK, TM))
119     SK = SectionKind::getExecuteOnly();
120 
121   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, SK, TM);
122 }
123