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::fixup32(uint32_t U, uint64_t Offset) { |
53 | const uint32_t Swapped = support::endian::byte_swap(value: U, endian: ByteOrder); |
54 | OS.pwrite(Ptr: reinterpret_cast<const char *>(&Swapped), Size: sizeof(Swapped), |
55 | Offset); |
56 | } |
57 | |
58 | void FileWriter::writeData(llvm::ArrayRef<uint8_t> Data) { |
59 | OS.write(Ptr: reinterpret_cast<const char *>(Data.data()), Size: Data.size()); |
60 | } |
61 | |
62 | void FileWriter::writeNullTerminated(llvm::StringRef Str) { |
63 | OS << Str << '\0'; |
64 | } |
65 | |
66 | uint64_t FileWriter::tell() { |
67 | return OS.tell(); |
68 | } |
69 | |
70 | void FileWriter::alignTo(size_t Align) { |
71 | off_t Offset = OS.tell(); |
72 | off_t AlignedOffset = (Offset + Align - 1) / Align * Align; |
73 | if (AlignedOffset == Offset) |
74 | return; |
75 | off_t PadCount = AlignedOffset - Offset; |
76 | OS.write_zeros(NumZeros: PadCount); |
77 | } |
78 | |