Write out global initializers and data rel directly to ELF file.

The local symbol relocations are a bit different from
llvm-mc, which are section-relative. E.g., instead "bytes",
it will be ".data + offsetof(bytes, .data)". So the
contents of the text/data/rodata sections can also differ
since the offsets written in place are different.

Still need to fill the symbol table with undefined
symbols (e.g., memset, and szrt lib functions) before
trying to link.

BUG=none
R=kschimpf@google.com, stichnot@chromium.org

Review URL: https://codereview.chromium.org/874353006
diff --git a/src/IceELFSection.cpp b/src/IceELFSection.cpp
index 5dcc206..f6fb526 100644
--- a/src/IceELFSection.cpp
+++ b/src/IceELFSection.cpp
@@ -11,6 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/MathExtras.h"
+
 #include "IceDefs.h"
 #include "IceELFSection.h"
 #include "IceELFStreamer.h"
@@ -35,6 +37,32 @@
   Header.sh_size += MoreData.size();
 }
 
+void ELFDataSection::appendZeros(ELFStreamer &Str, SizeT NumBytes) {
+  Str.writeZeroPadding(NumBytes);
+  Header.sh_size += NumBytes;
+}
+
+void ELFDataSection::appendRelocationOffset(ELFStreamer &Str, bool IsRela,
+                                            RelocOffsetT RelocOffset) {
+  if (IsRela) {
+    appendZeros(Str, RelocAddrSize);
+    return;
+  }
+  static_assert(RelocAddrSize == 4, " writeLE32 assumes RelocAddrSize is 4");
+  Str.writeLE32(RelocOffset);
+  Header.sh_size += RelocAddrSize;
+}
+
+void ELFDataSection::padToAlignment(ELFStreamer &Str, Elf64_Xword Align) {
+  assert(llvm::isPowerOf2_32(Align));
+  Elf64_Xword AlignDiff = Utils::OffsetToAlignment(Header.sh_size, Align);
+  if (AlignDiff == 0)
+    return;
+  if (Header.sh_type != llvm::ELF::SHT_NOBITS)
+    Str.writeZeroPadding(AlignDiff);
+  Header.sh_size += AlignDiff;
+}
+
 // Relocation sections.
 
 void ELFRelocationSection::addRelocations(RelocOffsetT BaseOff,
@@ -73,7 +101,7 @@
   NewSymbol.Section = Section;
   NewSymbol.Number = ELFSym::UnknownNumber;
   bool Unique;
-  if (Type == STB_LOCAL)
+  if (Binding == STB_LOCAL)
     Unique = LocalSymbols.insert(std::make_pair(Name, NewSymbol)).second;
   else
     Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second;