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, |
Chris Lattner | cc19521 | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 51 | SectionKind Kind) const { |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 52 | if (Kind.isText()) return TextSection; |
| 53 | if (Kind.isMergableCString()) |
Chris Lattner | 3a2a55a | 2009-07-26 03:06:11 +0000 | [diff] [blame] | 54 | return MergeableStringSection(cast<GlobalVariable>(GV)); |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 55 | if (Kind.isMergableConst()) { |
Chris Lattner | 3a2a55a | 2009-07-26 03:06:11 +0000 | [diff] [blame] | 56 | const Type *Ty = cast<GlobalVariable>(GV)->getInitializer()->getType(); |
Chris Lattner | a06e19b | 2009-07-24 05:01:55 +0000 | [diff] [blame] | 57 | const TargetData *TD = TM.getTargetData(); |
| 58 | return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0); |
| 59 | } |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 60 | if (Kind.isReadOnly()) return getReadOnlySection(); |
| 61 | |
| 62 | |
| 63 | if (Kind.isThreadData()) return TLSDataSection; |
| 64 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 65 | |
| 66 | if (Kind.isBSS()) return getBSSSection_(); |
| 67 | |
| 68 | |
| 69 | if (Kind.isDataNoRel()) return DataSection; |
| 70 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 71 | if (Kind.isDataRel()) return DataRelSection; |
| 72 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 73 | |
| 74 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 75 | return DataRelROSection; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 78 | /// getSectionForMergableConstant - Given a mergable constant with the |
| 79 | /// specified size and relocation information, return a section that it |
| 80 | /// should be placed in. |
| 81 | const Section * |
| 82 | ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size, |
| 83 | unsigned ReloInfo) const { |
Chris Lattner | 07203df | 2009-07-26 05:55:20 +0000 | [diff] [blame^] | 84 | // If this constant pool entry has relocations, stick it into a relocatable |
| 85 | // section. |
| 86 | if (ReloInfo == 2) |
| 87 | return DataRelROSection; |
| 88 | if (ReloInfo == 1) |
| 89 | return DataRelROLocalSection; |
| 90 | |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 91 | |
| 92 | const char *SecName = 0; |
| 93 | switch (Size) { |
| 94 | default: break; |
| 95 | case 4: SecName = ".rodata.cst4"; break; |
| 96 | case 8: SecName = ".rodata.cst8"; break; |
| 97 | case 16: SecName = ".rodata.cst16"; break; |
| 98 | } |
| 99 | |
| 100 | if (SecName) |
| 101 | return getNamedSection(SecName, |
Duncan Sands | 5bc722e | 2009-07-22 10:35:05 +0000 | [diff] [blame] | 102 | SectionFlags::setEntitySize(SectionFlags::Mergeable, |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 103 | Size)); |
| 104 | |
| 105 | return getReadOnlySection(); // .rodata |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | dfd1598 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 108 | /// getFlagsForNamedSection - If this target wants to be able to infer |
| 109 | /// section flags based on the name of the section specified for a global |
| 110 | /// variable, it can implement this. |
| 111 | unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const { |
| 112 | unsigned Flags = 0; |
| 113 | if (Name[0] != '.') return 0; |
| 114 | |
| 115 | // Some lame default implementation based on some magic section names. |
| 116 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 117 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 118 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 119 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
| 120 | Flags |= SectionFlags::BSS; |
| 121 | else if (strcmp(Name, ".tdata") == 0 || |
| 122 | strncmp(Name, ".tdata.", 7) == 0 || |
| 123 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 124 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 125 | Flags |= SectionFlags::TLS; |
| 126 | else if (strcmp(Name, ".tbss") == 0 || |
| 127 | strncmp(Name, ".tbss.", 6) == 0 || |
| 128 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 129 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 130 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
| 131 | |
| 132 | return Flags; |
| 133 | } |
| 134 | |
Chris Lattner | 680c6f6 | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 136 | |
| 137 | const char * |
Chris Lattner | cc19521 | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 138 | ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{ |
Chris Lattner | dc9d8cf | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 139 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 140 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 141 | |
| 142 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 143 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 144 | |
| 145 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 146 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 147 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 148 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 149 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 150 | |
| 151 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 152 | return ".gnu.linkonce.d.rel.ro."; |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | |
Anton Korobeynikov | fc54db1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 157 | const Section* |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 158 | ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | a004d7b | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 159 | const TargetData *TD = TM.getTargetData(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 160 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
asl | 4eeee01 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 161 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 162 | |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 163 | unsigned Size = TD->getTypeAllocSize(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 164 | if (Size <= 16) { |
Anton Korobeynikov | fb4bf86 | 2008-08-07 09:54:40 +0000 | [diff] [blame] | 165 | assert(getCStringSection() && "Should have string section prefix"); |
| 166 | |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 167 | // We also need alignment here |
Anton Korobeynikov | 6fbf6d5 | 2008-07-21 18:29:23 +0000 | [diff] [blame] | 168 | unsigned Align = TD->getPrefTypeAlignment(Ty); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 169 | if (Align < Size) |
| 170 | Align = Size; |
| 171 | |
| 172 | std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align); |
| 173 | unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable | |
| 174 | SectionFlags::Strings, |
| 175 | Size); |
| 176 | return getNamedSection(Name.c_str(), Flags); |
| 177 | } |
| 178 | |
Anton Korobeynikov | 4d43322 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 179 | return getReadOnlySection(); |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 180 | } |
| 181 | |
asl | 27ffd26 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 182 | std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const { |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 183 | std::string Flags = ",\""; |
| 184 | |
| 185 | if (!(flags & SectionFlags::Debug)) |
| 186 | Flags += 'a'; |
| 187 | if (flags & SectionFlags::Code) |
| 188 | Flags += 'x'; |
Chris Lattner | 4c530ee | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 189 | if (flags & SectionFlags::Writable) |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 190 | Flags += 'w'; |
| 191 | if (flags & SectionFlags::Mergeable) |
| 192 | Flags += 'M'; |
| 193 | if (flags & SectionFlags::Strings) |
| 194 | Flags += 'S'; |
| 195 | if (flags & SectionFlags::TLS) |
| 196 | Flags += 'T'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 197 | |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 198 | Flags += "\","; |
| 199 | |
| 200 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 201 | if (strcmp(CommentString, "@") == 0) |
| 202 | Flags += '%'; |
| 203 | else |
| 204 | Flags += '@'; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 205 | |
| 206 | // FIXME: There can be exceptions here |
| 207 | if (flags & SectionFlags::BSS) |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 208 | Flags += "nobits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 209 | else |
Anton Korobeynikov | 4183e11 | 2008-08-07 09:55:06 +0000 | [diff] [blame] | 210 | Flags += "progbits"; |
asl | e0d0f48 | 2008-07-19 13:14:11 +0000 | [diff] [blame] | 211 | |
| 212 | if (unsigned entitySize = SectionFlags::getEntitySize(flags)) |
| 213 | Flags += "," + utostr(entitySize); |
| 214 | |
| 215 | return Flags; |
| 216 | } |