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