Subzero: Add basic ELFObjectWriter (text section, symtab, strtab, headers).
Able to write out the ELF file header w/ a text section,
a symbol table, and string table. Write text buffer
directly to file after translating each CFG.
This means that the header is written out early w/ fake
data and then we seek back and write the real header
at the very end.
Does not yet handle relocations, data, rodata, constant
pools, bss, or -ffunction-sections, more than 64K sections
or more than 2^24 symbols.
Numbers w/ current NOASSERT=1 build on 176.gcc:
w/out -elf-writer:
0.233771 (21.1%): [ 1287] emit
28MB .s file
w/ -elf-writer:
0.051056 ( 5.6%): [ 1287] emit
2.4MB .o file
BUG=none
R=stichnot@chromium.org
Review URL: https://codereview.chromium.org/678533005
diff --git a/src/IceELFStreamer.h b/src/IceELFStreamer.h
new file mode 100644
index 0000000..ce70e86
--- /dev/null
+++ b/src/IceELFStreamer.h
@@ -0,0 +1,84 @@
+//===- subzero/src/IceELFStreamer.h - Low level ELF writing -----*- C++ -*-===//
+//
+// The Subzero Code Generator
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Interface for serializing bits for common ELF types (words, extended words,
+// etc.), based on the ELF Class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUBZERO_SRC_ICEELFSTREAMER_H
+#define SUBZERO_SRC_ICEELFSTREAMER_H
+
+#include "IceDefs.h"
+
+namespace Ice {
+
+// Low level writer that can that can handle ELFCLASS32/64.
+// Little endian only for now.
+class ELFStreamer {
+public:
+ explicit ELFStreamer(Fdstream &Out) : Out(Out) {}
+
+ void write8(uint8_t Value) { Out << char(Value); }
+
+ void writeLE16(uint16_t Value) {
+ write8(uint8_t(Value));
+ write8(uint8_t(Value >> 8));
+ }
+
+ void writeLE32(uint32_t Value) {
+ writeLE16(uint16_t(Value));
+ writeLE16(uint16_t(Value >> 16));
+ }
+
+ void writeLE64(uint64_t Value) {
+ writeLE32(uint32_t(Value));
+ writeLE32(uint32_t(Value >> 32));
+ }
+
+ template <bool IsELF64, typename T> void writeAddrOrOffset(T Value) {
+ if (IsELF64)
+ writeLE64(Value);
+ else
+ writeLE32(Value);
+ }
+
+ template <bool IsELF64, typename T> void writeELFWord(T Value) {
+ writeLE32(Value);
+ }
+
+ template <bool IsELF64, typename T> void writeELFXword(T Value) {
+ if (IsELF64)
+ writeLE64(Value);
+ else
+ writeLE32(Value);
+ }
+
+ void writeBytes(llvm::StringRef Bytes) { Out << Bytes; }
+
+ void writeZeroPadding(SizeT N) {
+ static const char Zeros[16] = {0};
+
+ for (SizeT i = 0, e = N / 16; i != e; ++i)
+ Out << llvm::StringRef(Zeros, 16);
+
+ Out << llvm::StringRef(Zeros, N % 16);
+ }
+
+ uint64_t tell() const { return Out.tell(); }
+
+ void seek(uint64_t Off) { Out.seek(Off); }
+
+private:
+ Fdstream &Out;
+};
+
+} // end of namespace Ice
+
+#endif // SUBZERO_SRC_ICEELFSTREAMER_H