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" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 22 | #include "llvm/Target/ELFTargetAsmInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 28 | ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) |
| 29 | : TargetAsmInfo(TM) { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 30 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 31 | BSSSection_ = getUnnamedSection("\t.bss", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 32 | SectionFlags::Writable | SectionFlags::BSS); |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 33 | ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None); |
Anton Korobeynikov | 9a213e4 | 2008-09-24 22:17:06 +0000 | [diff] [blame] | 34 | TLSDataSection = getNamedSection("\t.tdata", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 35 | SectionFlags::Writable | SectionFlags::TLS); |
Anton Korobeynikov | 9a213e4 | 2008-09-24 22:17:06 +0000 | [diff] [blame] | 36 | TLSBSSSection = getNamedSection("\t.tbss", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 37 | SectionFlags::Writable | SectionFlags::TLS | SectionFlags::BSS); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 39 | DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writable); |
Anton Korobeynikov | 8a4fbe4 | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 40 | DataRelLocalSection = getNamedSection("\t.data.rel.local", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 41 | SectionFlags::Writable); |
Anton Korobeynikov | 8a4fbe4 | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 42 | DataRelROSection = getNamedSection("\t.data.rel.ro", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 43 | SectionFlags::Writable); |
Anton Korobeynikov | 8a4fbe4 | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 44 | DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local", |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 45 | SectionFlags::Writable); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Anton Korobeynikov | b3882b6 | 2009-03-30 15:27:43 +0000 | [diff] [blame] | 48 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 49 | const Section* |
Chris Lattner | bb0c9bf | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 50 | ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, |
| 51 | SectionKind::Kind Kind) const { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 52 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 53 | switch (F->getLinkage()) { |
Chris Lattner | e09c7d7 | 2009-07-24 04:52:38 +0000 | [diff] [blame] | 54 | default: llvm_unreachable("Unknown linkage type!"); |
| 55 | case Function::PrivateLinkage: |
| 56 | case Function::LinkerPrivateLinkage: |
| 57 | case Function::InternalLinkage: |
| 58 | case Function::DLLExportLinkage: |
| 59 | case Function::ExternalLinkage: |
| 60 | return TextSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | a06e19b | 2009-07-24 05:01:55 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | const GlobalVariable *GVar = cast<GlobalVariable>(GV); |
| 65 | switch (Kind) { |
| 66 | default: llvm_unreachable("Unsuported section kind for global"); |
Chris Lattner | d398a8d | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 67 | case SectionKind::BSS: |
| 68 | return getBSSSection_(); |
Chris Lattner | a06e19b | 2009-07-24 05:01:55 +0000 | [diff] [blame] | 69 | case SectionKind::Data: |
| 70 | case SectionKind::DataRel: |
| 71 | return DataRelSection; |
| 72 | case SectionKind::DataRelLocal: |
| 73 | return DataRelLocalSection; |
| 74 | case SectionKind::DataRelRO: |
| 75 | return DataRelROSection; |
| 76 | case SectionKind::DataRelROLocal: |
| 77 | return DataRelROLocalSection; |
Chris Lattner | a06e19b | 2009-07-24 05:01:55 +0000 | [diff] [blame] | 78 | case SectionKind::ROData: |
| 79 | return getReadOnlySection(); |
| 80 | case SectionKind::RODataMergeStr: |
| 81 | return MergeableStringSection(GVar); |
| 82 | case SectionKind::RODataMergeConst: { |
| 83 | const Type *Ty = GVar->getInitializer()->getType(); |
| 84 | const TargetData *TD = TM.getTargetData(); |
| 85 | return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0); |
| 86 | } |
| 87 | case SectionKind::ThreadData: |
| 88 | // ELF targets usually support TLS stuff |
| 89 | return TLSDataSection; |
| 90 | case SectionKind::ThreadBSS: |
| 91 | return TLSBSSSection; |
Chris Lattner | e09c7d7 | 2009-07-24 04:52:38 +0000 | [diff] [blame] | 92 | } |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 95 | /// getSectionForMergableConstant - Given a mergable constant with the |
| 96 | /// specified size and relocation information, return a section that it |
| 97 | /// should be placed in. |
| 98 | const Section * |
| 99 | ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size, |
| 100 | unsigned ReloInfo) const { |
| 101 | // FIXME: IF this global requires a relocation, can we really put it in |
| 102 | // rodata??? This should check ReloInfo like darwin. |
| 103 | |
| 104 | const char *SecName = 0; |
| 105 | switch (Size) { |
| 106 | default: break; |
| 107 | case 4: SecName = ".rodata.cst4"; break; |
| 108 | case 8: SecName = ".rodata.cst8"; break; |
| 109 | case 16: SecName = ".rodata.cst16"; break; |
| 110 | } |
| 111 | |
| 112 | if (SecName) |
| 113 | return getNamedSection(SecName, |
Duncan Sands | 5bc722e | 2009-07-22 10:35:05 +0000 | [diff] [blame] | 114 | SectionFlags::setEntitySize(SectionFlags::Mergeable, |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 115 | Size)); |
| 116 | |
| 117 | return getReadOnlySection(); // .rodata |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 120 | /// getFlagsForNamedSection - If this target wants to be able to infer |
| 121 | /// section flags based on the name of the section specified for a global |
| 122 | /// variable, it can implement this. |
| 123 | unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const { |
| 124 | unsigned Flags = 0; |
| 125 | if (Name[0] != '.') return 0; |
| 126 | |
| 127 | // Some lame default implementation based on some magic section names. |
| 128 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 129 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 130 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 131 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
| 132 | Flags |= SectionFlags::BSS; |
| 133 | else if (strcmp(Name, ".tdata") == 0 || |
| 134 | strncmp(Name, ".tdata.", 7) == 0 || |
| 135 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 136 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 137 | Flags |= SectionFlags::TLS; |
| 138 | else if (strcmp(Name, ".tbss") == 0 || |
| 139 | strncmp(Name, ".tbss.", 6) == 0 || |
| 140 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 141 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 142 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
| 143 | |
| 144 | return Flags; |
| 145 | } |
| 146 | |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 148 | |
| 149 | const char * |
| 150 | ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind::Kind Kind) const{ |
| 151 | switch (Kind) { |
| 152 | default: llvm_unreachable("Unknown section kind"); |
| 153 | case SectionKind::Text: return ".gnu.linkonce.t."; |
| 154 | case SectionKind::Data: return ".gnu.linkonce.d."; |
| 155 | case SectionKind::DataRel: return ".gnu.linkonce.d.rel."; |
| 156 | case SectionKind::DataRelLocal: return ".gnu.linkonce.d.rel.local."; |
| 157 | case SectionKind::DataRelRO: return ".gnu.linkonce.d.rel.ro."; |
| 158 | case SectionKind::DataRelROLocal: return ".gnu.linkonce.d.rel.ro.local."; |
| 159 | case SectionKind::BSS: return ".gnu.linkonce.b."; |
| 160 | case SectionKind::ROData: |
| 161 | case SectionKind::RODataMergeConst: |
| 162 | case SectionKind::RODataMergeStr: return ".gnu.linkonce.r."; |
| 163 | case SectionKind::ThreadData: return ".gnu.linkonce.td."; |
| 164 | case SectionKind::ThreadBSS: return ".gnu.linkonce.tb."; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 170 | const Section* |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 171 | ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 172 | const TargetData *TD = TM.getTargetData(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 173 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
asl | 4eeee01 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 174 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 175 | |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 176 | unsigned Size = TD->getTypeAllocSize(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 177 | if (Size <= 16) { |
Anton Korobeynikov | fb4bf86 | 2008-08-07 09:54:40 +0000 | [diff] [blame] | 178 | assert(getCStringSection() && "Should have string section prefix"); |
| 179 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 180 | // We also need alignment here |
Anton Korobeynikov | 6fbf6d5 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 181 | unsigned Align = TD->getPrefTypeAlignment(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 182 | if (Align < Size) |
| 183 | Align = Size; |
| 184 | |
| 185 | std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align); |
| 186 | unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable | |
| 187 | SectionFlags::Strings, |
| 188 | Size); |
| 189 | return getNamedSection(Name.c_str(), Flags); |
| 190 | } |
| 191 | |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 192 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 193 | } |
| 194 | |
asl | 27ffd26 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 195 | std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 196 | std::string Flags = ",\""; |
| 197 | |
| 198 | if (!(flags & SectionFlags::Debug)) |
| 199 | Flags += 'a'; |
| 200 | if (flags & SectionFlags::Code) |
| 201 | Flags += 'x'; |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame^] | 202 | if (flags & SectionFlags::Writable) |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 203 | Flags += 'w'; |
| 204 | if (flags & SectionFlags::Mergeable) |
| 205 | Flags += 'M'; |
| 206 | if (flags & SectionFlags::Strings) |
| 207 | Flags += 'S'; |
| 208 | if (flags & SectionFlags::TLS) |
| 209 | Flags += 'T'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 210 | |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 211 | Flags += "\","; |
| 212 | |
| 213 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 214 | if (strcmp(CommentString, "@") == 0) |
| 215 | Flags += '%'; |
| 216 | else |
| 217 | Flags += '@'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 218 | |
| 219 | // FIXME: There can be exceptions here |
| 220 | if (flags & SectionFlags::BSS) |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 221 | Flags += "nobits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 222 | else |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 223 | Flags += "progbits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 224 | |
| 225 | if (unsigned entitySize = SectionFlags::getEntitySize(flags)) |
| 226 | Flags += "," + utostr(entitySize); |
| 227 | |
| 228 | return Flags; |
| 229 | } |