]> git.proxmox.com Git - rustc.git/blob - vendor/object-0.26.2/src/common.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / object-0.26.2 / src / common.rs
1 /// A CPU architecture.
2 #[allow(missing_docs)]
3 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4 #[non_exhaustive]
5 pub enum Architecture {
6 Unknown,
7 Aarch64,
8 Arm,
9 Avr,
10 Bpf,
11 I386,
12 X86_64,
13 #[allow(non_camel_case_types)]
14 X86_64_X32,
15 Hexagon,
16 Mips,
17 Mips64,
18 Msp430,
19 PowerPc,
20 PowerPc64,
21 Riscv32,
22 Riscv64,
23 S390x,
24 Sparc64,
25 Wasm32,
26 }
27
28 impl Architecture {
29 /// The size of an address value for this architecture.
30 ///
31 /// Returns `None` for unknown architectures.
32 pub fn address_size(self) -> Option<AddressSize> {
33 match self {
34 Architecture::Unknown => None,
35 Architecture::Aarch64 => Some(AddressSize::U64),
36 Architecture::Arm => Some(AddressSize::U32),
37 Architecture::Avr => Some(AddressSize::U8),
38 Architecture::Bpf => Some(AddressSize::U64),
39 Architecture::I386 => Some(AddressSize::U32),
40 Architecture::X86_64 => Some(AddressSize::U64),
41 Architecture::X86_64_X32 => Some(AddressSize::U32),
42 Architecture::Hexagon => Some(AddressSize::U32),
43 Architecture::Mips => Some(AddressSize::U32),
44 Architecture::Mips64 => Some(AddressSize::U64),
45 Architecture::Msp430 => Some(AddressSize::U16),
46 Architecture::PowerPc => Some(AddressSize::U32),
47 Architecture::PowerPc64 => Some(AddressSize::U64),
48 Architecture::Riscv32 => Some(AddressSize::U32),
49 Architecture::Riscv64 => Some(AddressSize::U64),
50 Architecture::S390x => Some(AddressSize::U64),
51 Architecture::Sparc64 => Some(AddressSize::U64),
52 Architecture::Wasm32 => Some(AddressSize::U32),
53 }
54 }
55 }
56
57 /// The size of an address value for an architecture.
58 ///
59 /// This may differ from the address size supported by the file format (such as for COFF).
60 #[allow(missing_docs)]
61 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
62 #[non_exhaustive]
63 #[repr(u8)]
64 pub enum AddressSize {
65 U8 = 1,
66 U16 = 2,
67 U32 = 4,
68 U64 = 8,
69 }
70
71 impl AddressSize {
72 /// The size in bytes of an address value.
73 #[inline]
74 pub fn bytes(self) -> u8 {
75 self as u8
76 }
77 }
78
79 /// A binary file format.
80 #[allow(missing_docs)]
81 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82 #[non_exhaustive]
83 pub enum BinaryFormat {
84 Coff,
85 Elf,
86 MachO,
87 Pe,
88 Wasm,
89 }
90
91 /// The kind of a section.
92 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
93 #[non_exhaustive]
94 pub enum SectionKind {
95 /// The section kind is unknown.
96 Unknown,
97 /// An executable code section.
98 ///
99 /// Example ELF sections: `.text`
100 ///
101 /// Example Mach-O sections: `__TEXT/__text`
102 Text,
103 /// A data section.
104 ///
105 /// Example ELF sections: `.data`
106 ///
107 /// Example Mach-O sections: `__DATA/__data`
108 Data,
109 /// A read only data section.
110 ///
111 /// Example ELF sections: `.rodata`
112 ///
113 /// Example Mach-O sections: `__TEXT/__const`, `__DATA/__const`, `__TEXT/__literal4`
114 ReadOnlyData,
115 /// A loadable string section.
116 ///
117 /// Example ELF sections: `.rodata.str`
118 ///
119 /// Example Mach-O sections: `__TEXT/__cstring`
120 ReadOnlyString,
121 /// An uninitialized data section.
122 ///
123 /// Example ELF sections: `.bss`
124 ///
125 /// Example Mach-O sections: `__DATA/__bss`
126 UninitializedData,
127 /// An uninitialized common data section.
128 ///
129 /// Example Mach-O sections: `__DATA/__common`
130 Common,
131 /// A TLS data section.
132 ///
133 /// Example ELF sections: `.tdata`
134 ///
135 /// Example Mach-O sections: `__DATA/__thread_data`
136 Tls,
137 /// An uninitialized TLS data section.
138 ///
139 /// Example ELF sections: `.tbss`
140 ///
141 /// Example Mach-O sections: `__DATA/__thread_bss`
142 UninitializedTls,
143 /// A TLS variables section.
144 ///
145 /// This contains TLS variable structures, rather than the variable initializers.
146 ///
147 /// Example Mach-O sections: `__DATA/__thread_vars`
148 TlsVariables,
149 /// A non-loadable string section.
150 ///
151 /// Example ELF sections: `.comment`, `.debug_str`
152 OtherString,
153 /// Some other non-loadable section.
154 ///
155 /// Example ELF sections: `.debug_info`
156 Other,
157 /// Debug information.
158 ///
159 /// Example Mach-O sections: `__DWARF/__debug_info`
160 Debug,
161 /// Information for the linker.
162 ///
163 /// Example COFF sections: `.drectve`
164 Linker,
165 /// ELF note section.
166 Note,
167 /// Metadata such as symbols or relocations.
168 ///
169 /// Example ELF sections: `.symtab`, `.strtab`, `.group`
170 Metadata,
171 /// Some other ELF section type.
172 ///
173 /// This is the `sh_type` field in the section header.
174 /// The meaning may be dependent on the architecture.
175 Elf(u32),
176 }
177
178 impl SectionKind {
179 /// Return true if this section contains zerofill data.
180 pub fn is_bss(self) -> bool {
181 self == SectionKind::UninitializedData
182 || self == SectionKind::UninitializedTls
183 || self == SectionKind::Common
184 }
185 }
186
187 /// The selection kind for a COMDAT section group.
188 ///
189 /// This determines the way in which the linker resolves multiple definitions of the COMDAT
190 /// sections.
191 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
192 #[non_exhaustive]
193 pub enum ComdatKind {
194 /// The selection kind is unknown.
195 Unknown,
196 /// Multiple definitions are allowed.
197 ///
198 /// An arbitrary definition is selected, and the rest are removed.
199 ///
200 /// This is the only supported selection kind for ELF.
201 Any,
202 /// Multiple definitions are not allowed.
203 ///
204 /// This is used to group sections without allowing duplicates.
205 NoDuplicates,
206 /// Multiple definitions must have the same size.
207 ///
208 /// An arbitrary definition is selected, and the rest are removed.
209 SameSize,
210 /// Multiple definitions must match exactly.
211 ///
212 /// An arbitrary definition is selected, and the rest are removed.
213 ExactMatch,
214 /// Multiple definitions are allowed, and the largest is selected.
215 ///
216 /// An arbitrary definition with the largest size is selected, and the rest are removed.
217 Largest,
218 /// Multiple definitions are allowed, and the newest is selected.
219 Newest,
220 }
221
222 /// The kind of a symbol.
223 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
224 #[non_exhaustive]
225 pub enum SymbolKind {
226 /// The symbol kind is unknown.
227 Unknown,
228 /// The symbol is a null placeholder.
229 Null,
230 /// The symbol is for executable code.
231 Text,
232 /// The symbol is for a data object.
233 Data,
234 /// The symbol is for a section.
235 Section,
236 /// The symbol is the name of a file. It precedes symbols within that file.
237 File,
238 /// The symbol is for a code label.
239 Label,
240 /// The symbol is for a thread local storage entity.
241 Tls,
242 }
243
244 /// A symbol scope.
245 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
246 pub enum SymbolScope {
247 /// Unknown scope.
248 Unknown,
249 /// Symbol is visible to the compilation unit.
250 Compilation,
251 /// Symbol is visible to the static linkage unit.
252 Linkage,
253 /// Symbol is visible to dynamically linked objects.
254 Dynamic,
255 }
256
257 /// The operation used to calculate the result of the relocation.
258 ///
259 /// The relocation descriptions use the following definitions. Note that
260 /// these definitions probably don't match any ELF ABI.
261 ///
262 /// * A - The value of the addend.
263 /// * G - The address of the symbol's entry within the global offset table.
264 /// * L - The address of the symbol's entry within the procedure linkage table.
265 /// * P - The address of the place of the relocation.
266 /// * S - The address of the symbol.
267 /// * GotBase - The address of the global offset table.
268 /// * Image - The base address of the image.
269 /// * Section - The address of the section containing the symbol.
270 ///
271 /// 'XxxRelative' means 'Xxx + A - P'. 'XxxOffset' means 'S + A - Xxx'.
272 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
273 #[non_exhaustive]
274 pub enum RelocationKind {
275 /// S + A
276 Absolute,
277 /// S + A - P
278 Relative,
279 /// G + A - GotBase
280 Got,
281 /// G + A - P
282 GotRelative,
283 /// GotBase + A - P
284 GotBaseRelative,
285 /// S + A - GotBase
286 GotBaseOffset,
287 /// L + A - P
288 PltRelative,
289 /// S + A - Image
290 ImageOffset,
291 /// S + A - Section
292 SectionOffset,
293 /// The index of the section containing the symbol.
294 SectionIndex,
295 /// Some other ELF relocation. The value is dependent on the architecture.
296 Elf(u32),
297 /// Some other Mach-O relocation. The value is dependent on the architecture.
298 MachO {
299 /// The relocation type.
300 value: u8,
301 /// Whether the relocation is relative to the place.
302 relative: bool,
303 },
304 /// Some other COFF relocation. The value is dependent on the architecture.
305 Coff(u16),
306 }
307
308 /// Information about how the result of the relocation operation is encoded in the place.
309 ///
310 /// This is usually architecture specific, such as specifying an addressing mode or
311 /// a specific instruction.
312 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
313 #[non_exhaustive]
314 pub enum RelocationEncoding {
315 /// Generic encoding.
316 Generic,
317
318 /// x86 sign extension at runtime.
319 ///
320 /// Used with `RelocationKind::Absolute`.
321 X86Signed,
322 /// x86 rip-relative addressing.
323 ///
324 /// The `RelocationKind` must be PC relative.
325 X86RipRelative,
326 /// x86 rip-relative addressing in movq instruction.
327 ///
328 /// The `RelocationKind` must be PC relative.
329 X86RipRelativeMovq,
330 /// x86 branch instruction.
331 ///
332 /// The `RelocationKind` must be PC relative.
333 X86Branch,
334
335 /// s390x PC-relative offset shifted right by one bit.
336 ///
337 /// The `RelocationKind` must be PC relative.
338 S390xDbl,
339
340 /// AArch64 call target.
341 ///
342 /// The `RelocationKind` must be PC relative.
343 AArch64Call,
344 }
345
346 /// File flags that are specific to each file format.
347 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
348 #[non_exhaustive]
349 pub enum FileFlags {
350 /// No file flags.
351 None,
352 /// ELF file flags.
353 Elf {
354 /// `e_flags` field in the ELF file header.
355 e_flags: u32,
356 },
357 /// Mach-O file flags.
358 MachO {
359 /// `flags` field in the Mach-O file header.
360 flags: u32,
361 },
362 /// COFF file flags.
363 Coff {
364 /// `Characteristics` field in the COFF file header.
365 characteristics: u16,
366 },
367 }
368
369 /// Section flags that are specific to each file format.
370 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
371 #[non_exhaustive]
372 pub enum SectionFlags {
373 /// No section flags.
374 None,
375 /// ELF section flags.
376 Elf {
377 /// `sh_flags` field in the section header.
378 sh_flags: u64,
379 },
380 /// Mach-O section flags.
381 MachO {
382 /// `flags` field in the section header.
383 flags: u32,
384 },
385 /// COFF section flags.
386 Coff {
387 /// `Characteristics` field in the section header.
388 characteristics: u32,
389 },
390 }
391
392 /// Symbol flags that are specific to each file format.
393 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
394 #[non_exhaustive]
395 pub enum SymbolFlags<Section> {
396 /// No symbol flags.
397 None,
398 /// ELF symbol flags.
399 Elf {
400 /// `st_info` field in the ELF symbol.
401 st_info: u8,
402 /// `st_other` field in the ELF symbol.
403 st_other: u8,
404 },
405 /// Mach-O symbol flags.
406 MachO {
407 /// `n_desc` field in the Mach-O symbol.
408 n_desc: u16,
409 },
410 /// COFF flags for a section symbol.
411 CoffSection {
412 /// `Selection` field in the auxiliary symbol for the section.
413 selection: u8,
414 /// `Number` field in the auxiliary symbol for the section.
415 associative_section: Option<Section>,
416 },
417 }