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