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
24using namespace llvm;
25using namespace dwarf;
26
27//===----------------------------------------------------------------------===//
28// ELF Target
29//===----------------------------------------------------------------------===//
30
31ARMElfTargetObjectFile::ARMElfTargetObjectFile() {
32 PLTRelativeSpecifier = ARM::S_PREL31;
33 SupportIndirectSymViaGOTPCRel = true;
34}
35
36void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
37 const TargetMachine &TM) {
38 const ARMBaseTargetMachine &ARM_TM =
39 static_cast<const ARMBaseTargetMachine &>(TM);
40 bool genExecuteOnly =
41 ARM_TM.getMCSubtargetInfo().hasFeature(Feature: ARM::FeatureExecuteOnly);
42
43 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
44
45 // Make code section unreadable when in execute-only mode
46 if (genExecuteOnly) {
47 unsigned Type = ELF::SHT_PROGBITS;
48 unsigned Flags =
49 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_ARM_PURECODE;
50 // Since we cannot modify flags for an existing section, we create a new
51 // section with the right flags, and use 0 as the unique ID for
52 // execute-only text
53 TextSection =
54 Ctx.getELFSection(Section: ".text", Type, Flags, EntrySize: 0, Group: "", IsComdat: false, UniqueID: 0U, LinkedToSym: nullptr);
55 }
56}
57
58void ARMElfTargetObjectFile::getModuleMetadata(Module &M) {
59 TargetLoweringObjectFileELF::getModuleMetadata(M);
60
61 const auto &ARM_TM = static_cast<const ARMBaseTargetMachine &>(*TM);
62 bool isAAPCS_ABI = ARM_TM.getEffectiveABI(M) == ARM::ARMABI::ARM_ABI_AAPCS;
63 InitializeELF(UseInitArray_: isAAPCS_ABI);
64
65 if (isAAPCS_ABI)
66 LSDASection = nullptr;
67}
68
69MCRegister ARMElfTargetObjectFile::getStaticBase() const { return ARM::R9; }
70
71const MCExpr *ARMElfTargetObjectFile::getIndirectSymViaGOTPCRel(
72 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
73 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
74 int64_t FinalOffset = Offset + MV.getConstant();
75 const MCExpr *Res =
76 MCSymbolRefExpr::create(Symbol: Sym, specifier: ARM::S_GOT_PREL, Ctx&: getContext());
77 const MCExpr *Off = MCConstantExpr::create(Value: FinalOffset, Ctx&: getContext());
78 return MCBinaryExpr::createAdd(LHS: Res, RHS: Off, Ctx&: getContext());
79}
80
81const MCExpr *ARMElfTargetObjectFile::
82getIndirectSymViaRWPI(const MCSymbol *Sym) const {
83 return MCSymbolRefExpr::create(Symbol: Sym, specifier: ARM::S_SBREL, Ctx&: getContext());
84}
85
86const MCExpr *ARMElfTargetObjectFile::getTTypeGlobalReference(
87 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
88 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
89 if (TM.getMCAsmInfo().getExceptionHandlingType() != ExceptionHandling::ARM)
90 return TargetLoweringObjectFileELF::getTTypeGlobalReference(
91 GV, Encoding, TM, MMI, Streamer);
92
93 assert(Encoding == DW_EH_PE_absptr && "Can handle absptr encoding only");
94
95 return MCSymbolRefExpr::create(Symbol: TM.getSymbol(GV), specifier: ARM::S_TARGET2,
96 Ctx&: getContext());
97}
98
99const MCExpr *ARMElfTargetObjectFile::
100getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
101 return MCSymbolRefExpr::create(Symbol: Sym, specifier: ARM::S_TLSLDO, Ctx&: getContext());
102}
103
104static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind SK,
105 const TargetMachine &TM) {
106 if (const Function *F = dyn_cast<Function>(Val: GO))
107 if (TM.getSubtarget<ARMSubtarget>(F: *F).genExecuteOnly() && SK.isText())
108 return true;
109 return false;
110}
111
112MCSection *ARMElfTargetObjectFile::getExplicitSectionGlobal(
113 const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
114 // Set execute-only access for the explicit section
115 if (isExecuteOnlyFunction(GO, SK, TM))
116 SK = SectionKind::getExecuteOnly();
117
118 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind: SK, TM);
119}
120
121MCSection *ARMElfTargetObjectFile::SelectSectionForGlobal(
122 const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
123 // Place the global in the execute-only text section
124 if (isExecuteOnlyFunction(GO, SK, TM))
125 SK = SectionKind::getExecuteOnly();
126
127 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind: SK, TM);
128}
129