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