|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use object::Object; |
| 4 | +use uuid::Uuid; |
| 5 | + |
| 6 | +/// An enum carrying an identifier for a binary. This is stores the same information |
| 7 | +/// as a [`debugid::CodeId`], but without projecting it down to a string. |
| 8 | +/// |
| 9 | +/// All types need to be treated rather differently, see their respective documentation. |
| 10 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 11 | +pub enum CodeId { |
| 12 | + /// The code ID for a Windows PE file. When combined with the binary name, |
| 13 | + /// the code ID lets you obtain binaries from symbol servers. It is not useful |
| 14 | + /// on its own, it has to be paired with the binary name. |
| 15 | + /// |
| 16 | + /// On Windows, a binary's code ID is distinct from its debug ID (= pdb GUID + age). |
| 17 | + /// If you have a binary file, you can get both the code ID and the debug ID |
| 18 | + /// from it. If you only have a PDB file, you usually *cannot* get the code ID of |
| 19 | + /// the corresponding binary from it. |
| 20 | + PeCodeId(PeCodeId), |
| 21 | + |
| 22 | + /// The code ID for a macOS / iOS binary (mach-O). This is just the mach-O UUID. |
| 23 | + /// The mach-O UUID is shared between both the binary file and the debug file (dSYM), |
| 24 | + /// and it can be used on its own to find dSYMs using Spotlight. |
| 25 | + /// |
| 26 | + /// The debug ID and the code ID contain the same information; the debug ID |
| 27 | + /// is literally just the UUID plus a zero at the end. |
| 28 | + MachoUuid(Uuid), |
| 29 | + |
| 30 | + /// The code ID for a Linux ELF file. This is the "ELF build ID" (also called "GNU build ID"). |
| 31 | + /// The build ID is usually 20 bytes, commonly written out as 40 hex chars. |
| 32 | + /// |
| 33 | + /// It can be used to find debug files on the local file system or to download |
| 34 | + /// binaries or debug files from a `debuginfod` symbol server. it does not have to be |
| 35 | + /// paired with the binary name. |
| 36 | + /// |
| 37 | + /// An ELF binary's code ID is more useful than its debug ID: The debug ID is truncated |
| 38 | + /// to 16 bytes (32 hex characters), whereas the code ID is the full ELF build ID. |
| 39 | + ElfBuildId(ElfBuildId), |
| 40 | +} |
| 41 | + |
| 42 | +impl FromStr for CodeId { |
| 43 | + type Err = (); |
| 44 | + |
| 45 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 46 | + if s.len() <= 17 { |
| 47 | + // 8 bytes timestamp + 1 to 8 bytes of image size |
| 48 | + Ok(CodeId::PeCodeId(PeCodeId::from_str(s)?)) |
| 49 | + } else if s.len() == 32 && is_uppercase_hex(s) { |
| 50 | + // mach-O UUID |
| 51 | + Ok(CodeId::MachoUuid(Uuid::from_str(s).map_err(|_| ())?)) |
| 52 | + } else { |
| 53 | + // ELF build ID. These are usually 40 hex characters (= 20 bytes). |
| 54 | + Ok(CodeId::ElfBuildId(ElfBuildId::from_str(s)?)) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn is_uppercase_hex(s: &str) -> bool { |
| 60 | + s.chars() |
| 61 | + .all(|c| c.is_ascii_hexdigit() && (c.is_ascii_digit() || c.is_ascii_uppercase())) |
| 62 | +} |
| 63 | + |
| 64 | +impl std::fmt::Display for CodeId { |
| 65 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 66 | + match self { |
| 67 | + CodeId::PeCodeId(pe) => std::fmt::Display::fmt(pe, f), |
| 68 | + CodeId::MachoUuid(uuid) => f.write_fmt(format_args!("{:X}", uuid.simple())), |
| 69 | + CodeId::ElfBuildId(elf) => std::fmt::Display::fmt(elf, f), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// The code ID for a Windows PE file. |
| 75 | +/// |
| 76 | +/// When combined with the binary name, the `PeCodeId` lets you obtain binaries from |
| 77 | +/// symbol servers. It is not useful on its own, it has to be paired with the binary name. |
| 78 | +/// |
| 79 | +/// A Windows binary's `PeCodeId` is distinct from its debug ID (= pdb GUID + age). |
| 80 | +/// If you have a binary file, you can get both the `PeCodeId` and the debug ID |
| 81 | +/// from it. If you only have a PDB file, you usually *cannot* get the `PeCodeId` of |
| 82 | +/// the corresponding binary from it. |
| 83 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 84 | +pub struct PeCodeId { |
| 85 | + pub timestamp: u32, |
| 86 | + pub image_size: u32, |
| 87 | +} |
| 88 | + |
| 89 | +impl FromStr for PeCodeId { |
| 90 | + type Err = (); |
| 91 | + |
| 92 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 93 | + if s.len() < 9 || s.len() > 16 { |
| 94 | + return Err(()); |
| 95 | + } |
| 96 | + let timestamp = u32::from_str_radix(&s[..8], 16).map_err(|_| ())?; |
| 97 | + let image_size = u32::from_str_radix(&s[8..], 16).map_err(|_| ())?; |
| 98 | + Ok(Self { |
| 99 | + timestamp, |
| 100 | + image_size, |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl std::fmt::Display for PeCodeId { |
| 106 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 107 | + f.write_fmt(format_args!("{:08X}{:x}", self.timestamp, self.image_size)) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// The build ID for an ELF file (also called "GNU build ID"). |
| 112 | +/// |
| 113 | +/// The build ID can be used to find debug files on the local file system or to download |
| 114 | +/// binaries or debug files from a `debuginfod` symbol server. it does not have to be |
| 115 | +/// paired with the binary name. |
| 116 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 117 | +pub struct ElfBuildId(pub Vec<u8>); |
| 118 | + |
| 119 | +impl ElfBuildId { |
| 120 | + /// Create a new `ElfBuildId` from a slice of bytes (commonly a sha1 hash |
| 121 | + /// generated by the linker, i.e. 20 bytes). |
| 122 | + pub fn from_bytes(bytes: &[u8]) -> Self { |
| 123 | + Self(bytes.to_owned()) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl FromStr for ElfBuildId { |
| 128 | + type Err = (); |
| 129 | + |
| 130 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 131 | + let byte_count = s.len() / 2; |
| 132 | + let mut bytes = Vec::with_capacity(byte_count); |
| 133 | + for i in 0..byte_count { |
| 134 | + let hex_byte = &s[i * 2..i * 2 + 2]; |
| 135 | + let b = u8::from_str_radix(hex_byte, 16).map_err(|_| ())?; |
| 136 | + bytes.push(b); |
| 137 | + } |
| 138 | + Ok(Self(bytes)) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl std::fmt::Display for ElfBuildId { |
| 143 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 144 | + for byte in &self.0 { |
| 145 | + f.write_fmt(format_args!("{byte:02x}"))?; |
| 146 | + } |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/// Tries to obtain a CodeId for an object. |
| 152 | +/// |
| 153 | +/// This currently only handles mach-O and ELF. |
| 154 | +pub fn code_id_for_object<'data>(obj: &impl Object<'data>) -> Option<CodeId> { |
| 155 | + // ELF |
| 156 | + if let Ok(Some(build_id)) = obj.build_id() { |
| 157 | + return Some(CodeId::ElfBuildId(ElfBuildId::from_bytes(build_id))); |
| 158 | + } |
| 159 | + |
| 160 | + // mach-O |
| 161 | + if let Ok(Some(uuid)) = obj.mach_uuid() { |
| 162 | + return Some(CodeId::MachoUuid(Uuid::from_bytes(uuid))); |
| 163 | + } |
| 164 | + |
| 165 | + None |
| 166 | +} |
0 commit comments