1 | /* $Id: elf32.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ELF 32-bit header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef IPRT_INCLUDED_formats_elf32_h
|
---|
38 | #define IPRT_INCLUDED_formats_elf32_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <iprt/assertcompile.h>
|
---|
44 | #include "elf-common.h"
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * ELF 32 standard types.
|
---|
48 | */
|
---|
49 | typedef uint32_t Elf32_Addr;
|
---|
50 | typedef uint16_t Elf32_Half;
|
---|
51 | typedef uint32_t Elf32_Off;
|
---|
52 | typedef int32_t Elf32_Sword;
|
---|
53 | typedef uint32_t Elf32_Word;
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * Ensure type size correctness in accordance to .
|
---|
57 | * Portable Format Specification (for ELF), Version 1.1, fig 1-2. .
|
---|
58 | */
|
---|
59 | AssertCompileSize(Elf32_Addr, 4);
|
---|
60 | AssertCompileSize(Elf32_Half, 2);
|
---|
61 | AssertCompileSize(Elf32_Off, 4);
|
---|
62 | AssertCompileSize(Elf32_Sword, 4);
|
---|
63 | AssertCompileSize(Elf32_Word, 4);
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * ELF 32 non-standard types for convenience.
|
---|
67 | */
|
---|
68 | typedef Elf32_Word Elf32_Size;
|
---|
69 | typedef Elf32_Word Elf32_Hashelt;
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * ELF header.
|
---|
73 | */
|
---|
74 | typedef struct
|
---|
75 | {
|
---|
76 | unsigned char e_ident[16]; /* ELF identification. */
|
---|
77 | Elf32_Half e_type; /* Object file type. */
|
---|
78 | Elf32_Half e_machine; /* Machine type. */
|
---|
79 | Elf32_Word e_version; /* Object file version. */
|
---|
80 | Elf32_Addr e_entry; /* Entry point address. */
|
---|
81 | Elf32_Off e_phoff; /* Program header offset. */
|
---|
82 | Elf32_Off e_shoff; /* Section header offset. */
|
---|
83 | Elf32_Word e_flags; /* Processor-specific flags. */
|
---|
84 | Elf32_Half e_ehsize; /* ELF header size. */
|
---|
85 | Elf32_Half e_phentsize; /* Size of program header entries. */
|
---|
86 | Elf32_Half e_phnum; /* Number of program headers. */
|
---|
87 | Elf32_Half e_shentsize; /* Size of section header entries. */
|
---|
88 | Elf32_Half e_shnum; /* Number of section headers. */
|
---|
89 | Elf32_Half e_shstrndx; /* Section name string table index. */
|
---|
90 | } Elf32_Ehdr;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Section header.
|
---|
94 | */
|
---|
95 | typedef struct
|
---|
96 | {
|
---|
97 | Elf32_Word sh_name; /* Section name. */
|
---|
98 | Elf32_Word sh_type; /* Section type. */
|
---|
99 | Elf32_Word sh_flags; /* Section attributes. */
|
---|
100 | Elf32_Addr sh_addr; /* Virtual address in memory. */
|
---|
101 | Elf32_Off sh_offset; /* Offset in file. */
|
---|
102 | Elf32_Word sh_size; /* Size of section. */
|
---|
103 | Elf32_Word sh_link; /* Link to other section. */
|
---|
104 | Elf32_Word sh_info; /* Miscellaneous information. */
|
---|
105 | Elf32_Word sh_addralign; /* Address alignment boundary. */
|
---|
106 | Elf32_Word sh_entsize; /* Size of entries, if section has table. */
|
---|
107 | } Elf32_Shdr;
|
---|
108 |
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Program header.
|
---|
112 | */
|
---|
113 | typedef struct
|
---|
114 | {
|
---|
115 | Elf32_Word p_type; /* Type of segment. */
|
---|
116 | Elf32_Off p_offset; /* Offset in file. */
|
---|
117 | Elf32_Addr p_vaddr; /* Virtual address in memory. */
|
---|
118 | Elf32_Addr p_paddr; /* Physical address (reserved). */
|
---|
119 | Elf32_Word p_filesz; /* Size of segment in file. */
|
---|
120 | Elf32_Word p_memsz; /* Size of segment in memory. */
|
---|
121 | Elf32_Word p_flags; /* Segment attributes. */
|
---|
122 | Elf32_Word p_align; /* Alignment of segment. */
|
---|
123 | } Elf32_Phdr;
|
---|
124 |
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Note header.
|
---|
128 | */
|
---|
129 | typedef struct
|
---|
130 | {
|
---|
131 | Elf32_Word n_namesz; /* Length of note's name. */
|
---|
132 | Elf32_Word n_descsz; /* Length of note's description. */
|
---|
133 | Elf32_Word n_type; /* Type of note. */
|
---|
134 | } Elf32_Nhdr;
|
---|
135 |
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Symbol table entry.
|
---|
139 | */
|
---|
140 | typedef struct
|
---|
141 | {
|
---|
142 | Elf32_Word st_name; /* Symbol name. */
|
---|
143 | Elf32_Addr st_value; /* Symbol value. */
|
---|
144 | Elf32_Word st_size; /* Size associated with symbol. */
|
---|
145 | unsigned char st_info; /* Type and binding attributes. */
|
---|
146 | unsigned char st_other; /* Reserved. */
|
---|
147 | Elf32_Half st_shndx; /* Section header table index. */
|
---|
148 | } Elf32_Sym;
|
---|
149 |
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Relocations.
|
---|
153 | */
|
---|
154 | typedef struct
|
---|
155 | {
|
---|
156 | Elf32_Addr r_offset; /* Location to be relocated. */
|
---|
157 | Elf32_Word r_info; /* Symbol index and type of relocation. */
|
---|
158 | } Elf32_Rel;
|
---|
159 |
|
---|
160 | typedef struct
|
---|
161 | {
|
---|
162 | Elf32_Addr r_offset; /* Location to be relocated. */
|
---|
163 | Elf32_Word r_info; /* Symbol index and type of relocation. */
|
---|
164 | Elf32_Sword r_addend; /* Constant part of expression. */
|
---|
165 | } Elf32_Rela;
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Dynamic section entry.
|
---|
169 | * ".dynamic" section contains an array of this.
|
---|
170 | */
|
---|
171 | typedef struct
|
---|
172 | {
|
---|
173 | Elf32_Sword d_tag; /* Type of entry. */
|
---|
174 | union
|
---|
175 | {
|
---|
176 | Elf32_Word d_val; /* Integer value. */
|
---|
177 | Elf32_Addr d_ptr; /* Virtual address value. */
|
---|
178 | } d_un;
|
---|
179 | } Elf32_Dyn;
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Helper macros.
|
---|
183 | */
|
---|
184 | /** The symbol's type. */
|
---|
185 | #define ELF32_ST_TYPE(info) ((info) & 0xF)
|
---|
186 | /** The symbol's binding. */
|
---|
187 | #define ELF32_ST_BIND(info) ((info) >> 4)
|
---|
188 | /** Make st_info. given binding and type. */
|
---|
189 | #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
---|
190 |
|
---|
191 | /** Relocation type. */
|
---|
192 | #define ELF32_R_TYPE(info) ((unsigned char)(info))
|
---|
193 | /** Relocation symbol index. */
|
---|
194 | #define ELF32_R_SYM(info) ((info) >> 8)
|
---|
195 | /** Make r_info given the symbol index and type. */
|
---|
196 | #define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type))
|
---|
197 |
|
---|
198 |
|
---|
199 | #endif /* !IPRT_INCLUDED_formats_elf32_h */
|
---|
200 |
|
---|