llvm-mc/Mach-O: Sketch symbol table support.
[oota-llvm.git] / lib / MC / MCAssembler.cpp
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCAssembler.h"
11
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"
20
21 using namespace llvm;
22
23 class MachObjectWriter;
24
25 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
26                           MachObjectWriter &MOW);
27
28 class MachObjectWriter {
29   // See <mach-o/loader.h>.
30   enum {
31     Header_Magic32 = 0xFEEDFACE,
32     Header_Magic64 = 0xFEEDFACF
33   };
34   
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;
42
43   enum HeaderFileType {
44     HFT_Object = 0x1
45   };
46
47   enum LoadCommandType {
48     LCT_Segment = 0x1,
49     LCT_Symtab = 0x2,
50     LCT_Dysymtab = 0xb
51   };
52
53   raw_ostream &OS;
54   bool IsLSB;
55
56 public:
57   MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true) 
58     : OS(_OS), IsLSB(_IsLSB) {
59   }
60
61   /// @name Helper Methods
62   /// @{
63
64   void Write8(uint8_t Value) {
65     OS << char(Value);
66   }
67
68   void Write16(uint16_t Value) {
69     if (IsLSB) {
70       Write8(uint8_t(Value >> 0));
71       Write8(uint8_t(Value >> 8));
72     } else {
73       Write8(uint8_t(Value >> 8));
74       Write8(uint8_t(Value >> 0));
75     }
76   }
77
78   void Write32(uint32_t Value) {
79     if (IsLSB) {
80       Write16(uint16_t(Value >> 0));
81       Write16(uint16_t(Value >> 16));
82     } else {
83       Write16(uint16_t(Value >> 16));
84       Write16(uint16_t(Value >> 0));
85     }
86   }
87
88   void Write64(uint64_t Value) {
89     if (IsLSB) {
90       Write32(uint32_t(Value >> 0));
91       Write32(uint32_t(Value >> 32));
92     } else {
93       Write32(uint32_t(Value >> 32));
94       Write32(uint32_t(Value >> 0));
95     }
96   }
97
98   void WriteZeros(unsigned N) {
99     const char Zeros[16] = { 0 };
100     
101     for (unsigned i = 0, e = N / 16; i != e; ++i)
102       OS << StringRef(Zeros, 16);
103     
104     OS << StringRef(Zeros, N % 16);
105   }
106
107   void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) {
108     OS << Str;
109     if (ZeroFillSize)
110       WriteZeros(ZeroFillSize - Str.size());
111   }
112
113   /// @}
114   
115   void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize) {
116     // struct mach_header (28 bytes)
117
118     uint64_t Start = OS.tell();
119     (void) Start;
120
121     Write32(Header_Magic32);
122
123     // FIXME: Support cputype.
124     Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386);
125
126     // FIXME: Support cpusubtype.
127     Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL);
128
129     Write32(HFT_Object);
130
131     // Object files have a single load command, the segment.
132     Write32(NumLoadCommands);
133     Write32(LoadCommandsSize);
134     Write32(0); // Flags
135
136     assert(OS.tell() - Start == Header32Size);
137   }
138
139   /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
140   ///
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)
147
148     uint64_t Start = OS.tell();
149     (void) Start;
150
151     Write32(LCT_Segment);
152     Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
153
154     WriteString("", 16);
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);
162     Write32(0); // flags
163
164     assert(OS.tell() - Start == SegmentLoadCommand32Size);
165   }
166
167   void WriteSection32(const MCSectionData &SD, uint64_t FileOffset) {
168     // struct section (68 bytes)
169
170     uint64_t Start = OS.tell();
171     (void) Start;
172
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
180     Write32(FileOffset);
181
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
189
190     assert(OS.tell() - Start == Section32Size);
191   }
192
193   void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
194                               uint32_t StringTableOffset,
195                               uint32_t StringTableSize) {
196     // struct symtab_command (24 bytes)
197
198     uint64_t Start = OS.tell();
199     (void) Start;
200
201     Write32(LCT_Symtab);
202     Write32(SymtabLoadCommandSize);
203     Write32(SymbolOffset);
204     Write32(NumSymbols);
205     Write32(StringTableOffset);
206     Write32(StringTableSize);
207
208     assert(OS.tell() - Start == SymtabLoadCommandSize);
209   }
210
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)
220
221     uint64_t Start = OS.tell();
222     (void) Start;
223
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
233     Write32(0); // ntoc
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
244
245     assert(OS.tell() - Start == DysymtabLoadCommandSize);
246   }
247
248   void WriteNlist32(uint32_t StringIndex, uint8_t Type, uint8_t Sect,
249                     int16_t Desc, uint32_t Value) {
250     // struct nlist (12 bytes)
251
252     Write32(StringIndex);
253     Write8(Type);
254     Write8(Sect);
255     Write16(Desc);
256     Write32(Value);
257   }
258
259   /// ComputeStringTable - Compute the string table, for use in the symbol
260   /// table.
261   ///
262   /// \param StringTable [out] - The string table data.
263   /// \param StringIndexMap [out] - Map from symbol names to offsets in the
264   /// string table.
265   void ComputeStringTable(MCAssembler &Asm, SmallString<256> &StringTable,
266                           StringMap<uint64_t> &StringIndexMap) {
267     // Build the string table.
268     //
269     // FIXME: Does 'as' ever bother to compress this when we have a suffix
270     // match?
271
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];
278
279       if (!Entry) {
280         Entry = StringTable.size();
281         StringTable += Name;
282         StringTable += '\x00';
283       }
284     }
285
286     // The string table is padded to a multiple of 4.
287     //
288     // FIXME: Check to see if this varies per arch.
289     while (StringTable.size() % 4)
290       StringTable += '\x00';
291   }
292
293   void WriteObject(MCAssembler &Asm) {
294     unsigned NumSections = Asm.size();
295
296     // Compute symbol table information.
297     SmallString<256> StringTable;
298     StringMap<uint64_t> StringIndexMap;
299     unsigned NumSymbols = Asm.symbol_size();
300
301     // No symbol table command is written if there are no symbols.
302     if (NumSymbols)
303       ComputeStringTable(Asm, StringTable, StringIndexMap);
304
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);
309   
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;
315
316     // Add the symbol table load command sizes, if used.
317     if (NumSymbols) {
318       NumLoadCommands += 2;
319       LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
320     }
321
322     uint64_t FileOffset = Header32Size + LoadCommandsSize;
323     uint64_t SectionDataStartOffset = FileOffset;
324     uint64_t SectionDataSize = 0;
325     unsigned Index = 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();
331     }
332
333     // Write the prolog, starting with the header and load command...
334     WriteHeader32(NumLoadCommands, LoadCommandsSize);
335     WriteSegmentLoadCommand32(NumSections, SectionDataStartOffset,
336                               SectionDataSize);
337   
338     // ... and then the section headers.
339     Index = 0;
340     for (MCAssembler::iterator it = Asm.begin(),
341            ie = Asm.end(); it != ie; ++it, ++Index)
342       WriteSection32(*it, SectionFileOffsets[Index]);
343
344     // Write the symbol table load command, if used.
345     if (NumSymbols) {
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());
352
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);
366     }
367
368     // Write the actual section data.
369     for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
370       WriteFileData(OS, *it, *this);
371
372     // Write the symbol table data, if used.
373     if (NumSymbols) {
374       // FIXME: Check that offsets match computed ones.
375
376       // FIXME: These need to be reordered, both to segregate into categories
377       // as well as to order some sublists.
378
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);
387       }
388
389       // Write the string table.
390       OS << StringTable.str();
391     }
392   }
393 };
394
395 /* *** */
396
397 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
398 }
399
400 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD)
401   : Kind(_Kind),
402     FileSize(~UINT64_C(0))
403 {
404   if (SD)
405     SD->getFragmentList().push_back(this);
406 }
407
408 MCFragment::~MCFragment() {
409 }
410
411 /* *** */
412
413 MCSectionData::MCSectionData() : Section(*(MCSection*)0) {}
414
415 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
416   : Section(_Section),
417     Alignment(1),
418     FileSize(~UINT64_C(0))
419 {
420   if (A)
421     A->getSectionList().push_back(this);
422 }
423
424 /* *** */
425
426 MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {}
427
428 MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment,
429                            uint64_t _Offset, MCAssembler *A)
430   : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset) 
431 {
432   if (A)
433     A->getSymbolList().push_back(this);
434 }
435
436 /* *** */
437
438 MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {}
439
440 MCAssembler::~MCAssembler() {
441 }
442
443 void MCAssembler::LayoutSection(MCSectionData &SD) {
444   uint64_t Offset = 0;
445
446   for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
447     MCFragment &F = *it;
448
449     F.setOffset(Offset);
450
451     // Evaluate fragment size.
452     switch (F.getKind()) {
453     case MCFragment::FT_Align: {
454       MCAlignFragment &AF = cast<MCAlignFragment>(F);
455       
456       uint64_t AlignedOffset = RoundUpToAlignment(Offset, AF.getAlignment());
457       uint64_t PaddingBytes = AlignedOffset - Offset;
458
459       if (PaddingBytes > AF.getMaxBytesToEmit())
460         AF.setFileSize(0);
461       else
462         AF.setFileSize(PaddingBytes);
463       break;
464     }
465
466     case MCFragment::FT_Data:
467     case MCFragment::FT_Fill:
468       F.setFileSize(F.getMaxFileSize());
469       break;
470
471     case MCFragment::FT_Org: {
472       MCOrgFragment &OF = cast<MCOrgFragment>(F);
473
474       if (!OF.getOffset().isAbsolute())
475         llvm_unreachable("FIXME: Not yet implemented!");
476       uint64_t OrgOffset = OF.getOffset().getConstant();
477
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) + "'");
482         
483       F.setFileSize(OrgOffset - Offset);
484       break;
485     }      
486     }
487
488     Offset += F.getFileSize();
489   }
490
491   // FIXME: Pad section?
492   SD.setFileSize(Offset);
493 }
494
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();
499   (void) Start;
500     
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();
506
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()) + "'");
515
516     for (uint64_t i = 0; i != Count; ++i) {
517       switch (AF.getValueSize()) {
518       default:
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;
524       }
525     }
526     break;
527   }
528
529   case MCFragment::FT_Data:
530     OS << cast<MCDataFragment>(F).getContents().str();
531     break;
532
533   case MCFragment::FT_Fill: {
534     MCFillFragment &FF = cast<MCFillFragment>(F);
535
536     if (!FF.getValue().isAbsolute())
537       llvm_unreachable("FIXME: Not yet implemented!");
538     int64_t Value = FF.getValue().getConstant();
539
540     for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
541       switch (FF.getValueSize()) {
542       default:
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;
548       }
549     }
550     break;
551   }
552     
553   case MCFragment::FT_Org: {
554     MCOrgFragment &OF = cast<MCOrgFragment>(F);
555
556     for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
557       MOW.Write8(uint8_t(OF.getValue()));
558
559     break;
560   }
561   }
562
563   assert(OS.tell() - Start == F.getFileSize());
564 }
565
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();
570   (void) Start;
571       
572   for (MCSectionData::const_iterator it = SD.begin(),
573          ie = SD.end(); it != ie; ++it)
574     WriteFileData(OS, *it, MOW);
575
576   assert(OS.tell() - Start == SD.getFileSize());
577 }
578
579 void MCAssembler::Finish() {
580   // Layout the sections and fragments.
581   for (iterator it = begin(), ie = end(); it != ie; ++it)
582     LayoutSection(*it);
583
584   // Write the object file.
585   MachObjectWriter MOW(OS);
586   MOW.WriteObject(*this);
587
588   OS.flush();
589 }