Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===// |
| 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 | |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCMachObjectWriter.h" |
| 11 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringMap.h" |
| 13 | #include "llvm/ADT/Twine.h" |
| 14 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCObjectWriter.h" |
| 18 | #include "llvm/MC/MCSectionMachO.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
Kevin Enderby | a6eeb6e | 2010-05-07 21:44:23 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCMachOSymbolFlags.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 22 | #include "llvm/Object/MachOFormat.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetAsmBackend.h" |
| 25 | |
| 26 | // FIXME: Gross. |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 27 | #include "../Target/ARM/ARMFixupKinds.h" |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 28 | #include "../Target/X86/X86FixupKinds.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | using namespace llvm; |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 32 | using namespace llvm::object; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 33 | |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 34 | // FIXME: this has been copied from (or to) X86AsmBackend.cpp |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 35 | static unsigned getFixupKindLog2Size(unsigned Kind) { |
| 36 | switch (Kind) { |
Daniel Dunbar | 5cc6390 | 2010-12-22 16:32:41 +0000 | [diff] [blame] | 37 | default: |
| 38 | llvm_unreachable("invalid fixup kind!"); |
Rafael Espindola | e04ed7e | 2010-11-28 14:17:56 +0000 | [diff] [blame] | 39 | case FK_PCRel_1: |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 40 | case FK_Data_1: return 0; |
Rafael Espindola | e04ed7e | 2010-11-28 14:17:56 +0000 | [diff] [blame] | 41 | case FK_PCRel_2: |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 42 | case FK_Data_2: return 1; |
Rafael Espindola | e04ed7e | 2010-11-28 14:17:56 +0000 | [diff] [blame] | 43 | case FK_PCRel_4: |
Daniel Dunbar | 5cc6390 | 2010-12-22 16:32:41 +0000 | [diff] [blame] | 44 | // FIXME: Remove these!!! |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 45 | case X86::reloc_riprel_4byte: |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 46 | case X86::reloc_riprel_4byte_movq_load: |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 47 | case X86::reloc_signed_4byte: |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 48 | case FK_Data_4: return 2; |
| 49 | case FK_Data_8: return 3; |
| 50 | } |
| 51 | } |
| 52 | |
Daniel Dunbar | e9460ec | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 53 | static bool doesSymbolRequireExternRelocation(MCSymbolData *SD) { |
| 54 | // Undefined symbols are always extern. |
| 55 | if (SD->Symbol->isUndefined()) |
| 56 | return true; |
| 57 | |
| 58 | // References to weak definitions require external relocation entries; the |
| 59 | // definition may not always be the one in the same object file. |
| 60 | if (SD->getFlags() & SF_WeakDefinition) |
| 61 | return true; |
| 62 | |
| 63 | // Otherwise, we can use an internal relocation. |
| 64 | return false; |
| 65 | } |
| 66 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 67 | static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, |
| 68 | const MCValue Target, |
| 69 | const MCSymbolData *BaseSymbol) { |
| 70 | // The effective fixup address is |
| 71 | // addr(atom(A)) + offset(A) |
| 72 | // - addr(atom(B)) - offset(B) |
| 73 | // - addr(BaseSymbol) + <fixup offset from base symbol> |
| 74 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 75 | // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0. |
| 76 | // |
| 77 | // Note that "false" is almost always conservatively correct (it means we emit |
| 78 | // a relocation which is unnecessary), except when it would force us to emit a |
| 79 | // relocation which the target cannot encode. |
| 80 | |
| 81 | const MCSymbolData *A_Base = 0, *B_Base = 0; |
| 82 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 83 | // Modified symbol references cannot be resolved. |
| 84 | if (A->getKind() != MCSymbolRefExpr::VK_None) |
| 85 | return false; |
| 86 | |
| 87 | A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); |
| 88 | if (!A_Base) |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 93 | // Modified symbol references cannot be resolved. |
| 94 | if (B->getKind() != MCSymbolRefExpr::VK_None) |
| 95 | return false; |
| 96 | |
| 97 | B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); |
| 98 | if (!B_Base) |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // If there is no base, A and B have to be the same atom for this fixup to be |
| 103 | // fully resolved. |
| 104 | if (!BaseSymbol) |
| 105 | return A_Base == B_Base; |
| 106 | |
| 107 | // Otherwise, B must be missing and A must be the base. |
| 108 | return !B_Base && BaseSymbol == A_Base; |
| 109 | } |
| 110 | |
| 111 | static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm, |
| 112 | const MCValue Target, |
| 113 | const MCSection *BaseSection) { |
| 114 | // The effective fixup address is |
| 115 | // addr(atom(A)) + offset(A) |
| 116 | // - addr(atom(B)) - offset(B) |
| 117 | // - addr(<base symbol>) + <fixup offset from base symbol> |
| 118 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 119 | // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0. |
| 120 | // |
| 121 | // The simple (Darwin, except on x86_64) way of dealing with this was to |
| 122 | // assume that any reference to a temporary symbol *must* be a temporary |
| 123 | // symbol in the same atom, unless the sections differ. Therefore, any PCrel |
| 124 | // relocation to a temporary symbol (in the same section) is fully |
| 125 | // resolved. This also works in conjunction with absolutized .set, which |
| 126 | // requires the compiler to use .set to absolutize the differences between |
| 127 | // symbols which the compiler knows to be assembly time constants, so we don't |
| 128 | // need to worry about considering symbol differences fully resolved. |
| 129 | |
| 130 | // Non-relative fixups are only resolved if constant. |
| 131 | if (!BaseSection) |
| 132 | return Target.isAbsolute(); |
| 133 | |
| 134 | // Otherwise, relative fixups are only resolved if not a difference and the |
| 135 | // target is a temporary in the same section. |
| 136 | if (Target.isAbsolute() || Target.getSymB()) |
| 137 | return false; |
| 138 | |
| 139 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 140 | if (!A->isTemporary() || !A->isInSection() || |
| 141 | &A->getSection() != BaseSection) |
| 142 | return false; |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 147 | namespace { |
| 148 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 149 | class MachObjectWriter : public MCObjectWriter { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 150 | /// MachSymbolData - Helper struct for containing some precomputed information |
| 151 | /// on symbols. |
| 152 | struct MachSymbolData { |
| 153 | MCSymbolData *SymbolData; |
| 154 | uint64_t StringIndex; |
| 155 | uint8_t SectionIndex; |
| 156 | |
| 157 | // Support lexicographic sorting. |
| 158 | bool operator<(const MachSymbolData &RHS) const { |
Benjamin Kramer | c37791e | 2010-05-20 14:14:22 +0000 | [diff] [blame] | 159 | return SymbolData->getSymbol().getName() < |
| 160 | RHS.SymbolData->getSymbol().getName(); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 164 | /// The target specific Mach-O writer instance. |
| 165 | llvm::OwningPtr<MCMachObjectTargetWriter> TargetObjectWriter; |
Daniel Dunbar | 7e06af8 | 2010-12-16 15:42:31 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 167 | /// @name Relocation Data |
| 168 | /// @{ |
| 169 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 170 | llvm::DenseMap<const MCSectionData*, |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 171 | std::vector<macho::RelocationEntry> > Relocations; |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 172 | llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 173 | |
| 174 | /// @} |
| 175 | /// @name Symbol Table Data |
| 176 | /// @{ |
| 177 | |
| 178 | SmallString<256> StringTable; |
| 179 | std::vector<MachSymbolData> LocalSymbolData; |
| 180 | std::vector<MachSymbolData> ExternalSymbolData; |
| 181 | std::vector<MachSymbolData> UndefinedSymbolData; |
| 182 | |
| 183 | /// @} |
| 184 | |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 185 | private: |
| 186 | /// @name Utility Methods |
| 187 | /// @{ |
| 188 | |
| 189 | bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { |
| 190 | const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo( |
| 191 | (MCFixupKind) Kind); |
| 192 | |
| 193 | return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 194 | } |
| 195 | |
| 196 | /// @} |
| 197 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 198 | SectionAddrMap SectionAddress; |
| 199 | uint64_t getSectionAddress(const MCSectionData* SD) const { |
| 200 | return SectionAddress.lookup(SD); |
| 201 | } |
| 202 | uint64_t getSymbolAddress(const MCSymbolData* SD, |
| 203 | const MCAsmLayout &Layout) const { |
| 204 | return getSectionAddress(SD->getFragment()->getParent()) + |
| 205 | Layout.getSymbolOffset(SD); |
| 206 | } |
| 207 | uint64_t getFragmentAddress(const MCFragment *Fragment, |
| 208 | const MCAsmLayout &Layout) const { |
| 209 | return getSectionAddress(Fragment->getParent()) + |
| 210 | Layout.getFragmentOffset(Fragment); |
| 211 | } |
| 212 | |
| 213 | uint64_t getPaddingSize(const MCSectionData *SD, |
| 214 | const MCAsmLayout &Layout) const { |
| 215 | uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD); |
| 216 | unsigned Next = SD->getLayoutOrder() + 1; |
| 217 | if (Next >= Layout.getSectionOrder().size()) |
| 218 | return 0; |
| 219 | |
| 220 | const MCSectionData &NextSD = *Layout.getSectionOrder()[Next]; |
| 221 | if (NextSD.getSection().isVirtualSection()) |
| 222 | return 0; |
| 223 | return OffsetToAlignment(EndAddr, NextSD.getAlignment()); |
| 224 | } |
| 225 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 226 | public: |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 227 | MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS, |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 228 | bool _IsLittleEndian) |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 229 | : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 232 | /// @name Target Writer Proxy Accessors |
| 233 | /// @{ |
| 234 | |
| 235 | bool is64Bit() const { return TargetObjectWriter->is64Bit(); } |
Daniel Dunbar | 532c456 | 2010-12-22 13:49:43 +0000 | [diff] [blame] | 236 | bool isARM() const { |
Daniel Dunbar | fdfbc6a | 2010-12-22 16:19:20 +0000 | [diff] [blame] | 237 | uint32_t CPUType = TargetObjectWriter->getCPUType() & ~mach::CTFM_ArchMask; |
Daniel Dunbar | 532c456 | 2010-12-22 13:49:43 +0000 | [diff] [blame] | 238 | return CPUType == mach::CTM_ARM; |
| 239 | } |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 240 | |
| 241 | /// @} |
| 242 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 243 | void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize, |
| 244 | bool SubsectionsViaSymbols) { |
| 245 | uint32_t Flags = 0; |
| 246 | |
| 247 | if (SubsectionsViaSymbols) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 248 | Flags |= macho::HF_SubsectionsViaSymbols; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 249 | |
| 250 | // struct mach_header (28 bytes) or |
| 251 | // struct mach_header_64 (32 bytes) |
| 252 | |
| 253 | uint64_t Start = OS.tell(); |
| 254 | (void) Start; |
| 255 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 256 | Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 257 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 258 | Write32(TargetObjectWriter->getCPUType()); |
| 259 | Write32(TargetObjectWriter->getCPUSubtype()); |
Jim Grosbach | c9d1439 | 2010-11-05 18:48:58 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 261 | Write32(macho::HFT_Object); |
Daniel Dunbar | 590956f | 2010-11-27 07:39:37 +0000 | [diff] [blame] | 262 | Write32(NumLoadCommands); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 263 | Write32(LoadCommandsSize); |
| 264 | Write32(Flags); |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 265 | if (is64Bit()) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 266 | Write32(0); // reserved |
| 267 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 268 | assert(OS.tell() - Start == is64Bit() ? |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 269 | macho::Header64Size : macho::Header32Size); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /// WriteSegmentLoadCommand - Write a segment load command. |
| 273 | /// |
| 274 | /// \arg NumSections - The number of sections in this segment. |
| 275 | /// \arg SectionDataSize - The total size of the sections. |
| 276 | void WriteSegmentLoadCommand(unsigned NumSections, |
| 277 | uint64_t VMSize, |
| 278 | uint64_t SectionDataStartOffset, |
| 279 | uint64_t SectionDataSize) { |
| 280 | // struct segment_command (56 bytes) or |
| 281 | // struct segment_command_64 (72 bytes) |
| 282 | |
| 283 | uint64_t Start = OS.tell(); |
| 284 | (void) Start; |
| 285 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 286 | unsigned SegmentLoadCommandSize = |
| 287 | is64Bit() ? macho::SegmentLoadCommand64Size: |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 288 | macho::SegmentLoadCommand32Size; |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 289 | Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 290 | Write32(SegmentLoadCommandSize + |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 291 | NumSections * (is64Bit() ? macho::Section64Size : |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 292 | macho::Section32Size)); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 293 | |
| 294 | WriteBytes("", 16); |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 295 | if (is64Bit()) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 296 | Write64(0); // vmaddr |
| 297 | Write64(VMSize); // vmsize |
| 298 | Write64(SectionDataStartOffset); // file offset |
| 299 | Write64(SectionDataSize); // file size |
| 300 | } else { |
| 301 | Write32(0); // vmaddr |
| 302 | Write32(VMSize); // vmsize |
| 303 | Write32(SectionDataStartOffset); // file offset |
| 304 | Write32(SectionDataSize); // file size |
| 305 | } |
| 306 | Write32(0x7); // maxprot |
| 307 | Write32(0x7); // initprot |
| 308 | Write32(NumSections); |
| 309 | Write32(0); // flags |
| 310 | |
| 311 | assert(OS.tell() - Start == SegmentLoadCommandSize); |
| 312 | } |
| 313 | |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 314 | void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 315 | const MCSectionData &SD, uint64_t FileOffset, |
| 316 | uint64_t RelocationsStart, unsigned NumRelocations) { |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 317 | uint64_t SectionSize = Layout.getSectionAddressSize(&SD); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 318 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 319 | // The offset is unused for virtual sections. |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 320 | if (SD.getSection().isVirtualSection()) { |
Daniel Dunbar | b026d64 | 2010-03-25 07:10:05 +0000 | [diff] [blame] | 321 | assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!"); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 322 | FileOffset = 0; |
| 323 | } |
| 324 | |
| 325 | // struct section (68 bytes) or |
| 326 | // struct section_64 (80 bytes) |
| 327 | |
| 328 | uint64_t Start = OS.tell(); |
| 329 | (void) Start; |
| 330 | |
Daniel Dunbar | 56279f4 | 2010-05-18 17:28:20 +0000 | [diff] [blame] | 331 | const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 332 | WriteBytes(Section.getSectionName(), 16); |
| 333 | WriteBytes(Section.getSegmentName(), 16); |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 334 | if (is64Bit()) { |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 335 | Write64(getSectionAddress(&SD)); // address |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 336 | Write64(SectionSize); // size |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 337 | } else { |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 338 | Write32(getSectionAddress(&SD)); // address |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 339 | Write32(SectionSize); // size |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 340 | } |
| 341 | Write32(FileOffset); |
| 342 | |
| 343 | unsigned Flags = Section.getTypeAndAttributes(); |
| 344 | if (SD.hasInstructions()) |
| 345 | Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS; |
| 346 | |
| 347 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 348 | Write32(Log2_32(SD.getAlignment())); |
| 349 | Write32(NumRelocations ? RelocationsStart : 0); |
| 350 | Write32(NumRelocations); |
| 351 | Write32(Flags); |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 352 | Write32(IndirectSymBase.lookup(&SD)); // reserved1 |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 353 | Write32(Section.getStubSize()); // reserved2 |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 354 | if (is64Bit()) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 355 | Write32(0); // reserved3 |
| 356 | |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 357 | assert(OS.tell() - Start == is64Bit() ? macho::Section64Size : |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 358 | macho::Section32Size); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, |
| 362 | uint32_t StringTableOffset, |
| 363 | uint32_t StringTableSize) { |
| 364 | // struct symtab_command (24 bytes) |
| 365 | |
| 366 | uint64_t Start = OS.tell(); |
| 367 | (void) Start; |
| 368 | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 369 | Write32(macho::LCT_Symtab); |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 370 | Write32(macho::SymtabLoadCommandSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 371 | Write32(SymbolOffset); |
| 372 | Write32(NumSymbols); |
| 373 | Write32(StringTableOffset); |
| 374 | Write32(StringTableSize); |
| 375 | |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 376 | assert(OS.tell() - Start == macho::SymtabLoadCommandSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
| 380 | uint32_t NumLocalSymbols, |
| 381 | uint32_t FirstExternalSymbol, |
| 382 | uint32_t NumExternalSymbols, |
| 383 | uint32_t FirstUndefinedSymbol, |
| 384 | uint32_t NumUndefinedSymbols, |
| 385 | uint32_t IndirectSymbolOffset, |
| 386 | uint32_t NumIndirectSymbols) { |
| 387 | // struct dysymtab_command (80 bytes) |
| 388 | |
| 389 | uint64_t Start = OS.tell(); |
| 390 | (void) Start; |
| 391 | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 392 | Write32(macho::LCT_Dysymtab); |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 393 | Write32(macho::DysymtabLoadCommandSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 394 | Write32(FirstLocalSymbol); |
| 395 | Write32(NumLocalSymbols); |
| 396 | Write32(FirstExternalSymbol); |
| 397 | Write32(NumExternalSymbols); |
| 398 | Write32(FirstUndefinedSymbol); |
| 399 | Write32(NumUndefinedSymbols); |
| 400 | Write32(0); // tocoff |
| 401 | Write32(0); // ntoc |
| 402 | Write32(0); // modtaboff |
| 403 | Write32(0); // nmodtab |
| 404 | Write32(0); // extrefsymoff |
| 405 | Write32(0); // nextrefsyms |
| 406 | Write32(IndirectSymbolOffset); |
| 407 | Write32(NumIndirectSymbols); |
| 408 | Write32(0); // extreloff |
| 409 | Write32(0); // nextrel |
| 410 | Write32(0); // locreloff |
| 411 | Write32(0); // nlocrel |
| 412 | |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 413 | assert(OS.tell() - Start == macho::DysymtabLoadCommandSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 416 | void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 417 | MCSymbolData &Data = *MSD.SymbolData; |
| 418 | const MCSymbol &Symbol = Data.getSymbol(); |
| 419 | uint8_t Type = 0; |
| 420 | uint16_t Flags = Data.getFlags(); |
| 421 | uint32_t Address = 0; |
| 422 | |
| 423 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 424 | // |
| 425 | // FIXME: Are the prebound or indirect fields possible here? |
| 426 | if (Symbol.isUndefined()) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 427 | Type = macho::STT_Undefined; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 428 | else if (Symbol.isAbsolute()) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 429 | Type = macho::STT_Absolute; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 430 | else |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 431 | Type = macho::STT_Section; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 432 | |
| 433 | // FIXME: Set STAB bits. |
| 434 | |
| 435 | if (Data.isPrivateExtern()) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 436 | Type |= macho::STF_PrivateExtern; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 437 | |
| 438 | // Set external bit. |
| 439 | if (Data.isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 440 | Type |= macho::STF_External; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 441 | |
| 442 | // Compute the symbol address. |
| 443 | if (Symbol.isDefined()) { |
| 444 | if (Symbol.isAbsolute()) { |
Daniel Dunbar | 2d7fd61 | 2010-05-05 19:01:05 +0000 | [diff] [blame] | 445 | Address = cast<MCConstantExpr>(Symbol.getVariableValue())->getValue(); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 446 | } else { |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 447 | Address = getSymbolAddress(&Data, Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 448 | } |
| 449 | } else if (Data.isCommon()) { |
| 450 | // Common symbols are encoded with the size in the address |
| 451 | // field, and their alignment in the flags. |
| 452 | Address = Data.getCommonSize(); |
| 453 | |
| 454 | // Common alignment is packed into the 'desc' bits. |
| 455 | if (unsigned Align = Data.getCommonAlignment()) { |
| 456 | unsigned Log2Size = Log2_32(Align); |
| 457 | assert((1U << Log2Size) == Align && "Invalid 'common' alignment!"); |
| 458 | if (Log2Size > 15) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 459 | report_fatal_error("invalid 'common' alignment '" + |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 460 | Twine(Align) + "'"); |
| 461 | // FIXME: Keep this mask with the SymbolFlags enumeration. |
| 462 | Flags = (Flags & 0xF0FF) | (Log2Size << 8); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // struct nlist (12 bytes) |
| 467 | |
| 468 | Write32(MSD.StringIndex); |
| 469 | Write8(Type); |
| 470 | Write8(MSD.SectionIndex); |
| 471 | |
| 472 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 473 | // value. |
| 474 | Write16(Flags); |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 475 | if (is64Bit()) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 476 | Write64(Address); |
| 477 | else |
| 478 | Write32(Address); |
| 479 | } |
| 480 | |
Daniel Dunbar | 35b0657 | 2010-03-22 23:16:43 +0000 | [diff] [blame] | 481 | // FIXME: We really need to improve the relocation validation. Basically, we |
| 482 | // want to implement a separate computation which evaluates the relocation |
| 483 | // entry as the linker would, and verifies that the resultant fixup value is |
| 484 | // exactly what the encoder wanted. This will catch several classes of |
| 485 | // problems: |
| 486 | // |
| 487 | // - Relocation entry bugs, the two algorithms are unlikely to have the same |
| 488 | // exact bug. |
| 489 | // |
| 490 | // - Relaxation issues, where we forget to relax something. |
| 491 | // |
| 492 | // - Input errors, where something cannot be correctly encoded. 'as' allows |
| 493 | // these through in many cases. |
| 494 | |
Daniel Dunbar | 7e06af8 | 2010-12-16 15:42:31 +0000 | [diff] [blame] | 495 | static bool isFixupKindRIPRel(unsigned Kind) { |
| 496 | return Kind == X86::reloc_riprel_4byte || |
| 497 | Kind == X86::reloc_riprel_4byte_movq_load; |
| 498 | } |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 499 | void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 500 | const MCFragment *Fragment, |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 501 | const MCFixup &Fixup, MCValue Target, |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 502 | uint64_t &FixedValue) { |
Daniel Dunbar | 7e06af8 | 2010-12-16 15:42:31 +0000 | [diff] [blame] | 503 | unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 504 | unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind()); |
| 505 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 506 | |
| 507 | // See <reloc.h>. |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 508 | uint32_t FixupOffset = |
| 509 | Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 510 | uint32_t FixupAddress = |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 511 | getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 512 | int64_t Value = 0; |
| 513 | unsigned Index = 0; |
| 514 | unsigned IsExtern = 0; |
| 515 | unsigned Type = 0; |
| 516 | |
| 517 | Value = Target.getConstant(); |
| 518 | |
| 519 | if (IsPCRel) { |
| 520 | // Compensate for the relocation offset, Darwin x86_64 relocations only |
| 521 | // have the addend and appear to have attempted to define it to be the |
| 522 | // actual expression addend without the PCrel bias. However, instructions |
| 523 | // with data following the relocation are not accomodated for (see comment |
| 524 | // below regarding SIGNED{1,2,4}), so it isn't exactly that either. |
Benjamin Kramer | 454c4ce | 2010-04-08 15:25:57 +0000 | [diff] [blame] | 525 | Value += 1LL << Log2Size; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (Target.isAbsolute()) { // constant |
| 529 | // SymbolNum of 0 indicates the absolute section. |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 530 | Type = macho::RIT_X86_64_Unsigned; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 531 | Index = 0; |
| 532 | |
| 533 | // FIXME: I believe this is broken, I don't think the linker can |
| 534 | // understand it. I think it would require a local relocation, but I'm not |
| 535 | // sure if that would work either. The official way to get an absolute |
| 536 | // PCrel relocation is to use an absolute symbol (which we don't support |
| 537 | // yet). |
| 538 | if (IsPCRel) { |
| 539 | IsExtern = 1; |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 540 | Type = macho::RIT_X86_64_Branch; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 541 | } |
| 542 | } else if (Target.getSymB()) { // A - B + constant |
| 543 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 544 | MCSymbolData &A_SD = Asm.getSymbolData(*A); |
Rafael Espindola | b814110 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 545 | const MCSymbolData *A_Base = Asm.getAtom(&A_SD); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 546 | |
| 547 | const MCSymbol *B = &Target.getSymB()->getSymbol(); |
| 548 | MCSymbolData &B_SD = Asm.getSymbolData(*B); |
Rafael Espindola | b814110 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 549 | const MCSymbolData *B_Base = Asm.getAtom(&B_SD); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 550 | |
| 551 | // Neither symbol can be modified. |
| 552 | if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || |
| 553 | Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 554 | report_fatal_error("unsupported relocation of modified symbol"); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 555 | |
| 556 | // We don't support PCrel relocations of differences. Darwin 'as' doesn't |
| 557 | // implement most of these correctly. |
| 558 | if (IsPCRel) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 559 | report_fatal_error("unsupported pc-relative relocation of difference"); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 560 | |
Kevin Enderby | 8c9aa92 | 2010-10-02 00:13:41 +0000 | [diff] [blame] | 561 | // The support for the situation where one or both of the symbols would |
| 562 | // require a local relocation is handled just like if the symbols were |
| 563 | // external. This is certainly used in the case of debug sections where |
| 564 | // the section has only temporary symbols and thus the symbols don't have |
| 565 | // base symbols. This is encoded using the section ordinal and |
| 566 | // non-extern relocation entries. |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 567 | |
| 568 | // Darwin 'as' doesn't emit correct relocations for this (it ends up with |
Kevin Enderby | 8c9aa92 | 2010-10-02 00:13:41 +0000 | [diff] [blame] | 569 | // a single SIGNED relocation); reject it for now. Except the case where |
| 570 | // both symbols don't have a base, equal but both NULL. |
| 571 | if (A_Base == B_Base && A_Base) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 572 | report_fatal_error("unsupported relocation with identical base"); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 573 | |
Rafael Espindola | f10d2be | 2010-12-07 01:09:54 +0000 | [diff] [blame] | 574 | Value += getSymbolAddress(&A_SD, Layout) - |
| 575 | (A_Base == NULL ? 0 : getSymbolAddress(A_Base, Layout)); |
| 576 | Value -= getSymbolAddress(&B_SD, Layout) - |
| 577 | (B_Base == NULL ? 0 : getSymbolAddress(B_Base, Layout)); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 578 | |
Kevin Enderby | 8c9aa92 | 2010-10-02 00:13:41 +0000 | [diff] [blame] | 579 | if (A_Base) { |
| 580 | Index = A_Base->getIndex(); |
| 581 | IsExtern = 1; |
| 582 | } |
| 583 | else { |
| 584 | Index = A_SD.getFragment()->getParent()->getOrdinal() + 1; |
| 585 | IsExtern = 0; |
| 586 | } |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 587 | Type = macho::RIT_X86_64_Unsigned; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 588 | |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 589 | macho::RelocationEntry MRE; |
Daniel Dunbar | 640e948 | 2010-05-11 23:53:07 +0000 | [diff] [blame] | 590 | MRE.Word0 = FixupOffset; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 591 | MRE.Word1 = ((Index << 0) | |
| 592 | (IsPCRel << 24) | |
| 593 | (Log2Size << 25) | |
| 594 | (IsExtern << 27) | |
| 595 | (Type << 28)); |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 596 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 597 | |
Kevin Enderby | 8c9aa92 | 2010-10-02 00:13:41 +0000 | [diff] [blame] | 598 | if (B_Base) { |
| 599 | Index = B_Base->getIndex(); |
| 600 | IsExtern = 1; |
| 601 | } |
| 602 | else { |
| 603 | Index = B_SD.getFragment()->getParent()->getOrdinal() + 1; |
| 604 | IsExtern = 0; |
| 605 | } |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 606 | Type = macho::RIT_X86_64_Subtractor; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 607 | } else { |
| 608 | const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); |
| 609 | MCSymbolData &SD = Asm.getSymbolData(*Symbol); |
Rafael Espindola | b814110 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 610 | const MCSymbolData *Base = Asm.getAtom(&SD); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 611 | |
Daniel Dunbar | ae7fb0b | 2010-05-05 17:22:39 +0000 | [diff] [blame] | 612 | // Relocations inside debug sections always use local relocations when |
| 613 | // possible. This seems to be done because the debugger doesn't fully |
| 614 | // understand x86_64 relocation entries, and expects to find values that |
| 615 | // have already been fixed up. |
Daniel Dunbar | 2d7fd61 | 2010-05-05 19:01:05 +0000 | [diff] [blame] | 616 | if (Symbol->isInSection()) { |
Daniel Dunbar | ae7fb0b | 2010-05-05 17:22:39 +0000 | [diff] [blame] | 617 | const MCSectionMachO &Section = static_cast<const MCSectionMachO&>( |
| 618 | Fragment->getParent()->getSection()); |
| 619 | if (Section.hasAttribute(MCSectionMachO::S_ATTR_DEBUG)) |
| 620 | Base = 0; |
| 621 | } |
| 622 | |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 623 | // x86_64 almost always uses external relocations, except when there is no |
| 624 | // symbol to use as a base address (a local symbol with no preceeding |
| 625 | // non-local symbol). |
| 626 | if (Base) { |
| 627 | Index = Base->getIndex(); |
| 628 | IsExtern = 1; |
| 629 | |
| 630 | // Add the local offset, if needed. |
| 631 | if (Base != &SD) |
Rafael Espindola | 1dda29b | 2010-12-06 21:51:55 +0000 | [diff] [blame] | 632 | Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base); |
Daniel Dunbar | ef4591e | 2010-05-11 23:53:05 +0000 | [diff] [blame] | 633 | } else if (Symbol->isInSection()) { |
Daniel Dunbar | 8fb0403 | 2010-03-25 08:08:54 +0000 | [diff] [blame] | 634 | // The index is the section ordinal (1-based). |
| 635 | Index = SD.getFragment()->getParent()->getOrdinal() + 1; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 636 | IsExtern = 0; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 637 | Value += getSymbolAddress(&SD, Layout); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 638 | |
| 639 | if (IsPCRel) |
Daniel Dunbar | db4c7e6 | 2010-05-11 23:53:11 +0000 | [diff] [blame] | 640 | Value -= FixupAddress + (1 << Log2Size); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 641 | } else if (Symbol->isVariable()) { |
| 642 | const MCExpr *Value = Symbol->getVariableValue(); |
| 643 | int64_t Res; |
| 644 | bool isAbs = Value->EvaluateAsAbsolute(Res, Layout, SectionAddress); |
| 645 | if (isAbs) { |
| 646 | FixedValue = Res; |
| 647 | return; |
| 648 | } else { |
| 649 | report_fatal_error("unsupported relocation of variable '" + |
| 650 | Symbol->getName() + "'"); |
| 651 | } |
Daniel Dunbar | ef4591e | 2010-05-11 23:53:05 +0000 | [diff] [blame] | 652 | } else { |
| 653 | report_fatal_error("unsupported relocation of undefined symbol '" + |
| 654 | Symbol->getName() + "'"); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); |
| 658 | if (IsPCRel) { |
| 659 | if (IsRIPRel) { |
| 660 | if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { |
| 661 | // x86_64 distinguishes movq foo@GOTPCREL so that the linker can |
| 662 | // rewrite the movq to an leaq at link time if the symbol ends up in |
| 663 | // the same linkage unit. |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 664 | if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 665 | Type = macho::RIT_X86_64_GOTLoad; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 666 | else |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 667 | Type = macho::RIT_X86_64_GOT; |
Eric Christopher | aeed4d8 | 2010-05-27 00:52:31 +0000 | [diff] [blame] | 668 | } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 669 | Type = macho::RIT_X86_64_TLV; |
Eric Christopher | aeed4d8 | 2010-05-27 00:52:31 +0000 | [diff] [blame] | 670 | } else if (Modifier != MCSymbolRefExpr::VK_None) { |
| 671 | report_fatal_error("unsupported symbol modifier in relocation"); |
Daniel Dunbar | f0f6cdb | 2010-05-14 18:53:40 +0000 | [diff] [blame] | 672 | } else { |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 673 | Type = macho::RIT_X86_64_Signed; |
Daniel Dunbar | f0f6cdb | 2010-05-14 18:53:40 +0000 | [diff] [blame] | 674 | |
| 675 | // The Darwin x86_64 relocation format has a problem where it cannot |
| 676 | // encode an address (L<foo> + <constant>) which is outside the atom |
| 677 | // containing L<foo>. Generally, this shouldn't occur but it does |
| 678 | // happen when we have a RIPrel instruction with data following the |
| 679 | // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel |
| 680 | // adjustment Darwin x86_64 uses, the offset is still negative and |
| 681 | // the linker has no way to recognize this. |
| 682 | // |
| 683 | // To work around this, Darwin uses several special relocation types |
| 684 | // to indicate the offsets. However, the specification or |
| 685 | // implementation of these seems to also be incomplete; they should |
| 686 | // adjust the addend as well based on the actual encoded instruction |
| 687 | // (the additional bias), but instead appear to just look at the |
| 688 | // final offset. |
| 689 | switch (-(Target.getConstant() + (1LL << Log2Size))) { |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 690 | case 1: Type = macho::RIT_X86_64_Signed1; break; |
| 691 | case 2: Type = macho::RIT_X86_64_Signed2; break; |
| 692 | case 4: Type = macho::RIT_X86_64_Signed4; break; |
Daniel Dunbar | f0f6cdb | 2010-05-14 18:53:40 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 695 | } else { |
| 696 | if (Modifier != MCSymbolRefExpr::VK_None) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 697 | report_fatal_error("unsupported symbol modifier in branch " |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 698 | "relocation"); |
| 699 | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 700 | Type = macho::RIT_X86_64_Branch; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 701 | } |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 702 | } else { |
Daniel Dunbar | 1de558b | 2010-03-29 23:56:40 +0000 | [diff] [blame] | 703 | if (Modifier == MCSymbolRefExpr::VK_GOT) { |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 704 | Type = macho::RIT_X86_64_GOT; |
Daniel Dunbar | 1de558b | 2010-03-29 23:56:40 +0000 | [diff] [blame] | 705 | } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { |
| 706 | // GOTPCREL is allowed as a modifier on non-PCrel instructions, in |
| 707 | // which case all we do is set the PCrel bit in the relocation entry; |
| 708 | // this is used with exception handling, for example. The source is |
| 709 | // required to include any necessary offset directly. |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 710 | Type = macho::RIT_X86_64_GOT; |
Daniel Dunbar | 1de558b | 2010-03-29 23:56:40 +0000 | [diff] [blame] | 711 | IsPCRel = 1; |
Eric Christopher | 96ac515 | 2010-05-26 00:02:12 +0000 | [diff] [blame] | 712 | } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { |
| 713 | report_fatal_error("TLVP symbol modifier should have been rip-rel"); |
Daniel Dunbar | 1de558b | 2010-03-29 23:56:40 +0000 | [diff] [blame] | 714 | } else if (Modifier != MCSymbolRefExpr::VK_None) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 715 | report_fatal_error("unsupported symbol modifier in relocation"); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 716 | else |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 717 | Type = macho::RIT_X86_64_Unsigned; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | // x86_64 always writes custom values into the fixups. |
| 722 | FixedValue = Value; |
| 723 | |
| 724 | // struct relocation_info (8 bytes) |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 725 | macho::RelocationEntry MRE; |
Daniel Dunbar | 640e948 | 2010-05-11 23:53:07 +0000 | [diff] [blame] | 726 | MRE.Word0 = FixupOffset; |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 727 | MRE.Word1 = ((Index << 0) | |
| 728 | (IsPCRel << 24) | |
| 729 | (Log2Size << 25) | |
| 730 | (IsExtern << 27) | |
| 731 | (Type << 28)); |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 732 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 735 | void RecordScatteredRelocation(const MCAssembler &Asm, |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 736 | const MCAsmLayout &Layout, |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 737 | const MCFragment *Fragment, |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 738 | const MCFixup &Fixup, MCValue Target, |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 739 | uint64_t &FixedValue) { |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 740 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
Daniel Dunbar | 7e06af8 | 2010-12-16 15:42:31 +0000 | [diff] [blame] | 741 | unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 742 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 743 | unsigned Type = macho::RIT_Vanilla; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 744 | |
| 745 | // See <reloc.h>. |
| 746 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 747 | MCSymbolData *A_SD = &Asm.getSymbolData(*A); |
| 748 | |
| 749 | if (!A_SD->getFragment()) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 750 | report_fatal_error("symbol '" + A->getName() + |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 751 | "' can not be undefined in a subtraction expression"); |
| 752 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 753 | uint32_t Value = getSymbolAddress(A_SD, Layout); |
| 754 | uint64_t SecAddr = getSectionAddress(A_SD->getFragment()->getParent()); |
| 755 | FixedValue += SecAddr; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 756 | uint32_t Value2 = 0; |
| 757 | |
| 758 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 759 | MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol()); |
| 760 | |
| 761 | if (!B_SD->getFragment()) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 762 | report_fatal_error("symbol '" + B->getSymbol().getName() + |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 763 | "' can not be undefined in a subtraction expression"); |
| 764 | |
| 765 | // Select the appropriate difference relocation type. |
| 766 | // |
| 767 | // Note that there is no longer any semantic difference between these two |
| 768 | // relocation types from the linkers point of view, this is done solely |
| 769 | // for pedantic compatibility with 'as'. |
Matt Beaumont-Gay | e733cf8 | 2010-12-21 23:43:23 +0000 | [diff] [blame] | 770 | Type = A_SD->isExternal() ? (unsigned)macho::RIT_Difference : |
| 771 | (unsigned)macho::RIT_Generic_LocalDifference; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 772 | Value2 = getSymbolAddress(B_SD, Layout); |
| 773 | FixedValue -= getSectionAddress(B_SD->getFragment()->getParent()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // Relocations are written out in reverse order, so the PAIR comes first. |
Daniel Dunbar | e1feeb9 | 2010-12-21 15:26:45 +0000 | [diff] [blame] | 777 | if (Type == macho::RIT_Difference || |
| 778 | Type == macho::RIT_Generic_LocalDifference) { |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 779 | macho::RelocationEntry MRE; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 780 | MRE.Word0 = ((0 << 0) | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 781 | (macho::RIT_Pair << 24) | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 782 | (Log2Size << 28) | |
| 783 | (IsPCRel << 30) | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 784 | macho::RF_Scattered); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 785 | MRE.Word1 = Value2; |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 786 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 789 | macho::RelocationEntry MRE; |
Daniel Dunbar | 640e948 | 2010-05-11 23:53:07 +0000 | [diff] [blame] | 790 | MRE.Word0 = ((FixupOffset << 0) | |
| 791 | (Type << 24) | |
| 792 | (Log2Size << 28) | |
| 793 | (IsPCRel << 30) | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 794 | macho::RF_Scattered); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 795 | MRE.Word1 = Value; |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 796 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Daniel Dunbar | 25bcc9c | 2010-12-22 16:45:29 +0000 | [diff] [blame^] | 799 | void RecordARMScatteredRelocation(const MCAssembler &Asm, |
| 800 | const MCAsmLayout &Layout, |
| 801 | const MCFragment *Fragment, |
| 802 | const MCFixup &Fixup, MCValue Target, |
| 803 | uint64_t &FixedValue) { |
| 804 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 805 | unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
| 806 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
| 807 | unsigned Type = macho::RIT_Vanilla; |
| 808 | |
| 809 | // See <reloc.h>. |
| 810 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 811 | MCSymbolData *A_SD = &Asm.getSymbolData(*A); |
| 812 | |
| 813 | if (!A_SD->getFragment()) |
| 814 | report_fatal_error("symbol '" + A->getName() + |
| 815 | "' can not be undefined in a subtraction expression"); |
| 816 | |
| 817 | uint32_t Value = getSymbolAddress(A_SD, Layout); |
| 818 | uint64_t SecAddr = getSectionAddress(A_SD->getFragment()->getParent()); |
| 819 | FixedValue += SecAddr; |
| 820 | uint32_t Value2 = 0; |
| 821 | |
| 822 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 823 | MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol()); |
| 824 | |
| 825 | if (!B_SD->getFragment()) |
| 826 | report_fatal_error("symbol '" + B->getSymbol().getName() + |
| 827 | "' can not be undefined in a subtraction expression"); |
| 828 | |
| 829 | // Select the appropriate difference relocation type. |
| 830 | // |
| 831 | // Note that there is no longer any semantic difference between these two |
| 832 | // relocation types from the linkers point of view, this is done solely |
| 833 | // for pedantic compatibility with 'as'. |
| 834 | Type = A_SD->isExternal() ? (unsigned)macho::RIT_Difference : |
| 835 | (unsigned)macho::RIT_Generic_LocalDifference; |
| 836 | Value2 = getSymbolAddress(B_SD, Layout); |
| 837 | FixedValue -= getSectionAddress(B_SD->getFragment()->getParent()); |
| 838 | } |
| 839 | |
| 840 | // Relocations are written out in reverse order, so the PAIR comes first. |
| 841 | if (Type == macho::RIT_Difference || |
| 842 | Type == macho::RIT_Generic_LocalDifference) { |
| 843 | macho::RelocationEntry MRE; |
| 844 | MRE.Word0 = ((0 << 0) | |
| 845 | (macho::RIT_Pair << 24) | |
| 846 | (Log2Size << 28) | |
| 847 | (IsPCRel << 30) | |
| 848 | macho::RF_Scattered); |
| 849 | MRE.Word1 = Value2; |
| 850 | Relocations[Fragment->getParent()].push_back(MRE); |
| 851 | } |
| 852 | |
| 853 | macho::RelocationEntry MRE; |
| 854 | MRE.Word0 = ((FixupOffset << 0) | |
| 855 | (Type << 24) | |
| 856 | (Log2Size << 28) | |
| 857 | (IsPCRel << 30) | |
| 858 | macho::RF_Scattered); |
| 859 | MRE.Word1 = Value; |
| 860 | Relocations[Fragment->getParent()].push_back(MRE); |
| 861 | } |
| 862 | |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 863 | void RecordTLVPRelocation(const MCAssembler &Asm, |
Eric Christopher | e48dbf8 | 2010-06-16 00:26:36 +0000 | [diff] [blame] | 864 | const MCAsmLayout &Layout, |
| 865 | const MCFragment *Fragment, |
| 866 | const MCFixup &Fixup, MCValue Target, |
| 867 | uint64_t &FixedValue) { |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 868 | assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP && |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 869 | !is64Bit() && |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 870 | "Should only be called with a 32-bit TLVP relocation!"); |
| 871 | |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 872 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
| 873 | uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 874 | unsigned IsPCRel = 0; |
| 875 | |
| 876 | // Get the symbol data. |
| 877 | MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol()); |
| 878 | unsigned Index = SD_A->getIndex(); |
| 879 | |
Eric Christopher | bc06737 | 2010-06-16 21:32:38 +0000 | [diff] [blame] | 880 | // We're only going to have a second symbol in pic mode and it'll be a |
| 881 | // subtraction from the picbase. For 32-bit pic the addend is the difference |
Eric Christopher | 04b8d3c | 2010-06-17 00:49:46 +0000 | [diff] [blame] | 882 | // between the picbase and the next address. For 32-bit static the addend |
| 883 | // is zero. |
Eric Christopher | bc06737 | 2010-06-16 21:32:38 +0000 | [diff] [blame] | 884 | if (Target.getSymB()) { |
Eric Christopher | 1008d35 | 2010-06-22 23:51:47 +0000 | [diff] [blame] | 885 | // If this is a subtraction then we're pcrel. |
| 886 | uint32_t FixupAddress = |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 887 | getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); |
Eric Christopher | 1008d35 | 2010-06-22 23:51:47 +0000 | [diff] [blame] | 888 | MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol()); |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 889 | IsPCRel = 1; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 890 | FixedValue = (FixupAddress - getSymbolAddress(SD_B, Layout) + |
Eric Christopher | 1008d35 | 2010-06-22 23:51:47 +0000 | [diff] [blame] | 891 | Target.getConstant()); |
Chris Lattner | abf8f9c | 2010-08-16 16:35:20 +0000 | [diff] [blame] | 892 | FixedValue += 1ULL << Log2Size; |
Eric Christopher | bc06737 | 2010-06-16 21:32:38 +0000 | [diff] [blame] | 893 | } else { |
| 894 | FixedValue = 0; |
| 895 | } |
Jim Grosbach | 3c38492 | 2010-11-11 20:16:23 +0000 | [diff] [blame] | 896 | |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 897 | // struct relocation_info (8 bytes) |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 898 | macho::RelocationEntry MRE; |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 899 | MRE.Word0 = Value; |
Daniel Dunbar | e1feeb9 | 2010-12-21 15:26:45 +0000 | [diff] [blame] | 900 | MRE.Word1 = ((Index << 0) | |
| 901 | (IsPCRel << 24) | |
| 902 | (Log2Size << 25) | |
| 903 | (1 << 27) | // Extern |
| 904 | (macho::RIT_Generic_TLV << 28)); // Type |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 905 | Relocations[Fragment->getParent()].push_back(MRE); |
| 906 | } |
Jim Grosbach | 3c38492 | 2010-11-11 20:16:23 +0000 | [diff] [blame] | 907 | |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 908 | static bool getARMFixupKindMachOInfo(unsigned Kind, bool &Is24BitBranch, |
| 909 | unsigned &Log2Size) { |
Daniel Dunbar | 3664564 | 2010-12-22 16:32:37 +0000 | [diff] [blame] | 910 | Is24BitBranch = false; |
| 911 | Log2Size = ~0U; |
| 912 | |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 913 | switch (Kind) { |
| 914 | default: |
| 915 | return false; |
| 916 | |
Daniel Dunbar | 3664564 | 2010-12-22 16:32:37 +0000 | [diff] [blame] | 917 | case FK_Data_1: |
| 918 | Log2Size = llvm::Log2_32(1); |
| 919 | return true; |
| 920 | case FK_Data_2: |
| 921 | Log2Size = llvm::Log2_32(2); |
| 922 | return true; |
| 923 | case FK_Data_4: |
| 924 | Log2Size = llvm::Log2_32(4); |
| 925 | return true; |
| 926 | case FK_Data_8: |
| 927 | Log2Size = llvm::Log2_32(8); |
| 928 | return true; |
| 929 | |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 930 | // Handle 24-bit branch kinds. |
| 931 | case ARM::fixup_arm_ldst_pcrel_12: |
| 932 | case ARM::fixup_arm_pcrel_10: |
| 933 | case ARM::fixup_arm_adr_pcrel_12: |
| 934 | case ARM::fixup_arm_branch: |
| 935 | Is24BitBranch = true; |
| 936 | // Report as 'long', even though that is not quite accurate. |
| 937 | Log2Size = llvm::Log2_32(4); |
| 938 | return true; |
| 939 | } |
| 940 | } |
Daniel Dunbar | 532c456 | 2010-12-22 13:49:43 +0000 | [diff] [blame] | 941 | void RecordARMRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 942 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 943 | MCValue Target, uint64_t &FixedValue) { |
Daniel Dunbar | 4d74305 | 2010-12-22 13:50:05 +0000 | [diff] [blame] | 944 | unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 945 | unsigned Log2Size; |
| 946 | bool Is24BitBranch; |
| 947 | if (!getARMFixupKindMachOInfo(Fixup.getKind(), Is24BitBranch, Log2Size)) { |
| 948 | report_fatal_error("unknown ARM fixup kind!"); |
| 949 | return; |
| 950 | } |
Daniel Dunbar | 4d74305 | 2010-12-22 13:50:05 +0000 | [diff] [blame] | 951 | |
| 952 | // If this is a difference or a defined symbol plus an offset, then we need |
| 953 | // a scattered relocation entry. Differences always require scattered |
| 954 | // relocations. |
| 955 | if (Target.getSymB()) |
Daniel Dunbar | 25bcc9c | 2010-12-22 16:45:29 +0000 | [diff] [blame^] | 956 | return RecordARMScatteredRelocation(Asm, Layout, Fragment, Fixup, |
| 957 | Target, FixedValue); |
Daniel Dunbar | 4d74305 | 2010-12-22 13:50:05 +0000 | [diff] [blame] | 958 | |
| 959 | // Get the symbol data, if any. |
| 960 | MCSymbolData *SD = 0; |
| 961 | if (Target.getSymA()) |
| 962 | SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); |
| 963 | |
| 964 | // FIXME: For other platforms, we need to use scattered relocations for |
| 965 | // internal relocations with offsets. If this is an internal relocation |
| 966 | // with an offset, it also needs a scattered relocation entry. |
| 967 | // |
| 968 | // Is this right for ARM? |
| 969 | uint32_t Offset = Target.getConstant(); |
| 970 | if (IsPCRel) |
| 971 | Offset += 1 << Log2Size; |
| 972 | if (Offset && SD && !doesSymbolRequireExternRelocation(SD)) |
Daniel Dunbar | 25bcc9c | 2010-12-22 16:45:29 +0000 | [diff] [blame^] | 973 | return RecordARMScatteredRelocation(Asm, Layout, Fragment, Fixup, |
| 974 | Target, FixedValue); |
Daniel Dunbar | 4d74305 | 2010-12-22 13:50:05 +0000 | [diff] [blame] | 975 | |
| 976 | // See <reloc.h>. |
| 977 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 978 | unsigned Index = 0; |
| 979 | unsigned IsExtern = 0; |
| 980 | unsigned Type = 0; |
| 981 | |
| 982 | if (Target.isAbsolute()) { // constant |
| 983 | // FIXME! |
| 984 | report_fatal_error("FIXME: relocations to absolute targets " |
| 985 | "not yet implemented"); |
| 986 | } else if (SD->getSymbol().isVariable()) { |
| 987 | int64_t Res; |
| 988 | if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( |
| 989 | Res, Layout, SectionAddress)) { |
| 990 | FixedValue = Res; |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | report_fatal_error("unsupported relocation of variable '" + |
| 995 | SD->getSymbol().getName() + "'"); |
| 996 | } else { |
| 997 | // Check whether we need an external or internal relocation. |
| 998 | if (doesSymbolRequireExternRelocation(SD)) { |
| 999 | IsExtern = 1; |
| 1000 | Index = SD->getIndex(); |
| 1001 | // For external relocations, make sure to offset the fixup value to |
| 1002 | // compensate for the addend of the symbol address, if it was |
| 1003 | // undefined. This occurs with weak definitions, for example. |
| 1004 | if (!SD->Symbol->isUndefined()) |
| 1005 | FixedValue -= Layout.getSymbolOffset(SD); |
| 1006 | } else { |
| 1007 | // The index is the section ordinal (1-based). |
| 1008 | Index = SD->getFragment()->getParent()->getOrdinal() + 1; |
| 1009 | FixedValue += getSectionAddress(SD->getFragment()->getParent()); |
| 1010 | } |
| 1011 | if (IsPCRel) |
| 1012 | FixedValue -= getSectionAddress(Fragment->getParent()); |
| 1013 | |
Daniel Dunbar | 294e678 | 2010-12-22 16:19:24 +0000 | [diff] [blame] | 1014 | // Determine the appropriate type based on the fixup kind. |
| 1015 | Type = Is24BitBranch ? macho::RIT_ARM_Branch24Bit : macho::RIT_Vanilla; |
Daniel Dunbar | 4d74305 | 2010-12-22 13:50:05 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // struct relocation_info (8 bytes) |
| 1019 | macho::RelocationEntry MRE; |
| 1020 | MRE.Word0 = FixupOffset; |
| 1021 | MRE.Word1 = ((Index << 0) | |
| 1022 | (IsPCRel << 24) | |
| 1023 | (Log2Size << 25) | |
| 1024 | (IsExtern << 27) | |
| 1025 | (Type << 28)); |
| 1026 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 532c456 | 2010-12-22 13:49:43 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1029 | void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 1030 | const MCFragment *Fragment, const MCFixup &Fixup, |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1031 | MCValue Target, uint64_t &FixedValue) { |
Daniel Dunbar | 532c456 | 2010-12-22 13:49:43 +0000 | [diff] [blame] | 1032 | // FIXME: These needs to be factored into the target Mach-O writer. |
| 1033 | if (isARM()) { |
| 1034 | RecordARMRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); |
| 1035 | return; |
| 1036 | } |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1037 | if (is64Bit()) { |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1038 | RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); |
Daniel Dunbar | 602b40f | 2010-03-19 18:07:55 +0000 | [diff] [blame] | 1039 | return; |
| 1040 | } |
| 1041 | |
Daniel Dunbar | 7e06af8 | 2010-12-16 15:42:31 +0000 | [diff] [blame] | 1042 | unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 1043 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1044 | |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 1045 | // If this is a 32-bit TLVP reloc it's handled a bit differently. |
Daniel Dunbar | 23bea41 | 2010-09-17 15:21:50 +0000 | [diff] [blame] | 1046 | if (Target.getSymA() && |
| 1047 | Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) { |
Eric Christopher | c9ada47 | 2010-06-15 22:59:05 +0000 | [diff] [blame] | 1048 | RecordTLVPRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); |
| 1049 | return; |
| 1050 | } |
Jim Grosbach | 3c38492 | 2010-11-11 20:16:23 +0000 | [diff] [blame] | 1051 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1052 | // If this is a difference or a defined symbol plus an offset, then we need |
| 1053 | // a scattered relocation entry. |
Daniel Dunbar | a8251fa | 2010-05-10 23:15:20 +0000 | [diff] [blame] | 1054 | // Differences always require scattered relocations. |
| 1055 | if (Target.getSymB()) |
| 1056 | return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup, |
| 1057 | Target, FixedValue); |
| 1058 | |
| 1059 | // Get the symbol data, if any. |
| 1060 | MCSymbolData *SD = 0; |
| 1061 | if (Target.getSymA()) |
| 1062 | SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); |
| 1063 | |
| 1064 | // If this is an internal relocation with an offset, it also needs a |
| 1065 | // scattered relocation entry. |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1066 | uint32_t Offset = Target.getConstant(); |
| 1067 | if (IsPCRel) |
| 1068 | Offset += 1 << Log2Size; |
Daniel Dunbar | a8251fa | 2010-05-10 23:15:20 +0000 | [diff] [blame] | 1069 | if (Offset && SD && !doesSymbolRequireExternRelocation(SD)) |
| 1070 | return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup, |
| 1071 | Target, FixedValue); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1072 | |
| 1073 | // See <reloc.h>. |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 1074 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1075 | unsigned Index = 0; |
| 1076 | unsigned IsExtern = 0; |
| 1077 | unsigned Type = 0; |
| 1078 | |
| 1079 | if (Target.isAbsolute()) { // constant |
| 1080 | // SymbolNum of 0 indicates the absolute section. |
| 1081 | // |
| 1082 | // FIXME: Currently, these are never generated (see code below). I cannot |
| 1083 | // find a case where they are actually emitted. |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 1084 | Type = macho::RIT_Vanilla; |
Rafael Espindola | 545b77e | 2010-12-07 17:12:32 +0000 | [diff] [blame] | 1085 | } else if (SD->getSymbol().isVariable()) { |
Rafael Espindola | 545b77e | 2010-12-07 17:12:32 +0000 | [diff] [blame] | 1086 | int64_t Res; |
Daniel Dunbar | 42b5286 | 2010-12-22 13:49:56 +0000 | [diff] [blame] | 1087 | if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( |
| 1088 | Res, Layout, SectionAddress)) { |
Rafael Espindola | 545b77e | 2010-12-07 17:12:32 +0000 | [diff] [blame] | 1089 | FixedValue = Res; |
| 1090 | return; |
Rafael Espindola | 545b77e | 2010-12-07 17:12:32 +0000 | [diff] [blame] | 1091 | } |
Daniel Dunbar | 42b5286 | 2010-12-22 13:49:56 +0000 | [diff] [blame] | 1092 | |
| 1093 | report_fatal_error("unsupported relocation of variable '" + |
| 1094 | SD->getSymbol().getName() + "'"); |
Michael J. Spencer | b0f3b3e | 2010-08-10 16:00:49 +0000 | [diff] [blame] | 1095 | } else { |
Daniel Dunbar | e9460ec | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 1096 | // Check whether we need an external or internal relocation. |
| 1097 | if (doesSymbolRequireExternRelocation(SD)) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1098 | IsExtern = 1; |
| 1099 | Index = SD->getIndex(); |
Daniel Dunbar | e9460ec | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 1100 | // For external relocations, make sure to offset the fixup value to |
| 1101 | // compensate for the addend of the symbol address, if it was |
| 1102 | // undefined. This occurs with weak definitions, for example. |
| 1103 | if (!SD->Symbol->isUndefined()) |
Rafael Espindola | 3b3148f | 2010-12-07 05:57:28 +0000 | [diff] [blame] | 1104 | FixedValue -= Layout.getSymbolOffset(SD); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1105 | } else { |
Daniel Dunbar | 8fb0403 | 2010-03-25 08:08:54 +0000 | [diff] [blame] | 1106 | // The index is the section ordinal (1-based). |
| 1107 | Index = SD->getFragment()->getParent()->getOrdinal() + 1; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1108 | FixedValue += getSectionAddress(SD->getFragment()->getParent()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1109 | } |
Rafael Espindola | bf60dad | 2010-12-07 03:50:14 +0000 | [diff] [blame] | 1110 | if (IsPCRel) |
| 1111 | FixedValue -= getSectionAddress(Fragment->getParent()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1112 | |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 1113 | Type = macho::RIT_Vanilla; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | // struct relocation_info (8 bytes) |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 1117 | macho::RelocationEntry MRE; |
Daniel Dunbar | 640e948 | 2010-05-11 23:53:07 +0000 | [diff] [blame] | 1118 | MRE.Word0 = FixupOffset; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1119 | MRE.Word1 = ((Index << 0) | |
| 1120 | (IsPCRel << 24) | |
| 1121 | (Log2Size << 25) | |
| 1122 | (IsExtern << 27) | |
| 1123 | (Type << 28)); |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 1124 | Relocations[Fragment->getParent()].push_back(MRE); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | void BindIndirectSymbols(MCAssembler &Asm) { |
| 1128 | // This is the point where 'as' creates actual symbols for indirect symbols |
| 1129 | // (in the following two passes). It would be easier for us to do this |
| 1130 | // sooner when we see the attribute, but that makes getting the order in the |
| 1131 | // symbol table much more complicated than it is worth. |
| 1132 | // |
| 1133 | // FIXME: Revisit this when the dust settles. |
| 1134 | |
| 1135 | // Bind non lazy symbol pointers first. |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1136 | unsigned IndirectIndex = 0; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1137 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1138 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1139 | const MCSectionMachO &Section = |
Daniel Dunbar | 56279f4 | 2010-05-18 17:28:20 +0000 | [diff] [blame] | 1140 | cast<MCSectionMachO>(it->SectionData->getSection()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) |
| 1143 | continue; |
| 1144 | |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1145 | // Initialize the section indirect symbol base, if necessary. |
| 1146 | if (!IndirectSymBase.count(it->SectionData)) |
| 1147 | IndirectSymBase[it->SectionData] = IndirectIndex; |
Jim Grosbach | 3c38492 | 2010-11-11 20:16:23 +0000 | [diff] [blame] | 1148 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1149 | Asm.getOrCreateSymbolData(*it->Symbol); |
| 1150 | } |
| 1151 | |
| 1152 | // Then lazy symbol pointers and symbol stubs. |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1153 | IndirectIndex = 0; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1154 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1155 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1156 | const MCSectionMachO &Section = |
Daniel Dunbar | 56279f4 | 2010-05-18 17:28:20 +0000 | [diff] [blame] | 1157 | cast<MCSectionMachO>(it->SectionData->getSection()); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS && |
| 1160 | Section.getType() != MCSectionMachO::S_SYMBOL_STUBS) |
| 1161 | continue; |
| 1162 | |
Daniel Dunbar | 2ae4bfd | 2010-05-18 17:28:24 +0000 | [diff] [blame] | 1163 | // Initialize the section indirect symbol base, if necessary. |
| 1164 | if (!IndirectSymBase.count(it->SectionData)) |
| 1165 | IndirectSymBase[it->SectionData] = IndirectIndex; |
| 1166 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1167 | // Set the symbol type to undefined lazy, but only on construction. |
| 1168 | // |
| 1169 | // FIXME: Do not hardcode. |
| 1170 | bool Created; |
| 1171 | MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created); |
| 1172 | if (Created) |
| 1173 | Entry.setFlags(Entry.getFlags() | 0x0001); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | /// ComputeSymbolTable - Compute the symbol table data |
| 1178 | /// |
| 1179 | /// \param StringTable [out] - The string table data. |
| 1180 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 1181 | /// string table. |
| 1182 | void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable, |
| 1183 | std::vector<MachSymbolData> &LocalSymbolData, |
| 1184 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 1185 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
| 1186 | // Build section lookup table. |
| 1187 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 1188 | unsigned Index = 1; |
| 1189 | for (MCAssembler::iterator it = Asm.begin(), |
| 1190 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 1191 | SectionIndexMap[&it->getSection()] = Index; |
| 1192 | assert(Index <= 256 && "Too many sections!"); |
| 1193 | |
| 1194 | // Index 0 is always the empty string. |
| 1195 | StringMap<uint64_t> StringIndexMap; |
| 1196 | StringTable += '\x00'; |
| 1197 | |
| 1198 | // Build the symbol arrays and the string table, but only for non-local |
| 1199 | // symbols. |
| 1200 | // |
| 1201 | // The particular order that we collect the symbols and create the string |
| 1202 | // table, then sort the symbols is chosen to match 'as'. Even though it |
| 1203 | // doesn't matter for correctness, this is important for letting us diff .o |
| 1204 | // files. |
| 1205 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 1206 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 1207 | const MCSymbol &Symbol = it->getSymbol(); |
| 1208 | |
| 1209 | // Ignore non-linker visible symbols. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 1210 | if (!Asm.isSymbolLinkerVisible(it->getSymbol())) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1211 | continue; |
| 1212 | |
| 1213 | if (!it->isExternal() && !Symbol.isUndefined()) |
| 1214 | continue; |
| 1215 | |
| 1216 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 1217 | if (!Entry) { |
| 1218 | Entry = StringTable.size(); |
| 1219 | StringTable += Symbol.getName(); |
| 1220 | StringTable += '\x00'; |
| 1221 | } |
| 1222 | |
| 1223 | MachSymbolData MSD; |
| 1224 | MSD.SymbolData = it; |
| 1225 | MSD.StringIndex = Entry; |
| 1226 | |
| 1227 | if (Symbol.isUndefined()) { |
| 1228 | MSD.SectionIndex = 0; |
| 1229 | UndefinedSymbolData.push_back(MSD); |
| 1230 | } else if (Symbol.isAbsolute()) { |
| 1231 | MSD.SectionIndex = 0; |
| 1232 | ExternalSymbolData.push_back(MSD); |
| 1233 | } else { |
| 1234 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 1235 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 1236 | ExternalSymbolData.push_back(MSD); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | // Now add the data for local symbols. |
| 1241 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 1242 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 1243 | const MCSymbol &Symbol = it->getSymbol(); |
| 1244 | |
| 1245 | // Ignore non-linker visible symbols. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 1246 | if (!Asm.isSymbolLinkerVisible(it->getSymbol())) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1247 | continue; |
| 1248 | |
| 1249 | if (it->isExternal() || Symbol.isUndefined()) |
| 1250 | continue; |
| 1251 | |
| 1252 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 1253 | if (!Entry) { |
| 1254 | Entry = StringTable.size(); |
| 1255 | StringTable += Symbol.getName(); |
| 1256 | StringTable += '\x00'; |
| 1257 | } |
| 1258 | |
| 1259 | MachSymbolData MSD; |
| 1260 | MSD.SymbolData = it; |
| 1261 | MSD.StringIndex = Entry; |
| 1262 | |
| 1263 | if (Symbol.isAbsolute()) { |
| 1264 | MSD.SectionIndex = 0; |
| 1265 | LocalSymbolData.push_back(MSD); |
| 1266 | } else { |
| 1267 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 1268 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 1269 | LocalSymbolData.push_back(MSD); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | // External and undefined symbols are required to be in lexicographic order. |
| 1274 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 1275 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 1276 | |
| 1277 | // Set the symbol indices. |
| 1278 | Index = 0; |
| 1279 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 1280 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
| 1281 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 1282 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 1283 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 1284 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
| 1285 | |
| 1286 | // The string table is padded to a multiple of 4. |
| 1287 | while (StringTable.size() % 4) |
| 1288 | StringTable += '\x00'; |
| 1289 | } |
| 1290 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1291 | void computeSectionAddresses(const MCAssembler &Asm, |
| 1292 | const MCAsmLayout &Layout) { |
| 1293 | uint64_t StartAddress = 0; |
| 1294 | const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder(); |
| 1295 | for (int i = 0, n = Order.size(); i != n ; ++i) { |
| 1296 | const MCSectionData *SD = Order[i]; |
| 1297 | StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment()); |
| 1298 | SectionAddress[SD] = StartAddress; |
| 1299 | StartAddress += Layout.getSectionAddressSize(SD); |
| 1300 | // Explicitly pad the section to match the alignment requirements of the |
| 1301 | // following one. This is for 'gas' compatibility, it shouldn't |
| 1302 | /// strictly be necessary. |
| 1303 | StartAddress += getPaddingSize(SD, Layout); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) { |
| 1308 | computeSectionAddresses(Asm, Layout); |
| 1309 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1310 | // Create symbol data for any indirect symbols. |
| 1311 | BindIndirectSymbols(Asm); |
| 1312 | |
| 1313 | // Compute symbol table information and bind symbol indices. |
| 1314 | ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, |
| 1315 | UndefinedSymbolData); |
| 1316 | } |
| 1317 | |
Daniel Dunbar | 1f3662a | 2010-12-17 04:54:54 +0000 | [diff] [blame] | 1318 | bool IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm, |
| 1319 | const MCSymbolRefExpr *A, |
Rafael Espindola | 3132780 | 2010-12-18 06:27:54 +0000 | [diff] [blame] | 1320 | const MCSymbolRefExpr *B, |
| 1321 | bool InSet) const { |
| 1322 | if (InSet) |
| 1323 | return true; |
| 1324 | |
Daniel Dunbar | b874227 | 2010-12-17 05:50:29 +0000 | [diff] [blame] | 1325 | if (!TargetObjectWriter->useAggressiveSymbolFolding()) |
| 1326 | return false; |
| 1327 | |
Daniel Dunbar | 32c1c5a | 2010-12-17 04:54:58 +0000 | [diff] [blame] | 1328 | // The effective address is |
| 1329 | // addr(atom(A)) + offset(A) |
| 1330 | // - addr(atom(B)) - offset(B) |
| 1331 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 1332 | // addr(atom(A)) - addr(atom(B)) == 0. |
| 1333 | const MCSymbolData *A_Base = 0, *B_Base = 0; |
| 1334 | |
| 1335 | // Modified symbol references cannot be resolved. |
| 1336 | if (A->getKind() != MCSymbolRefExpr::VK_None || |
| 1337 | B->getKind() != MCSymbolRefExpr::VK_None) |
| 1338 | return false; |
| 1339 | |
| 1340 | A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); |
| 1341 | if (!A_Base) |
| 1342 | return false; |
| 1343 | |
| 1344 | B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); |
| 1345 | if (!B_Base) |
| 1346 | return false; |
| 1347 | |
| 1348 | // If the atoms are the same, they are guaranteed to have the same address. |
| 1349 | if (A_Base == B_Base) |
| 1350 | return true; |
| 1351 | |
| 1352 | // Otherwise, we can't prove this is fully resolved. |
Daniel Dunbar | 1f3662a | 2010-12-17 04:54:54 +0000 | [diff] [blame] | 1353 | return false; |
| 1354 | } |
| 1355 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1356 | bool IsFixupFullyResolved(const MCAssembler &Asm, |
| 1357 | const MCValue Target, |
| 1358 | bool IsPCRel, |
| 1359 | const MCFragment *DF) const { |
Daniel Dunbar | 7976b88 | 2010-11-27 05:18:48 +0000 | [diff] [blame] | 1360 | // Otherwise, determine whether this value is actually resolved; scattering |
| 1361 | // may cause atoms to move. |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1362 | |
Daniel Dunbar | 7976b88 | 2010-11-27 05:18:48 +0000 | [diff] [blame] | 1363 | // Check if we are using the "simple" resolution algorithm (e.g., |
| 1364 | // i386). |
| 1365 | if (!Asm.getBackend().hasReliableSymbolDifference()) { |
| 1366 | const MCSection *BaseSection = 0; |
| 1367 | if (IsPCRel) |
| 1368 | BaseSection = &DF->getParent()->getSection(); |
| 1369 | |
| 1370 | return isScatteredFixupFullyResolvedSimple(Asm, Target, BaseSection); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1371 | } |
Daniel Dunbar | 7976b88 | 2010-11-27 05:18:48 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Otherwise, compute the proper answer as reliably as possible. |
| 1374 | |
| 1375 | // If this is a PCrel relocation, find the base atom (identified by its |
| 1376 | // symbol) that the fixup value is relative to. |
| 1377 | const MCSymbolData *BaseSymbol = 0; |
| 1378 | if (IsPCRel) { |
| 1379 | BaseSymbol = DF->getAtom(); |
| 1380 | if (!BaseSymbol) |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | return isScatteredFixupFullyResolved(Asm, Target, BaseSymbol); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1387 | void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) { |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1388 | unsigned NumSections = Asm.size(); |
| 1389 | |
| 1390 | // The section data starts after the header, the segment load command (and |
| 1391 | // section headers) and the symbol table. |
| 1392 | unsigned NumLoadCommands = 1; |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1393 | uint64_t LoadCommandsSize = is64Bit() ? |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 1394 | macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size : |
| 1395 | macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1396 | |
| 1397 | // Add the symbol table load command sizes, if used. |
| 1398 | unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() + |
| 1399 | UndefinedSymbolData.size(); |
| 1400 | if (NumSymbols) { |
| 1401 | NumLoadCommands += 2; |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 1402 | LoadCommandsSize += (macho::SymtabLoadCommandSize + |
| 1403 | macho::DysymtabLoadCommandSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | // Compute the total size of the section data, as well as its file size and |
| 1407 | // vm size. |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1408 | uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size : |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 1409 | macho::Header32Size) + LoadCommandsSize; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1410 | uint64_t SectionDataSize = 0; |
| 1411 | uint64_t SectionDataFileSize = 0; |
| 1412 | uint64_t VMSize = 0; |
| 1413 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1414 | ie = Asm.end(); it != ie; ++it) { |
| 1415 | const MCSectionData &SD = *it; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1416 | uint64_t Address = getSectionAddress(&SD); |
| 1417 | uint64_t Size = Layout.getSectionAddressSize(&SD); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 1418 | uint64_t FileSize = Layout.getSectionFileSize(&SD); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1419 | FileSize += getPaddingSize(&SD, Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1420 | |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 1421 | VMSize = std::max(VMSize, Address + Size); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1422 | |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 1423 | if (SD.getSection().isVirtualSection()) |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1424 | continue; |
| 1425 | |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 1426 | SectionDataSize = std::max(SectionDataSize, Address + Size); |
| 1427 | SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | // The section data is padded to 4 bytes. |
| 1431 | // |
| 1432 | // FIXME: Is this machine dependent? |
| 1433 | unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4); |
| 1434 | SectionDataFileSize += SectionDataPadding; |
| 1435 | |
| 1436 | // Write the prolog, starting with the header and load command... |
| 1437 | WriteHeader(NumLoadCommands, LoadCommandsSize, |
| 1438 | Asm.getSubsectionsViaSymbols()); |
| 1439 | WriteSegmentLoadCommand(NumSections, VMSize, |
| 1440 | SectionDataStart, SectionDataSize); |
| 1441 | |
| 1442 | // ... and then the section headers. |
| 1443 | uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; |
| 1444 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1445 | ie = Asm.end(); it != ie; ++it) { |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 1446 | std::vector<macho::RelocationEntry> &Relocs = Relocations[it]; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1447 | unsigned NumRelocs = Relocs.size(); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1448 | uint64_t SectionStart = SectionDataStart + getSectionAddress(it); |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1449 | WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs); |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 1450 | RelocTableEnd += NumRelocs * macho::RelocationInfoSize; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | // Write the symbol table load command, if used. |
| 1454 | if (NumSymbols) { |
| 1455 | unsigned FirstLocalSymbol = 0; |
| 1456 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 1457 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 1458 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 1459 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 1460 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
| 1461 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
| 1462 | unsigned NumSymTabSymbols = |
| 1463 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
| 1464 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
| 1465 | uint64_t IndirectSymbolOffset = 0; |
| 1466 | |
| 1467 | // If used, the indirect symbols are written after the section data. |
| 1468 | if (NumIndirectSymbols) |
| 1469 | IndirectSymbolOffset = RelocTableEnd; |
| 1470 | |
| 1471 | // The symbol table is written after the indirect symbol data. |
| 1472 | uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize; |
| 1473 | |
| 1474 | // The string table is written after symbol table. |
| 1475 | uint64_t StringTableOffset = |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1476 | SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size : |
Daniel Dunbar | 821ecd7 | 2010-11-27 04:19:38 +0000 | [diff] [blame] | 1477 | macho::Nlist32Size); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1478 | WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
| 1479 | StringTableOffset, StringTable.size()); |
| 1480 | |
| 1481 | WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
| 1482 | FirstExternalSymbol, NumExternalSymbols, |
| 1483 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 1484 | IndirectSymbolOffset, NumIndirectSymbols); |
| 1485 | } |
| 1486 | |
| 1487 | // Write the actual section data. |
| 1488 | for (MCAssembler::const_iterator it = Asm.begin(), |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1489 | ie = Asm.end(); it != ie; ++it) { |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 1490 | Asm.WriteSectionData(it, Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1491 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1492 | uint64_t Pad = getPaddingSize(it, Layout); |
| 1493 | for (unsigned int i = 0; i < Pad; ++i) |
| 1494 | Write8(0); |
| 1495 | } |
| 1496 | |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1497 | // Write the extra padding. |
| 1498 | WriteZeros(SectionDataPadding); |
| 1499 | |
| 1500 | // Write the relocation entries. |
| 1501 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1502 | ie = Asm.end(); it != ie; ++it) { |
| 1503 | // Write the section relocation entries, in reverse order to match 'as' |
| 1504 | // (approximately, the exact algorithm is more complicated than this). |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 1505 | std::vector<macho::RelocationEntry> &Relocs = Relocations[it]; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1506 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 1507 | Write32(Relocs[e - i - 1].Word0); |
| 1508 | Write32(Relocs[e - i - 1].Word1); |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | // Write the symbol table data, if used. |
| 1513 | if (NumSymbols) { |
| 1514 | // Write the indirect symbol entries. |
| 1515 | for (MCAssembler::const_indirect_symbol_iterator |
| 1516 | it = Asm.indirect_symbol_begin(), |
| 1517 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 1518 | // Indirect symbols in the non lazy symbol pointer section have some |
| 1519 | // special handling. |
| 1520 | const MCSectionMachO &Section = |
| 1521 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 1522 | if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) { |
| 1523 | // If this symbol is defined and internal, mark it as such. |
| 1524 | if (it->Symbol->isDefined() && |
| 1525 | !Asm.getSymbolData(*it->Symbol).isExternal()) { |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 1526 | uint32_t Flags = macho::ISF_Local; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1527 | if (it->Symbol->isAbsolute()) |
Daniel Dunbar | f52788f | 2010-11-27 04:59:14 +0000 | [diff] [blame] | 1528 | Flags |= macho::ISF_Absolute; |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1529 | Write32(Flags); |
| 1530 | continue; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | Write32(Asm.getSymbolData(*it->Symbol).getIndex()); |
| 1535 | } |
| 1536 | |
| 1537 | // FIXME: Check that offsets match computed ones. |
| 1538 | |
| 1539 | // Write the symbol table entries. |
| 1540 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1541 | WriteNlist(LocalSymbolData[i], Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1542 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1543 | WriteNlist(ExternalSymbolData[i], Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1544 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 1545 | WriteNlist(UndefinedSymbolData[i], Layout); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Write the string table. |
| 1548 | OS << StringTable.str(); |
| 1549 | } |
| 1550 | } |
| 1551 | }; |
| 1552 | |
| 1553 | } |
| 1554 | |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 1555 | MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW, |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1556 | raw_ostream &OS, |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1557 | bool IsLittleEndian) { |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1558 | return new MachObjectWriter(MOTW, OS, IsLittleEndian); |
Daniel Dunbar | 2df4ceb | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1559 | } |