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