|
| 1 | +//! A `Comdat` helps resolve linker errors for duplicate sections. |
| 2 | +// https://llvm.org/doxygen/IR_2Comdat_8h_source.html |
| 3 | +// https://stackoverflow.com/questions/1834597/what-is-the-comdat-section-used-for |
| 4 | + |
| 5 | +use llvm_sys::comdat::{LLVMComdatSelectionKind, LLVMSetComdatSelectionKind, LLVMGetComdatSelectionKind}; |
| 6 | +use llvm_sys::prelude::LLVMComdatRef; |
| 7 | + |
| 8 | +enum_rename!{ |
| 9 | + /// Determines how linker conflicts are to be resolved. |
| 10 | + ComdatSelectionKind <=> LLVMComdatSelectionKind { |
| 11 | + /// The linker may choose any COMDAT. |
| 12 | + Any <=> LLVMAnyComdatSelectionKind, |
| 13 | + /// The data referenced by the COMDAT must be the same. |
| 14 | + ExactMatch <=> LLVMExactMatchComdatSelectionKind, |
| 15 | + /// The linker will choose the largest COMDAT. |
| 16 | + Largest <=> LLVMLargestComdatSelectionKind, |
| 17 | + /// No other Module may specify this COMDAT. |
| 18 | + NoDuplicates <=> LLVMNoDuplicatesComdatSelectionKind, |
| 19 | + /// The data referenced by the COMDAT must be the same size. |
| 20 | + SameSize <=> LLVMSameSizeComdatSelectionKind, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// A `Comdat` determines how to resolve duplicate sections when linking. |
| 25 | +#[derive(Debug, PartialEq, Copy, Clone)] |
| 26 | +pub struct Comdat(pub(crate) LLVMComdatRef); |
| 27 | + |
| 28 | +impl Comdat { |
| 29 | + pub(crate) fn new(comdat: LLVMComdatRef) -> Self { |
| 30 | + debug_assert!(!comdat.is_null()); |
| 31 | + |
| 32 | + Comdat(comdat) |
| 33 | + } |
| 34 | + |
| 35 | + /// Gets what kind of `Comdat` this is. |
| 36 | + pub fn get_selection_kind(&self) -> ComdatSelectionKind { |
| 37 | + let kind_ptr = unsafe { |
| 38 | + LLVMGetComdatSelectionKind(self.0) |
| 39 | + }; |
| 40 | + |
| 41 | + ComdatSelectionKind::new(kind_ptr) |
| 42 | + } |
| 43 | + |
| 44 | + /// Sets what kind of `Comdat` this should be. |
| 45 | + pub fn set_selection_kind(&self, kind: ComdatSelectionKind) { |
| 46 | + unsafe { |
| 47 | + LLVMSetComdatSelectionKind(self.0, kind.as_llvm_enum()) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments