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