classfile.h
1 /* 2 * Bean Java VM 3 * Copyright (C) 2005-2023 Christian Lins <christian@lins.me> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _CLASSFILE_H_ 19 #define _CLASSFILE_H_ 20 21 #include <types.h> 22 23 #define JAVAMAGIC 0xCAFEBABE 24 25 #define CONSTANTPOOL_CLASS 7 26 #define CONSTANTPOOL_FIELDREF 9 27 #define CONSTANTPOOL_METHODREF 10 28 #define CONSTANTPOOL_INTERFACEMETHODREF 11 29 #define CONSTANTPOOL_STRING 8 30 #define CONSTANTPOOL_INTEGER 3 31 #define CONSTANTPOOL_FLOAT 4 32 #define CONSTANTPOOL_LONG 5 33 #define CONSTANTPOOL_DOUBLE 6 34 #define CONSTANTPOOL_NAMEANDTYPE 12 35 #define CONSTANTPOOL_UTF8 1 36 37 struct VERSION { 38 uint16_t Major; 39 uint16_t Minor; 40 }; 41 42 struct CONSTANT_CLASS_INFO { 43 uint16_t NameIndex; 44 }; 45 46 typedef struct CONSTANT_REF_INFO { 47 uint16_t ClassIndex; 48 uint16_t NameAndTypeIndex; 49 } ConstantRefInfo; 50 51 struct CONSTANT_STRING_INFO { 52 uint16_t StringIndex; 53 }; 54 55 struct CONSTANT_FLOAT_INFO { 56 float Value; 57 }; 58 59 struct CONSTANT_INTEGER_INFO { 60 int32_t Value; 61 }; 62 63 struct CONSTANT_LONG_INFO { 64 int64_t Value; 65 }; 66 67 struct CONSTANT_DOUBLE_INFO { 68 double Value; 69 }; 70 71 struct CONSTANT_NAMETYPE_INFO { 72 uint16_t NameIndex; 73 uint16_t DescriptorIndex; 74 }; 75 76 struct CONSTANT_UTF8_INFO { 77 uint16_t Length; 78 char* Text; 79 }; 80 81 typedef struct CONSTANTPOOL { 82 uint8_t Tag; 83 void* Data; /* Pointer to a dynamically allocated structure. */ 84 } ConstantPool; 85 86 /********************* 87 * ATTRIBUTE STRUCTS * 88 *********************/ 89 90 struct ATTRIBUTE_INFO { 91 uint16_t AttributeNameIndex; 92 uint32_t AttributeLength; 93 unsigned char* Info; 94 }; 95 96 struct ATTRIBUTE_INFO_CONSTANTVALUE { 97 uint16_t AttributeNameIndex; 98 uint32_t AttributeLength; 99 uint16_t ConstantValueIndex; 100 }; 101 102 struct EXCEPTIONTABLE_ENTRY { 103 uint16_t StartPC, EndPC; 104 uint16_t HandlerPC; 105 uint16_t CatchType; 106 }; 107 108 struct ATTRIBUTE_INFO_CODE { 109 uint16_t AttributeNameIndex; 110 uint32_t AttributeLength; 111 uint16_t MaxStack; 112 uint16_t MaxLocals; 113 uint32_t CodeLength; 114 unsigned char* Code; 115 uint16_t ExceptionTableLength; 116 struct EXCEPTIONTABLE_ENTRY* ExceptionTable; 117 uint16_t AttributesNum; 118 struct ATTRIBUTE_INFO* Attributes; 119 }; 120 121 struct ATTRIBUTE_INFO_EXCEPTION { 122 uint16_t AttributeNameIndex; 123 uint32_t AttributeLength; 124 uint16_t ExceptionNum; 125 uint16_t* ExceptionIndexTable; 126 }; 127 128 struct ATTRIBUTE_INFO_INNERCLASS { 129 uint16_t AttributeNameIndex; 130 uint32_t AttributeLength; 131 uint16_t InnerClassesNum; 132 /* Inner classes table */ 133 }; 134 135 struct ATTRIBUTE_INFO_SYNTHETIC { 136 uint16_t AttributeNameIndex; 137 uint32_t AttributeLength; /* Must be zero. */ 138 }; 139 140 struct ATTRIBUTE_INFO_SOURCEFILE { 141 uint16_t AttributeNameIndex; 142 uint32_t AttributeLength; 143 uint16_t SourceFileIndex; /* Index in the constant pool table. */ 144 }; 145 146 struct LINENUMBERTABLE_ENTRY { 147 uint16_t StartPC; 148 uint16_t LineNumber; 149 }; 150 151 struct ATTRIBUTE_INFO_LINENUMBERTABLE { 152 uint16_t AttributeNameIndex; 153 uint32_t AttributeLength; 154 uint16_t LineNumberTableLength; 155 struct LINENUMBERTABLE_ENTRY* LineNumberTable; 156 }; 157 158 struct LOCALVARIABLETABLE_ENTRY { 159 uint16_t StartPC; 160 uint16_t Length; 161 uint16_t NameIndex; 162 uint16_t DescriptorIndex; 163 uint16_t Index; 164 }; 165 166 struct ATTRIBUTE_INFO_LOCALVARIABLETABLE { 167 uint16_t AttributeNameIndex; 168 uint32_t AttributeLength; 169 uint16_t LocalVariableTableLength; 170 struct LOCALVARIABLETABLE_ENTRY* LocalVariableTable; 171 }; 172 173 struct ATTRIBUTE_INFO_DEPRECATED { 174 uint16_t AttributeNameIndex; 175 uint32_t AttributeLength; /* Must be zero. */ 176 }; 177 178 /* INFO STRUCTS */ 179 180 typedef struct FIELD_INFO { 181 uint16_t AccessFlags; 182 uint16_t NameIndex; 183 uint16_t DescriptorIndex; 184 uint16_t AttributesNum; 185 struct ATTRIBUTE_INFO *Attributes; 186 } FieldInfo; 187 188 #endif