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