1 //===-- X86MachObjectWriter.cpp - X86 Mach-O 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 "MCTargetDesc/X86MCTargetDesc.h" 11 #include "MCTargetDesc/X86FixupKinds.h" 12 #include "llvm/ADT/Twine.h" 13 #include "llvm/MC/MCAsmLayout.h" 14 #include "llvm/MC/MCAssembler.h" 15 #include "llvm/MC/MCContext.h" 16 #include "llvm/MC/MCMachObjectWriter.h" 17 #include "llvm/MC/MCSectionMachO.h" 18 #include "llvm/MC/MCValue.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/Format.h" 21 #include "llvm/Support/MachO.h" 22 23 using namespace llvm; 24 25 namespace { 26 class X86MachObjectWriter : public MCMachObjectTargetWriter { 27 bool RecordScatteredRelocation(MachObjectWriter *Writer, 28 const MCAssembler &Asm, 29 const MCAsmLayout &Layout, 30 const MCFragment *Fragment, 31 const MCFixup &Fixup, 32 MCValue Target, 33 unsigned Log2Size, 34 uint64_t &FixedValue); 35 void RecordTLVPRelocation(MachObjectWriter *Writer, 36 const MCAssembler &Asm, 37 const MCAsmLayout &Layout, 38 const MCFragment *Fragment, 39 const MCFixup &Fixup, 40 MCValue Target, 41 uint64_t &FixedValue); 42 43 void RecordX86Relocation(MachObjectWriter *Writer, 44 const MCAssembler &Asm, 45 const MCAsmLayout &Layout, 46 const MCFragment *Fragment, 47 const MCFixup &Fixup, 48 MCValue Target, 49 uint64_t &FixedValue); 50 void RecordX86_64Relocation(MachObjectWriter *Writer, 51 const MCAssembler &Asm, 52 const MCAsmLayout &Layout, 53 const MCFragment *Fragment, 54 const MCFixup &Fixup, 55 MCValue Target, 56 uint64_t &FixedValue); 57 public: 58 X86MachObjectWriter(bool Is64Bit, uint32_t CPUType, 59 uint32_t CPUSubtype) 60 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype, 61 /*UseAggressiveSymbolFolding=*/Is64Bit) {} 62 63 void RecordRelocation(MachObjectWriter *Writer, 64 const MCAssembler &Asm, const MCAsmLayout &Layout, 65 const MCFragment *Fragment, const MCFixup &Fixup, 66 MCValue Target, uint64_t &FixedValue) override { 67 if (Writer->is64Bit()) 68 RecordX86_64Relocation(Writer, Asm, Layout, Fragment, Fixup, Target, 69 FixedValue); 70 else 71 RecordX86Relocation(Writer, Asm, Layout, Fragment, Fixup, Target, 72 FixedValue); 73 } 74 }; 75 } 76 77 static bool isFixupKindRIPRel(unsigned Kind) { 78 return Kind == X86::reloc_riprel_4byte || 79 Kind == X86::reloc_riprel_4byte_movq_load; 80 } 81 82 static unsigned getFixupKindLog2Size(unsigned Kind) { 83 switch (Kind) { 84 default: 85 llvm_unreachable("invalid fixup kind!"); 86 case FK_PCRel_1: 87 case FK_Data_1: return 0; 88 case FK_PCRel_2: 89 case FK_Data_2: return 1; 90 case FK_PCRel_4: 91 // FIXME: Remove these!!! 92 case X86::reloc_riprel_4byte: 93 case X86::reloc_riprel_4byte_movq_load: 94 case X86::reloc_signed_4byte: 95 case FK_Data_4: return 2; 96 case FK_Data_8: return 3; 97 } 98 } 99 100 void X86MachObjectWriter::RecordX86_64Relocation(MachObjectWriter *Writer, 101 const MCAssembler &Asm, 102 const MCAsmLayout &Layout, 103 const MCFragment *Fragment, 104 const MCFixup &Fixup, 105 MCValue Target, 106 uint64_t &FixedValue) { 107 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); 108 unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind()); 109 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); 110 111 // See <reloc.h>. 112 uint32_t FixupOffset = 113 Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); 114 uint32_t FixupAddress = 115 Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); 116 int64_t Value = 0; 117 unsigned Index = 0; 118 unsigned IsExtern = 0; 119 unsigned Type = 0; 120 121 Value = Target.getConstant(); 122 123 if (IsPCRel) { 124 // Compensate for the relocation offset, Darwin x86_64 relocations only have 125 // the addend and appear to have attempted to define it to be the actual 126 // expression addend without the PCrel bias. However, instructions with data 127 // following the relocation are not accommodated for (see comment below 128 // regarding SIGNED{1,2,4}), so it isn't exactly that either. 129 Value += 1LL << Log2Size; 130 } 131 132 if (Target.isAbsolute()) { // constant 133 // SymbolNum of 0 indicates the absolute section. 134 Type = MachO::X86_64_RELOC_UNSIGNED; 135 Index = 0; 136 137 // FIXME: I believe this is broken, I don't think the linker can understand 138 // it. I think it would require a local relocation, but I'm not sure if that 139 // would work either. The official way to get an absolute PCrel relocation 140 // is to use an absolute symbol (which we don't support yet). 141 if (IsPCRel) { 142 IsExtern = 1; 143 Type = MachO::X86_64_RELOC_BRANCH; 144 } 145 } else if (Target.getSymB()) { // A - B + constant 146 const MCSymbol *A = &Target.getSymA()->getSymbol(); 147 if (A->isTemporary()) 148 A = &A->AliasedSymbol(); 149 const MCSymbolData &A_SD = Asm.getSymbolData(*A); 150 const MCSymbolData *A_Base = Asm.getAtom(&A_SD); 151 152 const MCSymbol *B = &Target.getSymB()->getSymbol(); 153 if (B->isTemporary()) 154 B = &B->AliasedSymbol(); 155 const MCSymbolData &B_SD = Asm.getSymbolData(*B); 156 const MCSymbolData *B_Base = Asm.getAtom(&B_SD); 157 158 // Neither symbol can be modified. 159 if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || 160 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) 161 report_fatal_error("unsupported relocation of modified symbol", false); 162 163 // We don't support PCrel relocations of differences. Darwin 'as' doesn't 164 // implement most of these correctly. 165 if (IsPCRel) 166 report_fatal_error("unsupported pc-relative relocation of difference", 167 false); 168 169 // The support for the situation where one or both of the symbols would 170 // require a local relocation is handled just like if the symbols were 171 // external. This is certainly used in the case of debug sections where the 172 // section has only temporary symbols and thus the symbols don't have base 173 // symbols. This is encoded using the section ordinal and non-extern 174 // relocation entries. 175 176 // Darwin 'as' doesn't emit correct relocations for this (it ends up with a 177 // single SIGNED relocation); reject it for now. Except the case where both 178 // symbols don't have a base, equal but both NULL. 179 if (A_Base == B_Base && A_Base) 180 report_fatal_error("unsupported relocation with identical base", false); 181 182 // A subtraction expression where either symbol is undefined is a 183 // non-relocatable expression. 184 if (A->isUndefined() || B->isUndefined()) { 185 StringRef Name = A->isUndefined() ? A->getName() : B->getName(); 186 Asm.getContext().FatalError(Fixup.getLoc(), 187 "unsupported relocation with subtraction expression, symbol '" + 188 Name + "' can not be undefined in a subtraction expression"); 189 } 190 191 Value += Writer->getSymbolAddress(&A_SD, Layout) - 192 (!A_Base ? 0 : Writer->getSymbolAddress(A_Base, Layout)); 193 Value -= Writer->getSymbolAddress(&B_SD, Layout) - 194 (!B_Base ? 0 : Writer->getSymbolAddress(B_Base, Layout)); 195 196 if (A_Base) { 197 Index = A_Base->getIndex(); 198 IsExtern = 1; 199 } else { 200 Index = A_SD.getFragment()->getParent()->getOrdinal() + 1; 201 IsExtern = 0; 202 } 203 Type = MachO::X86_64_RELOC_UNSIGNED; 204 205 MachO::any_relocation_info MRE; 206 MRE.r_word0 = FixupOffset; 207 MRE.r_word1 = ((Index << 0) | 208 (IsPCRel << 24) | 209 (Log2Size << 25) | 210 (IsExtern << 27) | 211 (Type << 28)); 212 Writer->addRelocation(Fragment->getParent(), MRE); 213 214 if (B_Base) { 215 Index = B_Base->getIndex(); 216 IsExtern = 1; 217 } else { 218 Index = B_SD.getFragment()->getParent()->getOrdinal() + 1; 219 IsExtern = 0; 220 } 221 Type = MachO::X86_64_RELOC_SUBTRACTOR; 222 } else { 223 const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); 224 const MCSymbolData &SD = Asm.getSymbolData(*Symbol); 225 const MCSymbolData *Base = Asm.getAtom(&SD); 226 227 // Relocations inside debug sections always use local relocations when 228 // possible. This seems to be done because the debugger doesn't fully 229 // understand x86_64 relocation entries, and expects to find values that 230 // have already been fixed up. 231 if (Symbol->isInSection()) { 232 const MCSectionMachO &Section = static_cast<const MCSectionMachO&>( 233 Fragment->getParent()->getSection()); 234 if (Section.hasAttribute(MachO::S_ATTR_DEBUG)) 235 Base = nullptr; 236 } 237 238 // x86_64 almost always uses external relocations, except when there is no 239 // symbol to use as a base address (a local symbol with no preceding 240 // non-local symbol). 241 if (Base) { 242 Index = Base->getIndex(); 243 IsExtern = 1; 244 245 // Add the local offset, if needed. 246 if (Base != &SD) 247 Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base); 248 } else if (Symbol->isInSection() && !Symbol->isVariable()) { 249 // The index is the section ordinal (1-based). 250 Index = SD.getFragment()->getParent()->getOrdinal() + 1; 251 IsExtern = 0; 252 Value += Writer->getSymbolAddress(&SD, Layout); 253 254 if (IsPCRel) 255 Value -= FixupAddress + (1 << Log2Size); 256 } else if (Symbol->isVariable()) { 257 const MCExpr *Value = Symbol->getVariableValue(); 258 int64_t Res; 259 bool isAbs = Value->EvaluateAsAbsolute(Res, Layout, 260 Writer->getSectionAddressMap()); 261 if (isAbs) { 262 FixedValue = Res; 263 return; 264 } else { 265 report_fatal_error("unsupported relocation of variable '" + 266 Symbol->getName() + "'", false); 267 } 268 } else { 269 report_fatal_error("unsupported relocation of undefined symbol '" + 270 Symbol->getName() + "'", false); 271 } 272 273 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); 274 if (IsPCRel) { 275 if (IsRIPRel) { 276 if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { 277 // x86_64 distinguishes movq foo@GOTPCREL so that the linker can 278 // rewrite the movq to an leaq at link time if the symbol ends up in 279 // the same linkage unit. 280 if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load) 281 Type = MachO::X86_64_RELOC_GOT_LOAD; 282 else 283 Type = MachO::X86_64_RELOC_GOT; 284 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { 285 Type = MachO::X86_64_RELOC_TLV; 286 } else if (Modifier != MCSymbolRefExpr::VK_None) { 287 report_fatal_error("unsupported symbol modifier in relocation", 288 false); 289 } else { 290 Type = MachO::X86_64_RELOC_SIGNED; 291 292 // The Darwin x86_64 relocation format has a problem where it cannot 293 // encode an address (L<foo> + <constant>) which is outside the atom 294 // containing L<foo>. Generally, this shouldn't occur but it does 295 // happen when we have a RIPrel instruction with data following the 296 // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel 297 // adjustment Darwin x86_64 uses, the offset is still negative and the 298 // linker has no way to recognize this. 299 // 300 // To work around this, Darwin uses several special relocation types 301 // to indicate the offsets. However, the specification or 302 // implementation of these seems to also be incomplete; they should 303 // adjust the addend as well based on the actual encoded instruction 304 // (the additional bias), but instead appear to just look at the final 305 // offset. 306 switch (-(Target.getConstant() + (1LL << Log2Size))) { 307 case 1: Type = MachO::X86_64_RELOC_SIGNED_1; break; 308 case 2: Type = MachO::X86_64_RELOC_SIGNED_2; break; 309 case 4: Type = MachO::X86_64_RELOC_SIGNED_4; break; 310 } 311 } 312 } else { 313 if (Modifier != MCSymbolRefExpr::VK_None) 314 report_fatal_error("unsupported symbol modifier in branch " 315 "relocation", false); 316 317 Type = MachO::X86_64_RELOC_BRANCH; 318 } 319 } else { 320 if (Modifier == MCSymbolRefExpr::VK_GOT) { 321 Type = MachO::X86_64_RELOC_GOT; 322 } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { 323 // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which 324 // case all we do is set the PCrel bit in the relocation entry; this is 325 // used with exception handling, for example. The source is required to 326 // include any necessary offset directly. 327 Type = MachO::X86_64_RELOC_GOT; 328 IsPCRel = 1; 329 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { 330 report_fatal_error("TLVP symbol modifier should have been rip-rel", 331 false); 332 } else if (Modifier != MCSymbolRefExpr::VK_None) 333 report_fatal_error("unsupported symbol modifier in relocation", false); 334 else { 335 Type = MachO::X86_64_RELOC_UNSIGNED; 336 unsigned Kind = Fixup.getKind(); 337 if (Kind == X86::reloc_signed_4byte) 338 report_fatal_error("32-bit absolute addressing is not supported in " 339 "64-bit mode", false); 340 } 341 } 342 } 343 344 // x86_64 always writes custom values into the fixups. 345 FixedValue = Value; 346 347 // struct relocation_info (8 bytes) 348 MachO::any_relocation_info MRE; 349 MRE.r_word0 = FixupOffset; 350 MRE.r_word1 = ((Index << 0) | 351 (IsPCRel << 24) | 352 (Log2Size << 25) | 353 (IsExtern << 27) | 354 (Type << 28)); 355 Writer->addRelocation(Fragment->getParent(), MRE); 356 } 357 358 bool X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter *Writer, 359 const MCAssembler &Asm, 360 const MCAsmLayout &Layout, 361 const MCFragment *Fragment, 362 const MCFixup &Fixup, 363 MCValue Target, 364 unsigned Log2Size, 365 uint64_t &FixedValue) { 366 uint64_t OriginalFixedValue = FixedValue; 367 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); 368 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); 369 unsigned Type = MachO::GENERIC_RELOC_VANILLA; 370 371 // See <reloc.h>. 372 const MCSymbol *A = &Target.getSymA()->getSymbol(); 373 const MCSymbolData *A_SD = &Asm.getSymbolData(*A); 374 375 if (!A_SD->getFragment()) 376 report_fatal_error("symbol '" + A->getName() + 377 "' can not be undefined in a subtraction expression", 378 false); 379 380 uint32_t Value = Writer->getSymbolAddress(A_SD, Layout); 381 uint64_t SecAddr = Writer->getSectionAddress(A_SD->getFragment()->getParent()); 382 FixedValue += SecAddr; 383 uint32_t Value2 = 0; 384 385 if (const MCSymbolRefExpr *B = Target.getSymB()) { 386 const MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol()); 387 388 if (!B_SD->getFragment()) 389 report_fatal_error("symbol '" + B->getSymbol().getName() + 390 "' can not be undefined in a subtraction expression", 391 false); 392 393 // Select the appropriate difference relocation type. 394 // 395 // Note that there is no longer any semantic difference between these two 396 // relocation types from the linkers point of view, this is done solely for 397 // pedantic compatibility with 'as'. 398 Type = A_SD->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF : 399 (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF; 400 Value2 = Writer->getSymbolAddress(B_SD, Layout); 401 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent()); 402 } 403 404 // Relocations are written out in reverse order, so the PAIR comes first. 405 if (Type == MachO::GENERIC_RELOC_SECTDIFF || 406 Type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { 407 // If the offset is too large to fit in a scattered relocation, 408 // we're hosed. It's an unfortunate limitation of the MachO format. 409 if (FixupOffset > 0xffffff) { 410 char Buffer[32]; 411 format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer)); 412 Asm.getContext().FatalError(Fixup.getLoc(), 413 Twine("Section too large, can't encode " 414 "r_address (") + Buffer + 415 ") into 24 bits of scattered " 416 "relocation entry."); 417 llvm_unreachable("fatal error returned?!"); 418 } 419 420 MachO::any_relocation_info MRE; 421 MRE.r_word0 = ((0 << 0) | // r_address 422 (MachO::GENERIC_RELOC_PAIR << 24) | // r_type 423 (Log2Size << 28) | 424 (IsPCRel << 30) | 425 MachO::R_SCATTERED); 426 MRE.r_word1 = Value2; 427 Writer->addRelocation(Fragment->getParent(), MRE); 428 } else { 429 // If the offset is more than 24-bits, it won't fit in a scattered 430 // relocation offset field, so we fall back to using a non-scattered 431 // relocation. This is a bit risky, as if the offset reaches out of 432 // the block and the linker is doing scattered loading on this 433 // symbol, things can go badly. 434 // 435 // Required for 'as' compatibility. 436 if (FixupOffset > 0xffffff) { 437 FixedValue = OriginalFixedValue; 438 return false; 439 } 440 } 441 442 MachO::any_relocation_info MRE; 443 MRE.r_word0 = ((FixupOffset << 0) | 444 (Type << 24) | 445 (Log2Size << 28) | 446 (IsPCRel << 30) | 447 MachO::R_SCATTERED); 448 MRE.r_word1 = Value; 449 Writer->addRelocation(Fragment->getParent(), MRE); 450 return true; 451 } 452 453 void X86MachObjectWriter::RecordTLVPRelocation(MachObjectWriter *Writer, 454 const MCAssembler &Asm, 455 const MCAsmLayout &Layout, 456 const MCFragment *Fragment, 457 const MCFixup &Fixup, 458 MCValue Target, 459 uint64_t &FixedValue) { 460 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP && 461 !is64Bit() && 462 "Should only be called with a 32-bit TLVP relocation!"); 463 464 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); 465 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); 466 unsigned IsPCRel = 0; 467 468 // Get the symbol data. 469 const MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol()); 470 unsigned Index = SD_A->getIndex(); 471 472 // We're only going to have a second symbol in pic mode and it'll be a 473 // subtraction from the picbase. For 32-bit pic the addend is the difference 474 // between the picbase and the next address. For 32-bit static the addend is 475 // zero. 476 if (Target.getSymB()) { 477 // If this is a subtraction then we're pcrel. 478 uint32_t FixupAddress = 479 Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); 480 const MCSymbolData *SD_B = 481 &Asm.getSymbolData(Target.getSymB()->getSymbol()); 482 IsPCRel = 1; 483 FixedValue = (FixupAddress - Writer->getSymbolAddress(SD_B, Layout) + 484 Target.getConstant()); 485 FixedValue += 1ULL << Log2Size; 486 } else { 487 FixedValue = 0; 488 } 489 490 // struct relocation_info (8 bytes) 491 MachO::any_relocation_info MRE; 492 MRE.r_word0 = Value; 493 MRE.r_word1 = ((Index << 0) | 494 (IsPCRel << 24) | 495 (Log2Size << 25) | 496 (1 << 27) | // r_extern 497 (MachO::GENERIC_RELOC_TLV << 28)); // r_type 498 Writer->addRelocation(Fragment->getParent(), MRE); 499 } 500 501 void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer, 502 const MCAssembler &Asm, 503 const MCAsmLayout &Layout, 504 const MCFragment *Fragment, 505 const MCFixup &Fixup, 506 MCValue Target, 507 uint64_t &FixedValue) { 508 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); 509 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); 510 511 // If this is a 32-bit TLVP reloc it's handled a bit differently. 512 if (Target.getSymA() && 513 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) { 514 RecordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, 515 FixedValue); 516 return; 517 } 518 519 // If this is a difference or a defined symbol plus an offset, then we need a 520 // scattered relocation entry. Differences always require scattered 521 // relocations. 522 if (Target.getSymB()) { 523 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, 524 Target, Log2Size, FixedValue); 525 return; 526 } 527 528 // Get the symbol data, if any. 529 const MCSymbolData *SD = nullptr; 530 if (Target.getSymA()) 531 SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); 532 533 // If this is an internal relocation with an offset, it also needs a scattered 534 // relocation entry. 535 uint32_t Offset = Target.getConstant(); 536 if (IsPCRel) 537 Offset += 1 << Log2Size; 538 // Try to record the scattered relocation if needed. Fall back to non 539 // scattered if necessary (see comments in RecordScatteredRelocation() 540 // for details). 541 if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD) && 542 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, 543 Target, Log2Size, FixedValue)) 544 return; 545 546 // See <reloc.h>. 547 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); 548 unsigned Index = 0; 549 unsigned IsExtern = 0; 550 unsigned Type = 0; 551 552 if (Target.isAbsolute()) { // constant 553 // SymbolNum of 0 indicates the absolute section. 554 // 555 // FIXME: Currently, these are never generated (see code below). I cannot 556 // find a case where they are actually emitted. 557 Type = MachO::GENERIC_RELOC_VANILLA; 558 } else { 559 // Resolve constant variables. 560 if (SD->getSymbol().isVariable()) { 561 int64_t Res; 562 if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( 563 Res, Layout, Writer->getSectionAddressMap())) { 564 FixedValue = Res; 565 return; 566 } 567 } 568 569 // Check whether we need an external or internal relocation. 570 if (Writer->doesSymbolRequireExternRelocation(SD)) { 571 IsExtern = 1; 572 Index = SD->getIndex(); 573 // For external relocations, make sure to offset the fixup value to 574 // compensate for the addend of the symbol address, if it was 575 // undefined. This occurs with weak definitions, for example. 576 if (!SD->getSymbol().isUndefined()) 577 FixedValue -= Layout.getSymbolOffset(SD); 578 } else { 579 // The index is the section ordinal (1-based). 580 const MCSectionData &SymSD = Asm.getSectionData( 581 SD->getSymbol().getSection()); 582 Index = SymSD.getOrdinal() + 1; 583 FixedValue += Writer->getSectionAddress(&SymSD); 584 } 585 if (IsPCRel) 586 FixedValue -= Writer->getSectionAddress(Fragment->getParent()); 587 588 Type = MachO::GENERIC_RELOC_VANILLA; 589 } 590 591 // struct relocation_info (8 bytes) 592 MachO::any_relocation_info MRE; 593 MRE.r_word0 = FixupOffset; 594 MRE.r_word1 = ((Index << 0) | 595 (IsPCRel << 24) | 596 (Log2Size << 25) | 597 (IsExtern << 27) | 598 (Type << 28)); 599 Writer->addRelocation(Fragment->getParent(), MRE); 600 } 601 602 MCObjectWriter *llvm::createX86MachObjectWriter(raw_ostream &OS, 603 bool Is64Bit, 604 uint32_t CPUType, 605 uint32_t CPUSubtype) { 606 return createMachObjectWriter(new X86MachObjectWriter(Is64Bit, 607 CPUType, 608 CPUSubtype), 609 OS, /*IsLittleEndian=*/true); 610 } 611