asl | e0d0f48 | 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 | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
asl | e0d0f48 | 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 | |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 27 | ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) |
| 28 | : TargetAsmInfo(TM) { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 29 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 30 | BSSSection_ = getUnnamedSection("\t.bss", |
| 31 | SectionFlags::Writeable | SectionFlags::BSS); |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 32 | ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None); |
Anton Korobeynikov | 9a213e4 | 2008-09-24 22:17:06 +0000 | [diff] [blame] | 33 | TLSDataSection = getNamedSection("\t.tdata", |
| 34 | SectionFlags::Writeable | SectionFlags::TLS); |
| 35 | TLSBSSSection = getNamedSection("\t.tbss", |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 36 | SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS); |
| 37 | |
Anton Korobeynikov | 8a4fbe4 | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 38 | DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writeable); |
| 39 | DataRelLocalSection = getNamedSection("\t.data.rel.local", |
| 40 | SectionFlags::Writeable); |
| 41 | DataRelROSection = getNamedSection("\t.data.rel.ro", |
| 42 | SectionFlags::Writeable); |
| 43 | DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local", |
| 44 | SectionFlags::Writeable); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Anton Korobeynikov | b3882b6 | 2009-03-30 15:27:43 +0000 | [diff] [blame^] | 47 | SectionKind::Kind |
| 48 | ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const { |
| 49 | SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV); |
| 50 | |
| 51 | if (Kind != SectionKind::Data) |
| 52 | return Kind; |
| 53 | |
| 54 | // Decide, whether we need data.rel stuff |
| 55 | const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV); |
| 56 | if (GVar->hasInitializer()) { |
| 57 | Constant *C = GVar->getInitializer(); |
| 58 | bool isConstant = GVar->isConstant(); |
| 59 | unsigned Reloc = RelocBehaviour(); |
| 60 | if (Reloc != Reloc::None && C->ContainsRelocations(Reloc)) |
| 61 | return (C->ContainsRelocations(Reloc::Local) ? |
| 62 | (isConstant ? |
| 63 | SectionKind::DataRelROLocal : SectionKind::DataRelLocal) : |
| 64 | (isConstant ? |
| 65 | SectionKind::DataRelRO : SectionKind::DataRel)); |
| 66 | } |
| 67 | |
| 68 | return Kind; |
| 69 | } |
| 70 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 71 | const Section* |
Evan Cheng | eb774e1 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 72 | ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 73 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 74 | |
| 75 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 76 | switch (F->getLinkage()) { |
| 77 | default: assert(0 && "Unknown linkage type!"); |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 78 | case Function::PrivateLinkage: |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 79 | case Function::InternalLinkage: |
| 80 | case Function::DLLExportLinkage: |
| 81 | case Function::ExternalLinkage: |
Anton Korobeynikov | f760ffb | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 82 | return TextSection; |
Duncan Sands | 19d161f | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 83 | case Function::WeakAnyLinkage: |
| 84 | case Function::WeakODRLinkage: |
| 85 | case Function::LinkOnceAnyLinkage: |
| 86 | case Function::LinkOnceODRLinkage: |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 87 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 88 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 89 | return getNamedSection(Name.c_str(), Flags); |
| 90 | } |
| 91 | } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
Duncan Sands | 19d161f | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 92 | if (GVar->isWeakForLinker()) { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 93 | std::string Name = UniqueSectionForGlobal(GVar, Kind); |
| 94 | unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str()); |
| 95 | return getNamedSection(Name.c_str(), Flags); |
| 96 | } else { |
| 97 | switch (Kind) { |
| 98 | case SectionKind::Data: |
Anton Korobeynikov | 96e01f1 | 2008-07-22 17:09:41 +0000 | [diff] [blame] | 99 | case SectionKind::SmallData: |
Anton Korobeynikov | f760ffb | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 100 | return DataSection; |
Anton Korobeynikov | 8a4fbe4 | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 101 | case SectionKind::DataRel: |
| 102 | return DataRelSection; |
| 103 | case SectionKind::DataRelLocal: |
| 104 | return DataRelLocalSection; |
| 105 | case SectionKind::DataRelRO: |
| 106 | return DataRelROSection; |
| 107 | case SectionKind::DataRelROLocal: |
| 108 | return DataRelROLocalSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 109 | case SectionKind::BSS: |
Anton Korobeynikov | 96e01f1 | 2008-07-22 17:09:41 +0000 | [diff] [blame] | 110 | case SectionKind::SmallBSS: |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 111 | // ELF targets usually have BSS sections |
| 112 | return getBSSSection_(); |
| 113 | case SectionKind::ROData: |
Anton Korobeynikov | 96e01f1 | 2008-07-22 17:09:41 +0000 | [diff] [blame] | 114 | case SectionKind::SmallROData: |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 115 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 116 | case SectionKind::RODataMergeStr: |
| 117 | return MergeableStringSection(GVar); |
| 118 | case SectionKind::RODataMergeConst: |
| 119 | return MergeableConstSection(GVar); |
| 120 | case SectionKind::ThreadData: |
| 121 | // ELF targets usually support TLS stuff |
Anton Korobeynikov | 9a213e4 | 2008-09-24 22:17:06 +0000 | [diff] [blame] | 122 | return TLSDataSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 123 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | 9a213e4 | 2008-09-24 22:17:06 +0000 | [diff] [blame] | 124 | return TLSBSSSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 125 | default: |
| 126 | assert(0 && "Unsuported section kind for global"); |
| 127 | } |
| 128 | } |
| 129 | } else |
| 130 | assert(0 && "Unsupported global"); |
Devang Patel | f3707e8 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 131 | |
| 132 | return NULL; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const Section* |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 136 | ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 137 | // FIXME: Support data.rel stuff someday |
| 138 | return MergeableConstSection(Ty); |
| 139 | } |
| 140 | |
| 141 | const Section* |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 142 | ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | 08f10bc | 2008-09-24 22:17:27 +0000 | [diff] [blame] | 143 | Constant *C = GV->getInitializer(); |
Anton Korobeynikov | fee95e0 | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 144 | return MergeableConstSection(C->getType()); |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | inline const Section* |
| 148 | ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const { |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 149 | const TargetData *TD = TM.getTargetData(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 150 | |
| 151 | // FIXME: string here is temporary, until stuff will fully land in. |
| 152 | // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's |
| 153 | // currently directly used by asmprinter. |
Duncan Sands | d68f13b | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 154 | unsigned Size = TD->getTypePaddedSize(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 155 | if (Size == 4 || Size == 8 || Size == 16) { |
| 156 | std::string Name = ".rodata.cst" + utostr(Size); |
| 157 | |
| 158 | return getNamedSection(Name.c_str(), |
| 159 | SectionFlags::setEntitySize(SectionFlags::Mergeable, |
| 160 | Size)); |
| 161 | } |
| 162 | |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 163 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | const Section* |
| 167 | ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 168 | const TargetData *TD = TM.getTargetData(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 169 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
asl | 4eeee01 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 170 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 171 | |
Duncan Sands | d68f13b | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 172 | unsigned Size = TD->getTypePaddedSize(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 173 | if (Size <= 16) { |
Anton Korobeynikov | fb4bf86 | 2008-08-07 09:54:40 +0000 | [diff] [blame] | 174 | assert(getCStringSection() && "Should have string section prefix"); |
| 175 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 176 | // We also need alignment here |
Anton Korobeynikov | 6fbf6d5 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 177 | unsigned Align = TD->getPrefTypeAlignment(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 178 | if (Align < Size) |
| 179 | Align = Size; |
| 180 | |
| 181 | std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align); |
| 182 | unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable | |
| 183 | SectionFlags::Strings, |
| 184 | Size); |
| 185 | return getNamedSection(Name.c_str(), Flags); |
| 186 | } |
| 187 | |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 188 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 189 | } |
| 190 | |
asl | 27ffd26 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 191 | std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 192 | std::string Flags = ",\""; |
| 193 | |
| 194 | if (!(flags & SectionFlags::Debug)) |
| 195 | Flags += 'a'; |
| 196 | if (flags & SectionFlags::Code) |
| 197 | Flags += 'x'; |
| 198 | if (flags & SectionFlags::Writeable) |
| 199 | Flags += 'w'; |
| 200 | if (flags & SectionFlags::Mergeable) |
| 201 | Flags += 'M'; |
| 202 | if (flags & SectionFlags::Strings) |
| 203 | Flags += 'S'; |
| 204 | if (flags & SectionFlags::TLS) |
| 205 | Flags += 'T'; |
Anton Korobeynikov | 96e01f1 | 2008-07-22 17:09:41 +0000 | [diff] [blame] | 206 | if (flags & SectionFlags::Small) |
| 207 | Flags += 's'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 208 | |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 209 | Flags += "\","; |
| 210 | |
| 211 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 212 | if (strcmp(CommentString, "@") == 0) |
| 213 | Flags += '%'; |
| 214 | else |
| 215 | Flags += '@'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 216 | |
| 217 | // FIXME: There can be exceptions here |
| 218 | if (flags & SectionFlags::BSS) |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 219 | Flags += "nobits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 220 | else |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 221 | Flags += "progbits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 222 | |
| 223 | if (unsigned entitySize = SectionFlags::getEntitySize(flags)) |
| 224 | Flags += "," + utostr(entitySize); |
| 225 | |
| 226 | return Flags; |
| 227 | } |