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