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