| 1 | //===- FileWriter.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/DebugInfo/GSYM/FileWriter.h" |
| 10 | #include "llvm/Support/LEB128.h" |
| 11 | #include "llvm/Support/raw_ostream.h" |
| 12 | #include <cassert> |
| 13 | |
| 14 | using namespace llvm; |
| 15 | using namespace gsym; |
| 16 | |
| 17 | FileWriter::~FileWriter() { OS.flush(); } |
| 18 | |
| 19 | void FileWriter::writeSLEB(int64_t S) { |
| 20 | uint8_t Bytes[32]; |
| 21 | auto Length = encodeSLEB128(Value: S, p: Bytes); |
| 22 | assert(Length < sizeof(Bytes)); |
| 23 | OS.write(Ptr: reinterpret_cast<const char *>(Bytes), Size: Length); |
| 24 | } |
| 25 | |
| 26 | void FileWriter::writeULEB(uint64_t U) { |
| 27 | uint8_t Bytes[32]; |
| 28 | auto Length = encodeULEB128(Value: U, p: Bytes); |
| 29 | assert(Length < sizeof(Bytes)); |
| 30 | OS.write(Ptr: reinterpret_cast<const char *>(Bytes), Size: Length); |
| 31 | } |
| 32 | |
| 33 | void FileWriter::writeU8(uint8_t U) { |
| 34 | OS.write(Ptr: reinterpret_cast<const char *>(&U), Size: sizeof(U)); |
| 35 | } |
| 36 | |
| 37 | void FileWriter::writeU16(uint16_t U) { |
| 38 | const uint16_t Swapped = support::endian::byte_swap(value: U, endian: ByteOrder); |
| 39 | OS.write(Ptr: reinterpret_cast<const char *>(&Swapped), Size: sizeof(Swapped)); |
| 40 | } |
| 41 | |
| 42 | void FileWriter::writeU32(uint32_t U) { |
| 43 | const uint32_t Swapped = support::endian::byte_swap(value: U, endian: ByteOrder); |
| 44 | OS.write(Ptr: reinterpret_cast<const char *>(&Swapped), Size: sizeof(Swapped)); |
| 45 | } |
| 46 | |
| 47 | void FileWriter::writeU64(uint64_t U) { |
| 48 | const uint64_t Swapped = support::endian::byte_swap(value: U, endian: ByteOrder); |
| 49 | OS.write(Ptr: reinterpret_cast<const char *>(&Swapped), Size: sizeof(Swapped)); |
| 50 | } |
| 51 | |
| 52 | void FileWriter::writeUnsigned(uint64_t Value, size_t ByteSize) { |
| 53 | assert(ByteSize <= 8 && "invalid byte size" ); |
| 54 | // Make sure the value fits in the number of bytes specified. |
| 55 | assert((ByteSize == 8 || (Value & (uint64_t)-1 << (8 * ByteSize)) == 0) && |
| 56 | "potential data loss: higher bits are non-zero" ); |
| 57 | // Swap and shift bytes if endianness doesn't match. |
| 58 | if (ByteOrder != llvm::endianness::native) |
| 59 | Value = sys::getSwappedBytes(C: Value) >> (8 * (8 - ByteSize)); |
| 60 | // Write from the least significant bytes of Value regardless of host |
| 61 | // endianness. |
| 62 | OS.write(Ptr: reinterpret_cast<const char *>(&Value) + |
| 63 | (sys::IsLittleEndianHost ? 0 : 8 - ByteSize), |
| 64 | Size: ByteSize); |
| 65 | } |
| 66 | |
| 67 | void FileWriter::fixup32(uint32_t U, uint64_t Offset) { |
| 68 | const uint32_t Swapped = support::endian::byte_swap(value: U, endian: ByteOrder); |
| 69 | OS.pwrite(Ptr: reinterpret_cast<const char *>(&Swapped), Size: sizeof(Swapped), |
| 70 | Offset); |
| 71 | } |
| 72 | |
| 73 | void FileWriter::writeData(llvm::ArrayRef<uint8_t> Data) { |
| 74 | OS.write(Ptr: reinterpret_cast<const char *>(Data.data()), Size: Data.size()); |
| 75 | } |
| 76 | |
| 77 | void FileWriter::writeNullTerminated(llvm::StringRef Str) { |
| 78 | OS << Str << '\0'; |
| 79 | } |
| 80 | |
| 81 | uint64_t FileWriter::tell() { |
| 82 | return OS.tell(); |
| 83 | } |
| 84 | |
| 85 | void FileWriter::alignTo(size_t Align) { |
| 86 | off_t Offset = OS.tell(); |
| 87 | off_t AlignedOffset = (Offset + Align - 1) / Align * Align; |
| 88 | if (AlignedOffset == Offset) |
| 89 | return; |
| 90 | off_t PadCount = AlignedOffset - Offset; |
| 91 | OS.write_zeros(NumZeros: PadCount); |
| 92 | } |
| 93 | |