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