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