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