1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __LUME_COFF__
16 #define __LUME_COFF__
17 #include <cstdint>
18 #define IMAGE_FILE_MACHINE_UNKNOWN 0
19 #define IMAGE_FILE_MACHINE_I386 0x014c
20 #define IMAGE_FILE_MACHINE_AMD64 0x8664
21 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
22 #define IMAGE_SCN_ALIGN_4BYTES 0x300000
23 #define IMAGE_SCN_MEM_READ 0x40000000
24 #define IMAGE_SYM_TYPE_CHAR 0x2
25 #define IMAGE_SYM_DTYPE_ARRAY 0x3
26 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
27 #pragma pack(push, 4)
28 typedef struct _IMAGE_FILE_HEADER {
29     uint16_t Machine;
30     uint16_t NumberOfSections;
31     uint32_t TimeDateStamp;
32     uint32_t PointerToSymbolTable;
33     uint32_t NumberOfSymbols;
34     uint16_t SizeOfOptionalHeader;
35     uint16_t Characteristics;
36 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
37 
38 typedef struct _IMAGE_SECTION_HEADER {
39     uint8_t Name[8];
40     union {
41         uint32_t PhysicalAddress;
42         uint32_t VirtualSize;
43     } Misc;
44     uint32_t VirtualAddress;
45     uint32_t SizeOfRawData;
46     uint32_t PointerToRawData;
47     uint32_t PointerToRelocations;
48     uint32_t PointerToLinenumbers;
49     uint16_t NumberOfRelocations;
50     uint16_t NumberOfLinenumbers;
51     uint32_t Characteristics;
52 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
53 #pragma pack(pop)
54 #pragma pack(push, 2)
55 typedef struct _IMAGE_SYMBOL {
56     union {
57         uint8_t ShortName[8];
58         struct {
59             uint32_t Short;
60             uint32_t Long;
61         } Name;
62         uint32_t LongName[2];
63     } N;
64 
65     uint32_t Value;
66     int16_t SectionNumber;
67     uint16_t Type;
68     uint8_t StorageClass;
69     uint8_t NumberOfAuxSymbols;
70 } IMAGE_SYMBOL;
71 #pragma pack(pop)
72 #endif