Like [`Self::from_elf`], but on an already parsed `object`. `path` is only used to locate a separate debug file.
(object: &object::File, path: &Path)
| 99 | /// Like [`Self::from_elf`], but on an already parsed `object`. `path` is |
| 100 | /// only used to locate a separate debug file. |
| 101 | pub fn from_object(object: &object::File, path: &Path) -> anyhow::Result<Self> { |
| 102 | let mut symbols = Self::extract_symbols_from_object(object); |
| 103 | |
| 104 | // Merge symbols from a separate debug file if available |
| 105 | if let Some(debug_path) = debug_file::find_debug_file(object, path) { |
| 106 | trace!("Merging symbols from debug file {debug_path:?} for {path:?}"); |
| 107 | let debug_symbols = std::fs::read(&debug_path).ok().and_then(|c| { |
| 108 | object::File::parse(&*c) |
| 109 | .ok() |
| 110 | .map(|o| Self::extract_symbols_from_object(&o)) |
| 111 | }); |
| 112 | |
| 113 | if let Some(debug_symbols) = debug_symbols { |
| 114 | let existing: HashSet<(u64, String)> = |
| 115 | symbols.iter().map(|s| (s.addr, s.name.clone())).collect(); |
| 116 | symbols.extend( |
| 117 | debug_symbols |
| 118 | .into_iter() |
| 119 | .filter(|s| !existing.contains(&(s.addr, s.name.clone()))), |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Filter out |
| 125 | // - ARM ELF "mapping symbols" (https://github.com/torvalds/linux/blob/9448598b22c50c8a5bb77a9103e2d49f134c9578/tools/perf/util/symbol-elf.c#L1591C1-L1598C4) |
| 126 | // - symbols that have en empty name |
| 127 | symbols.retain(|symbol| { |
| 128 | if symbol.name.is_empty() { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // Reject ARM ELF "mapping symbols" as does perf |
| 133 | let name = symbol.name.as_str(); |
| 134 | if let [b'$', b'a' | b'd' | b't' | b'x', rest @ ..] = name.as_bytes() |
| 135 | && (rest.is_empty() || rest.starts_with(b".")) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | true |
| 141 | }); |
| 142 | |
| 143 | // Update zero-sized symbols to cover the range until the next symbol |
| 144 | // This is what perf does |
| 145 | // https://github.com/torvalds/linux/blob/e538109ac71d801d26776af5f3c54f548296c29c/tools/perf/util/symbol.c#L256 |
| 146 | // A common source for these is inline assembly functions. |
| 147 | symbols.sort_by_key(|symbol| symbol.addr); |
| 148 | for i in 0..symbols.len() { |
| 149 | if symbols[i].size == 0 { |
| 150 | if i + 1 < symbols.len() { |
| 151 | // Set size to the distance to the next symbol |
| 152 | symbols[i].size = symbols[i + 1].addr.saturating_sub(symbols[i].addr); |
| 153 | } else { |
| 154 | // Last symbol: round up to next 4KB page boundary and add 4KiB |
| 155 | // This matches perf's behavior: roundup(curr->start, 4096) + 4096 |
| 156 | const PAGE_SIZE: u64 = 4096; |
| 157 | let addr = symbols[i].addr; |
| 158 | let end_addr = addr.next_multiple_of(PAGE_SIZE) + PAGE_SIZE; |
nothing calls this directly
no test coverage detected