blob: 0d4f98cc030e33bad3fc4f525d9b044a0191b2a0 [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
173public:
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000174 MachObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool _IsLSB = true)
175 : OS(_OS), Is64Bit(_Is64Bit), IsLSB(_IsLSB) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176 }
177
178 /// @name Helper Methods
179 /// @{
180
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000181 void Write8(uint8_t Value) {
182 OS << char(Value);
183 }
184
185 void Write16(uint16_t Value) {
186 if (IsLSB) {
187 Write8(uint8_t(Value >> 0));
188 Write8(uint8_t(Value >> 8));
189 } else {
190 Write8(uint8_t(Value >> 8));
191 Write8(uint8_t(Value >> 0));
192 }
193 }
194
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000195 void Write32(uint32_t Value) {
196 if (IsLSB) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000197 Write16(uint16_t(Value >> 0));
198 Write16(uint16_t(Value >> 16));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000199 } else {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000200 Write16(uint16_t(Value >> 16));
201 Write16(uint16_t(Value >> 0));
202 }
203 }
204
205 void Write64(uint64_t Value) {
206 if (IsLSB) {
207 Write32(uint32_t(Value >> 0));
208 Write32(uint32_t(Value >> 32));
209 } else {
210 Write32(uint32_t(Value >> 32));
211 Write32(uint32_t(Value >> 0));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000212 }
213 }
214
215 void WriteZeros(unsigned N) {
216 const char Zeros[16] = { 0 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000217
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000218 for (unsigned i = 0, e = N / 16; i != e; ++i)
219 OS << StringRef(Zeros, 16);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000220
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000221 OS << StringRef(Zeros, N % 16);
222 }
223
Daniel Dunbar2928c832009-11-06 10:58:06 +0000224 void WriteString(StringRef Str, unsigned ZeroFillSize = 0) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000225 OS << Str;
226 if (ZeroFillSize)
227 WriteZeros(ZeroFillSize - Str.size());
228 }
229
230 /// @}
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000231
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000232 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
233 bool SubsectionsViaSymbols) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000234 uint32_t Flags = 0;
235
236 if (SubsectionsViaSymbols)
237 Flags |= HF_SubsectionsViaSymbols;
238
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000239 // struct mach_header (28 bytes) or
240 // struct mach_header_64 (32 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000241
242 uint64_t Start = OS.tell();
243 (void) Start;
244
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000245 Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000246
247 // FIXME: Support cputype.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000248 Write32(Is64Bit ? MachO::CPUTypeX86_64 : MachO::CPUTypeI386);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000249 // FIXME: Support cpusubtype.
Chris Lattner45f8c092010-02-02 19:38:14 +0000250 Write32(MachO::CPUSubType_I386_ALL);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000251 Write32(HFT_Object);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000252 Write32(NumLoadCommands); // Object files have a single load command, the
253 // segment.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000254 Write32(LoadCommandsSize);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000255 Write32(Flags);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000256 if (Is64Bit)
257 Write32(0); // reserved
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000258
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000259 assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000260 }
261
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000262 /// WriteSegmentLoadCommand - Write a segment load command.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000263 ///
264 /// \arg NumSections - The number of sections in this segment.
265 /// \arg SectionDataSize - The total size of the sections.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000266 void WriteSegmentLoadCommand(unsigned NumSections,
267 uint64_t VMSize,
268 uint64_t SectionDataStartOffset,
269 uint64_t SectionDataSize) {
270 // struct segment_command (56 bytes) or
271 // struct segment_command_64 (72 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000272
273 uint64_t Start = OS.tell();
274 (void) Start;
275
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000276 unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
277 SegmentLoadCommand32Size;
278 Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
279 Write32(SegmentLoadCommandSize +
280 NumSections * (Is64Bit ? Section64Size : Section32Size));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000281
282 WriteString("", 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000283 if (Is64Bit) {
284 Write64(0); // vmaddr
285 Write64(VMSize); // vmsize
286 Write64(SectionDataStartOffset); // file offset
287 Write64(SectionDataSize); // file size
288 } else {
289 Write32(0); // vmaddr
290 Write32(VMSize); // vmsize
291 Write32(SectionDataStartOffset); // file offset
292 Write32(SectionDataSize); // file size
293 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000294 Write32(0x7); // maxprot
295 Write32(0x7); // initprot
296 Write32(NumSections);
297 Write32(0); // flags
298
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000299 assert(OS.tell() - Start == SegmentLoadCommandSize);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000300 }
301
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000302 void WriteSection(const MCSectionData &SD, uint64_t FileOffset,
303 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000304 // The offset is unused for virtual sections.
305 if (isVirtualSection(SD.getSection())) {
306 assert(SD.getFileSize() == 0 && "Invalid file size!");
307 FileOffset = 0;
308 }
309
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000310 // struct section (68 bytes) or
311 // struct section_64 (80 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000312
313 uint64_t Start = OS.tell();
314 (void) Start;
315
316 // FIXME: cast<> support!
317 const MCSectionMachO &Section =
318 static_cast<const MCSectionMachO&>(SD.getSection());
319 WriteString(Section.getSectionName(), 16);
320 WriteString(Section.getSegmentName(), 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000321 if (Is64Bit) {
322 Write64(SD.getAddress()); // address
323 Write64(SD.getSize()); // size
324 } else {
325 Write32(SD.getAddress()); // address
326 Write32(SD.getSize()); // size
327 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000328 Write32(FileOffset);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000329
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000330 unsigned Flags = Section.getTypeAndAttributes();
331 if (SD.hasInstructions())
332 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
333
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000334 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
335 Write32(Log2_32(SD.getAlignment()));
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000336 Write32(NumRelocations ? RelocationsStart : 0);
337 Write32(NumRelocations);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000338 Write32(Flags);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000339 Write32(0); // reserved1
340 Write32(Section.getStubSize()); // reserved2
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000341 if (Is64Bit)
342 Write32(0); // reserved3
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000343
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000344 assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000345 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000346
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000347 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
348 uint32_t StringTableOffset,
349 uint32_t StringTableSize) {
350 // struct symtab_command (24 bytes)
351
352 uint64_t Start = OS.tell();
353 (void) Start;
354
355 Write32(LCT_Symtab);
356 Write32(SymtabLoadCommandSize);
357 Write32(SymbolOffset);
358 Write32(NumSymbols);
359 Write32(StringTableOffset);
360 Write32(StringTableSize);
361
362 assert(OS.tell() - Start == SymtabLoadCommandSize);
363 }
364
365 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
366 uint32_t NumLocalSymbols,
367 uint32_t FirstExternalSymbol,
368 uint32_t NumExternalSymbols,
369 uint32_t FirstUndefinedSymbol,
370 uint32_t NumUndefinedSymbols,
371 uint32_t IndirectSymbolOffset,
372 uint32_t NumIndirectSymbols) {
373 // struct dysymtab_command (80 bytes)
374
375 uint64_t Start = OS.tell();
376 (void) Start;
377
378 Write32(LCT_Dysymtab);
379 Write32(DysymtabLoadCommandSize);
380 Write32(FirstLocalSymbol);
381 Write32(NumLocalSymbols);
382 Write32(FirstExternalSymbol);
383 Write32(NumExternalSymbols);
384 Write32(FirstUndefinedSymbol);
385 Write32(NumUndefinedSymbols);
386 Write32(0); // tocoff
387 Write32(0); // ntoc
388 Write32(0); // modtaboff
389 Write32(0); // nmodtab
390 Write32(0); // extrefsymoff
391 Write32(0); // nextrefsyms
392 Write32(IndirectSymbolOffset);
393 Write32(NumIndirectSymbols);
394 Write32(0); // extreloff
395 Write32(0); // nextrel
396 Write32(0); // locreloff
397 Write32(0); // nlocrel
398
399 assert(OS.tell() - Start == DysymtabLoadCommandSize);
400 }
401
Daniel Dunbar5691e742010-03-13 22:49:35 +0000402 void WriteNlist(MachSymbolData &MSD) {
Daniel Dunbar5e835962009-08-26 02:48:04 +0000403 MCSymbolData &Data = *MSD.SymbolData;
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000404 const MCSymbol &Symbol = Data.getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000405 uint8_t Type = 0;
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000406 uint16_t Flags = Data.getFlags();
407 uint32_t Address = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000408
409 // Set the N_TYPE bits. See <mach-o/nlist.h>.
410 //
411 // FIXME: Are the prebound or indirect fields possible here?
412 if (Symbol.isUndefined())
413 Type = STT_Undefined;
414 else if (Symbol.isAbsolute())
415 Type = STT_Absolute;
416 else
417 Type = STT_Section;
418
419 // FIXME: Set STAB bits.
420
Daniel Dunbar5e835962009-08-26 02:48:04 +0000421 if (Data.isPrivateExtern())
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000422 Type |= STF_PrivateExtern;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000423
424 // Set external bit.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000425 if (Data.isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000426 Type |= STF_External;
427
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000428 // Compute the symbol address.
429 if (Symbol.isDefined()) {
430 if (Symbol.isAbsolute()) {
431 llvm_unreachable("FIXME: Not yet implemented!");
432 } else {
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000433 Address = Data.getAddress();
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000434 }
435 } else if (Data.isCommon()) {
436 // Common symbols are encoded with the size in the address
437 // field, and their alignment in the flags.
438 Address = Data.getCommonSize();
439
440 // Common alignment is packed into the 'desc' bits.
441 if (unsigned Align = Data.getCommonAlignment()) {
442 unsigned Log2Size = Log2_32(Align);
443 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
444 if (Log2Size > 15)
445 llvm_report_error("invalid 'common' alignment '" +
446 Twine(Align) + "'");
447 // FIXME: Keep this mask with the SymbolFlags enumeration.
448 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
449 }
450 }
451
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000452 // struct nlist (12 bytes)
453
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000454 Write32(MSD.StringIndex);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000455 Write8(Type);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000456 Write8(MSD.SectionIndex);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000457
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000458 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
459 // value.
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000460 Write16(Flags);
Daniel Dunbar5691e742010-03-13 22:49:35 +0000461 if (Is64Bit)
462 Write64(Address);
463 else
464 Write32(Address);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000465 }
466
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000467 struct MachRelocationEntry {
468 uint32_t Word0;
469 uint32_t Word1;
470 };
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000471 void ComputeScatteredRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
Daniel Dunbar27ade182010-02-11 21:29:29 +0000472 MCAsmFixup &Fixup,
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000473 const MCValue &Target,
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000474 std::vector<MachRelocationEntry> &Relocs) {
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000475 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbare180fa92010-03-09 21:27:30 +0000476 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbareb3804e2010-02-17 23:45:16 +0000477 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000478 unsigned Type = RIT_Vanilla;
479
480 // See <reloc.h>.
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000481 const MCSymbol *A = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000482 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000483
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000484 if (!A_SD->getFragment())
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000485 llvm_report_error("symbol '" + A->getName() +
486 "' can not be undefined in a subtraction expression");
487
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000488 uint32_t Value = A_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000489 uint32_t Value2 = 0;
490
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000491 if (const MCSymbolRefExpr *B = Target.getSymB()) {
492 MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000493
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000494 if (!B_SD->getFragment())
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000495 llvm_report_error("symbol '" + B->getSymbol().getName() +
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000496 "' can not be undefined in a subtraction expression");
497
Daniel Dunbar07a96412010-03-10 02:10:29 +0000498 // Select the appropriate difference relocation type.
499 //
500 // Note that there is no longer any semantic difference between these two
501 // relocation types from the linkers point of view, this is done solely
502 // for pedantic compatibility with 'as'.
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000503 Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference;
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000504 Value2 = B_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000505 }
506
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000507 MachRelocationEntry MRE;
508 MRE.Word0 = ((Address << 0) |
509 (Type << 24) |
510 (Log2Size << 28) |
511 (IsPCRel << 30) |
512 RF_Scattered);
513 MRE.Word1 = Value;
514 Relocs.push_back(MRE);
515
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000516 if (Type == RIT_Difference || Type == RIT_LocalDifference) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000517 MachRelocationEntry MRE;
518 MRE.Word0 = ((0 << 0) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000519 (RIT_Pair << 24) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000520 (Log2Size << 28) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000521 (IsPCRel << 30) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000522 RF_Scattered);
523 MRE.Word1 = Value2;
524 Relocs.push_back(MRE);
525 }
526 }
527
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000528 void ComputeRelocationInfo(MCAssembler &Asm, MCDataFragment &Fragment,
Daniel Dunbar27ade182010-02-11 21:29:29 +0000529 MCAsmFixup &Fixup,
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000530 std::vector<MachRelocationEntry> &Relocs) {
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000531 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
532 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
533
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000534 // FIXME: Share layout object.
535 MCAsmLayout Layout(Asm);
536
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000537 // Evaluate the fixup; if the value was resolved, no relocation is needed.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000538 MCValue Target;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000539 if (Asm.EvaluateFixup(Layout, Fixup, &Fragment, Target, Fixup.FixedValue))
540 return;
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000541
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000542 // If this is a difference or a defined symbol plus an offset, then we need
543 // a scattered relocation entry.
544 uint32_t Offset = Target.getConstant();
545 if (IsPCRel)
546 Offset += 1 << Log2Size;
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000547 if (Target.getSymB() ||
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000548 (Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() &&
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000549 Offset))
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000550 return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target,
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000551 Relocs);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000552
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000553 // See <reloc.h>.
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000554 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000555 uint32_t Value = 0;
556 unsigned Index = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000557 unsigned IsExtern = 0;
558 unsigned Type = 0;
559
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000560 if (Target.isAbsolute()) { // constant
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000561 // SymbolNum of 0 indicates the absolute section.
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000562 //
Daniel Dunbar979ba5b2010-03-11 05:53:37 +0000563 // FIXME: Currently, these are never generated (see code below). I cannot
564 // find a case where they are actually emitted.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000565 Type = RIT_Vanilla;
566 Value = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000567 } else {
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000568 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000569 MCSymbolData *SD = &Asm.getSymbolData(*Symbol);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000570
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000571 if (Symbol->isUndefined()) {
572 IsExtern = 1;
573 Index = SD->getIndex();
574 Value = 0;
575 } else {
576 // The index is the section ordinal.
577 //
578 // FIXME: O(N)
579 Index = 1;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000580 MCAssembler::iterator it = Asm.begin(), ie = Asm.end();
581 for (; it != ie; ++it, ++Index)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000582 if (&*it == SD->getFragment()->getParent())
583 break;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000584 assert(it != ie && "Unable to find section index!");
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000585 Value = SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000586 }
587
588 Type = RIT_Vanilla;
589 }
590
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000591 // struct relocation_info (8 bytes)
592 MachRelocationEntry MRE;
593 MRE.Word0 = Address;
594 MRE.Word1 = ((Index << 0) |
595 (IsPCRel << 24) |
596 (Log2Size << 25) |
597 (IsExtern << 27) |
598 (Type << 28));
599 Relocs.push_back(MRE);
600 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000601
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000602 void BindIndirectSymbols(MCAssembler &Asm) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000603 // This is the point where 'as' creates actual symbols for indirect symbols
604 // (in the following two passes). It would be easier for us to do this
605 // sooner when we see the attribute, but that makes getting the order in the
606 // symbol table much more complicated than it is worth.
607 //
608 // FIXME: Revisit this when the dust settles.
609
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000610 // Bind non lazy symbol pointers first.
611 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
612 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
613 // FIXME: cast<> support!
614 const MCSectionMachO &Section =
615 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
616
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000617 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000618 continue;
619
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000620 Asm.getOrCreateSymbolData(*it->Symbol);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000621 }
622
623 // Then lazy symbol pointers and symbol stubs.
624 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
625 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
626 // FIXME: cast<> support!
627 const MCSectionMachO &Section =
628 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
629
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000630 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
631 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000632 continue;
633
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000634 // Set the symbol type to undefined lazy, but only on construction.
635 //
636 // FIXME: Do not hardcode.
637 bool Created;
638 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
639 if (Created)
640 Entry.setFlags(Entry.getFlags() | 0x0001);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000641 }
642 }
643
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000644 /// ComputeSymbolTable - Compute the symbol table data
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000645 ///
646 /// \param StringTable [out] - The string table data.
647 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
648 /// string table.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000649 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
650 std::vector<MachSymbolData> &LocalSymbolData,
651 std::vector<MachSymbolData> &ExternalSymbolData,
652 std::vector<MachSymbolData> &UndefinedSymbolData) {
653 // Build section lookup table.
654 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
655 unsigned Index = 1;
656 for (MCAssembler::iterator it = Asm.begin(),
657 ie = Asm.end(); it != ie; ++it, ++Index)
658 SectionIndexMap[&it->getSection()] = Index;
659 assert(Index <= 256 && "Too many sections!");
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000660
661 // Index 0 is always the empty string.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000662 StringMap<uint64_t> StringIndexMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000663 StringTable += '\x00';
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000664
665 // Build the symbol arrays and the string table, but only for non-local
666 // symbols.
667 //
668 // The particular order that we collect the symbols and create the string
669 // table, then sort the symbols is chosen to match 'as'. Even though it
670 // doesn't matter for correctness, this is important for letting us diff .o
671 // files.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000672 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
673 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000674 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000675
Daniel Dunbar23869852010-03-19 03:18:09 +0000676 // Ignore non-linker visible symbols.
677 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000678 continue;
679
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000680 if (!it->isExternal() && !Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000681 continue;
682
683 uint64_t &Entry = StringIndexMap[Symbol.getName()];
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000684 if (!Entry) {
685 Entry = StringTable.size();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000686 StringTable += Symbol.getName();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000687 StringTable += '\x00';
688 }
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000689
690 MachSymbolData MSD;
691 MSD.SymbolData = it;
692 MSD.StringIndex = Entry;
693
694 if (Symbol.isUndefined()) {
695 MSD.SectionIndex = 0;
696 UndefinedSymbolData.push_back(MSD);
697 } else if (Symbol.isAbsolute()) {
698 MSD.SectionIndex = 0;
699 ExternalSymbolData.push_back(MSD);
700 } else {
701 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
702 assert(MSD.SectionIndex && "Invalid section index!");
703 ExternalSymbolData.push_back(MSD);
704 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000705 }
706
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000707 // Now add the data for local symbols.
708 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
709 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000710 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000711
Daniel Dunbar23869852010-03-19 03:18:09 +0000712 // Ignore non-linker visible symbols.
713 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000714 continue;
715
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000716 if (it->isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000717 continue;
718
719 uint64_t &Entry = StringIndexMap[Symbol.getName()];
720 if (!Entry) {
721 Entry = StringTable.size();
722 StringTable += Symbol.getName();
723 StringTable += '\x00';
724 }
725
726 MachSymbolData MSD;
727 MSD.SymbolData = it;
728 MSD.StringIndex = Entry;
729
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000730 if (Symbol.isAbsolute()) {
731 MSD.SectionIndex = 0;
732 LocalSymbolData.push_back(MSD);
733 } else {
734 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
735 assert(MSD.SectionIndex && "Invalid section index!");
736 LocalSymbolData.push_back(MSD);
737 }
738 }
739
740 // External and undefined symbols are required to be in lexicographic order.
741 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
742 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
743
Daniel Dunbarbe963552009-08-26 13:57:54 +0000744 // Set the symbol indices.
745 Index = 0;
746 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
747 LocalSymbolData[i].SymbolData->setIndex(Index++);
748 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
749 ExternalSymbolData[i].SymbolData->setIndex(Index++);
750 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
751 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
752
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000753 // The string table is padded to a multiple of 4.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000754 while (StringTable.size() % 4)
755 StringTable += '\x00';
756 }
757
758 void WriteObject(MCAssembler &Asm) {
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000759 unsigned NumSections = Asm.size();
760
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000761 // Create symbol data for any indirect symbols.
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000762 BindIndirectSymbols(Asm);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000763
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000764 // Compute symbol table information.
765 SmallString<256> StringTable;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000766 std::vector<MachSymbolData> LocalSymbolData;
767 std::vector<MachSymbolData> ExternalSymbolData;
768 std::vector<MachSymbolData> UndefinedSymbolData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000769 unsigned NumSymbols = Asm.symbol_size();
770
771 // No symbol table command is written if there are no symbols.
772 if (NumSymbols)
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000773 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
774 UndefinedSymbolData);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000775
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000776 // The section data starts after the header, the segment load command (and
777 // section headers) and the symbol table.
778 unsigned NumLoadCommands = 1;
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000779 uint64_t LoadCommandsSize = Is64Bit ?
780 SegmentLoadCommand64Size + NumSections * Section64Size :
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000781 SegmentLoadCommand32Size + NumSections * Section32Size;
782
783 // Add the symbol table load command sizes, if used.
784 if (NumSymbols) {
785 NumLoadCommands += 2;
786 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
787 }
788
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000789 // Compute the total size of the section data, as well as its file size and
790 // vm size.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000791 uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
792 + LoadCommandsSize;
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000793 uint64_t SectionDataSize = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000794 uint64_t SectionDataFileSize = 0;
795 uint64_t VMSize = 0;
796 for (MCAssembler::iterator it = Asm.begin(),
797 ie = Asm.end(); it != ie; ++it) {
798 MCSectionData &SD = *it;
799
800 VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
801
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000802 if (isVirtualSection(SD.getSection()))
803 continue;
804
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000805 SectionDataSize = std::max(SectionDataSize,
806 SD.getAddress() + SD.getSize());
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000807 SectionDataFileSize = std::max(SectionDataFileSize,
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000808 SD.getAddress() + SD.getFileSize());
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000809 }
810
Daniel Dunbar6b716532010-02-09 23:00:14 +0000811 // The section data is padded to 4 bytes.
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000812 //
813 // FIXME: Is this machine dependent?
814 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
815 SectionDataFileSize += SectionDataPadding;
816
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000817 // Write the prolog, starting with the header and load command...
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000818 WriteHeader(NumLoadCommands, LoadCommandsSize,
819 Asm.getSubsectionsViaSymbols());
820 WriteSegmentLoadCommand(NumSections, VMSize,
821 SectionDataStart, SectionDataSize);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000822
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000823 // ... and then the section headers.
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000824 //
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000825 // We also compute the section relocations while we do this. Note that
Daniel Dunbar6b716532010-02-09 23:00:14 +0000826 // computing relocation info will also update the fixup to have the correct
827 // value; this will overwrite the appropriate data in the fragment when it
828 // is written.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000829 std::vector<MachRelocationEntry> RelocInfos;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000830 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000831 for (MCAssembler::iterator it = Asm.begin(),
832 ie = Asm.end(); it != ie; ++it) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000833 MCSectionData &SD = *it;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000834
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000835 // The assembler writes relocations in the reverse order they were seen.
836 //
837 // FIXME: It is probably more complicated than this.
838 unsigned NumRelocsStart = RelocInfos.size();
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000839 for (MCSectionData::reverse_iterator it2 = SD.rbegin(),
840 ie2 = SD.rend(); it2 != ie2; ++it2)
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000841 if (MCDataFragment *DF = dyn_cast<MCDataFragment>(&*it2))
842 for (unsigned i = 0, e = DF->fixup_size(); i != e; ++i)
843 ComputeRelocationInfo(Asm, *DF, DF->getFixups()[e - i - 1],
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000844 RelocInfos);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000845
846 unsigned NumRelocs = RelocInfos.size() - NumRelocsStart;
847 uint64_t SectionStart = SectionDataStart + SD.getAddress();
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000848 WriteSection(SD, SectionStart, RelocTableEnd, NumRelocs);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000849 RelocTableEnd += NumRelocs * RelocationInfoSize;
850 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000851
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000852 // Write the symbol table load command, if used.
853 if (NumSymbols) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000854 unsigned FirstLocalSymbol = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000855 unsigned NumLocalSymbols = LocalSymbolData.size();
856 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
857 unsigned NumExternalSymbols = ExternalSymbolData.size();
858 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
859 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000860 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
861 unsigned NumSymTabSymbols =
862 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
863 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
864 uint64_t IndirectSymbolOffset = 0;
865
866 // If used, the indirect symbols are written after the section data.
867 if (NumIndirectSymbols)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000868 IndirectSymbolOffset = RelocTableEnd;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000869
870 // The symbol table is written after the indirect symbol data.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000871 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000872
873 // The string table is written after symbol table.
874 uint64_t StringTableOffset =
Daniel Dunbar5691e742010-03-13 22:49:35 +0000875 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
876 Nlist32Size);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000877 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
878 StringTableOffset, StringTable.size());
879
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000880 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
881 FirstExternalSymbol, NumExternalSymbols,
882 FirstUndefinedSymbol, NumUndefinedSymbols,
883 IndirectSymbolOffset, NumIndirectSymbols);
884 }
885
886 // Write the actual section data.
887 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
888 WriteFileData(OS, *it, *this);
889
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000890 // Write the extra padding.
891 WriteZeros(SectionDataPadding);
892
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000893 // Write the relocation entries.
894 for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) {
895 Write32(RelocInfos[i].Word0);
896 Write32(RelocInfos[i].Word1);
897 }
898
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000899 // Write the symbol table data, if used.
900 if (NumSymbols) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000901 // Write the indirect symbol entries.
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000902 for (MCAssembler::indirect_symbol_iterator
903 it = Asm.indirect_symbol_begin(),
904 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
905 // Indirect symbols in the non lazy symbol pointer section have some
906 // special handling.
907 const MCSectionMachO &Section =
908 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000909 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000910 // If this symbol is defined and internal, mark it as such.
911 if (it->Symbol->isDefined() &&
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000912 !Asm.getSymbolData(*it->Symbol).isExternal()) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000913 uint32_t Flags = ISF_Local;
914 if (it->Symbol->isAbsolute())
915 Flags |= ISF_Absolute;
916 Write32(Flags);
917 continue;
918 }
919 }
920
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000921 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000922 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000923
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000924 // FIXME: Check that offsets match computed ones.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000925
926 // Write the symbol table entries.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000927 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000928 WriteNlist(LocalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000929 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000930 WriteNlist(ExternalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000931 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000932 WriteNlist(UndefinedSymbolData[i]);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000933
934 // Write the string table.
935 OS << StringTable.str();
936 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000937 }
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000938
939 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000940 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
941
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000942 // FIXME: Endianness assumption.
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000943 assert(Fixup.Offset + Size <= DF.getContents().size() &&
944 "Invalid fixup offset!");
945 for (unsigned i = 0; i != Size; ++i)
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000946 DF.getContents()[Fixup.Offset + i] = uint8_t(Fixup.FixedValue >> (i * 8));
947 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000948};
949
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000950/* *** */
951
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000952MCFragment::MCFragment() : Kind(FragmentType(~0)) {
953}
954
Daniel Dunbar5e835962009-08-26 02:48:04 +0000955MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000956 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000957 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000958 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000959{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000960 if (Parent)
961 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000962}
963
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000964MCFragment::~MCFragment() {
965}
966
Daniel Dunbar5e835962009-08-26 02:48:04 +0000967uint64_t MCFragment::getAddress() const {
968 assert(getParent() && "Missing Section!");
969 return getParent()->getAddress() + Offset;
970}
971
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000972/* *** */
973
Daniel Dunbar81e40002009-08-27 00:38:04 +0000974MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000975
976MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000977 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000978 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000979 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +0000980 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000981 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000982 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000983{
984 if (A)
985 A->getSectionList().push_back(this);
986}
987
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000988/* *** */
989
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000990MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000991
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000992MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000993 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000994 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000995 IsExternal(false), IsPrivateExtern(false),
996 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000997{
998 if (A)
999 A->getSymbolList().push_back(this);
1000}
1001
1002/* *** */
1003
Daniel Dunbar1f3e4452010-03-11 01:34:27 +00001004MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
1005 raw_ostream &_OS)
1006 : Context(_Context), Backend(_Backend), OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +00001007{
1008}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001009
1010MCAssembler::~MCAssembler() {
1011}
1012
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001013static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
1014 const MCAsmFixup &Fixup,
1015 const MCDataFragment *DF,
1016 const MCValue Target,
1017 const MCSection *BaseSection) {
1018 // The effective fixup address is
1019 // addr(atom(A)) + offset(A)
1020 // - addr(atom(B)) - offset(B)
1021 // - addr(<base symbol>) + <fixup offset from base symbol>
1022 // and the offsets are not relocatable, so the fixup is fully resolved when
1023 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
1024 //
1025 // The simple (Darwin, except on x86_64) way of dealing with this was to
1026 // assume that any reference to a temporary symbol *must* be a temporary
1027 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
1028 // relocation to a temporary symbol (in the same section) is fully
1029 // resolved. This also works in conjunction with absolutized .set, which
1030 // requires the compiler to use .set to absolutize the differences between
1031 // symbols which the compiler knows to be assembly time constants, so we don't
1032 // need to worry about consider symbol differences fully resolved.
1033
1034 // Non-relative fixups are only resolved if constant.
1035 if (!BaseSection)
1036 return Target.isAbsolute();
1037
1038 // Otherwise, relative fixups are only resolved if not a difference and the
1039 // target is a temporary in the same section.
1040 if (Target.isAbsolute() || Target.getSymB())
1041 return false;
1042
1043 const MCSymbol *A = &Target.getSymA()->getSymbol();
1044 if (!A->isTemporary() || !A->isInSection() ||
1045 &A->getSection() != BaseSection)
1046 return false;
1047
1048 return true;
1049}
1050
Daniel Dunbar23869852010-03-19 03:18:09 +00001051bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
1052 // Non-temporary labels should always be visible to the linker.
1053 if (!SD->getSymbol().isTemporary())
1054 return true;
1055
1056 // Absolute temporary labels are never visible.
1057 if (!SD->getFragment())
1058 return false;
1059
1060 // Otherwise, check if the section requires symbols even for temporary labels.
1061 return getBackend().doesSectionRequireSymbols(
1062 SD->getFragment()->getParent()->getSection());
1063}
1064
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +00001065const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
1066 uint64_t Address) const {
1067 const MCSymbolData *Best = 0;
1068 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
1069 ie = symbol_end(); it != ie; ++it) {
1070 // Ignore non-linker visible symbols.
1071 if (!isSymbolLinkerVisible(it))
1072 continue;
1073
1074 // Ignore symbols not in the same section.
1075 if (!it->getFragment() || it->getFragment()->getParent() != Section)
1076 continue;
1077
1078 // Otherwise, find the closest symbol preceding this address (ties are
1079 // resolved in favor of the last defined symbol).
1080 if (it->getAddress() <= Address &&
1081 (!Best || it->getAddress() >= Best->getAddress()))
1082 Best = it;
1083 }
1084
1085 return Best;
1086}
1087
1088const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
1089 // Linker visible symbols define atoms.
1090 if (isSymbolLinkerVisible(SD))
1091 return SD;
1092
1093 // Absolute and undefined symbols have no defining atom.
1094 if (!SD->getFragment())
1095 return 0;
1096
1097 // Otherwise, search by address.
1098 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
1099}
1100
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001101bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup,
1102 MCDataFragment *DF,
1103 MCValue &Target, uint64_t &Value) const {
1104 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
1105 llvm_report_error("expected relocatable expression");
1106
1107 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
1108 // doesn't support small relocations, but then under what criteria does the
1109 // assembler allow symbol differences?
1110
1111 Value = Target.getConstant();
1112
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001113 bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001114 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1115 if (A->getSymbol().isDefined())
1116 Value += getSymbolData(A->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001117 else
1118 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001119 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001120 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1121 if (B->getSymbol().isDefined())
1122 Value -= getSymbolData(B->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001123 else
1124 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001125 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001126
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001127 // If we are using scattered symbols, determine whether this value is actually
1128 // resolved; scattering may cause atoms to move.
1129 if (IsResolved && getBackend().hasScatteredSymbols()) {
1130 if (getBackend().hasReliableSymbolDifference()) {
1131 llvm_report_error("FIXME: Not yet implemented");
1132 } else {
1133 const MCSection *BaseSection = 0;
1134 if (IsPCRel)
1135 BaseSection = &DF->getParent()->getSection();
1136
1137 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, DF, Target,
1138 BaseSection);
1139 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001140 }
1141
1142 if (IsPCRel)
Daniel Dunbarda3e9f72010-03-13 02:38:00 +00001143 Value -= DF->getAddress() + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001144
1145 return IsResolved;
1146}
1147
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001148void MCAssembler::LayoutSection(MCSectionData &SD) {
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001149 MCAsmLayout Layout(*this);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001150 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001151
1152 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
1153 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001154
Daniel Dunbar6742e342009-08-26 04:13:32 +00001155 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001156
1157 // Evaluate fragment size.
1158 switch (F.getKind()) {
1159 case MCFragment::FT_Align: {
1160 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001161
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001162 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +00001163 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001164 AF.setFileSize(0);
1165 else
Daniel Dunbar6742e342009-08-26 04:13:32 +00001166 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001167 break;
1168 }
1169
1170 case MCFragment::FT_Data:
Daniel Dunbara4766d72010-02-13 09:28:32 +00001171 case MCFragment::FT_Fill:
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001172 F.setFileSize(F.getMaxFileSize());
1173 break;
1174
1175 case MCFragment::FT_Org: {
1176 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1177
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001178 int64_t TargetLocation;
1179 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
1180 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001181
1182 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001183 int64_t Offset = TargetLocation - F.getOffset();
1184 if (Offset < 0)
1185 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
1186 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001187
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001188 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001189 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001190 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001191
1192 case MCFragment::FT_ZeroFill: {
1193 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
1194
1195 // Align the fragment offset; it is safe to adjust the offset freely since
1196 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +00001197 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
1198 F.setOffset(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001199
1200 // FIXME: This is misnamed.
1201 F.setFileSize(ZFF.getSize());
1202 break;
1203 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001204 }
1205
Daniel Dunbar6742e342009-08-26 04:13:32 +00001206 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001207 }
1208
Daniel Dunbar6742e342009-08-26 04:13:32 +00001209 // Set the section sizes.
1210 SD.setSize(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001211 if (isVirtualSection(SD.getSection()))
1212 SD.setFileSize(0);
1213 else
1214 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001215}
1216
Kevin Enderby6e720482010-02-23 18:26:34 +00001217/// WriteNopData - Write optimal nops to the output file for the \arg Count
1218/// bytes. This returns the number of bytes written. It may return 0 if
1219/// the \arg Count is more than the maximum optimal nops.
1220///
1221/// FIXME this is X86 32-bit specific and should move to a better place.
1222static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW) {
Kevin Enderby76687082010-02-23 21:41:24 +00001223 static const uint8_t Nops[16][16] = {
1224 // nop
1225 {0x90},
1226 // xchg %ax,%ax
1227 {0x66, 0x90},
1228 // nopl (%[re]ax)
1229 {0x0f, 0x1f, 0x00},
1230 // nopl 0(%[re]ax)
1231 {0x0f, 0x1f, 0x40, 0x00},
1232 // nopl 0(%[re]ax,%[re]ax,1)
1233 {0x0f, 0x1f, 0x44, 0x00, 0x00},
1234 // nopw 0(%[re]ax,%[re]ax,1)
1235 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1236 // nopl 0L(%[re]ax)
1237 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1238 // nopl 0L(%[re]ax,%[re]ax,1)
1239 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1240 // nopw 0L(%[re]ax,%[re]ax,1)
1241 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1242 // nopw %cs:0L(%[re]ax,%[re]ax,1)
1243 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1244 // nopl 0(%[re]ax,%[re]ax,1)
1245 // nopw 0(%[re]ax,%[re]ax,1)
1246 {0x0f, 0x1f, 0x44, 0x00, 0x00,
1247 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1248 // nopw 0(%[re]ax,%[re]ax,1)
1249 // nopw 0(%[re]ax,%[re]ax,1)
1250 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1251 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1252 // nopw 0(%[re]ax,%[re]ax,1)
1253 // nopl 0L(%[re]ax) */
1254 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1255 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1256 // nopl 0L(%[re]ax)
1257 // nopl 0L(%[re]ax)
1258 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1259 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1260 // nopl 0L(%[re]ax)
1261 // nopl 0L(%[re]ax,%[re]ax,1)
1262 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1263 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
1264 };
1265
1266 if (Count > 15)
1267 return 0;
1268
Kevin Enderby6e720482010-02-23 18:26:34 +00001269 for (uint64_t i = 0; i < Count; i++)
Kevin Enderby76687082010-02-23 21:41:24 +00001270 MOW.Write8 (uint8_t(Nops[Count - 1][i]));
Kevin Enderby6e720482010-02-23 18:26:34 +00001271
1272 return Count;
1273}
1274
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001275/// WriteFileData - Write the \arg F data to the output file.
1276static void WriteFileData(raw_ostream &OS, const MCFragment &F,
1277 MachObjectWriter &MOW) {
1278 uint64_t Start = OS.tell();
1279 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001280
Daniel Dunbar0adcd352009-08-25 21:10:45 +00001281 ++EmittedFragments;
1282
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001283 // FIXME: Embed in fragments instead?
1284 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001285 case MCFragment::FT_Align: {
1286 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1287 uint64_t Count = AF.getFileSize() / AF.getValueSize();
1288
1289 // FIXME: This error shouldn't actually occur (the front end should emit
1290 // multiple .align directives to enforce the semantics it wants), but is
1291 // severe enough that we want to report it. How to handle this?
1292 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001293 llvm_report_error("undefined .align directive, value size '" +
1294 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001295 "' is not a divisor of padding size '" +
1296 Twine(AF.getFileSize()) + "'");
1297
Kevin Enderby6e720482010-02-23 18:26:34 +00001298 // See if we are aligning with nops, and if so do that first to try to fill
1299 // the Count bytes. Then if that did not fill any bytes or there are any
1300 // bytes left to fill use the the Value and ValueSize to fill the rest.
1301 if (AF.getEmitNops()) {
1302 uint64_t NopByteCount = WriteNopData(Count, MOW);
1303 Count -= NopByteCount;
1304 }
1305
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001306 for (uint64_t i = 0; i != Count; ++i) {
1307 switch (AF.getValueSize()) {
1308 default:
1309 assert(0 && "Invalid size!");
1310 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
1311 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
1312 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
1313 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
1314 }
1315 }
1316 break;
1317 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001318
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001319 case MCFragment::FT_Data: {
1320 MCDataFragment &DF = cast<MCDataFragment>(F);
1321
1322 // Apply the fixups.
1323 //
1324 // FIXME: Move elsewhere.
1325 for (MCDataFragment::const_fixup_iterator it = DF.fixup_begin(),
1326 ie = DF.fixup_end(); it != ie; ++it)
1327 MOW.ApplyFixup(*it, DF);
1328
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001329 OS << cast<MCDataFragment>(F).getContents().str();
1330 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001331 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001332
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001333 case MCFragment::FT_Fill: {
1334 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001335 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1336 switch (FF.getValueSize()) {
1337 default:
1338 assert(0 && "Invalid size!");
Daniel Dunbara4766d72010-02-13 09:28:32 +00001339 case 1: MOW.Write8 (uint8_t (FF.getValue())); break;
1340 case 2: MOW.Write16(uint16_t(FF.getValue())); break;
1341 case 4: MOW.Write32(uint32_t(FF.getValue())); break;
1342 case 8: MOW.Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001343 }
1344 }
1345 break;
1346 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001347
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001348 case MCFragment::FT_Org: {
1349 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1350
1351 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1352 MOW.Write8(uint8_t(OF.getValue()));
1353
1354 break;
1355 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001356
1357 case MCFragment::FT_ZeroFill: {
1358 assert(0 && "Invalid zero fill fragment in concrete section!");
1359 break;
1360 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001361 }
1362
1363 assert(OS.tell() - Start == F.getFileSize());
1364}
1365
1366/// WriteFileData - Write the \arg SD data to the output file.
1367static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1368 MachObjectWriter &MOW) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001369 // Ignore virtual sections.
1370 if (isVirtualSection(SD.getSection())) {
1371 assert(SD.getFileSize() == 0);
1372 return;
1373 }
1374
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001375 uint64_t Start = OS.tell();
1376 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001377
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001378 for (MCSectionData::const_iterator it = SD.begin(),
1379 ie = SD.end(); it != ie; ++it)
1380 WriteFileData(OS, *it, MOW);
1381
Daniel Dunbar6742e342009-08-26 04:13:32 +00001382 // Add section padding.
1383 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1384 MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1385
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001386 assert(OS.tell() - Start == SD.getFileSize());
1387}
1388
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001389void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001390 DEBUG_WITH_TYPE("mc-dump", {
1391 llvm::errs() << "assembler backend - pre-layout\n--\n";
1392 dump(); });
1393
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001394 // Layout until everything fits.
1395 while (LayoutOnce())
1396 continue;
1397
1398 DEBUG_WITH_TYPE("mc-dump", {
1399 llvm::errs() << "assembler backend - post-layout\n--\n";
1400 dump(); });
1401
1402 // Write the object file.
Daniel Dunbaree0d8922010-03-13 22:10:17 +00001403 //
1404 // FIXME: Factor out MCObjectWriter.
1405 bool Is64Bit = StringRef(getBackend().getTarget().getName()) == "x86-64";
1406 MachObjectWriter MOW(OS, Is64Bit);
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001407 MOW.WriteObject(*this);
1408
1409 OS.flush();
1410}
1411
1412bool MCAssembler::FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF) {
1413 // FIXME: Share layout object.
1414 MCAsmLayout Layout(*this);
1415
1416 // Currently we only need to relax X86::reloc_pcrel_1byte.
1417 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
1418 return false;
1419
1420 // If we cannot resolve the fixup value, it requires relaxation.
1421 MCValue Target;
1422 uint64_t Value;
1423 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
1424 return true;
1425
1426 // Otherwise, relax if the value is too big for a (signed) i8.
1427 return int64_t(Value) != int64_t(int8_t(Value));
1428}
1429
1430bool MCAssembler::LayoutOnce() {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001431 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +00001432 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001433 MCSectionData *Prev = 0;
1434 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +00001435 MCSectionData &SD = *it;
1436
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001437 // Skip virtual sections.
1438 if (isVirtualSection(SD.getSection()))
1439 continue;
1440
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001441 // Align this section if necessary by adding padding bytes to the previous
1442 // section.
1443 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1444 assert(Prev && "Missing prev section!");
1445 Prev->setFileSize(Prev->getFileSize() + Pad);
1446 Address += Pad;
1447 }
Daniel Dunbar6742e342009-08-26 04:13:32 +00001448
1449 // Layout the section fragments and its size.
1450 SD.setAddress(Address);
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001451 LayoutSection(SD);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001452 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001453
1454 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +00001455 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001456
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001457 // Layout the virtual sections.
1458 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1459 MCSectionData &SD = *it;
1460
1461 if (!isVirtualSection(SD.getSection()))
1462 continue;
1463
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001464 // Align this section if necessary by adding padding bytes to the previous
1465 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +00001466 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001467 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001468
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001469 SD.setAddress(Address);
1470 LayoutSection(SD);
1471 Address += SD.getSize();
1472 }
1473
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001474 // Scan the fixups in order and relax any that don't fit.
1475 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1476 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001477
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001478 for (MCSectionData::iterator it2 = SD.begin(),
1479 ie2 = SD.end(); it2 != ie2; ++it2) {
1480 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
1481 if (!DF)
1482 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001483
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001484 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
1485 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
1486 MCAsmFixup &Fixup = *it3;
1487
1488 // Check whether we need to relax this fixup.
1489 if (!FixupNeedsRelaxation(Fixup, DF))
1490 continue;
1491
1492 // Relax the instruction.
1493 //
1494 // FIXME: This is a huge temporary hack which just looks for x86
1495 // branches; the only thing we need to relax on x86 is
1496 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
1497 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
1498 // an individual MCInst.
1499 SmallVectorImpl<char> &C = DF->getContents();
1500 uint64_t PrevOffset = Fixup.Offset;
1501 unsigned Amt = 0;
1502
1503 // jcc instructions
1504 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
1505 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
1506 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
1507 C[Fixup.Offset-1] = char(0x0f);
1508 ++Fixup.Offset;
1509 Amt = 4;
1510
1511 // jmp rel8
1512 } else if (C[Fixup.Offset-1] == char(0xeb)) {
1513 C[Fixup.Offset-1] = char(0xe9);
1514 Amt = 3;
1515
1516 } else
1517 llvm_unreachable("unknown 1 byte pcrel instruction!");
1518
1519 Fixup.Value = MCBinaryExpr::Create(
1520 MCBinaryExpr::Sub, Fixup.Value,
1521 MCConstantExpr::Create(3, getContext()),
1522 getContext());
1523 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
1524 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
1525
1526 // Update the remaining fixups, which have slid.
1527 //
1528 // FIXME: This is bad for performance, but will be eliminated by the
1529 // move to MCInst specific fragments.
1530 ++it3;
1531 for (; it3 != ie3; ++it3)
1532 it3->Offset += Amt;
1533
1534 // Update all the symbols for this fragment, which may have slid.
1535 //
1536 // FIXME: This is really really bad for performance, but will be
1537 // eliminated by the move to MCInst specific fragments.
1538 for (MCAssembler::symbol_iterator it = symbol_begin(),
1539 ie = symbol_end(); it != ie; ++it) {
1540 MCSymbolData &SD = *it;
1541
1542 if (it->getFragment() != DF)
1543 continue;
1544
1545 if (SD.getOffset() > PrevOffset)
1546 SD.setOffset(SD.getOffset() + Amt);
1547 }
1548
1549 // Restart layout.
1550 //
1551 // FIXME: This is O(N^2), but will be eliminated once we have a smart
1552 // MCAsmLayout object.
1553 return true;
1554 }
1555 }
1556 }
1557
1558 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001559}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001560
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001561// Debugging methods
1562
1563namespace llvm {
1564
1565raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001566 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
1567 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001568 return OS;
1569}
1570
1571}
1572
1573void MCFragment::dump() {
1574 raw_ostream &OS = llvm::errs();
1575
1576 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1577 << " FileSize:" << FileSize;
1578
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001579 OS << ">";
1580}
1581
1582void MCAlignFragment::dump() {
1583 raw_ostream &OS = llvm::errs();
1584
1585 OS << "<MCAlignFragment ";
1586 this->MCFragment::dump();
1587 OS << "\n ";
1588 OS << " Alignment:" << getAlignment()
1589 << " Value:" << getValue() << " ValueSize:" << getValueSize()
1590 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1591}
1592
1593void MCDataFragment::dump() {
1594 raw_ostream &OS = llvm::errs();
1595
1596 OS << "<MCDataFragment ";
1597 this->MCFragment::dump();
1598 OS << "\n ";
1599 OS << " Contents:[";
1600 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1601 if (i) OS << ",";
1602 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1603 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001604 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001605
1606 if (!getFixups().empty()) {
1607 OS << ",\n ";
1608 OS << " Fixups:[";
1609 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001610 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001611 OS << *it;
1612 }
1613 OS << "]";
1614 }
1615
1616 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001617}
1618
1619void MCFillFragment::dump() {
1620 raw_ostream &OS = llvm::errs();
1621
1622 OS << "<MCFillFragment ";
1623 this->MCFragment::dump();
1624 OS << "\n ";
1625 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1626 << " Count:" << getCount() << ">";
1627}
1628
1629void MCOrgFragment::dump() {
1630 raw_ostream &OS = llvm::errs();
1631
1632 OS << "<MCOrgFragment ";
1633 this->MCFragment::dump();
1634 OS << "\n ";
1635 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1636}
1637
1638void MCZeroFillFragment::dump() {
1639 raw_ostream &OS = llvm::errs();
1640
1641 OS << "<MCZeroFillFragment ";
1642 this->MCFragment::dump();
1643 OS << "\n ";
1644 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1645}
1646
1647void MCSectionData::dump() {
1648 raw_ostream &OS = llvm::errs();
1649
1650 OS << "<MCSectionData";
1651 OS << " Alignment:" << getAlignment() << " Address:" << Address
1652 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001653 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001654 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1655 if (it != begin()) OS << ",\n ";
1656 it->dump();
1657 }
1658 OS << "]>";
1659}
1660
1661void MCSymbolData::dump() {
1662 raw_ostream &OS = llvm::errs();
1663
1664 OS << "<MCSymbolData Symbol:" << getSymbol()
1665 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1666 << " Flags:" << getFlags() << " Index:" << getIndex();
1667 if (isCommon())
1668 OS << " (common, size:" << getCommonSize()
1669 << " align: " << getCommonAlignment() << ")";
1670 if (isExternal())
1671 OS << " (external)";
1672 if (isPrivateExtern())
1673 OS << " (private extern)";
1674 OS << ">";
1675}
1676
1677void MCAssembler::dump() {
1678 raw_ostream &OS = llvm::errs();
1679
1680 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001681 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001682 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1683 if (it != begin()) OS << ",\n ";
1684 it->dump();
1685 }
1686 OS << "],\n";
1687 OS << " Symbols:[";
1688
1689 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001690 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001691 it->dump();
1692 }
1693 OS << "]>\n";
1694}