blob: a5df4ba42be3369434e4389f7e0fe774b34f09d1 [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 Dunbar1253a6f2009-10-16 01:58:03 +000012#include "llvm/MC/MCExpr.h"
Chris Lattnerbea2c952009-08-22 19:19:12 +000013#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000014#include "llvm/MC/MCSymbol.h"
15#include "llvm/MC/MCValue.h"
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +000016#include "llvm/ADT/DenseMap.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000018#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000019#include "llvm/ADT/StringExtras.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000020#include "llvm/ADT/StringMap.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000021#include "llvm/ADT/Twine.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner45f8c092010-02-02 19:38:14 +000023#include "llvm/Support/MachO.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000025#include "llvm/Support/Debug.h"
Chris Lattner23132b12009-08-24 03:52:50 +000026#include <vector>
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000027using namespace llvm;
28
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000029class MachObjectWriter;
30
Daniel Dunbar0adcd352009-08-25 21:10:45 +000031STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
32
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000033// FIXME FIXME FIXME: There are number of places in this file where we convert
34// what is a 64-bit assembler value used for computation into a value in the
35// object file, which may truncate it. We should detect that truncation where
36// invalid and report errors back.
37
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000038static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
39 MachObjectWriter &MOW);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000040
Daniel Dunbard5a8e982009-08-28 05:49:21 +000041/// isVirtualSection - Check if this is a section which does not actually exist
42/// in the object file.
43static bool isVirtualSection(const MCSection &Section) {
44 // FIXME: Lame.
45 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
46 unsigned Type = SMO.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
47 return (Type == MCSectionMachO::S_ZEROFILL);
48}
49
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050class MachObjectWriter {
51 // See <mach-o/loader.h>.
52 enum {
53 Header_Magic32 = 0xFEEDFACE,
54 Header_Magic64 = 0xFEEDFACF
55 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +000056
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000057 static const unsigned Header32Size = 28;
58 static const unsigned Header64Size = 32;
59 static const unsigned SegmentLoadCommand32Size = 56;
60 static const unsigned Section32Size = 68;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000061 static const unsigned SymtabLoadCommandSize = 24;
62 static const unsigned DysymtabLoadCommandSize = 80;
63 static const unsigned Nlist32Size = 12;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +000064 static const unsigned RelocationInfoSize = 8;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000065
66 enum HeaderFileType {
67 HFT_Object = 0x1
68 };
69
Daniel Dunbar6009db42009-08-26 21:22:22 +000070 enum HeaderFlags {
71 HF_SubsectionsViaSymbols = 0x2000
72 };
73
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000074 enum LoadCommandType {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000075 LCT_Segment = 0x1,
76 LCT_Symtab = 0x2,
77 LCT_Dysymtab = 0xb
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000078 };
79
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +000080 // See <mach-o/nlist.h>.
81 enum SymbolTypeType {
82 STT_Undefined = 0x00,
83 STT_Absolute = 0x02,
84 STT_Section = 0x0e
85 };
86
87 enum SymbolTypeFlags {
88 // If any of these bits are set, then the entry is a stab entry number (see
89 // <mach-o/stab.h>. Otherwise the other masks apply.
90 STF_StabsEntryMask = 0xe0,
91
92 STF_TypeMask = 0x0e,
93 STF_External = 0x01,
94 STF_PrivateExtern = 0x10
95 };
96
Daniel Dunbarad7c3d52009-08-26 00:18:21 +000097 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
98 /// symbol entry.
99 enum IndirectSymbolFlags {
100 ISF_Local = 0x80000000,
101 ISF_Absolute = 0x40000000
102 };
103
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000104 /// RelocationFlags - Special flags for addresses.
105 enum RelocationFlags {
106 RF_Scattered = 0x80000000
107 };
108
109 enum RelocationInfoType {
110 RIT_Vanilla = 0,
111 RIT_Pair = 1,
112 RIT_Difference = 2,
113 RIT_PreboundLazyPointer = 3,
114 RIT_LocalDifference = 4
115 };
116
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000117 /// MachSymbolData - Helper struct for containing some precomputed information
118 /// on symbols.
119 struct MachSymbolData {
120 MCSymbolData *SymbolData;
121 uint64_t StringIndex;
122 uint8_t SectionIndex;
123
124 // Support lexicographic sorting.
125 bool operator<(const MachSymbolData &RHS) const {
126 const std::string &Name = SymbolData->getSymbol().getName();
127 return Name < RHS.SymbolData->getSymbol().getName();
128 }
129 };
130
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000131 raw_ostream &OS;
132 bool IsLSB;
133
134public:
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000135 MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136 : OS(_OS), IsLSB(_IsLSB) {
137 }
138
139 /// @name Helper Methods
140 /// @{
141
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000142 void Write8(uint8_t Value) {
143 OS << char(Value);
144 }
145
146 void Write16(uint16_t Value) {
147 if (IsLSB) {
148 Write8(uint8_t(Value >> 0));
149 Write8(uint8_t(Value >> 8));
150 } else {
151 Write8(uint8_t(Value >> 8));
152 Write8(uint8_t(Value >> 0));
153 }
154 }
155
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000156 void Write32(uint32_t Value) {
157 if (IsLSB) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000158 Write16(uint16_t(Value >> 0));
159 Write16(uint16_t(Value >> 16));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000160 } else {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000161 Write16(uint16_t(Value >> 16));
162 Write16(uint16_t(Value >> 0));
163 }
164 }
165
166 void Write64(uint64_t Value) {
167 if (IsLSB) {
168 Write32(uint32_t(Value >> 0));
169 Write32(uint32_t(Value >> 32));
170 } else {
171 Write32(uint32_t(Value >> 32));
172 Write32(uint32_t(Value >> 0));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000173 }
174 }
175
176 void WriteZeros(unsigned N) {
177 const char Zeros[16] = { 0 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000178
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000179 for (unsigned i = 0, e = N / 16; i != e; ++i)
180 OS << StringRef(Zeros, 16);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000181
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000182 OS << StringRef(Zeros, N % 16);
183 }
184
Daniel Dunbar2928c832009-11-06 10:58:06 +0000185 void WriteString(StringRef Str, unsigned ZeroFillSize = 0) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000186 OS << Str;
187 if (ZeroFillSize)
188 WriteZeros(ZeroFillSize - Str.size());
189 }
190
191 /// @}
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000192
Daniel Dunbar6009db42009-08-26 21:22:22 +0000193 void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize,
194 bool SubsectionsViaSymbols) {
195 uint32_t Flags = 0;
196
197 if (SubsectionsViaSymbols)
198 Flags |= HF_SubsectionsViaSymbols;
199
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000200 // struct mach_header (28 bytes)
201
202 uint64_t Start = OS.tell();
203 (void) Start;
204
205 Write32(Header_Magic32);
206
207 // FIXME: Support cputype.
Chris Lattner45f8c092010-02-02 19:38:14 +0000208 Write32(MachO::CPUTypeI386);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000209 // FIXME: Support cpusubtype.
Chris Lattner45f8c092010-02-02 19:38:14 +0000210 Write32(MachO::CPUSubType_I386_ALL);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000211 Write32(HFT_Object);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000212 Write32(NumLoadCommands); // Object files have a single load command, the
213 // segment.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000214 Write32(LoadCommandsSize);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000215 Write32(Flags);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000216
217 assert(OS.tell() - Start == Header32Size);
218 }
219
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000220 /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
221 ///
222 /// \arg NumSections - The number of sections in this segment.
223 /// \arg SectionDataSize - The total size of the sections.
224 void WriteSegmentLoadCommand32(unsigned NumSections,
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000225 uint64_t VMSize,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000226 uint64_t SectionDataStartOffset,
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000227 uint64_t SectionDataSize) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000228 // struct segment_command (56 bytes)
229
230 uint64_t Start = OS.tell();
231 (void) Start;
232
233 Write32(LCT_Segment);
234 Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
235
236 WriteString("", 16);
237 Write32(0); // vmaddr
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000238 Write32(VMSize); // vmsize
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000239 Write32(SectionDataStartOffset); // file offset
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000240 Write32(SectionDataSize); // file size
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000241 Write32(0x7); // maxprot
242 Write32(0x7); // initprot
243 Write32(NumSections);
244 Write32(0); // flags
245
246 assert(OS.tell() - Start == SegmentLoadCommand32Size);
247 }
248
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000249 void WriteSection32(const MCSectionData &SD, uint64_t FileOffset,
250 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000251 // The offset is unused for virtual sections.
252 if (isVirtualSection(SD.getSection())) {
253 assert(SD.getFileSize() == 0 && "Invalid file size!");
254 FileOffset = 0;
255 }
256
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000257 // struct section (68 bytes)
258
259 uint64_t Start = OS.tell();
260 (void) Start;
261
262 // FIXME: cast<> support!
263 const MCSectionMachO &Section =
264 static_cast<const MCSectionMachO&>(SD.getSection());
265 WriteString(Section.getSectionName(), 16);
266 WriteString(Section.getSegmentName(), 16);
Daniel Dunbar5e835962009-08-26 02:48:04 +0000267 Write32(SD.getAddress()); // address
Daniel Dunbar6742e342009-08-26 04:13:32 +0000268 Write32(SD.getSize()); // size
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000269 Write32(FileOffset);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000270
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000271 unsigned Flags = Section.getTypeAndAttributes();
272 if (SD.hasInstructions())
273 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
274
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000275 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
276 Write32(Log2_32(SD.getAlignment()));
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000277 Write32(NumRelocations ? RelocationsStart : 0);
278 Write32(NumRelocations);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000279 Write32(Flags);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000280 Write32(0); // reserved1
281 Write32(Section.getStubSize()); // reserved2
282
283 assert(OS.tell() - Start == Section32Size);
284 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000285
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000286 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
287 uint32_t StringTableOffset,
288 uint32_t StringTableSize) {
289 // struct symtab_command (24 bytes)
290
291 uint64_t Start = OS.tell();
292 (void) Start;
293
294 Write32(LCT_Symtab);
295 Write32(SymtabLoadCommandSize);
296 Write32(SymbolOffset);
297 Write32(NumSymbols);
298 Write32(StringTableOffset);
299 Write32(StringTableSize);
300
301 assert(OS.tell() - Start == SymtabLoadCommandSize);
302 }
303
304 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
305 uint32_t NumLocalSymbols,
306 uint32_t FirstExternalSymbol,
307 uint32_t NumExternalSymbols,
308 uint32_t FirstUndefinedSymbol,
309 uint32_t NumUndefinedSymbols,
310 uint32_t IndirectSymbolOffset,
311 uint32_t NumIndirectSymbols) {
312 // struct dysymtab_command (80 bytes)
313
314 uint64_t Start = OS.tell();
315 (void) Start;
316
317 Write32(LCT_Dysymtab);
318 Write32(DysymtabLoadCommandSize);
319 Write32(FirstLocalSymbol);
320 Write32(NumLocalSymbols);
321 Write32(FirstExternalSymbol);
322 Write32(NumExternalSymbols);
323 Write32(FirstUndefinedSymbol);
324 Write32(NumUndefinedSymbols);
325 Write32(0); // tocoff
326 Write32(0); // ntoc
327 Write32(0); // modtaboff
328 Write32(0); // nmodtab
329 Write32(0); // extrefsymoff
330 Write32(0); // nextrefsyms
331 Write32(IndirectSymbolOffset);
332 Write32(NumIndirectSymbols);
333 Write32(0); // extreloff
334 Write32(0); // nextrel
335 Write32(0); // locreloff
336 Write32(0); // nlocrel
337
338 assert(OS.tell() - Start == DysymtabLoadCommandSize);
339 }
340
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000341 void WriteNlist32(MachSymbolData &MSD) {
Daniel Dunbar5e835962009-08-26 02:48:04 +0000342 MCSymbolData &Data = *MSD.SymbolData;
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000343 const MCSymbol &Symbol = Data.getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000344 uint8_t Type = 0;
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000345 uint16_t Flags = Data.getFlags();
346 uint32_t Address = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000347
348 // Set the N_TYPE bits. See <mach-o/nlist.h>.
349 //
350 // FIXME: Are the prebound or indirect fields possible here?
351 if (Symbol.isUndefined())
352 Type = STT_Undefined;
353 else if (Symbol.isAbsolute())
354 Type = STT_Absolute;
355 else
356 Type = STT_Section;
357
358 // FIXME: Set STAB bits.
359
Daniel Dunbar5e835962009-08-26 02:48:04 +0000360 if (Data.isPrivateExtern())
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000361 Type |= STF_PrivateExtern;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000362
363 // Set external bit.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000364 if (Data.isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000365 Type |= STF_External;
366
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000367 // Compute the symbol address.
368 if (Symbol.isDefined()) {
369 if (Symbol.isAbsolute()) {
370 llvm_unreachable("FIXME: Not yet implemented!");
371 } else {
372 Address = Data.getFragment()->getAddress() + Data.getOffset();
373 }
374 } else if (Data.isCommon()) {
375 // Common symbols are encoded with the size in the address
376 // field, and their alignment in the flags.
377 Address = Data.getCommonSize();
378
379 // Common alignment is packed into the 'desc' bits.
380 if (unsigned Align = Data.getCommonAlignment()) {
381 unsigned Log2Size = Log2_32(Align);
382 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
383 if (Log2Size > 15)
384 llvm_report_error("invalid 'common' alignment '" +
385 Twine(Align) + "'");
386 // FIXME: Keep this mask with the SymbolFlags enumeration.
387 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
388 }
389 }
390
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000391 // struct nlist (12 bytes)
392
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000393 Write32(MSD.StringIndex);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000394 Write8(Type);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000395 Write8(MSD.SectionIndex);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000396
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000397 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
398 // value.
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000399 Write16(Flags);
Daniel Dunbar5e835962009-08-26 02:48:04 +0000400 Write32(Address);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000401 }
402
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000403 struct MachRelocationEntry {
404 uint32_t Word0;
405 uint32_t Word1;
406 };
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000407 void ComputeScatteredRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
Daniel Dunbar27ade182010-02-11 21:29:29 +0000408 MCAsmFixup &Fixup,
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000409 const MCValue &Target,
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000410 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
411 std::vector<MachRelocationEntry> &Relocs) {
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000412 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000413 unsigned IsPCRel = 0;
414 unsigned Type = RIT_Vanilla;
415
416 // See <reloc.h>.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000417 const MCSymbol *A = Target.getSymA();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000418 MCSymbolData *SD = SymbolMap.lookup(A);
419 uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset();
420 uint32_t Value2 = 0;
421
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000422 if (const MCSymbol *B = Target.getSymB()) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000423 Type = RIT_LocalDifference;
424
425 MCSymbolData *SD = SymbolMap.lookup(B);
426 Value2 = SD->getFragment()->getAddress() + SD->getOffset();
427 }
428
429 unsigned Log2Size = Log2_32(Fixup.Size);
430 assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
431
432 // The value which goes in the fixup is current value of the expression.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000433 Fixup.FixedValue = Value - Value2 + Target.getConstant();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000434
435 MachRelocationEntry MRE;
436 MRE.Word0 = ((Address << 0) |
437 (Type << 24) |
438 (Log2Size << 28) |
439 (IsPCRel << 30) |
440 RF_Scattered);
441 MRE.Word1 = Value;
442 Relocs.push_back(MRE);
443
444 if (Type == RIT_LocalDifference) {
445 Type = RIT_Pair;
446
447 MachRelocationEntry MRE;
448 MRE.Word0 = ((0 << 0) |
449 (Type << 24) |
450 (Log2Size << 28) |
451 (0 << 30) |
452 RF_Scattered);
453 MRE.Word1 = Value2;
454 Relocs.push_back(MRE);
455 }
456 }
457
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000458 void ComputeRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
Daniel Dunbar27ade182010-02-11 21:29:29 +0000459 MCAsmFixup &Fixup,
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000460 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
461 std::vector<MachRelocationEntry> &Relocs) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000462 MCValue Target;
463 if (!Fixup.Value->EvaluateAsRelocatable(Target))
464 llvm_report_error("expected relocatable expression");
465
466 // If this is a difference or a local symbol plus an offset, then we need a
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000467 // scattered relocation entry.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000468 if (Target.getSymB() ||
469 (Target.getSymA() && !Target.getSymA()->isUndefined() &&
470 Target.getConstant()))
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000471 return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target,
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000472 SymbolMap, Relocs);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000473
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000474 // See <reloc.h>.
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000475 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000476 uint32_t Value = 0;
477 unsigned Index = 0;
478 unsigned IsPCRel = 0;
479 unsigned IsExtern = 0;
480 unsigned Type = 0;
481
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000482 if (Target.isAbsolute()) { // constant
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000483 // SymbolNum of 0 indicates the absolute section.
484 Type = RIT_Vanilla;
485 Value = 0;
486 llvm_unreachable("FIXME: Not yet implemented!");
487 } else {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000488 const MCSymbol *Symbol = Target.getSymA();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000489 MCSymbolData *SD = SymbolMap.lookup(Symbol);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000490
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000491 if (Symbol->isUndefined()) {
492 IsExtern = 1;
493 Index = SD->getIndex();
494 Value = 0;
495 } else {
496 // The index is the section ordinal.
497 //
498 // FIXME: O(N)
499 Index = 1;
500 for (MCAssembler::iterator it = Asm.begin(),
501 ie = Asm.end(); it != ie; ++it, ++Index)
502 if (&*it == SD->getFragment()->getParent())
503 break;
504 Value = SD->getFragment()->getAddress() + SD->getOffset();
505 }
506
507 Type = RIT_Vanilla;
508 }
509
510 // The value which goes in the fixup is current value of the expression.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000511 Fixup.FixedValue = Value + Target.getConstant();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000512
513 unsigned Log2Size = Log2_32(Fixup.Size);
514 assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
515
516 // struct relocation_info (8 bytes)
517 MachRelocationEntry MRE;
518 MRE.Word0 = Address;
519 MRE.Word1 = ((Index << 0) |
520 (IsPCRel << 24) |
521 (Log2Size << 25) |
522 (IsExtern << 27) |
523 (Type << 28));
524 Relocs.push_back(MRE);
525 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000526
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000527 void BindIndirectSymbols(MCAssembler &Asm,
Daniel Dunbarbe963552009-08-26 13:57:54 +0000528 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000529 // This is the point where 'as' creates actual symbols for indirect symbols
530 // (in the following two passes). It would be easier for us to do this
531 // sooner when we see the attribute, but that makes getting the order in the
532 // symbol table much more complicated than it is worth.
533 //
534 // FIXME: Revisit this when the dust settles.
535
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000536 // Bind non lazy symbol pointers first.
537 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
538 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
539 // FIXME: cast<> support!
540 const MCSectionMachO &Section =
541 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
542
543 unsigned Type =
544 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
545 if (Type != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
546 continue;
547
548 MCSymbolData *&Entry = SymbolMap[it->Symbol];
549 if (!Entry)
550 Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
551 }
552
553 // Then lazy symbol pointers and symbol stubs.
554 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
555 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
556 // FIXME: cast<> support!
557 const MCSectionMachO &Section =
558 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
559
560 unsigned Type =
561 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
562 if (Type != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
563 Type != MCSectionMachO::S_SYMBOL_STUBS)
564 continue;
565
566 MCSymbolData *&Entry = SymbolMap[it->Symbol];
567 if (!Entry) {
568 Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
569
570 // Set the symbol type to undefined lazy, but only on construction.
571 //
572 // FIXME: Do not hardcode.
573 Entry->setFlags(Entry->getFlags() | 0x0001);
574 }
575 }
576 }
577
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000578 /// ComputeSymbolTable - Compute the symbol table data
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000579 ///
580 /// \param StringTable [out] - The string table data.
581 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
582 /// string table.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000583 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
584 std::vector<MachSymbolData> &LocalSymbolData,
585 std::vector<MachSymbolData> &ExternalSymbolData,
586 std::vector<MachSymbolData> &UndefinedSymbolData) {
587 // Build section lookup table.
588 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
589 unsigned Index = 1;
590 for (MCAssembler::iterator it = Asm.begin(),
591 ie = Asm.end(); it != ie; ++it, ++Index)
592 SectionIndexMap[&it->getSection()] = Index;
593 assert(Index <= 256 && "Too many sections!");
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000594
595 // Index 0 is always the empty string.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000596 StringMap<uint64_t> StringIndexMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000597 StringTable += '\x00';
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000598
599 // Build the symbol arrays and the string table, but only for non-local
600 // symbols.
601 //
602 // The particular order that we collect the symbols and create the string
603 // table, then sort the symbols is chosen to match 'as'. Even though it
604 // doesn't matter for correctness, this is important for letting us diff .o
605 // files.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000606 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
607 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000608 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000609
Daniel Dunbar959fd882009-08-26 22:13:22 +0000610 // Ignore assembler temporaries.
611 if (it->getSymbol().isTemporary())
612 continue;
613
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000614 if (!it->isExternal() && !Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000615 continue;
616
617 uint64_t &Entry = StringIndexMap[Symbol.getName()];
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000618 if (!Entry) {
619 Entry = StringTable.size();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000620 StringTable += Symbol.getName();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000621 StringTable += '\x00';
622 }
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000623
624 MachSymbolData MSD;
625 MSD.SymbolData = it;
626 MSD.StringIndex = Entry;
627
628 if (Symbol.isUndefined()) {
629 MSD.SectionIndex = 0;
630 UndefinedSymbolData.push_back(MSD);
631 } else if (Symbol.isAbsolute()) {
632 MSD.SectionIndex = 0;
633 ExternalSymbolData.push_back(MSD);
634 } else {
635 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
636 assert(MSD.SectionIndex && "Invalid section index!");
637 ExternalSymbolData.push_back(MSD);
638 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000639 }
640
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000641 // Now add the data for local symbols.
642 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
643 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000644 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000645
Daniel Dunbar959fd882009-08-26 22:13:22 +0000646 // Ignore assembler temporaries.
647 if (it->getSymbol().isTemporary())
648 continue;
649
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000650 if (it->isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000651 continue;
652
653 uint64_t &Entry = StringIndexMap[Symbol.getName()];
654 if (!Entry) {
655 Entry = StringTable.size();
656 StringTable += Symbol.getName();
657 StringTable += '\x00';
658 }
659
660 MachSymbolData MSD;
661 MSD.SymbolData = it;
662 MSD.StringIndex = Entry;
663
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000664 if (Symbol.isAbsolute()) {
665 MSD.SectionIndex = 0;
666 LocalSymbolData.push_back(MSD);
667 } else {
668 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
669 assert(MSD.SectionIndex && "Invalid section index!");
670 LocalSymbolData.push_back(MSD);
671 }
672 }
673
674 // External and undefined symbols are required to be in lexicographic order.
675 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
676 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
677
Daniel Dunbarbe963552009-08-26 13:57:54 +0000678 // Set the symbol indices.
679 Index = 0;
680 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
681 LocalSymbolData[i].SymbolData->setIndex(Index++);
682 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
683 ExternalSymbolData[i].SymbolData->setIndex(Index++);
684 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
685 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
686
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000687 // The string table is padded to a multiple of 4.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000688 while (StringTable.size() % 4)
689 StringTable += '\x00';
690 }
691
692 void WriteObject(MCAssembler &Asm) {
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000693 unsigned NumSections = Asm.size();
694
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000695 // Compute the symbol -> symbol data map.
696 //
697 // FIXME: This should not be here.
Daniel Dunbarbe963552009-08-26 13:57:54 +0000698 DenseMap<const MCSymbol*, MCSymbolData *> SymbolMap;
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000699 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
700 ie = Asm.symbol_end(); it != ie; ++it)
701 SymbolMap[&it->getSymbol()] = it;
702
703 // Create symbol data for any indirect symbols.
704 BindIndirectSymbols(Asm, SymbolMap);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000705
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000706 // Compute symbol table information.
707 SmallString<256> StringTable;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000708 std::vector<MachSymbolData> LocalSymbolData;
709 std::vector<MachSymbolData> ExternalSymbolData;
710 std::vector<MachSymbolData> UndefinedSymbolData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000711 unsigned NumSymbols = Asm.symbol_size();
712
713 // No symbol table command is written if there are no symbols.
714 if (NumSymbols)
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000715 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
716 UndefinedSymbolData);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000717
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000718 // The section data starts after the header, the segment load command (and
719 // section headers) and the symbol table.
720 unsigned NumLoadCommands = 1;
721 uint64_t LoadCommandsSize =
722 SegmentLoadCommand32Size + NumSections * Section32Size;
723
724 // Add the symbol table load command sizes, if used.
725 if (NumSymbols) {
726 NumLoadCommands += 2;
727 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
728 }
729
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000730 // Compute the total size of the section data, as well as its file size and
731 // vm size.
Daniel Dunbar6742e342009-08-26 04:13:32 +0000732 uint64_t SectionDataStart = Header32Size + LoadCommandsSize;
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000733 uint64_t SectionDataSize = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000734 uint64_t SectionDataFileSize = 0;
735 uint64_t VMSize = 0;
736 for (MCAssembler::iterator it = Asm.begin(),
737 ie = Asm.end(); it != ie; ++it) {
738 MCSectionData &SD = *it;
739
740 VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
741
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000742 if (isVirtualSection(SD.getSection()))
743 continue;
744
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000745 SectionDataSize = std::max(SectionDataSize,
746 SD.getAddress() + SD.getSize());
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000747 SectionDataFileSize = std::max(SectionDataFileSize,
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000748 SD.getAddress() + SD.getFileSize());
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000749 }
750
Daniel Dunbar6b716532010-02-09 23:00:14 +0000751 // The section data is padded to 4 bytes.
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000752 //
753 // FIXME: Is this machine dependent?
754 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
755 SectionDataFileSize += SectionDataPadding;
756
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000757 // Write the prolog, starting with the header and load command...
Daniel Dunbar6009db42009-08-26 21:22:22 +0000758 WriteHeader32(NumLoadCommands, LoadCommandsSize,
759 Asm.getSubsectionsViaSymbols());
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000760 WriteSegmentLoadCommand32(NumSections, VMSize,
761 SectionDataStart, SectionDataSize);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000762
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000763 // ... and then the section headers.
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000764 //
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000765 // We also compute the section relocations while we do this. Note that
Daniel Dunbar6b716532010-02-09 23:00:14 +0000766 // computing relocation info will also update the fixup to have the correct
767 // value; this will overwrite the appropriate data in the fragment when it
768 // is written.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000769 std::vector<MachRelocationEntry> RelocInfos;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000770 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000771 for (MCAssembler::iterator it = Asm.begin(),
772 ie = Asm.end(); it != ie; ++it) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000773 MCSectionData &SD = *it;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000774
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000775 // The assembler writes relocations in the reverse order they were seen.
776 //
777 // FIXME: It is probably more complicated than this.
778 unsigned NumRelocsStart = RelocInfos.size();
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000779 for (MCSectionData::reverse_iterator it2 = SD.rbegin(),
780 ie2 = SD.rend(); it2 != ie2; ++it2)
781 for (unsigned i = 0, e = it2->fixup_size(); i != e; ++i)
782 ComputeRelocationInfo(Asm, *it2, it2->getFixups()[e - i - 1],
783 SymbolMap, RelocInfos);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000784
785 unsigned NumRelocs = RelocInfos.size() - NumRelocsStart;
786 uint64_t SectionStart = SectionDataStart + SD.getAddress();
787 WriteSection32(SD, SectionStart, RelocTableEnd, NumRelocs);
788 RelocTableEnd += NumRelocs * RelocationInfoSize;
789 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000790
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000791 // Write the symbol table load command, if used.
792 if (NumSymbols) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000793 unsigned FirstLocalSymbol = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000794 unsigned NumLocalSymbols = LocalSymbolData.size();
795 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
796 unsigned NumExternalSymbols = ExternalSymbolData.size();
797 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
798 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000799 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
800 unsigned NumSymTabSymbols =
801 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
802 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
803 uint64_t IndirectSymbolOffset = 0;
804
805 // If used, the indirect symbols are written after the section data.
806 if (NumIndirectSymbols)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000807 IndirectSymbolOffset = RelocTableEnd;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000808
809 // The symbol table is written after the indirect symbol data.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000810 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000811
812 // The string table is written after symbol table.
813 uint64_t StringTableOffset =
814 SymbolTableOffset + NumSymTabSymbols * Nlist32Size;
815 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
816 StringTableOffset, StringTable.size());
817
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000818 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
819 FirstExternalSymbol, NumExternalSymbols,
820 FirstUndefinedSymbol, NumUndefinedSymbols,
821 IndirectSymbolOffset, NumIndirectSymbols);
822 }
823
824 // Write the actual section data.
825 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
826 WriteFileData(OS, *it, *this);
827
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000828 // Write the extra padding.
829 WriteZeros(SectionDataPadding);
830
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000831 // Write the relocation entries.
832 for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) {
833 Write32(RelocInfos[i].Word0);
834 Write32(RelocInfos[i].Word1);
835 }
836
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000837 // Write the symbol table data, if used.
838 if (NumSymbols) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000839 // Write the indirect symbol entries.
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000840 for (MCAssembler::indirect_symbol_iterator
841 it = Asm.indirect_symbol_begin(),
842 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
843 // Indirect symbols in the non lazy symbol pointer section have some
844 // special handling.
845 const MCSectionMachO &Section =
846 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
847 unsigned Type =
848 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
849 if (Type == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
850 // If this symbol is defined and internal, mark it as such.
851 if (it->Symbol->isDefined() &&
852 !SymbolMap.lookup(it->Symbol)->isExternal()) {
853 uint32_t Flags = ISF_Local;
854 if (it->Symbol->isAbsolute())
855 Flags |= ISF_Absolute;
856 Write32(Flags);
857 continue;
858 }
859 }
860
Daniel Dunbarbe963552009-08-26 13:57:54 +0000861 Write32(SymbolMap[it->Symbol]->getIndex());
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000862 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000863
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000864 // FIXME: Check that offsets match computed ones.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000865
866 // Write the symbol table entries.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000867 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
868 WriteNlist32(LocalSymbolData[i]);
869 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
870 WriteNlist32(ExternalSymbolData[i]);
871 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
872 WriteNlist32(UndefinedSymbolData[i]);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000873
874 // Write the string table.
875 OS << StringTable.str();
876 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000877 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000878};
879
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000880/* *** */
881
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000882MCFragment::MCFragment() : Kind(FragmentType(~0)) {
883}
884
Daniel Dunbar5e835962009-08-26 02:48:04 +0000885MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000886 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000887 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000888 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000889{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000890 if (Parent)
891 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000892}
893
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000894MCFragment::~MCFragment() {
895}
896
Daniel Dunbar5e835962009-08-26 02:48:04 +0000897uint64_t MCFragment::getAddress() const {
898 assert(getParent() && "Missing Section!");
899 return getParent()->getAddress() + Offset;
900}
901
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000902/* *** */
903
Daniel Dunbar81e40002009-08-27 00:38:04 +0000904MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000905
906MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000907 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000908 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000909 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +0000910 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000911 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000912 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000913{
914 if (A)
915 A->getSectionList().push_back(this);
916}
917
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000918/* *** */
919
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000920MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000921
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000922MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000923 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000924 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000925 IsExternal(false), IsPrivateExtern(false),
926 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000927{
928 if (A)
929 A->getSymbolList().push_back(this);
930}
931
932/* *** */
933
Daniel Dunbara03a3682009-08-31 08:07:55 +0000934MCAssembler::MCAssembler(MCContext &_Context, raw_ostream &_OS)
935 : Context(_Context), OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000936{
937}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000938
939MCAssembler::~MCAssembler() {
940}
941
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000942void MCAssembler::LayoutSection(MCSectionData &SD) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000943 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000944
945 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
946 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000947
Daniel Dunbar6742e342009-08-26 04:13:32 +0000948 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000949
950 // Evaluate fragment size.
951 switch (F.getKind()) {
952 case MCFragment::FT_Align: {
953 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000954
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000955 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000956 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000957 AF.setFileSize(0);
958 else
Daniel Dunbar6742e342009-08-26 04:13:32 +0000959 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000960 break;
961 }
962
963 case MCFragment::FT_Data:
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000964 F.setFileSize(F.getMaxFileSize());
965 break;
966
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000967 case MCFragment::FT_Fill: {
968 MCFillFragment &FF = cast<MCFillFragment>(F);
969
970 F.setFileSize(F.getMaxFileSize());
971
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000972 MCValue Target;
973 if (!FF.getValue().EvaluateAsRelocatable(Target))
974 llvm_report_error("expected relocatable expression");
975
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000976 // If the fill value is constant, thats it.
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000977 if (Target.isAbsolute())
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000978 break;
979
980 // Otherwise, add fixups for the values.
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000981 //
982 // FIXME: What we want to do here is lower this to a data fragment once we
983 // realize it will need relocations. This means that the only place we
984 // need to worry about relocations and fixing is on data fragments.
985 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i)
986 FF.getFixups().push_back(MCAsmFixup(i*FF.getValueSize(), FF.getValue(),
987 FF.getValueSize()));
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000988 break;
989 }
990
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000991 case MCFragment::FT_Org: {
992 MCOrgFragment &OF = cast<MCOrgFragment>(F);
993
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000994 MCValue Target;
995 if (!OF.getOffset().EvaluateAsRelocatable(Target))
996 llvm_report_error("expected relocatable expression");
997
998 if (!Target.isAbsolute())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000999 llvm_unreachable("FIXME: Not yet implemented!");
Daniel Dunbar1253a6f2009-10-16 01:58:03 +00001000 uint64_t OrgOffset = Target.getConstant();
Daniel Dunbar6742e342009-08-26 04:13:32 +00001001 uint64_t Offset = Address - SD.getAddress();
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001002
1003 // FIXME: We need a way to communicate this error.
Daniel Dunbara5441fe2009-08-22 08:27:54 +00001004 if (OrgOffset < Offset)
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001005 llvm_report_error("invalid .org offset '" + Twine(OrgOffset) +
Daniel Dunbar6742e342009-08-26 04:13:32 +00001006 "' (at offset '" + Twine(Offset) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001007
Daniel Dunbara5441fe2009-08-22 08:27:54 +00001008 F.setFileSize(OrgOffset - Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001009 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001010 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001011
1012 case MCFragment::FT_ZeroFill: {
1013 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
1014
1015 // Align the fragment offset; it is safe to adjust the offset freely since
1016 // this is only in virtual sections.
1017 uint64_t Aligned = RoundUpToAlignment(Address, ZFF.getAlignment());
1018 F.setOffset(Aligned - SD.getAddress());
1019
1020 // FIXME: This is misnamed.
1021 F.setFileSize(ZFF.getSize());
1022 break;
1023 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001024 }
1025
Daniel Dunbar6742e342009-08-26 04:13:32 +00001026 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001027 }
1028
Daniel Dunbar6742e342009-08-26 04:13:32 +00001029 // Set the section sizes.
1030 SD.setSize(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001031 if (isVirtualSection(SD.getSection()))
1032 SD.setFileSize(0);
1033 else
1034 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001035}
1036
1037/// WriteFileData - Write the \arg F data to the output file.
1038static void WriteFileData(raw_ostream &OS, const MCFragment &F,
1039 MachObjectWriter &MOW) {
1040 uint64_t Start = OS.tell();
1041 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001042
Daniel Dunbar0adcd352009-08-25 21:10:45 +00001043 ++EmittedFragments;
1044
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001045 // FIXME: Embed in fragments instead?
1046 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001047 case MCFragment::FT_Align: {
1048 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1049 uint64_t Count = AF.getFileSize() / AF.getValueSize();
1050
1051 // FIXME: This error shouldn't actually occur (the front end should emit
1052 // multiple .align directives to enforce the semantics it wants), but is
1053 // severe enough that we want to report it. How to handle this?
1054 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001055 llvm_report_error("undefined .align directive, value size '" +
1056 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001057 "' is not a divisor of padding size '" +
1058 Twine(AF.getFileSize()) + "'");
1059
1060 for (uint64_t i = 0; i != Count; ++i) {
1061 switch (AF.getValueSize()) {
1062 default:
1063 assert(0 && "Invalid size!");
1064 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
1065 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
1066 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
1067 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
1068 }
1069 }
1070 break;
1071 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001072
1073 case MCFragment::FT_Data:
1074 OS << cast<MCDataFragment>(F).getContents().str();
1075 break;
1076
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001077 case MCFragment::FT_Fill: {
1078 MCFillFragment &FF = cast<MCFillFragment>(F);
1079
Daniel Dunbar3f6a9602009-08-26 13:58:10 +00001080 int64_t Value = 0;
Daniel Dunbar1253a6f2009-10-16 01:58:03 +00001081
1082 MCValue Target;
1083 if (!FF.getValue().EvaluateAsRelocatable(Target))
1084 llvm_report_error("expected relocatable expression");
1085
1086 if (Target.isAbsolute())
1087 Value = Target.getConstant();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001088 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +00001089 if (!Target.isAbsolute()) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +00001090 // Find the fixup.
1091 //
Daniel Dunbarcb7d7432010-02-11 21:29:46 +00001092 // FIXME: Find a better way to write in the fixes (move to
1093 // MCDataFragment).
1094 const MCAsmFixup *Fixup = FF.LookupFixup(i * FF.getValueSize());
Daniel Dunbar3f6a9602009-08-26 13:58:10 +00001095 assert(Fixup && "Missing fixup for fill value!");
1096 Value = Fixup->FixedValue;
1097 }
1098
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001099 switch (FF.getValueSize()) {
1100 default:
1101 assert(0 && "Invalid size!");
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001102 case 1: MOW.Write8 (uint8_t (Value)); break;
1103 case 2: MOW.Write16(uint16_t(Value)); break;
1104 case 4: MOW.Write32(uint32_t(Value)); break;
1105 case 8: MOW.Write64(uint64_t(Value)); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001106 }
1107 }
1108 break;
1109 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001110
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001111 case MCFragment::FT_Org: {
1112 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1113
1114 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1115 MOW.Write8(uint8_t(OF.getValue()));
1116
1117 break;
1118 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001119
1120 case MCFragment::FT_ZeroFill: {
1121 assert(0 && "Invalid zero fill fragment in concrete section!");
1122 break;
1123 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001124 }
1125
1126 assert(OS.tell() - Start == F.getFileSize());
1127}
1128
1129/// WriteFileData - Write the \arg SD data to the output file.
1130static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1131 MachObjectWriter &MOW) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001132 // Ignore virtual sections.
1133 if (isVirtualSection(SD.getSection())) {
1134 assert(SD.getFileSize() == 0);
1135 return;
1136 }
1137
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001138 uint64_t Start = OS.tell();
1139 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001140
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001141 for (MCSectionData::const_iterator it = SD.begin(),
1142 ie = SD.end(); it != ie; ++it)
1143 WriteFileData(OS, *it, MOW);
1144
Daniel Dunbar6742e342009-08-26 04:13:32 +00001145 // Add section padding.
1146 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1147 MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1148
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001149 assert(OS.tell() - Start == SD.getFileSize());
1150}
1151
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001152void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001153 DEBUG_WITH_TYPE("mc-dump", {
1154 llvm::errs() << "assembler backend - pre-layout\n--\n";
1155 dump(); });
1156
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001157 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +00001158 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001159 MCSectionData *Prev = 0;
1160 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +00001161 MCSectionData &SD = *it;
1162
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001163 // Skip virtual sections.
1164 if (isVirtualSection(SD.getSection()))
1165 continue;
1166
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001167 // Align this section if necessary by adding padding bytes to the previous
1168 // section.
1169 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1170 assert(Prev && "Missing prev section!");
1171 Prev->setFileSize(Prev->getFileSize() + Pad);
1172 Address += Pad;
1173 }
Daniel Dunbar6742e342009-08-26 04:13:32 +00001174
1175 // Layout the section fragments and its size.
1176 SD.setAddress(Address);
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001177 LayoutSection(SD);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001178 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001179
1180 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +00001181 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001182
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001183 // Layout the virtual sections.
1184 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1185 MCSectionData &SD = *it;
1186
1187 if (!isVirtualSection(SD.getSection()))
1188 continue;
1189
1190 SD.setAddress(Address);
1191 LayoutSection(SD);
1192 Address += SD.getSize();
1193 }
1194
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001195 DEBUG_WITH_TYPE("mc-dump", {
1196 llvm::errs() << "assembler backend - post-layout\n--\n";
1197 dump(); });
1198
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001199 // Write the object file.
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001200 MachObjectWriter MOW(OS);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001201 MOW.WriteObject(*this);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001202
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001203 OS.flush();
1204}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001205
1206
1207// Debugging methods
1208
1209namespace llvm {
1210
1211raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
1212 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << AF.Value
1213 << " Size:" << AF.Size << ">";
1214 return OS;
1215}
1216
1217}
1218
1219void MCFragment::dump() {
1220 raw_ostream &OS = llvm::errs();
1221
1222 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1223 << " FileSize:" << FileSize;
1224
1225 if (!Fixups.empty()) {
1226 OS << "\n";
1227 OS << " Fixups:[";
1228 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
1229 if (it != fixup_begin()) OS << ",\n ";
1230 OS << *it;
1231 }
1232 OS << "]";
1233 }
1234
1235 OS << ">";
1236}
1237
1238void MCAlignFragment::dump() {
1239 raw_ostream &OS = llvm::errs();
1240
1241 OS << "<MCAlignFragment ";
1242 this->MCFragment::dump();
1243 OS << "\n ";
1244 OS << " Alignment:" << getAlignment()
1245 << " Value:" << getValue() << " ValueSize:" << getValueSize()
1246 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1247}
1248
1249void MCDataFragment::dump() {
1250 raw_ostream &OS = llvm::errs();
1251
1252 OS << "<MCDataFragment ";
1253 this->MCFragment::dump();
1254 OS << "\n ";
1255 OS << " Contents:[";
1256 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1257 if (i) OS << ",";
1258 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1259 }
1260 OS << "]>";
1261}
1262
1263void MCFillFragment::dump() {
1264 raw_ostream &OS = llvm::errs();
1265
1266 OS << "<MCFillFragment ";
1267 this->MCFragment::dump();
1268 OS << "\n ";
1269 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1270 << " Count:" << getCount() << ">";
1271}
1272
1273void MCOrgFragment::dump() {
1274 raw_ostream &OS = llvm::errs();
1275
1276 OS << "<MCOrgFragment ";
1277 this->MCFragment::dump();
1278 OS << "\n ";
1279 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1280}
1281
1282void MCZeroFillFragment::dump() {
1283 raw_ostream &OS = llvm::errs();
1284
1285 OS << "<MCZeroFillFragment ";
1286 this->MCFragment::dump();
1287 OS << "\n ";
1288 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1289}
1290
1291void MCSectionData::dump() {
1292 raw_ostream &OS = llvm::errs();
1293
1294 OS << "<MCSectionData";
1295 OS << " Alignment:" << getAlignment() << " Address:" << Address
1296 << " Size:" << Size << " FileSize:" << FileSize
1297 << " Fragments:[";
1298 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1299 if (it != begin()) OS << ",\n ";
1300 it->dump();
1301 }
1302 OS << "]>";
1303}
1304
1305void MCSymbolData::dump() {
1306 raw_ostream &OS = llvm::errs();
1307
1308 OS << "<MCSymbolData Symbol:" << getSymbol()
1309 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1310 << " Flags:" << getFlags() << " Index:" << getIndex();
1311 if (isCommon())
1312 OS << " (common, size:" << getCommonSize()
1313 << " align: " << getCommonAlignment() << ")";
1314 if (isExternal())
1315 OS << " (external)";
1316 if (isPrivateExtern())
1317 OS << " (private extern)";
1318 OS << ">";
1319}
1320
1321void MCAssembler::dump() {
1322 raw_ostream &OS = llvm::errs();
1323
1324 OS << "<MCAssembler\n";
1325 OS << " Sections:[";
1326 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1327 if (it != begin()) OS << ",\n ";
1328 it->dump();
1329 }
1330 OS << "],\n";
1331 OS << " Symbols:[";
1332
1333 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1334 if (it != symbol_begin()) OS << ",\n ";
1335 it->dump();
1336 }
1337 OS << "]>\n";
1338}