Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:42

0001 //===- Header.h -------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_GSYM_HEADER_H
0010 #define LLVM_DEBUGINFO_GSYM_HEADER_H
0011 
0012 #include "llvm/Support/Error.h"
0013 
0014 #include <cstddef>
0015 #include <cstdint>
0016 
0017 namespace llvm {
0018 class raw_ostream;
0019 class DataExtractor;
0020 
0021 namespace gsym {
0022 class FileWriter;
0023 
0024 constexpr uint32_t GSYM_MAGIC = 0x4753594d; // 'GSYM'
0025 constexpr uint32_t GSYM_CIGAM = 0x4d595347; // 'MYSG'
0026 constexpr uint32_t GSYM_VERSION = 1;
0027 constexpr size_t GSYM_MAX_UUID_SIZE = 20;
0028 
0029 /// The GSYM header.
0030 ///
0031 /// The GSYM header is found at the start of a stand alone GSYM file, or as
0032 /// the first bytes in a section when GSYM is contained in a section of an
0033 /// executable file (ELF, mach-o, COFF).
0034 ///
0035 /// The structure is encoded exactly as it appears in the structure definition
0036 /// with no gaps between members. Alignment should not change from system to
0037 /// system as the members were laid out so that they shouldn't align
0038 /// differently on different architectures.
0039 ///
0040 /// When endianness of the system loading a GSYM file matches, the file can
0041 /// be mmap'ed in and a pointer to the header can be cast to the first bytes
0042 /// of the file (stand alone GSYM file) or section data (GSYM in a section).
0043 /// When endianness is swapped, the Header::decode() function should be used to
0044 /// decode the header.
0045 struct Header {
0046   /// The magic bytes should be set to GSYM_MAGIC. This helps detect if a file
0047   /// is a GSYM file by scanning the first 4 bytes of a file or section.
0048   /// This value might appear byte swapped
0049   uint32_t Magic;
0050   /// The version can number determines how the header is decoded and how each
0051   /// InfoType in FunctionInfo is encoded/decoded. As version numbers increase,
0052   /// "Magic" and "Version" members should always appear at offset zero and 4
0053   /// respectively to ensure clients figure out if they can parse the format.
0054   uint16_t Version;
0055   /// The size in bytes of each address offset in the address offsets table.
0056   uint8_t AddrOffSize;
0057   /// The size in bytes of the UUID encoded in the "UUID" member.
0058   uint8_t UUIDSize;
0059   /// The 64 bit base address that all address offsets in the address offsets
0060   /// table are relative to. Storing a full 64 bit address allows our address
0061   /// offsets table to be smaller on disk.
0062   uint64_t BaseAddress;
0063   /// The number of addresses stored in the address offsets table.
0064   uint32_t NumAddresses;
0065   /// The file relative offset of the start of the string table for strings
0066   /// contained in the GSYM file. If the GSYM in contained in a stand alone
0067   /// file this will be the file offset of the start of the string table. If
0068   /// the GSYM is contained in a section within an executable file, this can
0069   /// be the offset of the first string used in the GSYM file and can possibly
0070   /// span one or more executable string tables. This allows the strings to
0071   /// share string tables in an ELF or mach-o file.
0072   uint32_t StrtabOffset;
0073   /// The size in bytes of the string table. For a stand alone GSYM file, this
0074   /// will be the exact size in bytes of the string table. When the GSYM data
0075   /// is in a section within an executable file, this size can span one or more
0076   /// sections that contains strings. This allows any strings that are already
0077   /// stored in the executable file to be re-used, and any extra strings could
0078   /// be added to another string table and the string table offset and size
0079   /// can be set to span all needed string tables.
0080   uint32_t StrtabSize;
0081   /// The UUID of the original executable file. This is stored to allow
0082   /// matching a GSYM file to an executable file when symbolication is
0083   /// required. Only the first "UUIDSize" bytes of the UUID are valid. Any
0084   /// bytes in the UUID value that appear after the first UUIDSize bytes should
0085   /// be set to zero.
0086   uint8_t UUID[GSYM_MAX_UUID_SIZE];
0087 
0088   /// Check if a header is valid and return an error if anything is wrong.
0089   ///
0090   /// This function can be used prior to encoding a header to ensure it is
0091   /// valid, or after decoding a header to ensure it is valid and supported.
0092   ///
0093   /// Check a correctly byte swapped header for errors:
0094   ///   - check magic value
0095   ///   - check that version number is supported
0096   ///   - check that the address offset size is supported
0097   ///   - check that the UUID size is valid
0098   ///
0099   /// \returns An error if anything is wrong in the header, or Error::success()
0100   /// if there are no errors.
0101   llvm::Error checkForError() const;
0102 
0103   /// Decode an object from a binary data stream.
0104   ///
0105   /// \param Data The binary stream to read the data from. This object must
0106   /// have the data for the object starting at offset zero. The data
0107   /// can contain more data than needed.
0108   ///
0109   /// \returns A Header or an error describing the issue that was
0110   /// encountered during decoding.
0111   static llvm::Expected<Header> decode(DataExtractor &Data);
0112 
0113   /// Encode this object into FileWriter stream.
0114   ///
0115   /// \param O The binary stream to write the data to at the current file
0116   /// position.
0117   ///
0118   /// \returns An error object that indicates success or failure of the
0119   /// encoding process.
0120   llvm::Error encode(FileWriter &O) const;
0121 };
0122 
0123 bool operator==(const Header &LHS, const Header &RHS);
0124 raw_ostream &operator<<(raw_ostream &OS, const llvm::gsym::Header &H);
0125 
0126 } // namespace gsym
0127 } // namespace llvm
0128 
0129 #endif // LLVM_DEBUGINFO_GSYM_HEADER_H