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