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" |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 6463056 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 29 | ReadOnlySection = |
| 30 | getOrCreateSection("\t.rodata", false, SectionKind::ReadOnly); |
| 31 | TLSDataSection = |
| 32 | getOrCreateSection("\t.tdata", false, SectionKind::ThreadData); |
| 33 | TLSBSSSection = getOrCreateSection("\t.tbss", false, SectionKind::ThreadBSS); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 6463056 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 35 | DataRelSection = getOrCreateSection("\t.data.rel", false, |
| 36 | SectionKind::DataRel); |
| 37 | DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false, |
| 38 | SectionKind::DataRelLocal); |
| 39 | DataRelROSection = getOrCreateSection("\t.data.rel.ro", false, |
| 40 | SectionKind::ReadOnlyWithRel); |
| 41 | DataRelROLocalSection = |
| 42 | getOrCreateSection("\t.data.rel.ro.local", false, |
| 43 | SectionKind::ReadOnlyWithRelLocal); |
Chris Lattner | 5295484 | 2009-07-26 06:16:11 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 6463056 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 45 | MergeableConst4Section = getOrCreateSection(".rodata.cst4", false, |
| 46 | SectionKind::MergeableConst4); |
| 47 | MergeableConst8Section = getOrCreateSection(".rodata.cst8", false, |
| 48 | SectionKind::MergeableConst8); |
| 49 | MergeableConst16Section = getOrCreateSection(".rodata.cst16", false, |
| 50 | SectionKind::MergeableConst16); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Anton Korobeynikov | b3882b6 | 2009-03-30 15:27:43 +0000 | [diff] [blame] | 53 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 54 | const Section* |
Chris Lattner | bb0c9bf | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 55 | ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | cc19521 | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 56 | SectionKind Kind) const { |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 57 | if (Kind.isText()) return TextSection; |
Chris Lattner | 1c37924 | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 58 | if (Kind.isMergeableCString()) |
Chris Lattner | 3a2a55a | 2009-07-26 03:06:11 +0000 | [diff] [blame] | 59 | return MergeableStringSection(cast<GlobalVariable>(GV)); |
Chris Lattner | 2a7dd7d | 2009-07-26 06:11:33 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 1c37924 | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 61 | if (Kind.isMergeableConst()) { |
| 62 | if (Kind.isMergeableConst4()) |
| 63 | return MergeableConst4Section; |
| 64 | if (Kind.isMergeableConst8()) |
| 65 | return MergeableConst8Section; |
| 66 | if (Kind.isMergeableConst16()) |
| 67 | return MergeableConst16Section; |
Chris Lattner | 5295484 | 2009-07-26 06:16:11 +0000 | [diff] [blame] | 68 | return ReadOnlySection; // .const |
Chris Lattner | a06e19b | 2009-07-24 05:01:55 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 5295484 | 2009-07-26 06:16:11 +0000 | [diff] [blame] | 70 | |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 71 | if (Kind.isReadOnly()) return getReadOnlySection(); |
| 72 | |
| 73 | |
| 74 | if (Kind.isThreadData()) return TLSDataSection; |
| 75 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 76 | |
| 77 | if (Kind.isBSS()) return getBSSSection_(); |
| 78 | |
| 79 | |
| 80 | if (Kind.isDataNoRel()) return DataSection; |
| 81 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 82 | if (Kind.isDataRel()) return DataRelSection; |
| 83 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 84 | |
| 85 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 86 | return DataRelROSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | 1c37924 | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 89 | /// getSectionForMergeableConstant - Given a Mergeable constant with the |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 90 | /// specified size and relocation information, return a section that it |
| 91 | /// should be placed in. |
| 92 | const Section * |
Chris Lattner | 1c37924 | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 93 | ELFTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const { |
Chris Lattner | fc60ba1 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 94 | return SelectSectionForGlobal(0, Kind); |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 97 | /// getFlagsForNamedSection - If this target wants to be able to infer |
| 98 | /// section flags based on the name of the section specified for a global |
| 99 | /// variable, it can implement this. |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 100 | SectionKind::Kind ELFTargetAsmInfo::getKindForNamedSection(const char *Name, |
| 101 | SectionKind::Kind K) const { |
| 102 | if (Name[0] != '.') return K; |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 103 | |
| 104 | // Some lame default implementation based on some magic section names. |
| 105 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 106 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 107 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 108 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 109 | return SectionKind::BSS; |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 110 | |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 111 | if (strcmp(Name, ".tdata") == 0 || |
| 112 | strncmp(Name, ".tdata.", 7) == 0 || |
| 113 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 114 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 115 | return SectionKind::ThreadData; |
| 116 | |
| 117 | if (strcmp(Name, ".tbss") == 0 || |
| 118 | strncmp(Name, ".tbss.", 6) == 0 || |
| 119 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 120 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 121 | return SectionKind::ThreadBSS; |
| 122 | |
| 123 | return K; |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 126 | const char * |
Chris Lattner | cc19521 | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 127 | ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{ |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 128 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 129 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 130 | |
| 131 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 132 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 133 | |
| 134 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 135 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 136 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 137 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 138 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 139 | |
| 140 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 141 | return ".gnu.linkonce.d.rel.ro."; |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 146 | const Section* |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 147 | ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 148 | const TargetData *TD = TM.getTargetData(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 149 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
asl | 4eeee01 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 150 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 151 | |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 152 | unsigned Size = TD->getTypeAllocSize(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 153 | if (Size <= 16) { |
Anton Korobeynikov | fb4bf86 | 2008-08-07 09:54:40 +0000 | [diff] [blame] | 154 | assert(getCStringSection() && "Should have string section prefix"); |
| 155 | |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 156 | // We also need alignment here. |
| 157 | // FIXME: this is getting the alignment of the character, not the alignment |
| 158 | // of the string!! |
Anton Korobeynikov | 6fbf6d5 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 159 | unsigned Align = TD->getPrefTypeAlignment(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 160 | if (Align < Size) |
| 161 | Align = Size; |
| 162 | |
| 163 | std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align); |
Chris Lattner | 6463056 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 164 | return getOrCreateSection(Name.c_str(), false, |
| 165 | SectionKind::MergeableCString); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 168 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 171 | void ELFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind, |
| 172 | SmallVectorImpl<char> &Str) const { |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 173 | Str.push_back(','); |
| 174 | Str.push_back('"'); |
| 175 | |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 176 | if (!Kind.isMetadata()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 177 | Str.push_back('a'); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 178 | if (Kind.isText()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 179 | Str.push_back('x'); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 180 | if (Kind.isWriteable()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 181 | Str.push_back('w'); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 182 | if (Kind.isMergeableConst() || Kind.isMergeableCString()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 183 | Str.push_back('M'); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 184 | if (Kind.isMergeableCString()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 185 | Str.push_back('S'); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 186 | if (Kind.isThreadLocal()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 187 | Str.push_back('T'); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 189 | Str.push_back('"'); |
| 190 | Str.push_back(','); |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 191 | |
| 192 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 193 | if (strcmp(CommentString, "@") == 0) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 194 | Str.push_back('%'); |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 195 | else |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 196 | Str.push_back('@'); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 198 | const char *KindStr; |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 199 | if (Kind.isBSS()) |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 200 | KindStr = "nobits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 201 | else |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 202 | KindStr = "progbits"; |
| 203 | |
| 204 | Str.append(KindStr, KindStr+strlen(KindStr)); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 205 | |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 206 | if (Kind.isMergeableCString()) { |
| 207 | // TODO: Eventually handle multiple byte character strings. For now, all |
| 208 | // mergable C strings are single byte. |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 209 | Str.push_back(','); |
Chris Lattner | d831052 | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 210 | Str.push_back('1'); |
| 211 | } else if (Kind.isMergeableConst4()) { |
| 212 | Str.push_back(','); |
| 213 | Str.push_back('4'); |
| 214 | } else if (Kind.isMergeableConst8()) { |
| 215 | Str.push_back(','); |
| 216 | Str.push_back('8'); |
| 217 | } else if (Kind.isMergeableConst16()) { |
| 218 | Str.push_back(','); |
| 219 | Str.push_back('1'); |
| 220 | Str.push_back('6'); |
Chris Lattner | 94d8a76 | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 221 | } |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 222 | } |