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