Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// |
| 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 | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "assembler" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | bea2c95 | 2009-08-22 19:19:12 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
| 16 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 45f8c09 | 2010-02-02 19:38:14 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MachO.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetAsmBackend.h" |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 29 | |
| 30 | // FIXME: Gross. |
| 31 | #include "../Target/X86/X86FixupKinds.h" |
| 32 | |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 33 | #include <vector> |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 36 | class MachObjectWriter; |
| 37 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 38 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); |
| 39 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 40 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 41 | // what is a 64-bit assembler value used for computation into a value in the |
| 42 | // object file, which may truncate it. We should detect that truncation where |
| 43 | // invalid and report errors back. |
| 44 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 45 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 46 | MachObjectWriter &MOW); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 47 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 48 | static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW); |
| 49 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 50 | /// isVirtualSection - Check if this is a section which does not actually exist |
| 51 | /// in the object file. |
| 52 | static bool isVirtualSection(const MCSection &Section) { |
| 53 | // FIXME: Lame. |
| 54 | const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section); |
Daniel Dunbar | 99d22ad | 2010-03-15 21:56:38 +0000 | [diff] [blame] | 55 | return (SMO.getType() == MCSectionMachO::S_ZEROFILL); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Duncan Sands | 9a7795c | 2010-02-17 14:52:22 +0000 | [diff] [blame] | 58 | static unsigned getFixupKindLog2Size(unsigned Kind) { |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 59 | switch (Kind) { |
| 60 | default: llvm_unreachable("invalid fixup kind!"); |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 61 | case X86::reloc_pcrel_1byte: |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 62 | case FK_Data_1: return 0; |
| 63 | case FK_Data_2: return 1; |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 64 | case X86::reloc_pcrel_4byte: |
| 65 | case X86::reloc_riprel_4byte: |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 66 | case FK_Data_4: return 2; |
| 67 | case FK_Data_8: return 3; |
| 68 | } |
| 69 | } |
| 70 | |
Duncan Sands | 9a7795c | 2010-02-17 14:52:22 +0000 | [diff] [blame] | 71 | static bool isFixupKindPCRel(unsigned Kind) { |
Daniel Dunbar | 591047f | 2010-02-13 09:45:59 +0000 | [diff] [blame] | 72 | switch (Kind) { |
| 73 | default: |
| 74 | return false; |
| 75 | case X86::reloc_pcrel_1byte: |
| 76 | case X86::reloc_pcrel_4byte: |
| 77 | case X86::reloc_riprel_4byte: |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 82 | class MachObjectWriter { |
| 83 | // See <mach-o/loader.h>. |
| 84 | enum { |
| 85 | Header_Magic32 = 0xFEEDFACE, |
| 86 | Header_Magic64 = 0xFEEDFACF |
| 87 | }; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 89 | enum { |
| 90 | Header32Size = 28, |
| 91 | Header64Size = 32, |
| 92 | SegmentLoadCommand32Size = 56, |
| 93 | SegmentLoadCommand64Size = 72, |
| 94 | Section32Size = 68, |
| 95 | Section64Size = 80, |
| 96 | SymtabLoadCommandSize = 24, |
| 97 | DysymtabLoadCommandSize = 80, |
| 98 | Nlist32Size = 12, |
| 99 | Nlist64Size = 16, |
| 100 | RelocationInfoSize = 8 |
| 101 | }; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 102 | |
| 103 | enum HeaderFileType { |
| 104 | HFT_Object = 0x1 |
| 105 | }; |
| 106 | |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 107 | enum HeaderFlags { |
| 108 | HF_SubsectionsViaSymbols = 0x2000 |
| 109 | }; |
| 110 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 111 | enum LoadCommandType { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 112 | LCT_Segment = 0x1, |
| 113 | LCT_Symtab = 0x2, |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 114 | LCT_Dysymtab = 0xb, |
| 115 | LCT_Segment64 = 0x19 |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 118 | // See <mach-o/nlist.h>. |
| 119 | enum SymbolTypeType { |
| 120 | STT_Undefined = 0x00, |
| 121 | STT_Absolute = 0x02, |
| 122 | STT_Section = 0x0e |
| 123 | }; |
| 124 | |
| 125 | enum SymbolTypeFlags { |
| 126 | // If any of these bits are set, then the entry is a stab entry number (see |
| 127 | // <mach-o/stab.h>. Otherwise the other masks apply. |
| 128 | STF_StabsEntryMask = 0xe0, |
| 129 | |
| 130 | STF_TypeMask = 0x0e, |
| 131 | STF_External = 0x01, |
| 132 | STF_PrivateExtern = 0x10 |
| 133 | }; |
| 134 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 135 | /// IndirectSymbolFlags - Flags for encoding special values in the indirect |
| 136 | /// symbol entry. |
| 137 | enum IndirectSymbolFlags { |
| 138 | ISF_Local = 0x80000000, |
| 139 | ISF_Absolute = 0x40000000 |
| 140 | }; |
| 141 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 142 | /// RelocationFlags - Special flags for addresses. |
| 143 | enum RelocationFlags { |
| 144 | RF_Scattered = 0x80000000 |
| 145 | }; |
| 146 | |
| 147 | enum RelocationInfoType { |
| 148 | RIT_Vanilla = 0, |
| 149 | RIT_Pair = 1, |
| 150 | RIT_Difference = 2, |
| 151 | RIT_PreboundLazyPointer = 3, |
| 152 | RIT_LocalDifference = 4 |
| 153 | }; |
| 154 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 155 | /// MachSymbolData - Helper struct for containing some precomputed information |
| 156 | /// on symbols. |
| 157 | struct MachSymbolData { |
| 158 | MCSymbolData *SymbolData; |
| 159 | uint64_t StringIndex; |
| 160 | uint8_t SectionIndex; |
| 161 | |
| 162 | // Support lexicographic sorting. |
| 163 | bool operator<(const MachSymbolData &RHS) const { |
| 164 | const std::string &Name = SymbolData->getSymbol().getName(); |
| 165 | return Name < RHS.SymbolData->getSymbol().getName(); |
| 166 | } |
| 167 | }; |
| 168 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 169 | raw_ostream &OS; |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 170 | unsigned Is64Bit : 1; |
| 171 | unsigned IsLSB : 1; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 172 | |
| 173 | public: |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 174 | MachObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool _IsLSB = true) |
| 175 | : OS(_OS), Is64Bit(_Is64Bit), IsLSB(_IsLSB) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// @name Helper Methods |
| 179 | /// @{ |
| 180 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 181 | void Write8(uint8_t Value) { |
| 182 | OS << char(Value); |
| 183 | } |
| 184 | |
| 185 | void Write16(uint16_t Value) { |
| 186 | if (IsLSB) { |
| 187 | Write8(uint8_t(Value >> 0)); |
| 188 | Write8(uint8_t(Value >> 8)); |
| 189 | } else { |
| 190 | Write8(uint8_t(Value >> 8)); |
| 191 | Write8(uint8_t(Value >> 0)); |
| 192 | } |
| 193 | } |
| 194 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 195 | void Write32(uint32_t Value) { |
| 196 | if (IsLSB) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 197 | Write16(uint16_t(Value >> 0)); |
| 198 | Write16(uint16_t(Value >> 16)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 199 | } else { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 200 | Write16(uint16_t(Value >> 16)); |
| 201 | Write16(uint16_t(Value >> 0)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void Write64(uint64_t Value) { |
| 206 | if (IsLSB) { |
| 207 | Write32(uint32_t(Value >> 0)); |
| 208 | Write32(uint32_t(Value >> 32)); |
| 209 | } else { |
| 210 | Write32(uint32_t(Value >> 32)); |
| 211 | Write32(uint32_t(Value >> 0)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | void WriteZeros(unsigned N) { |
| 216 | const char Zeros[16] = { 0 }; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 217 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 218 | for (unsigned i = 0, e = N / 16; i != e; ++i) |
| 219 | OS << StringRef(Zeros, 16); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 220 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 221 | OS << StringRef(Zeros, N % 16); |
| 222 | } |
| 223 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 224 | void WriteString(StringRef Str, unsigned ZeroFillSize = 0) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 225 | OS << Str; |
| 226 | if (ZeroFillSize) |
| 227 | WriteZeros(ZeroFillSize - Str.size()); |
| 228 | } |
| 229 | |
| 230 | /// @} |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 231 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 232 | void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize, |
| 233 | bool SubsectionsViaSymbols) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 234 | uint32_t Flags = 0; |
| 235 | |
| 236 | if (SubsectionsViaSymbols) |
| 237 | Flags |= HF_SubsectionsViaSymbols; |
| 238 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 239 | // struct mach_header (28 bytes) or |
| 240 | // struct mach_header_64 (32 bytes) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 241 | |
| 242 | uint64_t Start = OS.tell(); |
| 243 | (void) Start; |
| 244 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 245 | Write32(Is64Bit ? Header_Magic64 : Header_Magic32); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 246 | |
| 247 | // FIXME: Support cputype. |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 248 | Write32(Is64Bit ? MachO::CPUTypeX86_64 : MachO::CPUTypeI386); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 249 | // FIXME: Support cpusubtype. |
Chris Lattner | 45f8c09 | 2010-02-02 19:38:14 +0000 | [diff] [blame] | 250 | Write32(MachO::CPUSubType_I386_ALL); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 251 | Write32(HFT_Object); |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 252 | Write32(NumLoadCommands); // Object files have a single load command, the |
| 253 | // segment. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 254 | Write32(LoadCommandsSize); |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 255 | Write32(Flags); |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 256 | if (Is64Bit) |
| 257 | Write32(0); // reserved |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 259 | assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 262 | /// WriteSegmentLoadCommand - Write a segment load command. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 263 | /// |
| 264 | /// \arg NumSections - The number of sections in this segment. |
| 265 | /// \arg SectionDataSize - The total size of the sections. |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 266 | void WriteSegmentLoadCommand(unsigned NumSections, |
| 267 | uint64_t VMSize, |
| 268 | uint64_t SectionDataStartOffset, |
| 269 | uint64_t SectionDataSize) { |
| 270 | // struct segment_command (56 bytes) or |
| 271 | // struct segment_command_64 (72 bytes) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 272 | |
| 273 | uint64_t Start = OS.tell(); |
| 274 | (void) Start; |
| 275 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 276 | unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size : |
| 277 | SegmentLoadCommand32Size; |
| 278 | Write32(Is64Bit ? LCT_Segment64 : LCT_Segment); |
| 279 | Write32(SegmentLoadCommandSize + |
| 280 | NumSections * (Is64Bit ? Section64Size : Section32Size)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 281 | |
| 282 | WriteString("", 16); |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 283 | if (Is64Bit) { |
| 284 | Write64(0); // vmaddr |
| 285 | Write64(VMSize); // vmsize |
| 286 | Write64(SectionDataStartOffset); // file offset |
| 287 | Write64(SectionDataSize); // file size |
| 288 | } else { |
| 289 | Write32(0); // vmaddr |
| 290 | Write32(VMSize); // vmsize |
| 291 | Write32(SectionDataStartOffset); // file offset |
| 292 | Write32(SectionDataSize); // file size |
| 293 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 294 | Write32(0x7); // maxprot |
| 295 | Write32(0x7); // initprot |
| 296 | Write32(NumSections); |
| 297 | Write32(0); // flags |
| 298 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 299 | assert(OS.tell() - Start == SegmentLoadCommandSize); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 302 | void WriteSection(const MCSectionData &SD, uint64_t FileOffset, |
| 303 | uint64_t RelocationsStart, unsigned NumRelocations) { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 304 | // The offset is unused for virtual sections. |
| 305 | if (isVirtualSection(SD.getSection())) { |
| 306 | assert(SD.getFileSize() == 0 && "Invalid file size!"); |
| 307 | FileOffset = 0; |
| 308 | } |
| 309 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 310 | // struct section (68 bytes) or |
| 311 | // struct section_64 (80 bytes) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 312 | |
| 313 | uint64_t Start = OS.tell(); |
| 314 | (void) Start; |
| 315 | |
| 316 | // FIXME: cast<> support! |
| 317 | const MCSectionMachO &Section = |
| 318 | static_cast<const MCSectionMachO&>(SD.getSection()); |
| 319 | WriteString(Section.getSectionName(), 16); |
| 320 | WriteString(Section.getSegmentName(), 16); |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 321 | if (Is64Bit) { |
| 322 | Write64(SD.getAddress()); // address |
| 323 | Write64(SD.getSize()); // size |
| 324 | } else { |
| 325 | Write32(SD.getAddress()); // address |
| 326 | Write32(SD.getSize()); // size |
| 327 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 328 | Write32(FileOffset); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 329 | |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 330 | unsigned Flags = Section.getTypeAndAttributes(); |
| 331 | if (SD.hasInstructions()) |
| 332 | Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS; |
| 333 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 334 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 335 | Write32(Log2_32(SD.getAlignment())); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 336 | Write32(NumRelocations ? RelocationsStart : 0); |
| 337 | Write32(NumRelocations); |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 338 | Write32(Flags); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 339 | Write32(0); // reserved1 |
| 340 | Write32(Section.getStubSize()); // reserved2 |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 341 | if (Is64Bit) |
| 342 | Write32(0); // reserved3 |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 344 | assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 345 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 347 | void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, |
| 348 | uint32_t StringTableOffset, |
| 349 | uint32_t StringTableSize) { |
| 350 | // struct symtab_command (24 bytes) |
| 351 | |
| 352 | uint64_t Start = OS.tell(); |
| 353 | (void) Start; |
| 354 | |
| 355 | Write32(LCT_Symtab); |
| 356 | Write32(SymtabLoadCommandSize); |
| 357 | Write32(SymbolOffset); |
| 358 | Write32(NumSymbols); |
| 359 | Write32(StringTableOffset); |
| 360 | Write32(StringTableSize); |
| 361 | |
| 362 | assert(OS.tell() - Start == SymtabLoadCommandSize); |
| 363 | } |
| 364 | |
| 365 | void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
| 366 | uint32_t NumLocalSymbols, |
| 367 | uint32_t FirstExternalSymbol, |
| 368 | uint32_t NumExternalSymbols, |
| 369 | uint32_t FirstUndefinedSymbol, |
| 370 | uint32_t NumUndefinedSymbols, |
| 371 | uint32_t IndirectSymbolOffset, |
| 372 | uint32_t NumIndirectSymbols) { |
| 373 | // struct dysymtab_command (80 bytes) |
| 374 | |
| 375 | uint64_t Start = OS.tell(); |
| 376 | (void) Start; |
| 377 | |
| 378 | Write32(LCT_Dysymtab); |
| 379 | Write32(DysymtabLoadCommandSize); |
| 380 | Write32(FirstLocalSymbol); |
| 381 | Write32(NumLocalSymbols); |
| 382 | Write32(FirstExternalSymbol); |
| 383 | Write32(NumExternalSymbols); |
| 384 | Write32(FirstUndefinedSymbol); |
| 385 | Write32(NumUndefinedSymbols); |
| 386 | Write32(0); // tocoff |
| 387 | Write32(0); // ntoc |
| 388 | Write32(0); // modtaboff |
| 389 | Write32(0); // nmodtab |
| 390 | Write32(0); // extrefsymoff |
| 391 | Write32(0); // nextrefsyms |
| 392 | Write32(IndirectSymbolOffset); |
| 393 | Write32(NumIndirectSymbols); |
| 394 | Write32(0); // extreloff |
| 395 | Write32(0); // nextrel |
| 396 | Write32(0); // locreloff |
| 397 | Write32(0); // nlocrel |
| 398 | |
| 399 | assert(OS.tell() - Start == DysymtabLoadCommandSize); |
| 400 | } |
| 401 | |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 402 | void WriteNlist(MachSymbolData &MSD) { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 403 | MCSymbolData &Data = *MSD.SymbolData; |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 404 | const MCSymbol &Symbol = Data.getSymbol(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 405 | uint8_t Type = 0; |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 406 | uint16_t Flags = Data.getFlags(); |
| 407 | uint32_t Address = 0; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 408 | |
| 409 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 410 | // |
| 411 | // FIXME: Are the prebound or indirect fields possible here? |
| 412 | if (Symbol.isUndefined()) |
| 413 | Type = STT_Undefined; |
| 414 | else if (Symbol.isAbsolute()) |
| 415 | Type = STT_Absolute; |
| 416 | else |
| 417 | Type = STT_Section; |
| 418 | |
| 419 | // FIXME: Set STAB bits. |
| 420 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 421 | if (Data.isPrivateExtern()) |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 422 | Type |= STF_PrivateExtern; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 423 | |
| 424 | // Set external bit. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 425 | if (Data.isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 426 | Type |= STF_External; |
| 427 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 428 | // Compute the symbol address. |
| 429 | if (Symbol.isDefined()) { |
| 430 | if (Symbol.isAbsolute()) { |
| 431 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 432 | } else { |
Daniel Dunbar | 8f0448c | 2010-03-11 18:22:51 +0000 | [diff] [blame] | 433 | Address = Data.getAddress(); |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 434 | } |
| 435 | } else if (Data.isCommon()) { |
| 436 | // Common symbols are encoded with the size in the address |
| 437 | // field, and their alignment in the flags. |
| 438 | Address = Data.getCommonSize(); |
| 439 | |
| 440 | // Common alignment is packed into the 'desc' bits. |
| 441 | if (unsigned Align = Data.getCommonAlignment()) { |
| 442 | unsigned Log2Size = Log2_32(Align); |
| 443 | assert((1U << Log2Size) == Align && "Invalid 'common' alignment!"); |
| 444 | if (Log2Size > 15) |
| 445 | llvm_report_error("invalid 'common' alignment '" + |
| 446 | Twine(Align) + "'"); |
| 447 | // FIXME: Keep this mask with the SymbolFlags enumeration. |
| 448 | Flags = (Flags & 0xF0FF) | (Log2Size << 8); |
| 449 | } |
| 450 | } |
| 451 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 452 | // struct nlist (12 bytes) |
| 453 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 454 | Write32(MSD.StringIndex); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 455 | Write8(Type); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 456 | Write8(MSD.SectionIndex); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 458 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 459 | // value. |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 460 | Write16(Flags); |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 461 | if (Is64Bit) |
| 462 | Write64(Address); |
| 463 | else |
| 464 | Write32(Address); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 467 | struct MachRelocationEntry { |
| 468 | uint32_t Word0; |
| 469 | uint32_t Word1; |
| 470 | }; |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 471 | void ComputeScatteredRelocationInfo(MCAssembler &Asm, MCFragment &Fragment, |
Daniel Dunbar | 27ade18 | 2010-02-11 21:29:29 +0000 | [diff] [blame] | 472 | MCAsmFixup &Fixup, |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 473 | const MCValue &Target, |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 474 | std::vector<MachRelocationEntry> &Relocs) { |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 475 | uint32_t Address = Fragment.getOffset() + Fixup.Offset; |
Daniel Dunbar | e180fa9 | 2010-03-09 21:27:30 +0000 | [diff] [blame] | 476 | unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind); |
Daniel Dunbar | eb3804e | 2010-02-17 23:45:16 +0000 | [diff] [blame] | 477 | unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 478 | unsigned Type = RIT_Vanilla; |
| 479 | |
| 480 | // See <reloc.h>. |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 481 | const MCSymbol *A = Target.getSymA(); |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 482 | MCSymbolData *A_SD = &Asm.getSymbolData(*A); |
Daniel Dunbar | 0ce6bd5 | 2010-03-08 21:10:39 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | a015c1c | 2010-03-10 00:58:25 +0000 | [diff] [blame] | 484 | if (!A_SD->getFragment()) |
Daniel Dunbar | 0ce6bd5 | 2010-03-08 21:10:39 +0000 | [diff] [blame] | 485 | llvm_report_error("symbol '" + A->getName() + |
| 486 | "' can not be undefined in a subtraction expression"); |
| 487 | |
Daniel Dunbar | 8f0448c | 2010-03-11 18:22:51 +0000 | [diff] [blame] | 488 | uint32_t Value = A_SD->getAddress(); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 489 | uint32_t Value2 = 0; |
| 490 | |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 491 | if (const MCSymbol *B = Target.getSymB()) { |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 492 | MCSymbolData *B_SD = &Asm.getSymbolData(*B); |
Daniel Dunbar | 0ce6bd5 | 2010-03-08 21:10:39 +0000 | [diff] [blame] | 493 | |
Daniel Dunbar | a015c1c | 2010-03-10 00:58:25 +0000 | [diff] [blame] | 494 | if (!B_SD->getFragment()) |
Daniel Dunbar | 0ce6bd5 | 2010-03-08 21:10:39 +0000 | [diff] [blame] | 495 | llvm_report_error("symbol '" + B->getName() + |
| 496 | "' can not be undefined in a subtraction expression"); |
| 497 | |
Daniel Dunbar | 07a9641 | 2010-03-10 02:10:29 +0000 | [diff] [blame] | 498 | // Select the appropriate difference relocation type. |
| 499 | // |
| 500 | // Note that there is no longer any semantic difference between these two |
| 501 | // relocation types from the linkers point of view, this is done solely |
| 502 | // for pedantic compatibility with 'as'. |
Daniel Dunbar | a015c1c | 2010-03-10 00:58:25 +0000 | [diff] [blame] | 503 | Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference; |
Daniel Dunbar | 8f0448c | 2010-03-11 18:22:51 +0000 | [diff] [blame] | 504 | Value2 = B_SD->getAddress(); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 507 | MachRelocationEntry MRE; |
| 508 | MRE.Word0 = ((Address << 0) | |
| 509 | (Type << 24) | |
| 510 | (Log2Size << 28) | |
| 511 | (IsPCRel << 30) | |
| 512 | RF_Scattered); |
| 513 | MRE.Word1 = Value; |
| 514 | Relocs.push_back(MRE); |
| 515 | |
Daniel Dunbar | a015c1c | 2010-03-10 00:58:25 +0000 | [diff] [blame] | 516 | if (Type == RIT_Difference || Type == RIT_LocalDifference) { |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 517 | MachRelocationEntry MRE; |
| 518 | MRE.Word0 = ((0 << 0) | |
Daniel Dunbar | aef9d7a | 2010-03-09 21:27:47 +0000 | [diff] [blame] | 519 | (RIT_Pair << 24) | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 520 | (Log2Size << 28) | |
Daniel Dunbar | aef9d7a | 2010-03-09 21:27:47 +0000 | [diff] [blame] | 521 | (IsPCRel << 30) | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 522 | RF_Scattered); |
| 523 | MRE.Word1 = Value2; |
| 524 | Relocs.push_back(MRE); |
| 525 | } |
| 526 | } |
| 527 | |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 528 | void ComputeRelocationInfo(MCAssembler &Asm, MCDataFragment &Fragment, |
Daniel Dunbar | 27ade18 | 2010-02-11 21:29:29 +0000 | [diff] [blame] | 529 | MCAsmFixup &Fixup, |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 530 | std::vector<MachRelocationEntry> &Relocs) { |
Daniel Dunbar | f3a066f | 2010-03-09 21:27:58 +0000 | [diff] [blame] | 531 | unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind); |
| 532 | unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind); |
| 533 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 534 | // FIXME: Share layout object. |
| 535 | MCAsmLayout Layout(Asm); |
| 536 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 537 | // Evaluate the fixup; if the value was resolved, no relocation is needed. |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 538 | MCValue Target; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 539 | if (Asm.EvaluateFixup(Layout, Fixup, &Fragment, Target, Fixup.FixedValue)) |
| 540 | return; |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | f3a066f | 2010-03-09 21:27:58 +0000 | [diff] [blame] | 542 | // If this is a difference or a defined symbol plus an offset, then we need |
| 543 | // a scattered relocation entry. |
| 544 | uint32_t Offset = Target.getConstant(); |
| 545 | if (IsPCRel) |
| 546 | Offset += 1 << Log2Size; |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 547 | if (Target.getSymB() || |
| 548 | (Target.getSymA() && !Target.getSymA()->isUndefined() && |
Daniel Dunbar | f3a066f | 2010-03-09 21:27:58 +0000 | [diff] [blame] | 549 | Offset)) |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 550 | return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target, |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 551 | Relocs); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 552 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 553 | // See <reloc.h>. |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 554 | uint32_t Address = Fragment.getOffset() + Fixup.Offset; |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 555 | uint32_t Value = 0; |
| 556 | unsigned Index = 0; |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 557 | unsigned IsExtern = 0; |
| 558 | unsigned Type = 0; |
| 559 | |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 560 | if (Target.isAbsolute()) { // constant |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 561 | // SymbolNum of 0 indicates the absolute section. |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 562 | // |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 563 | // FIXME: Currently, these are never generated (see code below). I cannot |
| 564 | // find a case where they are actually emitted. |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 565 | Type = RIT_Vanilla; |
| 566 | Value = 0; |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 567 | } else { |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 568 | const MCSymbol *Symbol = Target.getSymA(); |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 569 | MCSymbolData *SD = &Asm.getSymbolData(*Symbol); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 570 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 571 | if (Symbol->isUndefined()) { |
| 572 | IsExtern = 1; |
| 573 | Index = SD->getIndex(); |
| 574 | Value = 0; |
| 575 | } else { |
| 576 | // The index is the section ordinal. |
| 577 | // |
| 578 | // FIXME: O(N) |
| 579 | Index = 1; |
Daniel Dunbar | 591047f | 2010-02-13 09:45:59 +0000 | [diff] [blame] | 580 | MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); |
| 581 | for (; it != ie; ++it, ++Index) |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 582 | if (&*it == SD->getFragment()->getParent()) |
| 583 | break; |
Daniel Dunbar | 591047f | 2010-02-13 09:45:59 +0000 | [diff] [blame] | 584 | assert(it != ie && "Unable to find section index!"); |
Daniel Dunbar | 8f0448c | 2010-03-11 18:22:51 +0000 | [diff] [blame] | 585 | Value = SD->getAddress(); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | Type = RIT_Vanilla; |
| 589 | } |
| 590 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 591 | // struct relocation_info (8 bytes) |
| 592 | MachRelocationEntry MRE; |
| 593 | MRE.Word0 = Address; |
| 594 | MRE.Word1 = ((Index << 0) | |
| 595 | (IsPCRel << 24) | |
| 596 | (Log2Size << 25) | |
| 597 | (IsExtern << 27) | |
| 598 | (Type << 28)); |
| 599 | Relocs.push_back(MRE); |
| 600 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 602 | void BindIndirectSymbols(MCAssembler &Asm) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 603 | // This is the point where 'as' creates actual symbols for indirect symbols |
| 604 | // (in the following two passes). It would be easier for us to do this |
| 605 | // sooner when we see the attribute, but that makes getting the order in the |
| 606 | // symbol table much more complicated than it is worth. |
| 607 | // |
| 608 | // FIXME: Revisit this when the dust settles. |
| 609 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 610 | // Bind non lazy symbol pointers first. |
| 611 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 612 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 613 | // FIXME: cast<> support! |
| 614 | const MCSectionMachO &Section = |
| 615 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 616 | |
Daniel Dunbar | 99d22ad | 2010-03-15 21:56:38 +0000 | [diff] [blame] | 617 | if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 618 | continue; |
| 619 | |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 620 | Asm.getOrCreateSymbolData(*it->Symbol); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Then lazy symbol pointers and symbol stubs. |
| 624 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 625 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 626 | // FIXME: cast<> support! |
| 627 | const MCSectionMachO &Section = |
| 628 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 629 | |
Daniel Dunbar | 99d22ad | 2010-03-15 21:56:38 +0000 | [diff] [blame] | 630 | if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS && |
| 631 | Section.getType() != MCSectionMachO::S_SYMBOL_STUBS) |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 632 | continue; |
| 633 | |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 634 | // Set the symbol type to undefined lazy, but only on construction. |
| 635 | // |
| 636 | // FIXME: Do not hardcode. |
| 637 | bool Created; |
| 638 | MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created); |
| 639 | if (Created) |
| 640 | Entry.setFlags(Entry.getFlags() | 0x0001); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 644 | /// ComputeSymbolTable - Compute the symbol table data |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 645 | /// |
| 646 | /// \param StringTable [out] - The string table data. |
| 647 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 648 | /// string table. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 649 | void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable, |
| 650 | std::vector<MachSymbolData> &LocalSymbolData, |
| 651 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 652 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
| 653 | // Build section lookup table. |
| 654 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 655 | unsigned Index = 1; |
| 656 | for (MCAssembler::iterator it = Asm.begin(), |
| 657 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 658 | SectionIndexMap[&it->getSection()] = Index; |
| 659 | assert(Index <= 256 && "Too many sections!"); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 660 | |
| 661 | // Index 0 is always the empty string. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 662 | StringMap<uint64_t> StringIndexMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 663 | StringTable += '\x00'; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 664 | |
| 665 | // Build the symbol arrays and the string table, but only for non-local |
| 666 | // symbols. |
| 667 | // |
| 668 | // The particular order that we collect the symbols and create the string |
| 669 | // table, then sort the symbols is chosen to match 'as'. Even though it |
| 670 | // doesn't matter for correctness, this is important for letting us diff .o |
| 671 | // files. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 672 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 673 | ie = Asm.symbol_end(); it != ie; ++it) { |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 674 | const MCSymbol &Symbol = it->getSymbol(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 675 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 676 | // Ignore assembler temporaries. |
Daniel Dunbar | d6e5908 | 2010-03-15 21:56:50 +0000 | [diff] [blame] | 677 | if (it->getSymbol().isTemporary() && |
| 678 | (!it->getFragment() || |
| 679 | !Asm.getBackend().doesSectionRequireSymbols( |
| 680 | it->getFragment()->getParent()->getSection()))) |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 681 | continue; |
| 682 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 683 | if (!it->isExternal() && !Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 684 | continue; |
| 685 | |
| 686 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 687 | if (!Entry) { |
| 688 | Entry = StringTable.size(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 689 | StringTable += Symbol.getName(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 690 | StringTable += '\x00'; |
| 691 | } |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 692 | |
| 693 | MachSymbolData MSD; |
| 694 | MSD.SymbolData = it; |
| 695 | MSD.StringIndex = Entry; |
| 696 | |
| 697 | if (Symbol.isUndefined()) { |
| 698 | MSD.SectionIndex = 0; |
| 699 | UndefinedSymbolData.push_back(MSD); |
| 700 | } else if (Symbol.isAbsolute()) { |
| 701 | MSD.SectionIndex = 0; |
| 702 | ExternalSymbolData.push_back(MSD); |
| 703 | } else { |
| 704 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 705 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 706 | ExternalSymbolData.push_back(MSD); |
| 707 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 710 | // Now add the data for local symbols. |
| 711 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 712 | ie = Asm.symbol_end(); it != ie; ++it) { |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 713 | const MCSymbol &Symbol = it->getSymbol(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 715 | // Ignore assembler temporaries. |
Daniel Dunbar | d6e5908 | 2010-03-15 21:56:50 +0000 | [diff] [blame] | 716 | if (it->getSymbol().isTemporary() && |
| 717 | (!it->getFragment() || |
| 718 | !Asm.getBackend().doesSectionRequireSymbols( |
| 719 | it->getFragment()->getParent()->getSection()))) |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 720 | continue; |
| 721 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 722 | if (it->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 723 | continue; |
| 724 | |
| 725 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 726 | if (!Entry) { |
| 727 | Entry = StringTable.size(); |
| 728 | StringTable += Symbol.getName(); |
| 729 | StringTable += '\x00'; |
| 730 | } |
| 731 | |
| 732 | MachSymbolData MSD; |
| 733 | MSD.SymbolData = it; |
| 734 | MSD.StringIndex = Entry; |
| 735 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 736 | if (Symbol.isAbsolute()) { |
| 737 | MSD.SectionIndex = 0; |
| 738 | LocalSymbolData.push_back(MSD); |
| 739 | } else { |
| 740 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 741 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 742 | LocalSymbolData.push_back(MSD); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // External and undefined symbols are required to be in lexicographic order. |
| 747 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 748 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 749 | |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 750 | // Set the symbol indices. |
| 751 | Index = 0; |
| 752 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 753 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
| 754 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 755 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 756 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 757 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
| 758 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 759 | // The string table is padded to a multiple of 4. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 760 | while (StringTable.size() % 4) |
| 761 | StringTable += '\x00'; |
| 762 | } |
| 763 | |
| 764 | void WriteObject(MCAssembler &Asm) { |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 765 | unsigned NumSections = Asm.size(); |
| 766 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 767 | // Create symbol data for any indirect symbols. |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 768 | BindIndirectSymbols(Asm); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 770 | // Compute symbol table information. |
| 771 | SmallString<256> StringTable; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 772 | std::vector<MachSymbolData> LocalSymbolData; |
| 773 | std::vector<MachSymbolData> ExternalSymbolData; |
| 774 | std::vector<MachSymbolData> UndefinedSymbolData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 775 | unsigned NumSymbols = Asm.symbol_size(); |
| 776 | |
| 777 | // No symbol table command is written if there are no symbols. |
| 778 | if (NumSymbols) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 779 | ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, |
| 780 | UndefinedSymbolData); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 781 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 782 | // The section data starts after the header, the segment load command (and |
| 783 | // section headers) and the symbol table. |
| 784 | unsigned NumLoadCommands = 1; |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 785 | uint64_t LoadCommandsSize = Is64Bit ? |
| 786 | SegmentLoadCommand64Size + NumSections * Section64Size : |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 787 | SegmentLoadCommand32Size + NumSections * Section32Size; |
| 788 | |
| 789 | // Add the symbol table load command sizes, if used. |
| 790 | if (NumSymbols) { |
| 791 | NumLoadCommands += 2; |
| 792 | LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize; |
| 793 | } |
| 794 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 795 | // Compute the total size of the section data, as well as its file size and |
| 796 | // vm size. |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 797 | uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size) |
| 798 | + LoadCommandsSize; |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 799 | uint64_t SectionDataSize = 0; |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 800 | uint64_t SectionDataFileSize = 0; |
| 801 | uint64_t VMSize = 0; |
| 802 | for (MCAssembler::iterator it = Asm.begin(), |
| 803 | ie = Asm.end(); it != ie; ++it) { |
| 804 | MCSectionData &SD = *it; |
| 805 | |
| 806 | VMSize = std::max(VMSize, SD.getAddress() + SD.getSize()); |
| 807 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 808 | if (isVirtualSection(SD.getSection())) |
| 809 | continue; |
| 810 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 811 | SectionDataSize = std::max(SectionDataSize, |
| 812 | SD.getAddress() + SD.getSize()); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 813 | SectionDataFileSize = std::max(SectionDataFileSize, |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 814 | SD.getAddress() + SD.getFileSize()); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 817 | // The section data is padded to 4 bytes. |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 818 | // |
| 819 | // FIXME: Is this machine dependent? |
| 820 | unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4); |
| 821 | SectionDataFileSize += SectionDataPadding; |
| 822 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 823 | // Write the prolog, starting with the header and load command... |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 824 | WriteHeader(NumLoadCommands, LoadCommandsSize, |
| 825 | Asm.getSubsectionsViaSymbols()); |
| 826 | WriteSegmentLoadCommand(NumSections, VMSize, |
| 827 | SectionDataStart, SectionDataSize); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 828 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 829 | // ... and then the section headers. |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 830 | // |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 831 | // We also compute the section relocations while we do this. Note that |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 832 | // computing relocation info will also update the fixup to have the correct |
| 833 | // value; this will overwrite the appropriate data in the fragment when it |
| 834 | // is written. |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 835 | std::vector<MachRelocationEntry> RelocInfos; |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 836 | uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 837 | for (MCAssembler::iterator it = Asm.begin(), |
| 838 | ie = Asm.end(); it != ie; ++it) { |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 839 | MCSectionData &SD = *it; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 840 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 841 | // The assembler writes relocations in the reverse order they were seen. |
| 842 | // |
| 843 | // FIXME: It is probably more complicated than this. |
| 844 | unsigned NumRelocsStart = RelocInfos.size(); |
Daniel Dunbar | cb7d743 | 2010-02-11 21:29:46 +0000 | [diff] [blame] | 845 | for (MCSectionData::reverse_iterator it2 = SD.rbegin(), |
| 846 | ie2 = SD.rend(); it2 != ie2; ++it2) |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 847 | if (MCDataFragment *DF = dyn_cast<MCDataFragment>(&*it2)) |
| 848 | for (unsigned i = 0, e = DF->fixup_size(); i != e; ++i) |
| 849 | ComputeRelocationInfo(Asm, *DF, DF->getFixups()[e - i - 1], |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 850 | RelocInfos); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 851 | |
| 852 | unsigned NumRelocs = RelocInfos.size() - NumRelocsStart; |
| 853 | uint64_t SectionStart = SectionDataStart + SD.getAddress(); |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 854 | WriteSection(SD, SectionStart, RelocTableEnd, NumRelocs); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 855 | RelocTableEnd += NumRelocs * RelocationInfoSize; |
| 856 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 857 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 858 | // Write the symbol table load command, if used. |
| 859 | if (NumSymbols) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 860 | unsigned FirstLocalSymbol = 0; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 861 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 862 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 863 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 864 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 865 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 866 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
| 867 | unsigned NumSymTabSymbols = |
| 868 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
| 869 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
| 870 | uint64_t IndirectSymbolOffset = 0; |
| 871 | |
| 872 | // If used, the indirect symbols are written after the section data. |
| 873 | if (NumIndirectSymbols) |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 874 | IndirectSymbolOffset = RelocTableEnd; |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 875 | |
| 876 | // The symbol table is written after the indirect symbol data. |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 877 | uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize; |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 878 | |
| 879 | // The string table is written after symbol table. |
| 880 | uint64_t StringTableOffset = |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 881 | SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size : |
| 882 | Nlist32Size); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 883 | WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
| 884 | StringTableOffset, StringTable.size()); |
| 885 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 886 | WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
| 887 | FirstExternalSymbol, NumExternalSymbols, |
| 888 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 889 | IndirectSymbolOffset, NumIndirectSymbols); |
| 890 | } |
| 891 | |
| 892 | // Write the actual section data. |
| 893 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
| 894 | WriteFileData(OS, *it, *this); |
| 895 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 896 | // Write the extra padding. |
| 897 | WriteZeros(SectionDataPadding); |
| 898 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 899 | // Write the relocation entries. |
| 900 | for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) { |
| 901 | Write32(RelocInfos[i].Word0); |
| 902 | Write32(RelocInfos[i].Word1); |
| 903 | } |
| 904 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 905 | // Write the symbol table data, if used. |
| 906 | if (NumSymbols) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 907 | // Write the indirect symbol entries. |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 908 | for (MCAssembler::indirect_symbol_iterator |
| 909 | it = Asm.indirect_symbol_begin(), |
| 910 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 911 | // Indirect symbols in the non lazy symbol pointer section have some |
| 912 | // special handling. |
| 913 | const MCSectionMachO &Section = |
| 914 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
Daniel Dunbar | 99d22ad | 2010-03-15 21:56:38 +0000 | [diff] [blame] | 915 | if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 916 | // If this symbol is defined and internal, mark it as such. |
| 917 | if (it->Symbol->isDefined() && |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 918 | !Asm.getSymbolData(*it->Symbol).isExternal()) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 919 | uint32_t Flags = ISF_Local; |
| 920 | if (it->Symbol->isAbsolute()) |
| 921 | Flags |= ISF_Absolute; |
| 922 | Write32(Flags); |
| 923 | continue; |
| 924 | } |
| 925 | } |
| 926 | |
Daniel Dunbar | 2101d9c | 2010-03-10 20:58:31 +0000 | [diff] [blame] | 927 | Write32(Asm.getSymbolData(*it->Symbol).getIndex()); |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 928 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 929 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 930 | // FIXME: Check that offsets match computed ones. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 931 | |
| 932 | // Write the symbol table entries. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 933 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 934 | WriteNlist(LocalSymbolData[i]); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 935 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 936 | WriteNlist(ExternalSymbolData[i]); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 937 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
Daniel Dunbar | 5691e74 | 2010-03-13 22:49:35 +0000 | [diff] [blame] | 938 | WriteNlist(UndefinedSymbolData[i]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 939 | |
| 940 | // Write the string table. |
| 941 | OS << StringTable.str(); |
| 942 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 943 | } |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 944 | |
| 945 | void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF) { |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 946 | unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind); |
| 947 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 948 | // FIXME: Endianness assumption. |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 949 | assert(Fixup.Offset + Size <= DF.getContents().size() && |
| 950 | "Invalid fixup offset!"); |
| 951 | for (unsigned i = 0; i != Size; ++i) |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 952 | DF.getContents()[Fixup.Offset + i] = uint8_t(Fixup.FixedValue >> (i * 8)); |
| 953 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 954 | }; |
| 955 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 956 | /* *** */ |
| 957 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 958 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 959 | } |
| 960 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 961 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 962 | : Kind(_Kind), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 963 | Parent(_Parent), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 964 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 965 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 966 | if (Parent) |
| 967 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 970 | MCFragment::~MCFragment() { |
| 971 | } |
| 972 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 973 | uint64_t MCFragment::getAddress() const { |
| 974 | assert(getParent() && "Missing Section!"); |
| 975 | return getParent()->getAddress() + Offset; |
| 976 | } |
| 977 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 978 | /* *** */ |
| 979 | |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 980 | MCSectionData::MCSectionData() : Section(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 981 | |
| 982 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 983 | : Section(&_Section), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 984 | Alignment(1), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 985 | Address(~UINT64_C(0)), |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 986 | Size(~UINT64_C(0)), |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 987 | FileSize(~UINT64_C(0)), |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 988 | HasInstructions(false) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 989 | { |
| 990 | if (A) |
| 991 | A->getSectionList().push_back(this); |
| 992 | } |
| 993 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 994 | /* *** */ |
| 995 | |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 996 | MCSymbolData::MCSymbolData() : Symbol(0) {} |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 997 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 998 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 999 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 1000 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 1001 | IsExternal(false), IsPrivateExtern(false), |
| 1002 | CommonSize(0), CommonAlign(0), Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 1003 | { |
| 1004 | if (A) |
| 1005 | A->getSymbolList().push_back(this); |
| 1006 | } |
| 1007 | |
| 1008 | /* *** */ |
| 1009 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 1010 | MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, |
| 1011 | raw_ostream &_OS) |
| 1012 | : Context(_Context), Backend(_Backend), OS(_OS), SubsectionsViaSymbols(false) |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 1013 | { |
| 1014 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1015 | |
| 1016 | MCAssembler::~MCAssembler() { |
| 1017 | } |
| 1018 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 1019 | bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup, |
| 1020 | MCDataFragment *DF, |
| 1021 | MCValue &Target, uint64_t &Value) const { |
| 1022 | if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout)) |
| 1023 | llvm_report_error("expected relocatable expression"); |
| 1024 | |
| 1025 | // FIXME: How do non-scattered symbols work in ELF? I presume the linker |
| 1026 | // doesn't support small relocations, but then under what criteria does the |
| 1027 | // assembler allow symbol differences? |
| 1028 | |
| 1029 | Value = Target.getConstant(); |
| 1030 | |
| 1031 | // FIXME: This "resolved" check isn't quite right. The assumption is that if |
| 1032 | // we have a PCrel access to a temporary, then that temporary is in the same |
| 1033 | // atom, and so the value is resolved. We need explicit atom's to implement |
| 1034 | // this more precisely. |
| 1035 | bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind); |
| 1036 | if (const MCSymbol *Symbol = Target.getSymA()) { |
| 1037 | if (Symbol->isDefined()) |
| 1038 | Value += getSymbolData(*Symbol).getAddress(); |
| 1039 | else |
| 1040 | IsResolved = false; |
| 1041 | |
| 1042 | // With scattered symbols, we assume anything that isn't a PCrel temporary |
| 1043 | // access can have an arbitrary value. |
| 1044 | if (getBackend().hasScatteredSymbols() && |
| 1045 | (!IsPCRel || !Symbol->isTemporary())) |
| 1046 | IsResolved = false; |
| 1047 | } |
| 1048 | if (const MCSymbol *Symbol = Target.getSymB()) { |
| 1049 | if (Symbol->isDefined()) |
| 1050 | Value -= getSymbolData(*Symbol).getAddress(); |
| 1051 | else |
| 1052 | IsResolved = false; |
| 1053 | |
| 1054 | // With scattered symbols, we assume anything that isn't a PCrel temporary |
| 1055 | // access can have an arbitrary value. |
| 1056 | if (getBackend().hasScatteredSymbols() && |
| 1057 | (!IsPCRel || !Symbol->isTemporary())) |
| 1058 | IsResolved = false; |
| 1059 | } |
| 1060 | |
| 1061 | if (IsPCRel) |
Daniel Dunbar | da3e9f7 | 2010-03-13 02:38:00 +0000 | [diff] [blame] | 1062 | Value -= DF->getAddress() + Fixup.Offset; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 1063 | |
| 1064 | return IsResolved; |
| 1065 | } |
| 1066 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 1067 | void MCAssembler::LayoutSection(MCSectionData &SD) { |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 1068 | MCAsmLayout Layout(*this); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1069 | uint64_t Address = SD.getAddress(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1070 | |
| 1071 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 1072 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1073 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1074 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Evaluate fragment size. |
| 1077 | switch (F.getKind()) { |
| 1078 | case MCFragment::FT_Align: { |
| 1079 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1080 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1081 | uint64_t Size = OffsetToAlignment(Address, AF.getAlignment()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1082 | if (Size > AF.getMaxBytesToEmit()) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1083 | AF.setFileSize(0); |
| 1084 | else |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1085 | AF.setFileSize(Size); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | case MCFragment::FT_Data: |
Daniel Dunbar | a4766d7 | 2010-02-13 09:28:32 +0000 | [diff] [blame] | 1090 | case MCFragment::FT_Fill: |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1091 | F.setFileSize(F.getMaxFileSize()); |
| 1092 | break; |
| 1093 | |
| 1094 | case MCFragment::FT_Org: { |
| 1095 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 1096 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 1097 | int64_t TargetLocation; |
| 1098 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout)) |
| 1099 | llvm_report_error("expected assembly-time absolute expression"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1100 | |
| 1101 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 1102 | int64_t Offset = TargetLocation - F.getOffset(); |
| 1103 | if (Offset < 0) |
| 1104 | llvm_report_error("invalid .org offset '" + Twine(TargetLocation) + |
| 1105 | "' (at offset '" + Twine(F.getOffset()) + "'"); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1106 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 1107 | F.setFileSize(Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1108 | break; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1109 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1110 | |
| 1111 | case MCFragment::FT_ZeroFill: { |
| 1112 | MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F); |
| 1113 | |
| 1114 | // Align the fragment offset; it is safe to adjust the offset freely since |
| 1115 | // this is only in virtual sections. |
Daniel Dunbar | 37fad5c | 2010-03-08 21:10:42 +0000 | [diff] [blame] | 1116 | Address = RoundUpToAlignment(Address, ZFF.getAlignment()); |
| 1117 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1118 | |
| 1119 | // FIXME: This is misnamed. |
| 1120 | F.setFileSize(ZFF.getSize()); |
| 1121 | break; |
| 1122 | } |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1125 | Address += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1128 | // Set the section sizes. |
| 1129 | SD.setSize(Address - SD.getAddress()); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1130 | if (isVirtualSection(SD.getSection())) |
| 1131 | SD.setFileSize(0); |
| 1132 | else |
| 1133 | SD.setFileSize(Address - SD.getAddress()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 1136 | /// WriteNopData - Write optimal nops to the output file for the \arg Count |
| 1137 | /// bytes. This returns the number of bytes written. It may return 0 if |
| 1138 | /// the \arg Count is more than the maximum optimal nops. |
| 1139 | /// |
| 1140 | /// FIXME this is X86 32-bit specific and should move to a better place. |
| 1141 | static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW) { |
Kevin Enderby | 7668708 | 2010-02-23 21:41:24 +0000 | [diff] [blame] | 1142 | static const uint8_t Nops[16][16] = { |
| 1143 | // nop |
| 1144 | {0x90}, |
| 1145 | // xchg %ax,%ax |
| 1146 | {0x66, 0x90}, |
| 1147 | // nopl (%[re]ax) |
| 1148 | {0x0f, 0x1f, 0x00}, |
| 1149 | // nopl 0(%[re]ax) |
| 1150 | {0x0f, 0x1f, 0x40, 0x00}, |
| 1151 | // nopl 0(%[re]ax,%[re]ax,1) |
| 1152 | {0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 1153 | // nopw 0(%[re]ax,%[re]ax,1) |
| 1154 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 1155 | // nopl 0L(%[re]ax) |
| 1156 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 1157 | // nopl 0L(%[re]ax,%[re]ax,1) |
| 1158 | {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 1159 | // nopw 0L(%[re]ax,%[re]ax,1) |
| 1160 | {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 1161 | // nopw %cs:0L(%[re]ax,%[re]ax,1) |
| 1162 | {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 1163 | // nopl 0(%[re]ax,%[re]ax,1) |
| 1164 | // nopw 0(%[re]ax,%[re]ax,1) |
| 1165 | {0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 1166 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 1167 | // nopw 0(%[re]ax,%[re]ax,1) |
| 1168 | // nopw 0(%[re]ax,%[re]ax,1) |
| 1169 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 1170 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 1171 | // nopw 0(%[re]ax,%[re]ax,1) |
| 1172 | // nopl 0L(%[re]ax) */ |
| 1173 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 1174 | 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 1175 | // nopl 0L(%[re]ax) |
| 1176 | // nopl 0L(%[re]ax) |
| 1177 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, |
| 1178 | 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 1179 | // nopl 0L(%[re]ax) |
| 1180 | // nopl 0L(%[re]ax,%[re]ax,1) |
| 1181 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, |
| 1182 | 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 1183 | }; |
| 1184 | |
| 1185 | if (Count > 15) |
| 1186 | return 0; |
| 1187 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 1188 | for (uint64_t i = 0; i < Count; i++) |
Kevin Enderby | 7668708 | 2010-02-23 21:41:24 +0000 | [diff] [blame] | 1189 | MOW.Write8 (uint8_t(Nops[Count - 1][i])); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 1190 | |
| 1191 | return Count; |
| 1192 | } |
| 1193 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1194 | /// WriteFileData - Write the \arg F data to the output file. |
| 1195 | static void WriteFileData(raw_ostream &OS, const MCFragment &F, |
| 1196 | MachObjectWriter &MOW) { |
| 1197 | uint64_t Start = OS.tell(); |
| 1198 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1199 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 1200 | ++EmittedFragments; |
| 1201 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1202 | // FIXME: Embed in fragments instead? |
| 1203 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1204 | case MCFragment::FT_Align: { |
| 1205 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 1206 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 1207 | |
| 1208 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 1209 | // multiple .align directives to enforce the semantics it wants), but is |
| 1210 | // severe enough that we want to report it. How to handle this? |
| 1211 | if (Count * AF.getValueSize() != AF.getFileSize()) |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1212 | llvm_report_error("undefined .align directive, value size '" + |
| 1213 | Twine(AF.getValueSize()) + |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1214 | "' is not a divisor of padding size '" + |
| 1215 | Twine(AF.getFileSize()) + "'"); |
| 1216 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 1217 | // See if we are aligning with nops, and if so do that first to try to fill |
| 1218 | // the Count bytes. Then if that did not fill any bytes or there are any |
| 1219 | // bytes left to fill use the the Value and ValueSize to fill the rest. |
| 1220 | if (AF.getEmitNops()) { |
| 1221 | uint64_t NopByteCount = WriteNopData(Count, MOW); |
| 1222 | Count -= NopByteCount; |
| 1223 | } |
| 1224 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1225 | for (uint64_t i = 0; i != Count; ++i) { |
| 1226 | switch (AF.getValueSize()) { |
| 1227 | default: |
| 1228 | assert(0 && "Invalid size!"); |
| 1229 | case 1: MOW.Write8 (uint8_t (AF.getValue())); break; |
| 1230 | case 2: MOW.Write16(uint16_t(AF.getValue())); break; |
| 1231 | case 4: MOW.Write32(uint32_t(AF.getValue())); break; |
| 1232 | case 8: MOW.Write64(uint64_t(AF.getValue())); break; |
| 1233 | } |
| 1234 | } |
| 1235 | break; |
| 1236 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1237 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 1238 | case MCFragment::FT_Data: { |
| 1239 | MCDataFragment &DF = cast<MCDataFragment>(F); |
| 1240 | |
| 1241 | // Apply the fixups. |
| 1242 | // |
| 1243 | // FIXME: Move elsewhere. |
| 1244 | for (MCDataFragment::const_fixup_iterator it = DF.fixup_begin(), |
| 1245 | ie = DF.fixup_end(); it != ie; ++it) |
| 1246 | MOW.ApplyFixup(*it, DF); |
| 1247 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1248 | OS << cast<MCDataFragment>(F).getContents().str(); |
| 1249 | break; |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 1250 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1251 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1252 | case MCFragment::FT_Fill: { |
| 1253 | MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1254 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 1255 | switch (FF.getValueSize()) { |
| 1256 | default: |
| 1257 | assert(0 && "Invalid size!"); |
Daniel Dunbar | a4766d7 | 2010-02-13 09:28:32 +0000 | [diff] [blame] | 1258 | case 1: MOW.Write8 (uint8_t (FF.getValue())); break; |
| 1259 | case 2: MOW.Write16(uint16_t(FF.getValue())); break; |
| 1260 | case 4: MOW.Write32(uint32_t(FF.getValue())); break; |
| 1261 | case 8: MOW.Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | break; |
| 1265 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1266 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1267 | case MCFragment::FT_Org: { |
| 1268 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 1269 | |
| 1270 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
| 1271 | MOW.Write8(uint8_t(OF.getValue())); |
| 1272 | |
| 1273 | break; |
| 1274 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1275 | |
| 1276 | case MCFragment::FT_ZeroFill: { |
| 1277 | assert(0 && "Invalid zero fill fragment in concrete section!"); |
| 1278 | break; |
| 1279 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | assert(OS.tell() - Start == F.getFileSize()); |
| 1283 | } |
| 1284 | |
| 1285 | /// WriteFileData - Write the \arg SD data to the output file. |
| 1286 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 1287 | MachObjectWriter &MOW) { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1288 | // Ignore virtual sections. |
| 1289 | if (isVirtualSection(SD.getSection())) { |
| 1290 | assert(SD.getFileSize() == 0); |
| 1291 | return; |
| 1292 | } |
| 1293 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1294 | uint64_t Start = OS.tell(); |
| 1295 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 1296 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1297 | for (MCSectionData::const_iterator it = SD.begin(), |
| 1298 | ie = SD.end(); it != ie; ++it) |
| 1299 | WriteFileData(OS, *it, MOW); |
| 1300 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1301 | // Add section padding. |
| 1302 | assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!"); |
| 1303 | MOW.WriteZeros(SD.getFileSize() - SD.getSize()); |
| 1304 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1305 | assert(OS.tell() - Start == SD.getFileSize()); |
| 1306 | } |
| 1307 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1308 | void MCAssembler::Finish() { |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1309 | DEBUG_WITH_TYPE("mc-dump", { |
| 1310 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 1311 | dump(); }); |
| 1312 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1313 | // Layout until everything fits. |
| 1314 | while (LayoutOnce()) |
| 1315 | continue; |
| 1316 | |
| 1317 | DEBUG_WITH_TYPE("mc-dump", { |
| 1318 | llvm::errs() << "assembler backend - post-layout\n--\n"; |
| 1319 | dump(); }); |
| 1320 | |
| 1321 | // Write the object file. |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 1322 | // |
| 1323 | // FIXME: Factor out MCObjectWriter. |
| 1324 | bool Is64Bit = StringRef(getBackend().getTarget().getName()) == "x86-64"; |
| 1325 | MachObjectWriter MOW(OS, Is64Bit); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1326 | MOW.WriteObject(*this); |
| 1327 | |
| 1328 | OS.flush(); |
| 1329 | } |
| 1330 | |
| 1331 | bool MCAssembler::FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF) { |
| 1332 | // FIXME: Share layout object. |
| 1333 | MCAsmLayout Layout(*this); |
| 1334 | |
| 1335 | // Currently we only need to relax X86::reloc_pcrel_1byte. |
| 1336 | if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte) |
| 1337 | return false; |
| 1338 | |
| 1339 | // If we cannot resolve the fixup value, it requires relaxation. |
| 1340 | MCValue Target; |
| 1341 | uint64_t Value; |
| 1342 | if (!EvaluateFixup(Layout, Fixup, DF, Target, Value)) |
| 1343 | return true; |
| 1344 | |
| 1345 | // Otherwise, relax if the value is too big for a (signed) i8. |
| 1346 | return int64_t(Value) != int64_t(int8_t(Value)); |
| 1347 | } |
| 1348 | |
| 1349 | bool MCAssembler::LayoutOnce() { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1350 | // Layout the concrete sections and fragments. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 1351 | uint64_t Address = 0; |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 1352 | MCSectionData *Prev = 0; |
| 1353 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1354 | MCSectionData &SD = *it; |
| 1355 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1356 | // Skip virtual sections. |
| 1357 | if (isVirtualSection(SD.getSection())) |
| 1358 | continue; |
| 1359 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 1360 | // Align this section if necessary by adding padding bytes to the previous |
| 1361 | // section. |
| 1362 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) { |
| 1363 | assert(Prev && "Missing prev section!"); |
| 1364 | Prev->setFileSize(Prev->getFileSize() + Pad); |
| 1365 | Address += Pad; |
| 1366 | } |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Layout the section fragments and its size. |
| 1369 | SD.setAddress(Address); |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 1370 | LayoutSection(SD); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1371 | Address += SD.getFileSize(); |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 1372 | |
| 1373 | Prev = &SD; |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 1374 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1375 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1376 | // Layout the virtual sections. |
| 1377 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1378 | MCSectionData &SD = *it; |
| 1379 | |
| 1380 | if (!isVirtualSection(SD.getSection())) |
| 1381 | continue; |
| 1382 | |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 1383 | // Align this section if necessary by adding padding bytes to the previous |
| 1384 | // section. |
Daniel Dunbar | f8b8ad7 | 2010-03-09 01:12:20 +0000 | [diff] [blame] | 1385 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 1386 | Address += Pad; |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 1387 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 1388 | SD.setAddress(Address); |
| 1389 | LayoutSection(SD); |
| 1390 | Address += SD.getSize(); |
| 1391 | } |
| 1392 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1393 | // Scan the fixups in order and relax any that don't fit. |
| 1394 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1395 | MCSectionData &SD = *it; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1396 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1397 | for (MCSectionData::iterator it2 = SD.begin(), |
| 1398 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 1399 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
| 1400 | if (!DF) |
| 1401 | continue; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1402 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1403 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 1404 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 1405 | MCAsmFixup &Fixup = *it3; |
| 1406 | |
| 1407 | // Check whether we need to relax this fixup. |
| 1408 | if (!FixupNeedsRelaxation(Fixup, DF)) |
| 1409 | continue; |
| 1410 | |
| 1411 | // Relax the instruction. |
| 1412 | // |
| 1413 | // FIXME: This is a huge temporary hack which just looks for x86 |
| 1414 | // branches; the only thing we need to relax on x86 is |
| 1415 | // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be |
| 1416 | // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax |
| 1417 | // an individual MCInst. |
| 1418 | SmallVectorImpl<char> &C = DF->getContents(); |
| 1419 | uint64_t PrevOffset = Fixup.Offset; |
| 1420 | unsigned Amt = 0; |
| 1421 | |
| 1422 | // jcc instructions |
| 1423 | if (unsigned(C[Fixup.Offset-1]) >= 0x70 && |
| 1424 | unsigned(C[Fixup.Offset-1]) <= 0x7f) { |
| 1425 | C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10; |
| 1426 | C[Fixup.Offset-1] = char(0x0f); |
| 1427 | ++Fixup.Offset; |
| 1428 | Amt = 4; |
| 1429 | |
| 1430 | // jmp rel8 |
| 1431 | } else if (C[Fixup.Offset-1] == char(0xeb)) { |
| 1432 | C[Fixup.Offset-1] = char(0xe9); |
| 1433 | Amt = 3; |
| 1434 | |
| 1435 | } else |
| 1436 | llvm_unreachable("unknown 1 byte pcrel instruction!"); |
| 1437 | |
| 1438 | Fixup.Value = MCBinaryExpr::Create( |
| 1439 | MCBinaryExpr::Sub, Fixup.Value, |
| 1440 | MCConstantExpr::Create(3, getContext()), |
| 1441 | getContext()); |
| 1442 | C.insert(C.begin() + Fixup.Offset, Amt, char(0)); |
| 1443 | Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte); |
| 1444 | |
| 1445 | // Update the remaining fixups, which have slid. |
| 1446 | // |
| 1447 | // FIXME: This is bad for performance, but will be eliminated by the |
| 1448 | // move to MCInst specific fragments. |
| 1449 | ++it3; |
| 1450 | for (; it3 != ie3; ++it3) |
| 1451 | it3->Offset += Amt; |
| 1452 | |
| 1453 | // Update all the symbols for this fragment, which may have slid. |
| 1454 | // |
| 1455 | // FIXME: This is really really bad for performance, but will be |
| 1456 | // eliminated by the move to MCInst specific fragments. |
| 1457 | for (MCAssembler::symbol_iterator it = symbol_begin(), |
| 1458 | ie = symbol_end(); it != ie; ++it) { |
| 1459 | MCSymbolData &SD = *it; |
| 1460 | |
| 1461 | if (it->getFragment() != DF) |
| 1462 | continue; |
| 1463 | |
| 1464 | if (SD.getOffset() > PrevOffset) |
| 1465 | SD.setOffset(SD.getOffset() + Amt); |
| 1466 | } |
| 1467 | |
| 1468 | // Restart layout. |
| 1469 | // |
| 1470 | // FIXME: This is O(N^2), but will be eliminated once we have a smart |
| 1471 | // MCAsmLayout object. |
| 1472 | return true; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return false; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1478 | } |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1479 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1480 | // Debugging methods |
| 1481 | |
| 1482 | namespace llvm { |
| 1483 | |
| 1484 | raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) { |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 1485 | OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value |
| 1486 | << " Kind:" << AF.Kind << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1487 | return OS; |
| 1488 | } |
| 1489 | |
| 1490 | } |
| 1491 | |
| 1492 | void MCFragment::dump() { |
| 1493 | raw_ostream &OS = llvm::errs(); |
| 1494 | |
| 1495 | OS << "<MCFragment " << (void*) this << " Offset:" << Offset |
| 1496 | << " FileSize:" << FileSize; |
| 1497 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1498 | OS << ">"; |
| 1499 | } |
| 1500 | |
| 1501 | void MCAlignFragment::dump() { |
| 1502 | raw_ostream &OS = llvm::errs(); |
| 1503 | |
| 1504 | OS << "<MCAlignFragment "; |
| 1505 | this->MCFragment::dump(); |
| 1506 | OS << "\n "; |
| 1507 | OS << " Alignment:" << getAlignment() |
| 1508 | << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 1509 | << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">"; |
| 1510 | } |
| 1511 | |
| 1512 | void MCDataFragment::dump() { |
| 1513 | raw_ostream &OS = llvm::errs(); |
| 1514 | |
| 1515 | OS << "<MCDataFragment "; |
| 1516 | this->MCFragment::dump(); |
| 1517 | OS << "\n "; |
| 1518 | OS << " Contents:["; |
| 1519 | for (unsigned i = 0, e = getContents().size(); i != e; ++i) { |
| 1520 | if (i) OS << ","; |
| 1521 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
| 1522 | } |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 1523 | OS << "] (" << getContents().size() << " bytes)"; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 1524 | |
| 1525 | if (!getFixups().empty()) { |
| 1526 | OS << ",\n "; |
| 1527 | OS << " Fixups:["; |
| 1528 | for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1529 | if (it != fixup_begin()) OS << ",\n "; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 1530 | OS << *it; |
| 1531 | } |
| 1532 | OS << "]"; |
| 1533 | } |
| 1534 | |
| 1535 | OS << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | void MCFillFragment::dump() { |
| 1539 | raw_ostream &OS = llvm::errs(); |
| 1540 | |
| 1541 | OS << "<MCFillFragment "; |
| 1542 | this->MCFragment::dump(); |
| 1543 | OS << "\n "; |
| 1544 | OS << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 1545 | << " Count:" << getCount() << ">"; |
| 1546 | } |
| 1547 | |
| 1548 | void MCOrgFragment::dump() { |
| 1549 | raw_ostream &OS = llvm::errs(); |
| 1550 | |
| 1551 | OS << "<MCOrgFragment "; |
| 1552 | this->MCFragment::dump(); |
| 1553 | OS << "\n "; |
| 1554 | OS << " Offset:" << getOffset() << " Value:" << getValue() << ">"; |
| 1555 | } |
| 1556 | |
| 1557 | void MCZeroFillFragment::dump() { |
| 1558 | raw_ostream &OS = llvm::errs(); |
| 1559 | |
| 1560 | OS << "<MCZeroFillFragment "; |
| 1561 | this->MCFragment::dump(); |
| 1562 | OS << "\n "; |
| 1563 | OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">"; |
| 1564 | } |
| 1565 | |
| 1566 | void MCSectionData::dump() { |
| 1567 | raw_ostream &OS = llvm::errs(); |
| 1568 | |
| 1569 | OS << "<MCSectionData"; |
| 1570 | OS << " Alignment:" << getAlignment() << " Address:" << Address |
| 1571 | << " Size:" << Size << " FileSize:" << FileSize |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1572 | << " Fragments:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1573 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1574 | if (it != begin()) OS << ",\n "; |
| 1575 | it->dump(); |
| 1576 | } |
| 1577 | OS << "]>"; |
| 1578 | } |
| 1579 | |
| 1580 | void MCSymbolData::dump() { |
| 1581 | raw_ostream &OS = llvm::errs(); |
| 1582 | |
| 1583 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 1584 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 1585 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 1586 | if (isCommon()) |
| 1587 | OS << " (common, size:" << getCommonSize() |
| 1588 | << " align: " << getCommonAlignment() << ")"; |
| 1589 | if (isExternal()) |
| 1590 | OS << " (external)"; |
| 1591 | if (isPrivateExtern()) |
| 1592 | OS << " (private extern)"; |
| 1593 | OS << ">"; |
| 1594 | } |
| 1595 | |
| 1596 | void MCAssembler::dump() { |
| 1597 | raw_ostream &OS = llvm::errs(); |
| 1598 | |
| 1599 | OS << "<MCAssembler\n"; |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1600 | OS << " Sections:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1601 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1602 | if (it != begin()) OS << ",\n "; |
| 1603 | it->dump(); |
| 1604 | } |
| 1605 | OS << "],\n"; |
| 1606 | OS << " Symbols:["; |
| 1607 | |
| 1608 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1609 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1610 | it->dump(); |
| 1611 | } |
| 1612 | OS << "]>\n"; |
| 1613 | } |