blob: c18a3a64ce7d1ea71879b4e076b29dafb7bc03f1 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- 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 Dunbar0adcd352009-08-25 21:10:45 +000010#define DEBUG_TYPE "assembler"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000012#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000013#include "llvm/MC/MCExpr.h"
Chris Lattnerbea2c952009-08-22 19:19:12 +000014#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000015#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +000017#include "llvm/ADT/DenseMap.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000018#include "llvm/ADT/SmallString.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000019#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000021#include "llvm/ADT/StringMap.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000022#include "llvm/ADT/Twine.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner45f8c092010-02-02 19:38:14 +000024#include "llvm/Support/MachO.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000026#include "llvm/Support/Debug.h"
Daniel Dunbaree0d8922010-03-13 22:10:17 +000027#include "llvm/Target/TargetRegistry.h"
Daniel Dunbardf3c8f22010-03-12 21:00:49 +000028#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000029
30// FIXME: Gross.
31#include "../Target/X86/X86FixupKinds.h"
32
Chris Lattner23132b12009-08-24 03:52:50 +000033#include <vector>
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000034using namespace llvm;
35
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000036class MachObjectWriter;
37
Daniel Dunbar0adcd352009-08-25 21:10:45 +000038STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
39
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000040// FIXME FIXME FIXME: There are number of places in this file where we convert
41// what is a 64-bit assembler value used for computation into a value in the
42// object file, which may truncate it. We should detect that truncation where
43// invalid and report errors back.
44
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000045static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
46 MachObjectWriter &MOW);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000047
Kevin Enderby6e720482010-02-23 18:26:34 +000048static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW);
49
Daniel Dunbard5a8e982009-08-28 05:49:21 +000050/// isVirtualSection - Check if this is a section which does not actually exist
51/// in the object file.
52static bool isVirtualSection(const MCSection &Section) {
53 // FIXME: Lame.
54 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
Daniel Dunbar99d22ad2010-03-15 21:56:38 +000055 return (SMO.getType() == MCSectionMachO::S_ZEROFILL);
Daniel Dunbard5a8e982009-08-28 05:49:21 +000056}
57
Duncan Sands9a7795c2010-02-17 14:52:22 +000058static unsigned getFixupKindLog2Size(unsigned Kind) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000059 switch (Kind) {
60 default: llvm_unreachable("invalid fixup kind!");
Daniel Dunbarf6346762010-02-13 09:29:02 +000061 case X86::reloc_pcrel_1byte:
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000062 case FK_Data_1: return 0;
63 case FK_Data_2: return 1;
Daniel Dunbarf6346762010-02-13 09:29:02 +000064 case X86::reloc_pcrel_4byte:
65 case X86::reloc_riprel_4byte:
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000066 case FK_Data_4: return 2;
67 case FK_Data_8: return 3;
68 }
69}
70
Duncan Sands9a7795c2010-02-17 14:52:22 +000071static bool isFixupKindPCRel(unsigned Kind) {
Daniel Dunbar591047f2010-02-13 09:45:59 +000072 switch (Kind) {
73 default:
74 return false;
75 case X86::reloc_pcrel_1byte:
76 case X86::reloc_pcrel_4byte:
77 case X86::reloc_riprel_4byte:
78 return true;
79 }
80}
81
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000082class MachObjectWriter {
83 // See <mach-o/loader.h>.
84 enum {
85 Header_Magic32 = 0xFEEDFACE,
86 Header_Magic64 = 0xFEEDFACF
87 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +000088
Daniel Dunbaree0d8922010-03-13 22:10:17 +000089 enum {
90 Header32Size = 28,
91 Header64Size = 32,
92 SegmentLoadCommand32Size = 56,
93 SegmentLoadCommand64Size = 72,
94 Section32Size = 68,
95 Section64Size = 80,
96 SymtabLoadCommandSize = 24,
97 DysymtabLoadCommandSize = 80,
98 Nlist32Size = 12,
99 Nlist64Size = 16,
100 RelocationInfoSize = 8
101 };
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102
103 enum HeaderFileType {
104 HFT_Object = 0x1
105 };
106
Daniel Dunbar6009db42009-08-26 21:22:22 +0000107 enum HeaderFlags {
108 HF_SubsectionsViaSymbols = 0x2000
109 };
110
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000111 enum LoadCommandType {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000112 LCT_Segment = 0x1,
113 LCT_Symtab = 0x2,
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000114 LCT_Dysymtab = 0xb,
115 LCT_Segment64 = 0x19
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116 };
117
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000118 // See <mach-o/nlist.h>.
119 enum SymbolTypeType {
120 STT_Undefined = 0x00,
121 STT_Absolute = 0x02,
122 STT_Section = 0x0e
123 };
124
125 enum SymbolTypeFlags {
126 // If any of these bits are set, then the entry is a stab entry number (see
127 // <mach-o/stab.h>. Otherwise the other masks apply.
128 STF_StabsEntryMask = 0xe0,
129
130 STF_TypeMask = 0x0e,
131 STF_External = 0x01,
132 STF_PrivateExtern = 0x10
133 };
134
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000135 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
136 /// symbol entry.
137 enum IndirectSymbolFlags {
138 ISF_Local = 0x80000000,
139 ISF_Absolute = 0x40000000
140 };
141
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000142 /// RelocationFlags - Special flags for addresses.
143 enum RelocationFlags {
144 RF_Scattered = 0x80000000
145 };
146
147 enum RelocationInfoType {
148 RIT_Vanilla = 0,
149 RIT_Pair = 1,
150 RIT_Difference = 2,
151 RIT_PreboundLazyPointer = 3,
152 RIT_LocalDifference = 4
153 };
154
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000155 /// MachSymbolData - Helper struct for containing some precomputed information
156 /// on symbols.
157 struct MachSymbolData {
158 MCSymbolData *SymbolData;
159 uint64_t StringIndex;
160 uint8_t SectionIndex;
161
162 // Support lexicographic sorting.
163 bool operator<(const MachSymbolData &RHS) const {
164 const std::string &Name = SymbolData->getSymbol().getName();
165 return Name < RHS.SymbolData->getSymbol().getName();
166 }
167 };
168
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000169 raw_ostream &OS;
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000170 unsigned Is64Bit : 1;
171 unsigned IsLSB : 1;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000172
Daniel Dunbar17a06502010-03-19 07:09:18 +0000173 /// @name Relocation Data
174 /// @{
175
176 struct MachRelocationEntry {
177 uint32_t Word0;
178 uint32_t Word1;
179 };
180
181 llvm::DenseMap<const MCSectionData*,
182 std::vector<MachRelocationEntry> > Relocations;
183
184 /// @}
185 /// @name Symbol Table Data
186
187 SmallString<256> StringTable;
188 std::vector<MachSymbolData> LocalSymbolData;
189 std::vector<MachSymbolData> ExternalSymbolData;
190 std::vector<MachSymbolData> UndefinedSymbolData;
191
192 /// @}
193
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000194public:
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000195 MachObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool _IsLSB = true)
196 : OS(_OS), Is64Bit(_Is64Bit), IsLSB(_IsLSB) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000197 }
198
199 /// @name Helper Methods
200 /// @{
201
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000202 void Write8(uint8_t Value) {
203 OS << char(Value);
204 }
205
206 void Write16(uint16_t Value) {
207 if (IsLSB) {
208 Write8(uint8_t(Value >> 0));
209 Write8(uint8_t(Value >> 8));
210 } else {
211 Write8(uint8_t(Value >> 8));
212 Write8(uint8_t(Value >> 0));
213 }
214 }
215
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000216 void Write32(uint32_t Value) {
217 if (IsLSB) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000218 Write16(uint16_t(Value >> 0));
219 Write16(uint16_t(Value >> 16));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000220 } else {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000221 Write16(uint16_t(Value >> 16));
222 Write16(uint16_t(Value >> 0));
223 }
224 }
225
226 void Write64(uint64_t Value) {
227 if (IsLSB) {
228 Write32(uint32_t(Value >> 0));
229 Write32(uint32_t(Value >> 32));
230 } else {
231 Write32(uint32_t(Value >> 32));
232 Write32(uint32_t(Value >> 0));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000233 }
234 }
235
236 void WriteZeros(unsigned N) {
237 const char Zeros[16] = { 0 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000238
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000239 for (unsigned i = 0, e = N / 16; i != e; ++i)
240 OS << StringRef(Zeros, 16);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000241
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000242 OS << StringRef(Zeros, N % 16);
243 }
244
Daniel Dunbar2928c832009-11-06 10:58:06 +0000245 void WriteString(StringRef Str, unsigned ZeroFillSize = 0) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000246 OS << Str;
247 if (ZeroFillSize)
248 WriteZeros(ZeroFillSize - Str.size());
249 }
250
251 /// @}
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000252
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000253 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
254 bool SubsectionsViaSymbols) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000255 uint32_t Flags = 0;
256
257 if (SubsectionsViaSymbols)
258 Flags |= HF_SubsectionsViaSymbols;
259
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000260 // struct mach_header (28 bytes) or
261 // struct mach_header_64 (32 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000262
263 uint64_t Start = OS.tell();
264 (void) Start;
265
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000266 Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000267
268 // FIXME: Support cputype.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000269 Write32(Is64Bit ? MachO::CPUTypeX86_64 : MachO::CPUTypeI386);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000270 // FIXME: Support cpusubtype.
Chris Lattner45f8c092010-02-02 19:38:14 +0000271 Write32(MachO::CPUSubType_I386_ALL);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000272 Write32(HFT_Object);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000273 Write32(NumLoadCommands); // Object files have a single load command, the
274 // segment.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000275 Write32(LoadCommandsSize);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000276 Write32(Flags);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000277 if (Is64Bit)
278 Write32(0); // reserved
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000279
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000280 assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000281 }
282
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000283 /// WriteSegmentLoadCommand - Write a segment load command.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000284 ///
285 /// \arg NumSections - The number of sections in this segment.
286 /// \arg SectionDataSize - The total size of the sections.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000287 void WriteSegmentLoadCommand(unsigned NumSections,
288 uint64_t VMSize,
289 uint64_t SectionDataStartOffset,
290 uint64_t SectionDataSize) {
291 // struct segment_command (56 bytes) or
292 // struct segment_command_64 (72 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000293
294 uint64_t Start = OS.tell();
295 (void) Start;
296
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000297 unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
298 SegmentLoadCommand32Size;
299 Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
300 Write32(SegmentLoadCommandSize +
301 NumSections * (Is64Bit ? Section64Size : Section32Size));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000302
303 WriteString("", 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000304 if (Is64Bit) {
305 Write64(0); // vmaddr
306 Write64(VMSize); // vmsize
307 Write64(SectionDataStartOffset); // file offset
308 Write64(SectionDataSize); // file size
309 } else {
310 Write32(0); // vmaddr
311 Write32(VMSize); // vmsize
312 Write32(SectionDataStartOffset); // file offset
313 Write32(SectionDataSize); // file size
314 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000315 Write32(0x7); // maxprot
316 Write32(0x7); // initprot
317 Write32(NumSections);
318 Write32(0); // flags
319
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000320 assert(OS.tell() - Start == SegmentLoadCommandSize);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000321 }
322
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000323 void WriteSection(const MCSectionData &SD, uint64_t FileOffset,
324 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000325 // The offset is unused for virtual sections.
326 if (isVirtualSection(SD.getSection())) {
327 assert(SD.getFileSize() == 0 && "Invalid file size!");
328 FileOffset = 0;
329 }
330
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000331 // struct section (68 bytes) or
332 // struct section_64 (80 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000333
334 uint64_t Start = OS.tell();
335 (void) Start;
336
337 // FIXME: cast<> support!
338 const MCSectionMachO &Section =
339 static_cast<const MCSectionMachO&>(SD.getSection());
340 WriteString(Section.getSectionName(), 16);
341 WriteString(Section.getSegmentName(), 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000342 if (Is64Bit) {
343 Write64(SD.getAddress()); // address
344 Write64(SD.getSize()); // size
345 } else {
346 Write32(SD.getAddress()); // address
347 Write32(SD.getSize()); // size
348 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000349 Write32(FileOffset);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000350
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000351 unsigned Flags = Section.getTypeAndAttributes();
352 if (SD.hasInstructions())
353 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
354
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000355 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
356 Write32(Log2_32(SD.getAlignment()));
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000357 Write32(NumRelocations ? RelocationsStart : 0);
358 Write32(NumRelocations);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000359 Write32(Flags);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000360 Write32(0); // reserved1
361 Write32(Section.getStubSize()); // reserved2
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000362 if (Is64Bit)
363 Write32(0); // reserved3
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000364
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000365 assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000367
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000368 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
369 uint32_t StringTableOffset,
370 uint32_t StringTableSize) {
371 // struct symtab_command (24 bytes)
372
373 uint64_t Start = OS.tell();
374 (void) Start;
375
376 Write32(LCT_Symtab);
377 Write32(SymtabLoadCommandSize);
378 Write32(SymbolOffset);
379 Write32(NumSymbols);
380 Write32(StringTableOffset);
381 Write32(StringTableSize);
382
383 assert(OS.tell() - Start == SymtabLoadCommandSize);
384 }
385
386 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
387 uint32_t NumLocalSymbols,
388 uint32_t FirstExternalSymbol,
389 uint32_t NumExternalSymbols,
390 uint32_t FirstUndefinedSymbol,
391 uint32_t NumUndefinedSymbols,
392 uint32_t IndirectSymbolOffset,
393 uint32_t NumIndirectSymbols) {
394 // struct dysymtab_command (80 bytes)
395
396 uint64_t Start = OS.tell();
397 (void) Start;
398
399 Write32(LCT_Dysymtab);
400 Write32(DysymtabLoadCommandSize);
401 Write32(FirstLocalSymbol);
402 Write32(NumLocalSymbols);
403 Write32(FirstExternalSymbol);
404 Write32(NumExternalSymbols);
405 Write32(FirstUndefinedSymbol);
406 Write32(NumUndefinedSymbols);
407 Write32(0); // tocoff
408 Write32(0); // ntoc
409 Write32(0); // modtaboff
410 Write32(0); // nmodtab
411 Write32(0); // extrefsymoff
412 Write32(0); // nextrefsyms
413 Write32(IndirectSymbolOffset);
414 Write32(NumIndirectSymbols);
415 Write32(0); // extreloff
416 Write32(0); // nextrel
417 Write32(0); // locreloff
418 Write32(0); // nlocrel
419
420 assert(OS.tell() - Start == DysymtabLoadCommandSize);
421 }
422
Daniel Dunbar5691e742010-03-13 22:49:35 +0000423 void WriteNlist(MachSymbolData &MSD) {
Daniel Dunbar5e835962009-08-26 02:48:04 +0000424 MCSymbolData &Data = *MSD.SymbolData;
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000425 const MCSymbol &Symbol = Data.getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000426 uint8_t Type = 0;
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000427 uint16_t Flags = Data.getFlags();
428 uint32_t Address = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000429
430 // Set the N_TYPE bits. See <mach-o/nlist.h>.
431 //
432 // FIXME: Are the prebound or indirect fields possible here?
433 if (Symbol.isUndefined())
434 Type = STT_Undefined;
435 else if (Symbol.isAbsolute())
436 Type = STT_Absolute;
437 else
438 Type = STT_Section;
439
440 // FIXME: Set STAB bits.
441
Daniel Dunbar5e835962009-08-26 02:48:04 +0000442 if (Data.isPrivateExtern())
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000443 Type |= STF_PrivateExtern;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000444
445 // Set external bit.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000446 if (Data.isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000447 Type |= STF_External;
448
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000449 // Compute the symbol address.
450 if (Symbol.isDefined()) {
451 if (Symbol.isAbsolute()) {
452 llvm_unreachable("FIXME: Not yet implemented!");
453 } else {
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000454 Address = Data.getAddress();
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000455 }
456 } else if (Data.isCommon()) {
457 // Common symbols are encoded with the size in the address
458 // field, and their alignment in the flags.
459 Address = Data.getCommonSize();
460
461 // Common alignment is packed into the 'desc' bits.
462 if (unsigned Align = Data.getCommonAlignment()) {
463 unsigned Log2Size = Log2_32(Align);
464 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
465 if (Log2Size > 15)
466 llvm_report_error("invalid 'common' alignment '" +
467 Twine(Align) + "'");
468 // FIXME: Keep this mask with the SymbolFlags enumeration.
469 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
470 }
471 }
472
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000473 // struct nlist (12 bytes)
474
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000475 Write32(MSD.StringIndex);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000476 Write8(Type);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000477 Write8(MSD.SectionIndex);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000478
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000479 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
480 // value.
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000481 Write16(Flags);
Daniel Dunbar5691e742010-03-13 22:49:35 +0000482 if (Is64Bit)
483 Write64(Address);
484 else
485 Write32(Address);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000486 }
487
Daniel Dunbar17a06502010-03-19 07:09:18 +0000488 void RecordScatteredRelocation(MCAssembler &Asm, MCFragment &Fragment,
489 const MCAsmFixup &Fixup, MCValue Target,
490 uint64_t &FixedValue) {
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000491 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbare180fa92010-03-09 21:27:30 +0000492 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbareb3804e2010-02-17 23:45:16 +0000493 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000494 unsigned Type = RIT_Vanilla;
495
496 // See <reloc.h>.
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000497 const MCSymbol *A = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000498 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000499
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000500 if (!A_SD->getFragment())
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000501 llvm_report_error("symbol '" + A->getName() +
502 "' can not be undefined in a subtraction expression");
503
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000504 uint32_t Value = A_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000505 uint32_t Value2 = 0;
506
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000507 if (const MCSymbolRefExpr *B = Target.getSymB()) {
508 MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000509
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000510 if (!B_SD->getFragment())
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000511 llvm_report_error("symbol '" + B->getSymbol().getName() +
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000512 "' can not be undefined in a subtraction expression");
513
Daniel Dunbar07a96412010-03-10 02:10:29 +0000514 // Select the appropriate difference relocation type.
515 //
516 // Note that there is no longer any semantic difference between these two
517 // relocation types from the linkers point of view, this is done solely
518 // for pedantic compatibility with 'as'.
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000519 Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference;
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000520 Value2 = B_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000521 }
522
Daniel Dunbar17a06502010-03-19 07:09:18 +0000523 // Relocations are written out in reverse order, so the PAIR comes first.
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000524 if (Type == RIT_Difference || Type == RIT_LocalDifference) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000525 MachRelocationEntry MRE;
526 MRE.Word0 = ((0 << 0) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000527 (RIT_Pair << 24) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000528 (Log2Size << 28) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000529 (IsPCRel << 30) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000530 RF_Scattered);
531 MRE.Word1 = Value2;
Daniel Dunbar17a06502010-03-19 07:09:18 +0000532 Relocations[Fragment.getParent()].push_back(MRE);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000533 }
Daniel Dunbar17a06502010-03-19 07:09:18 +0000534
535 MachRelocationEntry MRE;
536 MRE.Word0 = ((Address << 0) |
537 (Type << 24) |
538 (Log2Size << 28) |
539 (IsPCRel << 30) |
540 RF_Scattered);
541 MRE.Word1 = Value;
542 Relocations[Fragment.getParent()].push_back(MRE);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000543 }
544
Daniel Dunbar17a06502010-03-19 07:09:18 +0000545 void RecordRelocation(MCAssembler &Asm, MCDataFragment &Fragment,
546 const MCAsmFixup &Fixup, MCValue Target,
547 uint64_t &FixedValue) {
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000548 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
549 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
550
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000551 // If this is a difference or a defined symbol plus an offset, then we need
552 // a scattered relocation entry.
553 uint32_t Offset = Target.getConstant();
554 if (IsPCRel)
555 Offset += 1 << Log2Size;
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000556 if (Target.getSymB() ||
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000557 (Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() &&
Daniel Dunbar17a06502010-03-19 07:09:18 +0000558 Offset)) {
559 RecordScatteredRelocation(Asm, Fragment, Fixup, Target, FixedValue);
560 return;
561 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000562
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000563 // See <reloc.h>.
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000564 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000565 uint32_t Value = 0;
566 unsigned Index = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000567 unsigned IsExtern = 0;
568 unsigned Type = 0;
569
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000570 if (Target.isAbsolute()) { // constant
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000571 // SymbolNum of 0 indicates the absolute section.
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000572 //
Daniel Dunbar979ba5b2010-03-11 05:53:37 +0000573 // FIXME: Currently, these are never generated (see code below). I cannot
574 // find a case where they are actually emitted.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000575 Type = RIT_Vanilla;
576 Value = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000577 } else {
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000578 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000579 MCSymbolData *SD = &Asm.getSymbolData(*Symbol);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000580
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000581 if (Symbol->isUndefined()) {
582 IsExtern = 1;
583 Index = SD->getIndex();
584 Value = 0;
585 } else {
586 // The index is the section ordinal.
587 //
588 // FIXME: O(N)
589 Index = 1;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000590 MCAssembler::iterator it = Asm.begin(), ie = Asm.end();
591 for (; it != ie; ++it, ++Index)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000592 if (&*it == SD->getFragment()->getParent())
593 break;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000594 assert(it != ie && "Unable to find section index!");
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000595 Value = SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000596 }
597
598 Type = RIT_Vanilla;
599 }
600
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000601 // struct relocation_info (8 bytes)
602 MachRelocationEntry MRE;
603 MRE.Word0 = Address;
604 MRE.Word1 = ((Index << 0) |
605 (IsPCRel << 24) |
606 (Log2Size << 25) |
607 (IsExtern << 27) |
608 (Type << 28));
Daniel Dunbar17a06502010-03-19 07:09:18 +0000609 Relocations[Fragment.getParent()].push_back(MRE);
610 }
611
612 void ComputeRelocationInfo(MCAssembler &Asm, MCDataFragment &Fragment,
613 MCAsmFixup &Fixup) {
614 // FIXME: Share layout object.
615 MCAsmLayout Layout(Asm);
616
617 // Evaluate the fixup; if the value was resolved, no relocation is needed.
618 MCValue Target;
619 if (Asm.EvaluateFixup(Layout, Fixup, &Fragment, Target, Fixup.FixedValue))
620 return;
621
622 RecordRelocation(Asm, Fragment, Fixup, Target, Fixup.FixedValue);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000623 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000624
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000625 void BindIndirectSymbols(MCAssembler &Asm) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000626 // This is the point where 'as' creates actual symbols for indirect symbols
627 // (in the following two passes). It would be easier for us to do this
628 // sooner when we see the attribute, but that makes getting the order in the
629 // symbol table much more complicated than it is worth.
630 //
631 // FIXME: Revisit this when the dust settles.
632
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000633 // Bind non lazy symbol pointers first.
634 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
635 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
636 // FIXME: cast<> support!
637 const MCSectionMachO &Section =
638 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
639
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000640 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000641 continue;
642
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000643 Asm.getOrCreateSymbolData(*it->Symbol);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000644 }
645
646 // Then lazy symbol pointers and symbol stubs.
647 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
648 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
649 // FIXME: cast<> support!
650 const MCSectionMachO &Section =
651 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
652
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000653 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
654 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000655 continue;
656
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000657 // Set the symbol type to undefined lazy, but only on construction.
658 //
659 // FIXME: Do not hardcode.
660 bool Created;
661 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
662 if (Created)
663 Entry.setFlags(Entry.getFlags() | 0x0001);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000664 }
665 }
666
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000667 /// ComputeSymbolTable - Compute the symbol table data
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000668 ///
669 /// \param StringTable [out] - The string table data.
670 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
671 /// string table.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000672 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
673 std::vector<MachSymbolData> &LocalSymbolData,
674 std::vector<MachSymbolData> &ExternalSymbolData,
675 std::vector<MachSymbolData> &UndefinedSymbolData) {
676 // Build section lookup table.
677 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
678 unsigned Index = 1;
679 for (MCAssembler::iterator it = Asm.begin(),
680 ie = Asm.end(); it != ie; ++it, ++Index)
681 SectionIndexMap[&it->getSection()] = Index;
682 assert(Index <= 256 && "Too many sections!");
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000683
684 // Index 0 is always the empty string.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000685 StringMap<uint64_t> StringIndexMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000686 StringTable += '\x00';
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000687
688 // Build the symbol arrays and the string table, but only for non-local
689 // symbols.
690 //
691 // The particular order that we collect the symbols and create the string
692 // table, then sort the symbols is chosen to match 'as'. Even though it
693 // doesn't matter for correctness, this is important for letting us diff .o
694 // files.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000695 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
696 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000697 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000698
Daniel Dunbar23869852010-03-19 03:18:09 +0000699 // Ignore non-linker visible symbols.
700 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000701 continue;
702
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000703 if (!it->isExternal() && !Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000704 continue;
705
706 uint64_t &Entry = StringIndexMap[Symbol.getName()];
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000707 if (!Entry) {
708 Entry = StringTable.size();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000709 StringTable += Symbol.getName();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000710 StringTable += '\x00';
711 }
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000712
713 MachSymbolData MSD;
714 MSD.SymbolData = it;
715 MSD.StringIndex = Entry;
716
717 if (Symbol.isUndefined()) {
718 MSD.SectionIndex = 0;
719 UndefinedSymbolData.push_back(MSD);
720 } else if (Symbol.isAbsolute()) {
721 MSD.SectionIndex = 0;
722 ExternalSymbolData.push_back(MSD);
723 } else {
724 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
725 assert(MSD.SectionIndex && "Invalid section index!");
726 ExternalSymbolData.push_back(MSD);
727 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000728 }
729
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000730 // Now add the data for local symbols.
731 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
732 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000733 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000734
Daniel Dunbar23869852010-03-19 03:18:09 +0000735 // Ignore non-linker visible symbols.
736 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000737 continue;
738
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000739 if (it->isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000740 continue;
741
742 uint64_t &Entry = StringIndexMap[Symbol.getName()];
743 if (!Entry) {
744 Entry = StringTable.size();
745 StringTable += Symbol.getName();
746 StringTable += '\x00';
747 }
748
749 MachSymbolData MSD;
750 MSD.SymbolData = it;
751 MSD.StringIndex = Entry;
752
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000753 if (Symbol.isAbsolute()) {
754 MSD.SectionIndex = 0;
755 LocalSymbolData.push_back(MSD);
756 } else {
757 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
758 assert(MSD.SectionIndex && "Invalid section index!");
759 LocalSymbolData.push_back(MSD);
760 }
761 }
762
763 // External and undefined symbols are required to be in lexicographic order.
764 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
765 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
766
Daniel Dunbarbe963552009-08-26 13:57:54 +0000767 // Set the symbol indices.
768 Index = 0;
769 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
770 LocalSymbolData[i].SymbolData->setIndex(Index++);
771 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
772 ExternalSymbolData[i].SymbolData->setIndex(Index++);
773 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
774 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
775
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000776 // The string table is padded to a multiple of 4.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000777 while (StringTable.size() % 4)
778 StringTable += '\x00';
779 }
780
Daniel Dunbarbacba992010-03-19 07:09:33 +0000781 void ExecutePostLayoutBinding(MCAssembler &Asm) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000782 // Create symbol data for any indirect symbols.
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000783 BindIndirectSymbols(Asm);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000784
Daniel Dunbarbacba992010-03-19 07:09:33 +0000785 // Compute symbol table information and bind symbol indices.
786 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
787 UndefinedSymbolData);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000788
Daniel Dunbarbacba992010-03-19 07:09:33 +0000789 // Compute relocations.
790 for (MCAssembler::iterator it = Asm.begin(),
791 ie = Asm.end(); it != ie; ++it) {
792 MCSectionData &SD = *it;
793 for (MCSectionData::iterator it2 = SD.begin(),
794 ie2 = SD.end(); it2 != ie2; ++it2)
795 if (MCDataFragment *DF = dyn_cast<MCDataFragment>(&*it2))
796 for (unsigned i = 0, e = DF->fixup_size(); i != e; ++i)
797 ComputeRelocationInfo(Asm, *DF, DF->getFixups()[i]);
798 }
799 }
800
801 void WriteObject(const MCAssembler &Asm) {
802 unsigned NumSections = Asm.size();
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000803
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000804 // The section data starts after the header, the segment load command (and
805 // section headers) and the symbol table.
806 unsigned NumLoadCommands = 1;
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000807 uint64_t LoadCommandsSize = Is64Bit ?
808 SegmentLoadCommand64Size + NumSections * Section64Size :
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000809 SegmentLoadCommand32Size + NumSections * Section32Size;
810
811 // Add the symbol table load command sizes, if used.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000812 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
813 UndefinedSymbolData.size();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000814 if (NumSymbols) {
815 NumLoadCommands += 2;
816 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
817 }
818
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000819 // Compute the total size of the section data, as well as its file size and
820 // vm size.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000821 uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
822 + LoadCommandsSize;
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000823 uint64_t SectionDataSize = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000824 uint64_t SectionDataFileSize = 0;
825 uint64_t VMSize = 0;
Daniel Dunbarbacba992010-03-19 07:09:33 +0000826 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000827 ie = Asm.end(); it != ie; ++it) {
Daniel Dunbarbacba992010-03-19 07:09:33 +0000828 const MCSectionData &SD = *it;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000829
830 VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
831
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000832 if (isVirtualSection(SD.getSection()))
833 continue;
834
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000835 SectionDataSize = std::max(SectionDataSize,
836 SD.getAddress() + SD.getSize());
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000837 SectionDataFileSize = std::max(SectionDataFileSize,
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000838 SD.getAddress() + SD.getFileSize());
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000839 }
840
Daniel Dunbar6b716532010-02-09 23:00:14 +0000841 // The section data is padded to 4 bytes.
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000842 //
843 // FIXME: Is this machine dependent?
844 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
845 SectionDataFileSize += SectionDataPadding;
846
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000847 // Write the prolog, starting with the header and load command...
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000848 WriteHeader(NumLoadCommands, LoadCommandsSize,
849 Asm.getSubsectionsViaSymbols());
850 WriteSegmentLoadCommand(NumSections, VMSize,
851 SectionDataStart, SectionDataSize);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000852
Daniel Dunbar17a06502010-03-19 07:09:18 +0000853 // ... and then the section headers.
854 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
Daniel Dunbarbacba992010-03-19 07:09:33 +0000855 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbar17a06502010-03-19 07:09:18 +0000856 ie = Asm.end(); it != ie; ++it) {
857 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
858 unsigned NumRelocs = Relocs.size();
859 uint64_t SectionStart = SectionDataStart + it->getAddress();
860 WriteSection(*it, SectionStart, RelocTableEnd, NumRelocs);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000861 RelocTableEnd += NumRelocs * RelocationInfoSize;
862 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000863
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000864 // Write the symbol table load command, if used.
865 if (NumSymbols) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000866 unsigned FirstLocalSymbol = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000867 unsigned NumLocalSymbols = LocalSymbolData.size();
868 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
869 unsigned NumExternalSymbols = ExternalSymbolData.size();
870 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
871 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000872 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
873 unsigned NumSymTabSymbols =
874 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
875 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
876 uint64_t IndirectSymbolOffset = 0;
877
878 // If used, the indirect symbols are written after the section data.
879 if (NumIndirectSymbols)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000880 IndirectSymbolOffset = RelocTableEnd;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000881
882 // The symbol table is written after the indirect symbol data.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000883 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000884
885 // The string table is written after symbol table.
886 uint64_t StringTableOffset =
Daniel Dunbar5691e742010-03-13 22:49:35 +0000887 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
888 Nlist32Size);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000889 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
890 StringTableOffset, StringTable.size());
891
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000892 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
893 FirstExternalSymbol, NumExternalSymbols,
894 FirstUndefinedSymbol, NumUndefinedSymbols,
895 IndirectSymbolOffset, NumIndirectSymbols);
896 }
897
898 // Write the actual section data.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000899 for (MCAssembler::const_iterator it = Asm.begin(),
900 ie = Asm.end(); it != ie; ++it)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000901 WriteFileData(OS, *it, *this);
902
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000903 // Write the extra padding.
904 WriteZeros(SectionDataPadding);
905
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000906 // Write the relocation entries.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000907 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbar17a06502010-03-19 07:09:18 +0000908 ie = Asm.end(); it != ie; ++it) {
909 // Write the section relocation entries, in reverse order to match 'as'
910 // (approximately, the exact algorithm is more complicated than this).
911 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
912 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
913 Write32(Relocs[e - i - 1].Word0);
914 Write32(Relocs[e - i - 1].Word1);
915 }
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000916 }
917
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000918 // Write the symbol table data, if used.
919 if (NumSymbols) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000920 // Write the indirect symbol entries.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000921 for (MCAssembler::const_indirect_symbol_iterator
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000922 it = Asm.indirect_symbol_begin(),
923 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
924 // Indirect symbols in the non lazy symbol pointer section have some
925 // special handling.
926 const MCSectionMachO &Section =
927 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000928 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000929 // If this symbol is defined and internal, mark it as such.
930 if (it->Symbol->isDefined() &&
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000931 !Asm.getSymbolData(*it->Symbol).isExternal()) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000932 uint32_t Flags = ISF_Local;
933 if (it->Symbol->isAbsolute())
934 Flags |= ISF_Absolute;
935 Write32(Flags);
936 continue;
937 }
938 }
939
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000940 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000941 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000942
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000943 // FIXME: Check that offsets match computed ones.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000944
945 // Write the symbol table entries.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000946 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000947 WriteNlist(LocalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000948 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000949 WriteNlist(ExternalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000950 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000951 WriteNlist(UndefinedSymbolData[i]);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000952
953 // Write the string table.
954 OS << StringTable.str();
955 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000956 }
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000957
958 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000959 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
960
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000961 // FIXME: Endianness assumption.
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000962 assert(Fixup.Offset + Size <= DF.getContents().size() &&
963 "Invalid fixup offset!");
964 for (unsigned i = 0; i != Size; ++i)
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000965 DF.getContents()[Fixup.Offset + i] = uint8_t(Fixup.FixedValue >> (i * 8));
966 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000967};
968
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000969/* *** */
970
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000971MCFragment::MCFragment() : Kind(FragmentType(~0)) {
972}
973
Daniel Dunbar5e835962009-08-26 02:48:04 +0000974MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000975 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000976 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000977 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000978{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000979 if (Parent)
980 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000981}
982
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000983MCFragment::~MCFragment() {
984}
985
Daniel Dunbar5e835962009-08-26 02:48:04 +0000986uint64_t MCFragment::getAddress() const {
987 assert(getParent() && "Missing Section!");
988 return getParent()->getAddress() + Offset;
989}
990
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000991/* *** */
992
Daniel Dunbar81e40002009-08-27 00:38:04 +0000993MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000994
995MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000996 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000997 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000998 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +0000999 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +00001000 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +00001001 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001002{
1003 if (A)
1004 A->getSectionList().push_back(this);
1005}
1006
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001007/* *** */
1008
Daniel Dunbarefbb5332009-09-01 04:09:03 +00001009MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001010
Daniel Dunbarcb579b32009-08-31 08:08:06 +00001011MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001012 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +00001013 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +00001014 IsExternal(false), IsPrivateExtern(false),
1015 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001016{
1017 if (A)
1018 A->getSymbolList().push_back(this);
1019}
1020
1021/* *** */
1022
Daniel Dunbar1f3e4452010-03-11 01:34:27 +00001023MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
1024 raw_ostream &_OS)
1025 : Context(_Context), Backend(_Backend), OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +00001026{
1027}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001028
1029MCAssembler::~MCAssembler() {
1030}
1031
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001032static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
1033 const MCAsmFixup &Fixup,
1034 const MCDataFragment *DF,
1035 const MCValue Target,
1036 const MCSection *BaseSection) {
1037 // The effective fixup address is
1038 // addr(atom(A)) + offset(A)
1039 // - addr(atom(B)) - offset(B)
1040 // - addr(<base symbol>) + <fixup offset from base symbol>
1041 // and the offsets are not relocatable, so the fixup is fully resolved when
1042 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
1043 //
1044 // The simple (Darwin, except on x86_64) way of dealing with this was to
1045 // assume that any reference to a temporary symbol *must* be a temporary
1046 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
1047 // relocation to a temporary symbol (in the same section) is fully
1048 // resolved. This also works in conjunction with absolutized .set, which
1049 // requires the compiler to use .set to absolutize the differences between
1050 // symbols which the compiler knows to be assembly time constants, so we don't
1051 // need to worry about consider symbol differences fully resolved.
1052
1053 // Non-relative fixups are only resolved if constant.
1054 if (!BaseSection)
1055 return Target.isAbsolute();
1056
1057 // Otherwise, relative fixups are only resolved if not a difference and the
1058 // target is a temporary in the same section.
1059 if (Target.isAbsolute() || Target.getSymB())
1060 return false;
1061
1062 const MCSymbol *A = &Target.getSymA()->getSymbol();
1063 if (!A->isTemporary() || !A->isInSection() ||
1064 &A->getSection() != BaseSection)
1065 return false;
1066
1067 return true;
1068}
1069
Daniel Dunbar034843a2010-03-19 03:18:18 +00001070static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
1071 const MCAsmFixup &Fixup,
1072 const MCDataFragment *DF,
1073 const MCValue Target,
1074 const MCSymbolData *BaseSymbol) {
1075 // The effective fixup address is
1076 // addr(atom(A)) + offset(A)
1077 // - addr(atom(B)) - offset(B)
1078 // - addr(BaseSymbol) + <fixup offset from base symbol>
1079 // and the offsets are not relocatable, so the fixup is fully resolved when
1080 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
1081 //
1082 // Note that "false" is almost always conservatively correct (it means we emit
1083 // a relocation which is unnecessary), except when it would force us to emit a
1084 // relocation which the target cannot encode.
1085
1086 const MCSymbolData *A_Base = 0, *B_Base = 0;
1087 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1088 // Modified symbol references cannot be resolved.
1089 if (A->getKind() != MCSymbolRefExpr::VK_None)
1090 return false;
1091
1092 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
1093 if (!A_Base)
1094 return false;
1095 }
1096
1097 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1098 // Modified symbol references cannot be resolved.
1099 if (B->getKind() != MCSymbolRefExpr::VK_None)
1100 return false;
1101
1102 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
1103 if (!B_Base)
1104 return false;
1105 }
1106
1107 // If there is no base, A and B have to be the same atom for this fixup to be
1108 // fully resolved.
1109 if (!BaseSymbol)
1110 return A_Base == B_Base;
1111
1112 // Otherwise, B must be missing and A must be the base.
1113 return !B_Base && BaseSymbol == A_Base;
1114}
1115
Daniel Dunbar23869852010-03-19 03:18:09 +00001116bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
1117 // Non-temporary labels should always be visible to the linker.
1118 if (!SD->getSymbol().isTemporary())
1119 return true;
1120
1121 // Absolute temporary labels are never visible.
1122 if (!SD->getFragment())
1123 return false;
1124
1125 // Otherwise, check if the section requires symbols even for temporary labels.
1126 return getBackend().doesSectionRequireSymbols(
1127 SD->getFragment()->getParent()->getSection());
1128}
1129
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +00001130const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
1131 uint64_t Address) const {
1132 const MCSymbolData *Best = 0;
1133 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
1134 ie = symbol_end(); it != ie; ++it) {
1135 // Ignore non-linker visible symbols.
1136 if (!isSymbolLinkerVisible(it))
1137 continue;
1138
1139 // Ignore symbols not in the same section.
1140 if (!it->getFragment() || it->getFragment()->getParent() != Section)
1141 continue;
1142
1143 // Otherwise, find the closest symbol preceding this address (ties are
1144 // resolved in favor of the last defined symbol).
1145 if (it->getAddress() <= Address &&
1146 (!Best || it->getAddress() >= Best->getAddress()))
1147 Best = it;
1148 }
1149
1150 return Best;
1151}
1152
1153const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
1154 // Linker visible symbols define atoms.
1155 if (isSymbolLinkerVisible(SD))
1156 return SD;
1157
1158 // Absolute and undefined symbols have no defining atom.
1159 if (!SD->getFragment())
1160 return 0;
1161
1162 // Otherwise, search by address.
1163 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
1164}
1165
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001166bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup,
1167 MCDataFragment *DF,
1168 MCValue &Target, uint64_t &Value) const {
1169 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
1170 llvm_report_error("expected relocatable expression");
1171
1172 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
1173 // doesn't support small relocations, but then under what criteria does the
1174 // assembler allow symbol differences?
1175
1176 Value = Target.getConstant();
1177
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001178 bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001179 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1180 if (A->getSymbol().isDefined())
1181 Value += getSymbolData(A->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001182 else
1183 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001184 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001185 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1186 if (B->getSymbol().isDefined())
1187 Value -= getSymbolData(B->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001188 else
1189 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001190 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001191
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001192 // If we are using scattered symbols, determine whether this value is actually
1193 // resolved; scattering may cause atoms to move.
1194 if (IsResolved && getBackend().hasScatteredSymbols()) {
1195 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +00001196 // If this is a PCrel relocation, find the base atom (identified by its
1197 // symbol) that the fixup value is relative to.
1198 const MCSymbolData *BaseSymbol = 0;
1199 if (IsPCRel) {
1200 BaseSymbol = getAtomForAddress(
1201 DF->getParent(), DF->getAddress() + Fixup.Offset);
1202 if (!BaseSymbol)
1203 IsResolved = false;
1204 }
1205
1206 if (IsResolved)
1207 IsResolved = isScatteredFixupFullyResolved(*this, Fixup, DF, Target,
1208 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001209 } else {
1210 const MCSection *BaseSection = 0;
1211 if (IsPCRel)
1212 BaseSection = &DF->getParent()->getSection();
1213
1214 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, DF, Target,
1215 BaseSection);
1216 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001217 }
1218
1219 if (IsPCRel)
Daniel Dunbarda3e9f72010-03-13 02:38:00 +00001220 Value -= DF->getAddress() + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001221
1222 return IsResolved;
1223}
1224
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001225void MCAssembler::LayoutSection(MCSectionData &SD) {
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001226 MCAsmLayout Layout(*this);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001227 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001228
1229 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
1230 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001231
Daniel Dunbar6742e342009-08-26 04:13:32 +00001232 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001233
1234 // Evaluate fragment size.
1235 switch (F.getKind()) {
1236 case MCFragment::FT_Align: {
1237 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001238
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001239 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +00001240 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001241 AF.setFileSize(0);
1242 else
Daniel Dunbar6742e342009-08-26 04:13:32 +00001243 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001244 break;
1245 }
1246
1247 case MCFragment::FT_Data:
Daniel Dunbara4766d72010-02-13 09:28:32 +00001248 case MCFragment::FT_Fill:
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001249 F.setFileSize(F.getMaxFileSize());
1250 break;
1251
1252 case MCFragment::FT_Org: {
1253 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1254
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001255 int64_t TargetLocation;
1256 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
1257 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001258
1259 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001260 int64_t Offset = TargetLocation - F.getOffset();
1261 if (Offset < 0)
1262 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
1263 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001264
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001265 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001266 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001267 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001268
1269 case MCFragment::FT_ZeroFill: {
1270 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
1271
1272 // Align the fragment offset; it is safe to adjust the offset freely since
1273 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +00001274 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
1275 F.setOffset(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001276
1277 // FIXME: This is misnamed.
1278 F.setFileSize(ZFF.getSize());
1279 break;
1280 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001281 }
1282
Daniel Dunbar6742e342009-08-26 04:13:32 +00001283 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001284 }
1285
Daniel Dunbar6742e342009-08-26 04:13:32 +00001286 // Set the section sizes.
1287 SD.setSize(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001288 if (isVirtualSection(SD.getSection()))
1289 SD.setFileSize(0);
1290 else
1291 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001292}
1293
Kevin Enderby6e720482010-02-23 18:26:34 +00001294/// WriteNopData - Write optimal nops to the output file for the \arg Count
1295/// bytes. This returns the number of bytes written. It may return 0 if
1296/// the \arg Count is more than the maximum optimal nops.
1297///
1298/// FIXME this is X86 32-bit specific and should move to a better place.
1299static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW) {
Kevin Enderby76687082010-02-23 21:41:24 +00001300 static const uint8_t Nops[16][16] = {
1301 // nop
1302 {0x90},
1303 // xchg %ax,%ax
1304 {0x66, 0x90},
1305 // nopl (%[re]ax)
1306 {0x0f, 0x1f, 0x00},
1307 // nopl 0(%[re]ax)
1308 {0x0f, 0x1f, 0x40, 0x00},
1309 // nopl 0(%[re]ax,%[re]ax,1)
1310 {0x0f, 0x1f, 0x44, 0x00, 0x00},
1311 // nopw 0(%[re]ax,%[re]ax,1)
1312 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1313 // nopl 0L(%[re]ax)
1314 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1315 // nopl 0L(%[re]ax,%[re]ax,1)
1316 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1317 // nopw 0L(%[re]ax,%[re]ax,1)
1318 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1319 // nopw %cs:0L(%[re]ax,%[re]ax,1)
1320 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1321 // nopl 0(%[re]ax,%[re]ax,1)
1322 // nopw 0(%[re]ax,%[re]ax,1)
1323 {0x0f, 0x1f, 0x44, 0x00, 0x00,
1324 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1325 // nopw 0(%[re]ax,%[re]ax,1)
1326 // nopw 0(%[re]ax,%[re]ax,1)
1327 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1328 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1329 // nopw 0(%[re]ax,%[re]ax,1)
1330 // nopl 0L(%[re]ax) */
1331 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1332 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1333 // nopl 0L(%[re]ax)
1334 // nopl 0L(%[re]ax)
1335 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1336 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1337 // nopl 0L(%[re]ax)
1338 // nopl 0L(%[re]ax,%[re]ax,1)
1339 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1340 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
1341 };
1342
1343 if (Count > 15)
1344 return 0;
1345
Kevin Enderby6e720482010-02-23 18:26:34 +00001346 for (uint64_t i = 0; i < Count; i++)
Kevin Enderby76687082010-02-23 21:41:24 +00001347 MOW.Write8 (uint8_t(Nops[Count - 1][i]));
Kevin Enderby6e720482010-02-23 18:26:34 +00001348
1349 return Count;
1350}
1351
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001352/// WriteFileData - Write the \arg F data to the output file.
1353static void WriteFileData(raw_ostream &OS, const MCFragment &F,
1354 MachObjectWriter &MOW) {
1355 uint64_t Start = OS.tell();
1356 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001357
Daniel Dunbar0adcd352009-08-25 21:10:45 +00001358 ++EmittedFragments;
1359
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001360 // FIXME: Embed in fragments instead?
1361 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001362 case MCFragment::FT_Align: {
1363 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1364 uint64_t Count = AF.getFileSize() / AF.getValueSize();
1365
1366 // FIXME: This error shouldn't actually occur (the front end should emit
1367 // multiple .align directives to enforce the semantics it wants), but is
1368 // severe enough that we want to report it. How to handle this?
1369 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001370 llvm_report_error("undefined .align directive, value size '" +
1371 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001372 "' is not a divisor of padding size '" +
1373 Twine(AF.getFileSize()) + "'");
1374
Kevin Enderby6e720482010-02-23 18:26:34 +00001375 // See if we are aligning with nops, and if so do that first to try to fill
1376 // the Count bytes. Then if that did not fill any bytes or there are any
1377 // bytes left to fill use the the Value and ValueSize to fill the rest.
1378 if (AF.getEmitNops()) {
1379 uint64_t NopByteCount = WriteNopData(Count, MOW);
1380 Count -= NopByteCount;
1381 }
1382
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001383 for (uint64_t i = 0; i != Count; ++i) {
1384 switch (AF.getValueSize()) {
1385 default:
1386 assert(0 && "Invalid size!");
1387 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
1388 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
1389 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
1390 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
1391 }
1392 }
1393 break;
1394 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001395
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001396 case MCFragment::FT_Data: {
1397 MCDataFragment &DF = cast<MCDataFragment>(F);
1398
1399 // Apply the fixups.
1400 //
1401 // FIXME: Move elsewhere.
1402 for (MCDataFragment::const_fixup_iterator it = DF.fixup_begin(),
1403 ie = DF.fixup_end(); it != ie; ++it)
1404 MOW.ApplyFixup(*it, DF);
1405
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001406 OS << cast<MCDataFragment>(F).getContents().str();
1407 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001408 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001409
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001410 case MCFragment::FT_Fill: {
1411 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001412 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1413 switch (FF.getValueSize()) {
1414 default:
1415 assert(0 && "Invalid size!");
Daniel Dunbara4766d72010-02-13 09:28:32 +00001416 case 1: MOW.Write8 (uint8_t (FF.getValue())); break;
1417 case 2: MOW.Write16(uint16_t(FF.getValue())); break;
1418 case 4: MOW.Write32(uint32_t(FF.getValue())); break;
1419 case 8: MOW.Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001420 }
1421 }
1422 break;
1423 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001424
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001425 case MCFragment::FT_Org: {
1426 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1427
1428 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1429 MOW.Write8(uint8_t(OF.getValue()));
1430
1431 break;
1432 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001433
1434 case MCFragment::FT_ZeroFill: {
1435 assert(0 && "Invalid zero fill fragment in concrete section!");
1436 break;
1437 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001438 }
1439
1440 assert(OS.tell() - Start == F.getFileSize());
1441}
1442
1443/// WriteFileData - Write the \arg SD data to the output file.
1444static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1445 MachObjectWriter &MOW) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001446 // Ignore virtual sections.
1447 if (isVirtualSection(SD.getSection())) {
1448 assert(SD.getFileSize() == 0);
1449 return;
1450 }
1451
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001452 uint64_t Start = OS.tell();
1453 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001454
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001455 for (MCSectionData::const_iterator it = SD.begin(),
1456 ie = SD.end(); it != ie; ++it)
1457 WriteFileData(OS, *it, MOW);
1458
Daniel Dunbar6742e342009-08-26 04:13:32 +00001459 // Add section padding.
1460 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1461 MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1462
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001463 assert(OS.tell() - Start == SD.getFileSize());
1464}
1465
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001466void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001467 DEBUG_WITH_TYPE("mc-dump", {
1468 llvm::errs() << "assembler backend - pre-layout\n--\n";
1469 dump(); });
1470
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001471 // Layout until everything fits.
1472 while (LayoutOnce())
1473 continue;
1474
1475 DEBUG_WITH_TYPE("mc-dump", {
1476 llvm::errs() << "assembler backend - post-layout\n--\n";
1477 dump(); });
1478
Daniel Dunbaree0d8922010-03-13 22:10:17 +00001479 // FIXME: Factor out MCObjectWriter.
1480 bool Is64Bit = StringRef(getBackend().getTarget().getName()) == "x86-64";
1481 MachObjectWriter MOW(OS, Is64Bit);
Daniel Dunbarbacba992010-03-19 07:09:33 +00001482
1483 // Allow the object writer a chance to perform post-layout binding (for
1484 // example, to set the index fields in the symbol data).
1485 MOW.ExecutePostLayoutBinding(*this);
1486
1487 // Write the object file.
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001488 MOW.WriteObject(*this);
1489
1490 OS.flush();
1491}
1492
1493bool MCAssembler::FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF) {
1494 // FIXME: Share layout object.
1495 MCAsmLayout Layout(*this);
1496
1497 // Currently we only need to relax X86::reloc_pcrel_1byte.
1498 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
1499 return false;
1500
1501 // If we cannot resolve the fixup value, it requires relaxation.
1502 MCValue Target;
1503 uint64_t Value;
1504 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
1505 return true;
1506
1507 // Otherwise, relax if the value is too big for a (signed) i8.
1508 return int64_t(Value) != int64_t(int8_t(Value));
1509}
1510
1511bool MCAssembler::LayoutOnce() {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001512 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +00001513 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001514 MCSectionData *Prev = 0;
1515 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +00001516 MCSectionData &SD = *it;
1517
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001518 // Skip virtual sections.
1519 if (isVirtualSection(SD.getSection()))
1520 continue;
1521
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001522 // Align this section if necessary by adding padding bytes to the previous
1523 // section.
1524 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1525 assert(Prev && "Missing prev section!");
1526 Prev->setFileSize(Prev->getFileSize() + Pad);
1527 Address += Pad;
1528 }
Daniel Dunbar6742e342009-08-26 04:13:32 +00001529
1530 // Layout the section fragments and its size.
1531 SD.setAddress(Address);
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001532 LayoutSection(SD);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001533 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001534
1535 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +00001536 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001537
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001538 // Layout the virtual sections.
1539 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1540 MCSectionData &SD = *it;
1541
1542 if (!isVirtualSection(SD.getSection()))
1543 continue;
1544
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001545 // Align this section if necessary by adding padding bytes to the previous
1546 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +00001547 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001548 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001549
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001550 SD.setAddress(Address);
1551 LayoutSection(SD);
1552 Address += SD.getSize();
1553 }
1554
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001555 // Scan the fixups in order and relax any that don't fit.
1556 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1557 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001558
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001559 for (MCSectionData::iterator it2 = SD.begin(),
1560 ie2 = SD.end(); it2 != ie2; ++it2) {
1561 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
1562 if (!DF)
1563 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001564
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001565 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
1566 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
1567 MCAsmFixup &Fixup = *it3;
1568
1569 // Check whether we need to relax this fixup.
1570 if (!FixupNeedsRelaxation(Fixup, DF))
1571 continue;
1572
1573 // Relax the instruction.
1574 //
1575 // FIXME: This is a huge temporary hack which just looks for x86
1576 // branches; the only thing we need to relax on x86 is
1577 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
1578 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
1579 // an individual MCInst.
1580 SmallVectorImpl<char> &C = DF->getContents();
1581 uint64_t PrevOffset = Fixup.Offset;
1582 unsigned Amt = 0;
1583
1584 // jcc instructions
1585 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
1586 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
1587 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
1588 C[Fixup.Offset-1] = char(0x0f);
1589 ++Fixup.Offset;
1590 Amt = 4;
1591
1592 // jmp rel8
1593 } else if (C[Fixup.Offset-1] == char(0xeb)) {
1594 C[Fixup.Offset-1] = char(0xe9);
1595 Amt = 3;
1596
1597 } else
1598 llvm_unreachable("unknown 1 byte pcrel instruction!");
1599
1600 Fixup.Value = MCBinaryExpr::Create(
1601 MCBinaryExpr::Sub, Fixup.Value,
1602 MCConstantExpr::Create(3, getContext()),
1603 getContext());
1604 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
1605 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
1606
1607 // Update the remaining fixups, which have slid.
1608 //
1609 // FIXME: This is bad for performance, but will be eliminated by the
1610 // move to MCInst specific fragments.
1611 ++it3;
1612 for (; it3 != ie3; ++it3)
1613 it3->Offset += Amt;
1614
1615 // Update all the symbols for this fragment, which may have slid.
1616 //
1617 // FIXME: This is really really bad for performance, but will be
1618 // eliminated by the move to MCInst specific fragments.
1619 for (MCAssembler::symbol_iterator it = symbol_begin(),
1620 ie = symbol_end(); it != ie; ++it) {
1621 MCSymbolData &SD = *it;
1622
1623 if (it->getFragment() != DF)
1624 continue;
1625
1626 if (SD.getOffset() > PrevOffset)
1627 SD.setOffset(SD.getOffset() + Amt);
1628 }
1629
1630 // Restart layout.
1631 //
1632 // FIXME: This is O(N^2), but will be eliminated once we have a smart
1633 // MCAsmLayout object.
1634 return true;
1635 }
1636 }
1637 }
1638
1639 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001640}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001641
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001642// Debugging methods
1643
1644namespace llvm {
1645
1646raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001647 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
1648 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001649 return OS;
1650}
1651
1652}
1653
1654void MCFragment::dump() {
1655 raw_ostream &OS = llvm::errs();
1656
1657 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1658 << " FileSize:" << FileSize;
1659
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001660 OS << ">";
1661}
1662
1663void MCAlignFragment::dump() {
1664 raw_ostream &OS = llvm::errs();
1665
1666 OS << "<MCAlignFragment ";
1667 this->MCFragment::dump();
1668 OS << "\n ";
1669 OS << " Alignment:" << getAlignment()
1670 << " Value:" << getValue() << " ValueSize:" << getValueSize()
1671 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1672}
1673
1674void MCDataFragment::dump() {
1675 raw_ostream &OS = llvm::errs();
1676
1677 OS << "<MCDataFragment ";
1678 this->MCFragment::dump();
1679 OS << "\n ";
1680 OS << " Contents:[";
1681 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1682 if (i) OS << ",";
1683 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1684 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001685 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001686
1687 if (!getFixups().empty()) {
1688 OS << ",\n ";
1689 OS << " Fixups:[";
1690 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001691 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001692 OS << *it;
1693 }
1694 OS << "]";
1695 }
1696
1697 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001698}
1699
1700void MCFillFragment::dump() {
1701 raw_ostream &OS = llvm::errs();
1702
1703 OS << "<MCFillFragment ";
1704 this->MCFragment::dump();
1705 OS << "\n ";
1706 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1707 << " Count:" << getCount() << ">";
1708}
1709
1710void MCOrgFragment::dump() {
1711 raw_ostream &OS = llvm::errs();
1712
1713 OS << "<MCOrgFragment ";
1714 this->MCFragment::dump();
1715 OS << "\n ";
1716 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1717}
1718
1719void MCZeroFillFragment::dump() {
1720 raw_ostream &OS = llvm::errs();
1721
1722 OS << "<MCZeroFillFragment ";
1723 this->MCFragment::dump();
1724 OS << "\n ";
1725 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1726}
1727
1728void MCSectionData::dump() {
1729 raw_ostream &OS = llvm::errs();
1730
1731 OS << "<MCSectionData";
1732 OS << " Alignment:" << getAlignment() << " Address:" << Address
1733 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001734 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001735 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1736 if (it != begin()) OS << ",\n ";
1737 it->dump();
1738 }
1739 OS << "]>";
1740}
1741
1742void MCSymbolData::dump() {
1743 raw_ostream &OS = llvm::errs();
1744
1745 OS << "<MCSymbolData Symbol:" << getSymbol()
1746 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1747 << " Flags:" << getFlags() << " Index:" << getIndex();
1748 if (isCommon())
1749 OS << " (common, size:" << getCommonSize()
1750 << " align: " << getCommonAlignment() << ")";
1751 if (isExternal())
1752 OS << " (external)";
1753 if (isPrivateExtern())
1754 OS << " (private extern)";
1755 OS << ">";
1756}
1757
1758void MCAssembler::dump() {
1759 raw_ostream &OS = llvm::errs();
1760
1761 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001762 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001763 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1764 if (it != begin()) OS << ",\n ";
1765 it->dump();
1766 }
1767 OS << "],\n";
1768 OS << " Symbols:[";
1769
1770 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001771 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001772 it->dump();
1773 }
1774 OS << "]>\n";
1775}