| ... | ... | @@ -0,0 +1,156 @@ |
| 1 | // SPDX-License-Identifier: MIT |
| 2 | // Copyright (c) 2015-2020 Zig Contributors |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. |
| 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | // and substantial portions of the software. |
| 6 | const magic = 0xeb9f; |
| 7 | const version = 1; |
| 8 | |
| 9 | pub const ext = @import("ext.zig"); |
| 10 | |
| 11 | /// All offsets are in bytes relative to the end of this header |
| 12 | pub const Header = packed struct { |
| 13 | magic: u16, |
| 14 | version: u8, |
| 15 | flags: u8, |
| 16 | hdr_len: u32, |
| 17 | |
| 18 | /// offset of type section |
| 19 | type_off: u32, |
| 20 | |
| 21 | /// length of type section |
| 22 | type_len: u32, |
| 23 | |
| 24 | /// offset of string section |
| 25 | str_off: u32, |
| 26 | |
| 27 | /// length of string section |
| 28 | str_len: u32, |
| 29 | }; |
| 30 | |
| 31 | /// Max number of type identifiers |
| 32 | pub const max_type = 0xfffff; |
| 33 | |
| 34 | /// Max offset into string section |
| 35 | pub const max_name_offset = 0xffffff; |
| 36 | |
| 37 | /// Max number of struct/union/enum member of func args |
| 38 | pub const max_vlen = 0xffff; |
| 39 | |
| 40 | pub const Type = packed struct { |
| 41 | name_off: u32, |
| 42 | info: packed struct { |
| 43 | /// number of struct's members |
| 44 | vlen: u16, |
| 45 | |
| 46 | unused_1: u8, |
| 47 | kind: Kind, |
| 48 | unused_2: u3, |
| 49 | |
| 50 | /// used by Struct, Union, and Fwd |
| 51 | kind_flag: bool, |
| 52 | }, |
| 53 | |
| 54 | /// size is used by Int, Enum, Struct, Union, and DataSec, it tells the size |
| 55 | /// of the type it is describing |
| 56 | /// |
| 57 | /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func, |
| 58 | /// FuncProto, and Var. It is a type_id referring to another type |
| 59 | size_type: union { size: u32, typ: u32 }, |
| 60 | }; |
| 61 | |
| 62 | /// For some kinds, Type is immediately followed by extra data |
| 63 | pub const Kind = enum(u4) { |
| 64 | unknown, |
| 65 | int, |
| 66 | ptr, |
| 67 | array, |
| 68 | structure, |
| 69 | kind_union, |
| 70 | enumeration, |
| 71 | fwd, |
| 72 | typedef, |
| 73 | kind_volatile, |
| 74 | constant, |
| 75 | restrict, |
| 76 | func, |
| 77 | funcProto, |
| 78 | variable, |
| 79 | dataSec, |
| 80 | }; |
| 81 | |
| 82 | /// Int kind is followed by this struct |
| 83 | pub const IntInfo = packed struct { |
| 84 | bits: u8, |
| 85 | unused: u8, |
| 86 | offset: u8, |
| 87 | encoding: enum(u4) { |
| 88 | signed = 1 << 0, |
| 89 | char = 1 << 1, |
| 90 | boolean = 1 << 2, |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | test "IntInfo is 32 bits" { |
| 95 | std.testing.expectEqual(@bitSizeOf(IntInfo), 32); |
| 96 | } |
| 97 | |
| 98 | /// Enum kind is followed by this struct |
| 99 | pub const Enum = packed struct { |
| 100 | name_off: u32, |
| 101 | val: i32, |
| 102 | }; |
| 103 | |
| 104 | /// Array kind is followd by this struct |
| 105 | pub const Array = packed struct { |
| 106 | typ: u32, |
| 107 | index_type: u32, |
| 108 | nelems: u32, |
| 109 | }; |
| 110 | |
| 111 | /// Struct and Union kinds are followed by multiple Member structs. The exact |
| 112 | /// number is stored in vlen |
| 113 | pub const Member = packed struct { |
| 114 | name_off: u32, |
| 115 | typ: u32, |
| 116 | |
| 117 | /// if the kind_flag is set, offset contains both member bitfield size and |
| 118 | /// bit offset, the bitfield size is set for bitfield members. If the type |
| 119 | /// info kind_flag is not set, the offset contains only bit offset |
| 120 | offset: packed struct { |
| 121 | bit: u24, |
| 122 | bitfield_size: u8, |
| 123 | }, |
| 124 | }; |
| 125 | |
| 126 | /// FuncProto is followed by multiple Params, the exact number is stored in vlen |
| 127 | pub const Param = packed struct { |
| 128 | name_off: u32, |
| 129 | typ: u32, |
| 130 | }; |
| 131 | |
| 132 | pub const VarLinkage = enum { |
| 133 | static, |
| 134 | global_allocated, |
| 135 | global_extern, |
| 136 | }; |
| 137 | |
| 138 | pub const FuncLinkage = enum { |
| 139 | static, |
| 140 | global, |
| 141 | external, |
| 142 | }; |
| 143 | |
| 144 | /// Var kind is followd by a single Var struct to describe additional |
| 145 | /// information related to the variable such as its linkage |
| 146 | pub const Var = packed struct { |
| 147 | linkage: u32, |
| 148 | }; |
| 149 | |
| 150 | /// Datasec kind is followed by multible VarSecInfo to describe all Var kind |
| 151 | /// types it contains along with it's in-section offset as well as size. |
| 152 | pub const VarSecInfo = packed struct { |
| 153 | typ: u32, |
| 154 | offset: u32, |
| 155 | size: u32, |
| 156 | }; |