1 //===- FileWriter.cpp -------------------------------------------*- C++ -*-===// 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/DebugInfo/GSYM/FileWriter.h" 11 #include "llvm/Support/LEB128.h" 12 #include "llvm/Support/raw_ostream.h" 13 #include <cassert> 14 15 using namespace llvm; 16 using namespace gsym; 17 18 FileWriter::~FileWriter() { OS.flush(); } 19 20 void FileWriter::writeSLEB(int64_t S) { 21 uint8_t Bytes[32]; 22 auto Length = encodeSLEB128(S, Bytes); 23 assert(Length < sizeof(Bytes)); 24 OS.write(reinterpret_cast<const char *>(Bytes), Length); 25 } 26 27 void FileWriter::writeULEB(uint64_t U) { 28 uint8_t Bytes[32]; 29 auto Length = encodeULEB128(U, Bytes); 30 assert(Length < sizeof(Bytes)); 31 OS.write(reinterpret_cast<const char *>(Bytes), Length); 32 } 33 34 void FileWriter::writeU8(uint8_t U) { 35 OS.write(reinterpret_cast<const char *>(&U), sizeof(U)); 36 } 37 38 void FileWriter::writeU16(uint16_t U) { 39 const uint16_t Swapped = support::endian::byte_swap(U, ByteOrder); 40 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped)); 41 } 42 43 void FileWriter::writeU32(uint32_t U) { 44 const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder); 45 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped)); 46 } 47 48 void FileWriter::writeU64(uint64_t U) { 49 const uint64_t Swapped = support::endian::byte_swap(U, ByteOrder); 50 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped)); 51 } 52 53 void FileWriter::fixup32(uint32_t U, uint64_t Offset) { 54 const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder); 55 OS.pwrite(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped), 56 Offset); 57 } 58 59 void FileWriter::writeData(llvm::ArrayRef<uint8_t> Data) { 60 OS.write(reinterpret_cast<const char *>(Data.data()), Data.size()); 61 } 62 63 void FileWriter::writeNullTerminated(llvm::StringRef Str) { 64 OS << Str << '\0'; 65 } 66 67 uint64_t FileWriter::tell() { 68 return OS.tell(); 69 } 70 71 void FileWriter::alignTo(size_t Align) { 72 off_t Offset = OS.tell(); 73 off_t AlignedOffset = (Offset + Align - 1) / Align * Align; 74 if (AlignedOffset == Offset) 75 return; 76 off_t PadCount = AlignedOffset - Offset; 77 OS.write_zeros(PadCount); 78 } 79