LLVM API Documentation

XCoreTargetObjectFile.cpp
Go to the documentation of this file.
00001 //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "XCoreTargetObjectFile.h"
00011 #include "XCoreSubtarget.h"
00012 #include "llvm/IR/DataLayout.h"
00013 #include "llvm/MC/MCContext.h"
00014 #include "llvm/MC/MCSectionELF.h"
00015 #include "llvm/Support/ELF.h"
00016 #include "llvm/Target/TargetMachine.h"
00017 
00018 using namespace llvm;
00019 
00020 
00021 void XCoreTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
00022   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
00023 
00024   BSSSection =
00025     Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
00026                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00027                       ELF::XCORE_SHF_DP_SECTION,
00028                       SectionKind::getBSS());
00029   BSSSectionLarge =
00030     Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
00031                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00032                       ELF::XCORE_SHF_DP_SECTION,
00033                       SectionKind::getBSS());
00034   DataSection =
00035     Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS,
00036                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00037                       ELF::XCORE_SHF_DP_SECTION,
00038                       SectionKind::getDataRel());
00039   DataSectionLarge =
00040     Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
00041                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00042                       ELF::XCORE_SHF_DP_SECTION,
00043                       SectionKind::getDataRel());
00044   DataRelROSection =
00045     Ctx.getELFSection(".dp.rodata", ELF::SHT_PROGBITS,
00046                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00047                       ELF::XCORE_SHF_DP_SECTION,
00048                       SectionKind::getReadOnlyWithRel());
00049   DataRelROSectionLarge =
00050     Ctx.getELFSection(".dp.rodata.large", ELF::SHT_PROGBITS,
00051                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
00052                       ELF::XCORE_SHF_DP_SECTION,
00053                       SectionKind::getReadOnlyWithRel());
00054   ReadOnlySection =
00055     Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
00056                       ELF::SHF_ALLOC |
00057                       ELF::XCORE_SHF_CP_SECTION,
00058                       SectionKind::getReadOnlyWithRel());
00059   ReadOnlySectionLarge =
00060     Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
00061                       ELF::SHF_ALLOC |
00062                       ELF::XCORE_SHF_CP_SECTION,
00063                       SectionKind::getReadOnlyWithRel());
00064   MergeableConst4Section = 
00065     Ctx.getELFSection(".cp.rodata.cst4", ELF::SHT_PROGBITS,
00066                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
00067                       ELF::XCORE_SHF_CP_SECTION,
00068                       SectionKind::getMergeableConst4());
00069   MergeableConst8Section = 
00070     Ctx.getELFSection(".cp.rodata.cst8", ELF::SHT_PROGBITS,
00071                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
00072                       ELF::XCORE_SHF_CP_SECTION,
00073                       SectionKind::getMergeableConst8());
00074   MergeableConst16Section = 
00075     Ctx.getELFSection(".cp.rodata.cst16", ELF::SHT_PROGBITS,
00076                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
00077                       ELF::XCORE_SHF_CP_SECTION,
00078                       SectionKind::getMergeableConst16());
00079   CStringSection =
00080     Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
00081                       ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS |
00082                       ELF::XCORE_SHF_CP_SECTION,
00083                       SectionKind::getReadOnlyWithRel());
00084   // TextSection       - see MObjectFileInfo.cpp
00085   // StaticCtorSection - see MObjectFileInfo.cpp
00086   // StaticDtorSection - see MObjectFileInfo.cpp
00087  }
00088 
00089 static unsigned getXCoreSectionType(SectionKind K) {
00090   if (K.isBSS())
00091     return ELF::SHT_NOBITS;
00092   return ELF::SHT_PROGBITS;
00093 }
00094 
00095 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
00096   unsigned Flags = 0;
00097 
00098   if (!K.isMetadata())
00099     Flags |= ELF::SHF_ALLOC;
00100 
00101   if (K.isText())
00102     Flags |= ELF::SHF_EXECINSTR;
00103   else if (IsCPRel)
00104     Flags |= ELF::XCORE_SHF_CP_SECTION;
00105   else
00106     Flags |= ELF::XCORE_SHF_DP_SECTION;
00107 
00108   if (K.isWriteable())
00109     Flags |= ELF::SHF_WRITE;
00110 
00111   if (K.isMergeableCString() || K.isMergeableConst4() ||
00112       K.isMergeableConst8() || K.isMergeableConst16())
00113     Flags |= ELF::SHF_MERGE;
00114 
00115   if (K.isMergeableCString())
00116     Flags |= ELF::SHF_STRINGS;
00117 
00118   return Flags;
00119 }
00120 
00121 const MCSection *
00122 XCoreTargetObjectFile::getExplicitSectionGlobal(const GlobalValue *GV,
00123                                                 SectionKind Kind, Mangler &Mang,
00124                                                 const TargetMachine &TM) const {
00125   StringRef SectionName = GV->getSection();
00126   // Infer section flags from the section name if we can.
00127   bool IsCPRel = SectionName.startswith(".cp.");
00128   if (IsCPRel && !Kind.isReadOnly())
00129     report_fatal_error("Using .cp. section for writeable object.");
00130   return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
00131                                     getXCoreSectionFlags(Kind, IsCPRel), Kind);
00132 }
00133 
00134 const MCSection *XCoreTargetObjectFile::
00135 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
00136                        const TargetMachine &TM) const{
00137 
00138   bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
00139 
00140   if (Kind.isText())                    return TextSection;
00141   if (UseCPRel) {
00142     if (Kind.isMergeable1ByteCString()) return CStringSection;
00143     if (Kind.isMergeableConst4())       return MergeableConst4Section;
00144     if (Kind.isMergeableConst8())       return MergeableConst8Section;
00145     if (Kind.isMergeableConst16())      return MergeableConst16Section;
00146   }
00147   Type *ObjType = GV->getType()->getPointerElementType();
00148   if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
00149       TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(ObjType) <
00150           CodeModelLargeSize) {
00151     if (Kind.isReadOnly())              return UseCPRel? ReadOnlySection
00152                                                        : DataRelROSection;
00153     if (Kind.isBSS() || Kind.isCommon())return BSSSection;
00154     if (Kind.isDataRel())               return DataSection;
00155     if (Kind.isReadOnlyWithRel())       return DataRelROSection;
00156   } else {
00157     if (Kind.isReadOnly())              return UseCPRel? ReadOnlySectionLarge
00158                                                        : DataRelROSectionLarge;
00159     if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
00160     if (Kind.isDataRel())               return DataSectionLarge;
00161     if (Kind.isReadOnlyWithRel())       return DataRelROSectionLarge;
00162   }
00163 
00164   assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
00165   report_fatal_error("Target does not support TLS or Common sections");
00166 }
00167 
00168 const MCSection *
00169 XCoreTargetObjectFile::getSectionForConstant(SectionKind Kind,
00170                                              const Constant *C) const {
00171   if (Kind.isMergeableConst4())           return MergeableConst4Section;
00172   if (Kind.isMergeableConst8())           return MergeableConst8Section;
00173   if (Kind.isMergeableConst16())          return MergeableConst16Section;
00174   assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
00175          "Unknown section kind");
00176   // We assume the size of the object is never greater than CodeModelLargeSize.
00177   // To handle CodeModelLargeSize changes to AsmPrinter would be required.
00178   return ReadOnlySection;
00179 }