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