Header.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Header.h -------------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_GSYM_HEADER_H
  14. #define LLVM_DEBUGINFO_GSYM_HEADER_H
  15. #include "llvm/Support/Error.h"
  16. #include <cstddef>
  17. #include <cstdint>
  18. namespace llvm {
  19. class raw_ostream;
  20. class DataExtractor;
  21. namespace gsym {
  22. class FileWriter;
  23. constexpr uint32_t GSYM_MAGIC = 0x4753594d; // 'GSYM'
  24. constexpr uint32_t GSYM_CIGAM = 0x4d595347; // 'MYSG'
  25. constexpr uint32_t GSYM_VERSION = 1;
  26. constexpr size_t GSYM_MAX_UUID_SIZE = 20;
  27. /// The GSYM header.
  28. ///
  29. /// The GSYM header is found at the start of a stand alone GSYM file, or as
  30. /// the first bytes in a section when GSYM is contained in a section of an
  31. /// executable file (ELF, mach-o, COFF).
  32. ///
  33. /// The structure is encoded exactly as it appears in the structure definition
  34. /// with no gaps between members. Alignment should not change from system to
  35. /// system as the members were laid out so that they shouldn't align
  36. /// differently on different architectures.
  37. ///
  38. /// When endianness of the system loading a GSYM file matches, the file can
  39. /// be mmap'ed in and a pointer to the header can be cast to the first bytes
  40. /// of the file (stand alone GSYM file) or section data (GSYM in a section).
  41. /// When endianness is swapped, the Header::decode() function should be used to
  42. /// decode the header.
  43. struct Header {
  44. /// The magic bytes should be set to GSYM_MAGIC. This helps detect if a file
  45. /// is a GSYM file by scanning the first 4 bytes of a file or section.
  46. /// This value might appear byte swapped
  47. uint32_t Magic;
  48. /// The version can number determines how the header is decoded and how each
  49. /// InfoType in FunctionInfo is encoded/decoded. As version numbers increase,
  50. /// "Magic" and "Version" members should always appear at offset zero and 4
  51. /// respectively to ensure clients figure out if they can parse the format.
  52. uint16_t Version;
  53. /// The size in bytes of each address offset in the address offsets table.
  54. uint8_t AddrOffSize;
  55. /// The size in bytes of the UUID encoded in the "UUID" member.
  56. uint8_t UUIDSize;
  57. /// The 64 bit base address that all address offsets in the address offsets
  58. /// table are relative to. Storing a full 64 bit address allows our address
  59. /// offsets table to be smaller on disk.
  60. uint64_t BaseAddress;
  61. /// The number of addresses stored in the address offsets table.
  62. uint32_t NumAddresses;
  63. /// The file relative offset of the start of the string table for strings
  64. /// contained in the GSYM file. If the GSYM in contained in a stand alone
  65. /// file this will be the file offset of the start of the string table. If
  66. /// the GSYM is contained in a section within an executable file, this can
  67. /// be the offset of the first string used in the GSYM file and can possibly
  68. /// span one or more executable string tables. This allows the strings to
  69. /// share string tables in an ELF or mach-o file.
  70. uint32_t StrtabOffset;
  71. /// The size in bytes of the string table. For a stand alone GSYM file, this
  72. /// will be the exact size in bytes of the string table. When the GSYM data
  73. /// is in a section within an executable file, this size can span one or more
  74. /// sections that contains strings. This allows any strings that are already
  75. /// stored in the executable file to be re-used, and any extra strings could
  76. /// be added to another string table and the string table offset and size
  77. /// can be set to span all needed string tables.
  78. uint32_t StrtabSize;
  79. /// The UUID of the original executable file. This is stored to allow
  80. /// matching a GSYM file to an executable file when symbolication is
  81. /// required. Only the first "UUIDSize" bytes of the UUID are valid. Any
  82. /// bytes in the UUID value that appear after the first UUIDSize bytes should
  83. /// be set to zero.
  84. uint8_t UUID[GSYM_MAX_UUID_SIZE];
  85. /// Check if a header is valid and return an error if anything is wrong.
  86. ///
  87. /// This function can be used prior to encoding a header to ensure it is
  88. /// valid, or after decoding a header to ensure it is valid and supported.
  89. ///
  90. /// Check a correctly byte swapped header for errors:
  91. /// - check magic value
  92. /// - check that version number is supported
  93. /// - check that the address offset size is supported
  94. /// - check that the UUID size is valid
  95. ///
  96. /// \returns An error if anything is wrong in the header, or Error::success()
  97. /// if there are no errors.
  98. llvm::Error checkForError() const;
  99. /// Decode an object from a binary data stream.
  100. ///
  101. /// \param Data The binary stream to read the data from. This object must
  102. /// have the data for the object starting at offset zero. The data
  103. /// can contain more data than needed.
  104. ///
  105. /// \returns A Header or an error describing the issue that was
  106. /// encountered during decoding.
  107. static llvm::Expected<Header> decode(DataExtractor &Data);
  108. /// Encode this object into FileWriter stream.
  109. ///
  110. /// \param O The binary stream to write the data to at the current file
  111. /// position.
  112. ///
  113. /// \returns An error object that indicates success or failure of the
  114. /// encoding process.
  115. llvm::Error encode(FileWriter &O) const;
  116. };
  117. bool operator==(const Header &LHS, const Header &RHS);
  118. raw_ostream &operator<<(raw_ostream &OS, const llvm::gsym::Header &H);
  119. } // namespace gsym
  120. } // namespace llvm
  121. #endif // #ifndef LLVM_DEBUGINFO_GSYM_HEADER_H
  122. #ifdef __GNUC__
  123. #pragma GCC diagnostic pop
  124. #endif