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 = 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(Feature: ARM::FeatureExecuteOnly);
42
43 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
44 InitializeELF(UseInitArray_: 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(Section: ".text", Type, Flags, EntrySize: 0, Group: "", IsComdat: false, UniqueID: 0U, LinkedToSym: nullptr);
60 }
61}
62
63MCRegister ARMElfTargetObjectFile::getStaticBase() const { return ARM::R9; }
64
65const 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(Symbol: Sym, specifier: ARM::S_GOT_PREL, Ctx&: getContext());
71 const MCExpr *Off = MCConstantExpr::create(Value: FinalOffset, Ctx&: getContext());
72 return MCBinaryExpr::createAdd(LHS: Res, RHS: Off, Ctx&: getContext());
73}
74
75const MCExpr *ARMElfTargetObjectFile::
76getIndirectSymViaRWPI(const MCSymbol *Sym) const {
77 return MCSymbolRefExpr::create(Symbol: Sym, specifier: ARM::S_SBREL, Ctx&: getContext());
78}
79
80const 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(Symbol: TM.getSymbol(GV), specifier: ARM::S_TARGET2,
90 Ctx&: getContext());
91}
92
93const MCExpr *ARMElfTargetObjectFile::
94getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
95 return MCSymbolRefExpr::create(Symbol: Sym, specifier: ARM::S_TLSLDO, Ctx&: getContext());
96}
97
98static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind SK,
99 const TargetMachine &TM) {
100 if (const Function *F = dyn_cast<Function>(Val: GO))
101 if (TM.getSubtarget<ARMSubtarget>(F: *F).genExecuteOnly() && SK.isText())
102 return true;
103 return false;
104}
105
106MCSection *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, Kind: SK, TM);
113}
114
115MCSection *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, Kind: SK, TM);
122}
123