1 //===-- llvm/Target/ARMTargetObjectFile.cpp - ARM Object Info Impl --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ARMSubtarget.h"
11 #include "ARMTargetMachine.h"
12 #include "ARMTargetObjectFile.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/MC/MCTargetOptions.h"
18 #include "llvm/MC/SectionKind.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/ELF.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include <cassert>
23 
24 using namespace llvm;
25 using namespace dwarf;
26 
27 //===----------------------------------------------------------------------===//
28 //                               ELF Target
29 //===----------------------------------------------------------------------===//
30 
31 void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
32                                         const TargetMachine &TM) {
33   const ARMTargetMachine &ARM_TM = static_cast<const ARMTargetMachine &>(TM);
34   bool isAAPCS_ABI = ARM_TM.TargetABI == ARMTargetMachine::ARMABI::ARM_ABI_AAPCS;
35   genExecuteOnly = ARM_TM.getSubtargetImpl()->genExecuteOnly();
36 
37   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
38   InitializeELF(isAAPCS_ABI);
39 
40   if (isAAPCS_ABI) {
41     LSDASection = nullptr;
42   }
43 
44   AttributesSection =
45       getContext().getELFSection(".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
46 
47   // Make code section unreadable when in execute-only mode
48   if (genExecuteOnly) {
49     unsigned  Type = ELF::SHT_PROGBITS;
50     unsigned Flags = ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_ARM_PURECODE;
51     // Since we cannot modify flags for an existing section, we create a new
52     // section with the right flags, and use 0 as the unique ID for
53     // execute-only text
54     TextSection = Ctx.getELFSection(".text", Type, Flags, 0, "", 0U);
55   }
56 }
57 
58 const MCExpr *ARMElfTargetObjectFile::getTTypeGlobalReference(
59     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
60     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
61   if (TM.getMCAsmInfo()->getExceptionHandlingType() != ExceptionHandling::ARM)
62     return TargetLoweringObjectFileELF::getTTypeGlobalReference(
63         GV, Encoding, TM, MMI, Streamer);
64 
65   assert(Encoding == DW_EH_PE_absptr && "Can handle absptr encoding only");
66 
67   return MCSymbolRefExpr::create(TM.getSymbol(GV),
68                                  MCSymbolRefExpr::VK_ARM_TARGET2, getContext());
69 }
70 
71 const MCExpr *ARMElfTargetObjectFile::
72 getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
73   return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_ARM_TLSLDO,
74                                  getContext());
75 }
76 
77 MCSection *
78 ARMElfTargetObjectFile::getExplicitSectionGlobal(const GlobalObject *GO,
79                                                  SectionKind SK, const TargetMachine &TM) const {
80   // Set execute-only access for the explicit section
81   if (genExecuteOnly && SK.isText())
82     SK = SectionKind::getExecuteOnly();
83 
84   return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, SK, TM);
85 }
86 
87 MCSection *
88 ARMElfTargetObjectFile::SelectSectionForGlobal(const GlobalObject *GO,
89                                                SectionKind SK, const TargetMachine &TM) const {
90   // Place the global in the execute-only text section
91   if (genExecuteOnly && SK.isText())
92     SK = SectionKind::getExecuteOnly();
93 
94   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, SK, TM);
95 }
96