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