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