Anton Korobeynikov | debe34b | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 1 | //===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===// |
| 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 | // This file defines target asm properties related what form asm statements |
| 11 | // should take in general on ELF-based targets |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/GlobalVariable.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/Target/ELFTargetAsmInfo.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) { |
| 27 | ETM = &TM; |
| 28 | |
| 29 | TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code); |
| 30 | DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable); |
| 31 | BSSSection_ = getUnnamedSection("\t.bss", |
| 32 | SectionFlags::Writeable | SectionFlags::BSS); |
| 33 | ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None); |
| 34 | TLSDataSection_ = getNamedSection("\t.tdata", |
| 35 | SectionFlags::Writeable | SectionFlags::TLS); |
| 36 | TLSBSSSection_ = getNamedSection("\t.tbss", |
| 37 | SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | const Section* |
| 42 | ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
| 43 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 44 | |
| 45 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 46 | switch (F->getLinkage()) { |
| 47 | default: assert(0 && "Unknown linkage type!"); |
| 48 | case Function::InternalLinkage: |
| 49 | case Function::DLLExportLinkage: |
| 50 | case Function::ExternalLinkage: |
| 51 | return getTextSection_(); |
| 52 | case Function::WeakLinkage: |
| 53 | case Function::LinkOnceLinkage: |
| 54 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 55 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 56 | return getNamedSection(Name.c_str(), Flags); |
| 57 | } |
| 58 | } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
| 59 | if (GVar->isWeakForLinker()) { |
| 60 | std::string Name = UniqueSectionForGlobal(GVar, Kind); |
| 61 | unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str()); |
| 62 | return getNamedSection(Name.c_str(), Flags); |
| 63 | } else { |
| 64 | switch (Kind) { |
| 65 | case SectionKind::Data: |
| 66 | return getDataSection_(); |
| 67 | case SectionKind::BSS: |
| 68 | // ELF targets usually have BSS sections |
| 69 | return getBSSSection_(); |
| 70 | case SectionKind::ROData: |
| 71 | return getReadOnlySection_(); |
| 72 | case SectionKind::RODataMergeStr: |
| 73 | return MergeableStringSection(GVar); |
| 74 | case SectionKind::RODataMergeConst: |
| 75 | return MergeableConstSection(GVar); |
| 76 | case SectionKind::ThreadData: |
| 77 | // ELF targets usually support TLS stuff |
| 78 | return getTLSDataSection_(); |
| 79 | case SectionKind::ThreadBSS: |
| 80 | return getTLSBSSSection_(); |
| 81 | default: |
| 82 | assert(0 && "Unsuported section kind for global"); |
| 83 | } |
| 84 | } |
| 85 | } else |
| 86 | assert(0 && "Unsupported global"); |
| 87 | } |
| 88 | |
| 89 | const Section* |
| 90 | ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
| 91 | const TargetData *TD = ETM->getTargetData(); |
| 92 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 93 | const Type *Type = C->getType(); |
| 94 | |
| 95 | // FIXME: string here is temporary, until stuff will fully land in. |
| 96 | // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's |
| 97 | // currently directly used by asmprinter. |
| 98 | unsigned Size = TD->getABITypeSize(Type); |
| 99 | if (Size == 4 || Size == 8 || Size == 16) { |
| 100 | std::string Name = ".rodata.cst" + utostr(Size); |
| 101 | |
| 102 | return getNamedSection(Name.c_str(), |
| 103 | SectionFlags::setEntitySize(SectionFlags::Mergeable, |
| 104 | Size)); |
| 105 | } |
| 106 | |
| 107 | return getReadOnlySection_(); |
| 108 | } |
| 109 | |
| 110 | const Section* |
| 111 | ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
| 112 | const TargetData *TD = ETM->getTargetData(); |
| 113 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 114 | const ConstantArray *CVA = cast<ConstantArray>(C); |
Anton Korobeynikov | 9685506 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 115 | const Type *Ty = CVA->getType()->getElementType(); |
Anton Korobeynikov | debe34b | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 116 | |
Anton Korobeynikov | 9685506 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 117 | unsigned Size = TD->getABITypeSize(Ty); |
Anton Korobeynikov | debe34b | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 118 | if (Size <= 16) { |
| 119 | // We also need alignment here |
| 120 | const TargetData *TD = ETM->getTargetData(); |
Anton Korobeynikov | 9685506 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 121 | unsigned Align = TD->getPrefTypeAlignment(Ty); |
Anton Korobeynikov | debe34b | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 122 | if (Align < Size) |
| 123 | Align = Size; |
| 124 | |
| 125 | std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align); |
| 126 | unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable | |
| 127 | SectionFlags::Strings, |
| 128 | Size); |
| 129 | return getNamedSection(Name.c_str(), Flags); |
| 130 | } |
| 131 | |
| 132 | return getReadOnlySection_(); |
| 133 | } |
| 134 | |
| 135 | std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const { |
| 136 | std::string Flags = ",\""; |
| 137 | |
| 138 | if (!(flags & SectionFlags::Debug)) |
| 139 | Flags += 'a'; |
| 140 | if (flags & SectionFlags::Code) |
| 141 | Flags += 'x'; |
| 142 | if (flags & SectionFlags::Writeable) |
| 143 | Flags += 'w'; |
| 144 | if (flags & SectionFlags::Mergeable) |
| 145 | Flags += 'M'; |
| 146 | if (flags & SectionFlags::Strings) |
| 147 | Flags += 'S'; |
| 148 | if (flags & SectionFlags::TLS) |
| 149 | Flags += 'T'; |
| 150 | |
| 151 | Flags += "\""; |
| 152 | |
| 153 | // FIXME: There can be exceptions here |
| 154 | if (flags & SectionFlags::BSS) |
| 155 | Flags += ",@nobits"; |
| 156 | else |
| 157 | Flags += ",@progbits"; |
| 158 | |
| 159 | if (unsigned entitySize = SectionFlags::getEntitySize(flags)) |
| 160 | Flags += "," + utostr(entitySize); |
| 161 | |
| 162 | return Flags; |
| 163 | } |
| 164 | |