xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/GSYM/FileWriter.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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 
~FileWriter()17 FileWriter::~FileWriter() { OS.flush(); }
18 
writeSLEB(int64_t S)19 void FileWriter::writeSLEB(int64_t S) {
20   uint8_t Bytes[32];
21   auto Length = encodeSLEB128(S, Bytes);
22   assert(Length < sizeof(Bytes));
23   OS.write(reinterpret_cast<const char *>(Bytes), Length);
24 }
25 
writeULEB(uint64_t U)26 void FileWriter::writeULEB(uint64_t U) {
27   uint8_t Bytes[32];
28   auto Length = encodeULEB128(U, Bytes);
29   assert(Length < sizeof(Bytes));
30   OS.write(reinterpret_cast<const char *>(Bytes), Length);
31 }
32 
writeU8(uint8_t U)33 void FileWriter::writeU8(uint8_t U) {
34   OS.write(reinterpret_cast<const char *>(&U), sizeof(U));
35 }
36 
writeU16(uint16_t U)37 void FileWriter::writeU16(uint16_t U) {
38   const uint16_t Swapped = support::endian::byte_swap(U, ByteOrder);
39   OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
40 }
41 
writeU32(uint32_t U)42 void FileWriter::writeU32(uint32_t U) {
43   const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
44   OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
45 }
46 
writeU64(uint64_t U)47 void FileWriter::writeU64(uint64_t U) {
48   const uint64_t Swapped = support::endian::byte_swap(U, ByteOrder);
49   OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
50 }
51 
fixup32(uint32_t U,uint64_t Offset)52 void FileWriter::fixup32(uint32_t U, uint64_t Offset) {
53   const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
54   OS.pwrite(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped),
55             Offset);
56 }
57 
writeData(llvm::ArrayRef<uint8_t> Data)58 void FileWriter::writeData(llvm::ArrayRef<uint8_t> Data) {
59   OS.write(reinterpret_cast<const char *>(Data.data()), Data.size());
60 }
61 
writeNullTerminated(llvm::StringRef Str)62 void FileWriter::writeNullTerminated(llvm::StringRef Str) {
63   OS << Str << '\0';
64 }
65 
tell()66 uint64_t FileWriter::tell() {
67   return OS.tell();
68 }
69 
alignTo(size_t Align)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(PadCount);
77 }
78