1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCSectionMachO.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Target/TargetMachOWriterInfo.h"
23 class MachObjectWriter;
25 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
26 MachObjectWriter &MOW);
28 class MachObjectWriter {
29 // See <mach-o/loader.h>.
31 Header_Magic32 = 0xFEEDFACE,
32 Header_Magic64 = 0xFEEDFACF
35 static const unsigned Header32Size = 28;
36 static const unsigned Header64Size = 32;
37 static const unsigned SegmentLoadCommand32Size = 56;
38 static const unsigned Section32Size = 68;
39 static const unsigned SymtabLoadCommandSize = 24;
40 static const unsigned DysymtabLoadCommandSize = 80;
41 static const unsigned Nlist32Size = 12;
47 enum LoadCommandType {
57 MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true)
58 : OS(_OS), IsLSB(_IsLSB) {
61 /// @name Helper Methods
64 void Write8(uint8_t Value) {
68 void Write16(uint16_t Value) {
70 Write8(uint8_t(Value >> 0));
71 Write8(uint8_t(Value >> 8));
73 Write8(uint8_t(Value >> 8));
74 Write8(uint8_t(Value >> 0));
78 void Write32(uint32_t Value) {
80 Write16(uint16_t(Value >> 0));
81 Write16(uint16_t(Value >> 16));
83 Write16(uint16_t(Value >> 16));
84 Write16(uint16_t(Value >> 0));
88 void Write64(uint64_t Value) {
90 Write32(uint32_t(Value >> 0));
91 Write32(uint32_t(Value >> 32));
93 Write32(uint32_t(Value >> 32));
94 Write32(uint32_t(Value >> 0));
98 void WriteZeros(unsigned N) {
99 const char Zeros[16] = { 0 };
101 for (unsigned i = 0, e = N / 16; i != e; ++i)
102 OS << StringRef(Zeros, 16);
104 OS << StringRef(Zeros, N % 16);
107 void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) {
110 WriteZeros(ZeroFillSize - Str.size());
115 void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize) {
116 // struct mach_header (28 bytes)
118 uint64_t Start = OS.tell();
121 Write32(Header_Magic32);
123 // FIXME: Support cputype.
124 Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386);
126 // FIXME: Support cpusubtype.
127 Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL);
131 // Object files have a single load command, the segment.
132 Write32(NumLoadCommands);
133 Write32(LoadCommandsSize);
136 assert(OS.tell() - Start == Header32Size);
139 /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
141 /// \arg NumSections - The number of sections in this segment.
142 /// \arg SectionDataSize - The total size of the sections.
143 void WriteSegmentLoadCommand32(unsigned NumSections,
144 uint64_t SectionDataStartOffset,
145 uint64_t SectionDataSize) {
146 // struct segment_command (56 bytes)
148 uint64_t Start = OS.tell();
151 Write32(LCT_Segment);
152 Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
155 Write32(0); // vmaddr
156 Write32(SectionDataSize); // vmsize
157 Write32(SectionDataStartOffset); // file offset
158 Write32(SectionDataSize); // file size
159 Write32(0x7); // maxprot
160 Write32(0x7); // initprot
161 Write32(NumSections);
164 assert(OS.tell() - Start == SegmentLoadCommand32Size);
167 void WriteSection32(const MCSectionData &SD, uint64_t FileOffset) {
168 // struct section (68 bytes)
170 uint64_t Start = OS.tell();
173 // FIXME: cast<> support!
174 const MCSectionMachO &Section =
175 static_cast<const MCSectionMachO&>(SD.getSection());
176 WriteString(Section.getSectionName(), 16);
177 WriteString(Section.getSegmentName(), 16);
178 Write32(0); // address
179 Write32(SD.getFileSize()); // size
182 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
183 Write32(Log2_32(SD.getAlignment()));
184 Write32(0); // file offset of relocation entries
185 Write32(0); // number of relocation entrions
186 Write32(Section.getTypeAndAttributes());
187 Write32(0); // reserved1
188 Write32(Section.getStubSize()); // reserved2
190 assert(OS.tell() - Start == Section32Size);
193 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
194 uint32_t StringTableOffset,
195 uint32_t StringTableSize) {
196 // struct symtab_command (24 bytes)
198 uint64_t Start = OS.tell();
202 Write32(SymtabLoadCommandSize);
203 Write32(SymbolOffset);
205 Write32(StringTableOffset);
206 Write32(StringTableSize);
208 assert(OS.tell() - Start == SymtabLoadCommandSize);
211 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
212 uint32_t NumLocalSymbols,
213 uint32_t FirstExternalSymbol,
214 uint32_t NumExternalSymbols,
215 uint32_t FirstUndefinedSymbol,
216 uint32_t NumUndefinedSymbols,
217 uint32_t IndirectSymbolOffset,
218 uint32_t NumIndirectSymbols) {
219 // struct dysymtab_command (80 bytes)
221 uint64_t Start = OS.tell();
224 Write32(LCT_Dysymtab);
225 Write32(DysymtabLoadCommandSize);
226 Write32(FirstLocalSymbol);
227 Write32(NumLocalSymbols);
228 Write32(FirstExternalSymbol);
229 Write32(NumExternalSymbols);
230 Write32(FirstUndefinedSymbol);
231 Write32(NumUndefinedSymbols);
232 Write32(0); // tocoff
234 Write32(0); // modtaboff
235 Write32(0); // nmodtab
236 Write32(0); // extrefsymoff
237 Write32(0); // nextrefsyms
238 Write32(IndirectSymbolOffset);
239 Write32(NumIndirectSymbols);
240 Write32(0); // extreloff
241 Write32(0); // nextrel
242 Write32(0); // locreloff
243 Write32(0); // nlocrel
245 assert(OS.tell() - Start == DysymtabLoadCommandSize);
248 void WriteNlist32(uint32_t StringIndex, uint8_t Type, uint8_t Sect,
249 int16_t Desc, uint32_t Value) {
250 // struct nlist (12 bytes)
252 Write32(StringIndex);
259 /// ComputeStringTable - Compute the string table, for use in the symbol
262 /// \param StringTable [out] - The string table data.
263 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
265 void ComputeStringTable(MCAssembler &Asm, SmallString<256> &StringTable,
266 StringMap<uint64_t> &StringIndexMap) {
267 // Build the string table.
269 // FIXME: Does 'as' ever bother to compress this when we have a suffix
272 // Index 0 is always the empty string.
273 StringTable += '\x00';
274 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
275 ie = Asm.symbol_end(); it != ie; ++it) {
276 StringRef Name = it->getSymbol().getName();
277 uint64_t &Entry = StringIndexMap[Name];
280 Entry = StringTable.size();
282 StringTable += '\x00';
286 // The string table is padded to a multiple of 4.
288 // FIXME: Check to see if this varies per arch.
289 while (StringTable.size() % 4)
290 StringTable += '\x00';
293 void WriteObject(MCAssembler &Asm) {
294 unsigned NumSections = Asm.size();
296 // Compute symbol table information.
297 SmallString<256> StringTable;
298 StringMap<uint64_t> StringIndexMap;
299 unsigned NumSymbols = Asm.symbol_size();
301 // No symbol table command is written if there are no symbols.
303 ComputeStringTable(Asm, StringTable, StringIndexMap);
305 // Compute the file offsets for all the sections in advance, so that we can
306 // write things out in order.
307 SmallVector<uint64_t, 16> SectionFileOffsets;
308 SectionFileOffsets.resize(NumSections);
310 // The section data starts after the header, the segment load command (and
311 // section headers) and the symbol table.
312 unsigned NumLoadCommands = 1;
313 uint64_t LoadCommandsSize =
314 SegmentLoadCommand32Size + NumSections * Section32Size;
316 // Add the symbol table load command sizes, if used.
318 NumLoadCommands += 2;
319 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
322 uint64_t FileOffset = Header32Size + LoadCommandsSize;
323 uint64_t SectionDataStartOffset = FileOffset;
324 uint64_t SectionDataSize = 0;
326 for (MCAssembler::iterator it = Asm.begin(),
327 ie = Asm.end(); it != ie; ++it, ++Index) {
328 SectionFileOffsets[Index] = FileOffset;
329 FileOffset += it->getFileSize();
330 SectionDataSize += it->getFileSize();
333 // Write the prolog, starting with the header and load command...
334 WriteHeader32(NumLoadCommands, LoadCommandsSize);
335 WriteSegmentLoadCommand32(NumSections, SectionDataStartOffset,
338 // ... and then the section headers.
340 for (MCAssembler::iterator it = Asm.begin(),
341 ie = Asm.end(); it != ie; ++it, ++Index)
342 WriteSection32(*it, SectionFileOffsets[Index]);
344 // Write the symbol table load command, if used.
346 // The string table is written after all the section data.
347 uint64_t SymbolTableOffset = SectionDataStartOffset + SectionDataSize;
348 uint64_t StringTableOffset =
349 SymbolTableOffset + NumSymbols * Nlist32Size;
350 WriteSymtabLoadCommand(SymbolTableOffset, NumSymbols,
351 StringTableOffset, StringTable.size());
353 // FIXME: Get correct symbol indices and counts.
354 unsigned FirstLocalSymbol = 0;
355 unsigned NumLocalSymbols = NumSymbols;
356 unsigned FirstExternalSymbol = NumLocalSymbols;
357 unsigned NumExternalSymbols = 0;
358 unsigned FirstUndefinedSymbol = NumLocalSymbols;
359 unsigned NumUndefinedSymbols = 0;
360 unsigned IndirectSymbolOffset = 0;
361 unsigned NumIndirectSymbols = 0;
362 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
363 FirstExternalSymbol, NumExternalSymbols,
364 FirstUndefinedSymbol, NumUndefinedSymbols,
365 IndirectSymbolOffset, NumIndirectSymbols);
368 // Write the actual section data.
369 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
370 WriteFileData(OS, *it, *this);
372 // Write the symbol table data, if used.
374 // FIXME: Check that offsets match computed ones.
376 // FIXME: These need to be reordered, both to segregate into categories
377 // as well as to order some sublists.
379 // Write the symbol table entries.
380 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
381 ie = Asm.symbol_end(); it != ie; ++it) {
382 MCSymbol &Sym = it->getSymbol();
383 uint64_t Index = StringIndexMap[Sym.getName()];
384 assert(Index && "Invalid index!");
385 WriteNlist32(Index, /*FIXME: Type=*/0, /*FIXME: Sect=*/0,
386 /*FIXME: Desc=*/0, /*FIXME: Value=*/0);
389 // Write the string table.
390 OS << StringTable.str();
397 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
400 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD)
402 FileSize(~UINT64_C(0))
405 SD->getFragmentList().push_back(this);
408 MCFragment::~MCFragment() {
413 MCSectionData::MCSectionData() : Section(*(MCSection*)0) {}
415 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
418 FileSize(~UINT64_C(0))
421 A->getSectionList().push_back(this);
426 MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {}
428 MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment,
429 uint64_t _Offset, MCAssembler *A)
430 : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset)
433 A->getSymbolList().push_back(this);
438 MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {}
440 MCAssembler::~MCAssembler() {
443 void MCAssembler::LayoutSection(MCSectionData &SD) {
446 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
451 // Evaluate fragment size.
452 switch (F.getKind()) {
453 case MCFragment::FT_Align: {
454 MCAlignFragment &AF = cast<MCAlignFragment>(F);
456 uint64_t AlignedOffset = RoundUpToAlignment(Offset, AF.getAlignment());
457 uint64_t PaddingBytes = AlignedOffset - Offset;
459 if (PaddingBytes > AF.getMaxBytesToEmit())
462 AF.setFileSize(PaddingBytes);
466 case MCFragment::FT_Data:
467 case MCFragment::FT_Fill:
468 F.setFileSize(F.getMaxFileSize());
471 case MCFragment::FT_Org: {
472 MCOrgFragment &OF = cast<MCOrgFragment>(F);
474 if (!OF.getOffset().isAbsolute())
475 llvm_unreachable("FIXME: Not yet implemented!");
476 uint64_t OrgOffset = OF.getOffset().getConstant();
478 // FIXME: We need a way to communicate this error.
479 if (OrgOffset < Offset)
480 llvm_report_error("invalid .org offset '" + Twine(OrgOffset) +
481 "' (section offset '" + Twine(Offset) + "'");
483 F.setFileSize(OrgOffset - Offset);
488 Offset += F.getFileSize();
491 // FIXME: Pad section?
492 SD.setFileSize(Offset);
495 /// WriteFileData - Write the \arg F data to the output file.
496 static void WriteFileData(raw_ostream &OS, const MCFragment &F,
497 MachObjectWriter &MOW) {
498 uint64_t Start = OS.tell();
501 // FIXME: Embed in fragments instead?
502 switch (F.getKind()) {
503 case MCFragment::FT_Align: {
504 MCAlignFragment &AF = cast<MCAlignFragment>(F);
505 uint64_t Count = AF.getFileSize() / AF.getValueSize();
507 // FIXME: This error shouldn't actually occur (the front end should emit
508 // multiple .align directives to enforce the semantics it wants), but is
509 // severe enough that we want to report it. How to handle this?
510 if (Count * AF.getValueSize() != AF.getFileSize())
511 llvm_report_error("undefined .align directive, value size '" +
512 Twine(AF.getValueSize()) +
513 "' is not a divisor of padding size '" +
514 Twine(AF.getFileSize()) + "'");
516 for (uint64_t i = 0; i != Count; ++i) {
517 switch (AF.getValueSize()) {
519 assert(0 && "Invalid size!");
520 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
521 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
522 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
523 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
529 case MCFragment::FT_Data:
530 OS << cast<MCDataFragment>(F).getContents().str();
533 case MCFragment::FT_Fill: {
534 MCFillFragment &FF = cast<MCFillFragment>(F);
536 if (!FF.getValue().isAbsolute())
537 llvm_unreachable("FIXME: Not yet implemented!");
538 int64_t Value = FF.getValue().getConstant();
540 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
541 switch (FF.getValueSize()) {
543 assert(0 && "Invalid size!");
544 case 1: MOW.Write8 (uint8_t (Value)); break;
545 case 2: MOW.Write16(uint16_t(Value)); break;
546 case 4: MOW.Write32(uint32_t(Value)); break;
547 case 8: MOW.Write64(uint64_t(Value)); break;
553 case MCFragment::FT_Org: {
554 MCOrgFragment &OF = cast<MCOrgFragment>(F);
556 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
557 MOW.Write8(uint8_t(OF.getValue()));
563 assert(OS.tell() - Start == F.getFileSize());
566 /// WriteFileData - Write the \arg SD data to the output file.
567 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
568 MachObjectWriter &MOW) {
569 uint64_t Start = OS.tell();
572 for (MCSectionData::const_iterator it = SD.begin(),
573 ie = SD.end(); it != ie; ++it)
574 WriteFileData(OS, *it, MOW);
576 assert(OS.tell() - Start == SD.getFileSize());
579 void MCAssembler::Finish() {
580 // Layout the sections and fragments.
581 for (iterator it = begin(), ie = end(); it != ie; ++it)
584 // Write the object file.
585 MachObjectWriter MOW(OS);
586 MOW.WriteObject(*this);