diff --git a/include/llvm/Support/COFF.h b/include/llvm/Support/COFF.h
index 6739255..d4cc0e7 100644
--- a/include/llvm/Support/COFF.h
+++ b/include/llvm/Support/COFF.h
@@ -53,6 +53,24 @@ namespace COFF {
     IMAGE_FILE_MACHINE_AMD64 = 0x8664
   };
 
+  enum Characteristics {
+    IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
+    IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
+    IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
+    IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
+    IMAGE_FILE_AGGRESSIVE_WS_TRIM = 0x0010,
+    IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
+    IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
+    IMAGE_FILE_32BIT_MACHINE = 0x0100,
+    IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
+    IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
+    IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
+    IMAGE_FILE_SYSTEM = 0x1000,
+    IMAGE_FILE_DLL = 0x2000,
+    IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
+    IMAGE_FILE_BYTES_REVERSED_HI = 0x8000
+  };
+
   struct symbol {
     char     Name[NameSize];
     uint32_t Value;
diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp
index fe3342c..279a2be 100644
--- a/lib/MC/WinCOFFObjectWriter.cpp
+++ b/lib/MC/WinCOFFObjectWriter.cpp
@@ -149,6 +149,7 @@ public:
   template <typename object_t, typename list_t>
   object_t *createCOFFEntity(llvm::StringRef Name, list_t &List);
 
+  void DefineFileSymbol();
   void DefineSection(MCSectionData const &SectionData);
   void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler);
 
@@ -180,6 +181,9 @@ public:
                         uint64_t &FixedValue);
 
   void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
+  uint64_t getPaddingSize(const MCSectionData *SD,
+                          const MCAsmLayout &Layout) const;
+
 };
 }
 
@@ -320,6 +324,11 @@ WinCOFFObjectWriter::WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit)
 
   Is64Bit ? Header.Machine = COFF::IMAGE_FILE_MACHINE_AMD64
           : Header.Machine = COFF::IMAGE_FILE_MACHINE_I386;
+
+  uint16_t &Characteristics = Header.Characteristics;
+  //Characteristics |= COFF::IMAGE_FILE_RELOCS_STRIPPED;
+  Characteristics |= COFF::IMAGE_FILE_LINE_NUMS_STRIPPED;
+  Characteristics |= COFF::IMAGE_FILE_32BIT_MACHINE;
 }
 
 WinCOFFObjectWriter::~WinCOFFObjectWriter() {
@@ -359,6 +368,19 @@ object_t *WinCOFFObjectWriter::createCOFFEntity(llvm::StringRef Name,
   return Object;
 }
 
+void WinCOFFObjectWriter::DefineFileSymbol() {
+  COFFSymbol  *coff_symbol = createSymbol(".file");
+  // coff_symbol->Section = coff_section;
+  coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
+  coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
+
+  coff_symbol->Aux.resize(1);
+  memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
+  coff_symbol->Aux[0].AuxType = ATFile;
+  char *p = reinterpret_cast<char *>(coff_symbol->Aux[0].Aux.File.FileName);
+  strcpy(p, "fake");
+}
+
 /// This function takes a section data object from the assembler
 /// and creates the associated COFF section staging object.
 void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
@@ -460,10 +482,20 @@ void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
       external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
   }
 
-  if (SymbolData.Fragment != NULL)
+  if (SymbolData.Fragment != NULL) {
     coff_symbol->Section =
       SectionMap[&SymbolData.Fragment->getParent()->getSection()];
 
+    static bool First = true;
+    unsigned Type = coff_symbol->Data.Type;
+    unsigned ComplexType = (Type & 0xF0) >> 4;
+    if (ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION && First) {
+      coff_symbol->Aux.resize(1);
+      memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
+      First = false;
+    }
+  }
+
   // Bind internal COFF symbol to MC symbol.
   coff_symbol->MCData = &SymbolData;
   SymbolMap[&SymbolData.getSymbol()] = coff_symbol;
@@ -490,7 +522,7 @@ void WinCOFFObjectWriter::MakeSectionReal(COFFSection &S, size_t Number) {
 
   S.Number = Number;
   S.Symbol->Data.SectionNumber = S.Number;
-  S.Symbol->Aux[0].Aux.SectionDefinition.Number = S.Number;
+  S.Symbol->Aux[0].Aux.SectionDefinition.Number = 0;
 }
 
 void WinCOFFObjectWriter::MakeSymbolReal(COFFSymbol &S, size_t Index) {
@@ -616,13 +648,68 @@ void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
   // "Define" each section & symbol. This creates section & symbol
   // entries in the staging area.
 
+  DefineFileSymbol();
+
+  std::vector<const MCSymbolData*> Symbols;
+  for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
+         e = Asm.symbol_end(); i != e; i++) {
+    Symbols.push_back(i);
+  }
+
+
+  // Define foo symbols
+  for (std::vector<const MCSymbolData*>::iterator i = Symbols.begin(),
+         e = Symbols.end(); i != e; i++) {
+    const MCSymbolData &SymbolData = **i;
+    unsigned Flags = SymbolData.getFlags();
+    unsigned Type = (Flags & COFF::SF_TypeMask) >> COFF::SF_TypeShift;
+    unsigned ComplexType = (Type & 0xF0) >> 4;
+    if (ComplexType != COFF::IMAGE_SYM_DTYPE_FUNCTION)
+      continue;
+    if (ExportSymbol(SymbolData, Asm))
+      DefineSymbol(SymbolData, Asm);
+  }
+
   for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++)
     DefineSection(*i);
 
+  // Define !foo symbols
+  for (std::vector<const MCSymbolData*>::iterator i = Symbols.begin(),
+         e = Symbols.end(); i != e; i++) {
+    const MCSymbolData &SymbolData = **i;
+    unsigned Flags = SymbolData.getFlags();
+    unsigned Type = (Flags & COFF::SF_TypeMask) >> COFF::SF_TypeShift;
+    unsigned ComplexType = (Type & 0xF0) >> 4;
+    if (ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION)
+      continue;
+    if (!SymbolData.Fragment)
+      continue;
+    if (ExportSymbol(SymbolData, Asm))
+      DefineSymbol(SymbolData, Asm);
+  }
+
+  for (std::vector<const MCSymbolData*>::iterator i = Symbols.begin(),
+         e = Symbols.end(); i != e; i++) {
+    const MCSymbolData &SymbolData = **i;
+    unsigned Flags = SymbolData.getFlags();
+    unsigned Type = (Flags & COFF::SF_TypeMask) >> COFF::SF_TypeShift;
+    unsigned ComplexType = (Type & 0xF0) >> 4;
+    if (ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION)
+      continue;
+    if (SymbolData.Fragment)
+      continue;
+    if (ExportSymbol(SymbolData, Asm))
+      DefineSymbol(SymbolData, Asm);
+  }
+
   for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
                                           e = Asm.symbol_end(); i != e; i++) {
-    if (ExportSymbol(*i, Asm))
-      DefineSymbol(*i, Asm);
+    const MCSymbolData &SymbolData = *i;
+    if (SymbolData.Fragment == NULL)
+      continue;
+    COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&SymbolData.getSymbol());
+    coff_symbol->Section =
+      SectionMap[&SymbolData.Fragment->getParent()->getSection()];
   }
 }
 
@@ -656,8 +743,13 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
     const MCSymbol *B = &Target.getSymB()->getSymbol();
     MCSymbolData &B_SD = Asm.getSymbolData(*B);
 
-    FixedValue = Layout.getSymbolOffset(&A_SD) - Layout.getSymbolOffset(&B_SD);
+    // Offset of the symbol in the section
+    int64_t a = Layout.getSymbolOffset(&B_SD);
 
+    // Ofeset of the relocation in the section
+    int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
+
+    FixedValue = b - a;
     // In the case where we have SymbA and SymB, we just need to store the delta
     // between the two symbols.  Update FixedValue to account for the delta, and
     // skip recording the relocation.
@@ -673,10 +765,11 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
   Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
 
   // Turn relocations for temporary symbols into section relocations.
-  if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection) {
+  if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection ||
+      coff_symbol->Section) {
     Reloc.Symb = coff_symbol->Section->Symbol;
     FixedValue += Layout.getFragmentOffset(coff_symbol->MCData->Fragment)
-                + coff_symbol->MCData->getOffset();
+      + coff_symbol->MCData->getOffset();
   } else
     Reloc.Symb = coff_symbol;
 
@@ -717,6 +810,15 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
   coff_section->Relocations.push_back(Reloc);
 }
 
+uint64_t WinCOFFObjectWriter::getPaddingSize(const MCSectionData *SD,
+                                             const MCAsmLayout &Layout) const {
+  COFFSection *coff_section = SectionMap.lookup(&SD->getSection());
+  uint64_t EndAddr = coff_section->Header.VirtualAddress +
+    Layout.getSectionAddressSize(SD);
+  return OffsetToAlignment(EndAddr, SD->getAlignment());
+}
+
+
 void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
                                       const MCAsmLayout &Layout) {
   // Assign symbol and section indexes and offsets.
@@ -724,7 +826,11 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
 
   for (sections::iterator i = Sections.begin(),
                           e = Sections.end(); i != e; i++) {
-    if (Layout.getSectionAddressSize((*i)->MCData) > 0) {
+    const MCSectionData *SD = (*i)->MCData;
+    const MCSection &Sec = SD->getSection();
+    SectionKind Kind = Sec.getKind();
+    if (Layout.getSectionAddressSize(SD) > 0 || Kind.isBSS() ||
+        Kind.isDataRel() || Kind.isText()) {
       MakeSectionReal(**i, ++Header.NumberOfSections);
     } else {
       (*i)->Number = -1;
@@ -785,13 +891,25 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
       continue;
 
     Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(i);
+    uint64_t Pad = getPaddingSize(i, Layout);
+    Sec->Header.SizeOfRawData += Pad;
 
-    if (IsPhysicalSection(Sec)) {
+    if (IsPhysicalSection(Sec) && Sec->Header.SizeOfRawData) {
       Sec->Header.PointerToRawData = offset;
 
       offset += Sec->Header.SizeOfRawData;
     }
+  }
 
+  for (MCAssembler::const_iterator i = Asm.begin(),
+                                   e = Asm.end();
+                                   i != e; i++) {
+    COFFSection *Sec = SectionMap[&i->getSection()];
+
+    if (Sec->Number == -1)
+      continue;
+
+    uint64_t Pad = getPaddingSize(i, Layout);
     if (Sec->Relocations.size() > 0) {
       Sec->Header.NumberOfRelocations = Sec->Relocations.size();
       Sec->Header.PointerToRelocations = offset;
@@ -811,7 +929,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
     AuxSymbol &Aux = Sec->Symbol->Aux[0];
     assert(Aux.AuxType == ATSectionDefinition &&
            "Section's symbol's aux symbol must be a Section Definition!");
-    Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
+    Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData - Pad;
     Aux.Aux.SectionDefinition.NumberOfRelocations =
                                                 Sec->Header.NumberOfRelocations;
     Aux.Aux.SectionDefinition.NumberOfLinenumbers =
@@ -820,7 +938,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
 
   Header.PointerToSymbolTable = offset;
 
-  Header.TimeDateStamp = sys::TimeValue::now().toEpochTime();
+  Header.TimeDateStamp = 0;
 
   // Write it all to disk...
   WriteFileHeader(Header);
@@ -845,7 +963,19 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
                "Section::PointerToRawData is insane!");
 
         Asm.WriteSectionData(j, Layout);
+        uint64_t Pad = getPaddingSize(j, Layout);
+        uint8_t Byte = (*i)->MCData->getSection().UseCodeAlign() ? 0x90 : 0;
+        for (unsigned int i = 0; i < Pad; ++i)
+          Write8(Byte);
       }
+    }
+
+    for (i = Sections.begin(), ie = Sections.end(),
+         j = Asm.begin(), je = Asm.end();
+         (i != ie) && (j != je); ++i, ++j) {
+
+      if ((*i)->Number == -1)
+        continue;
 
       if ((*i)->Relocations.size() > 0) {
         assert(OS.tell() == (*i)->Header.PointerToRelocations &&
