|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use object::{write, Object, ObjectSection, RelocationTarget, SectionKind, SymbolKind}; |
| 6 | +use slog::{info, warn}; |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::error::Error; |
| 9 | +use std::fmt; |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct NoRewriteError; |
| 13 | + |
| 14 | +impl fmt::Display for NoRewriteError { |
| 15 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 16 | + write!(f, "no object rewriting was performed") |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl Error for NoRewriteError { |
| 21 | + fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 22 | + // Generic error, underlying cause isn't tracked. |
| 23 | + None |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Rename object syn PyInit_foo to PyInit_<full_name> to avoid clashes |
| 28 | +pub fn rename_init( |
| 29 | + logger: &slog::Logger, |
| 30 | + name: &String, |
| 31 | + object_data: &[u8], |
| 32 | +) -> Result<Vec<u8>, NoRewriteError> { |
| 33 | + let mut rewritten = false; |
| 34 | + |
| 35 | + let name_prefix = name.split('.').next().unwrap(); |
| 36 | + |
| 37 | + let in_object = match object::File::parse(object_data) { |
| 38 | + Ok(object) => object, |
| 39 | + Err(err) => { |
| 40 | + let magic = [ |
| 41 | + object_data[0], |
| 42 | + object_data[1], |
| 43 | + object_data[2], |
| 44 | + object_data[3], |
| 45 | + ]; |
| 46 | + warn!( |
| 47 | + logger, |
| 48 | + "Failed to parse compiled object for {} (magic {:x?}): {}", name, magic, err |
| 49 | + ); |
| 50 | + return Err(NoRewriteError); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + let mut out_object = write::Object::new(in_object.format(), in_object.architecture()); |
| 55 | + |
| 56 | + let mut out_sections = HashMap::new(); |
| 57 | + for in_section in in_object.sections() { |
| 58 | + if in_section.kind() == SectionKind::Metadata { |
| 59 | + continue; |
| 60 | + } |
| 61 | + let section_id = out_object.add_section( |
| 62 | + in_section.segment_name().unwrap_or("").as_bytes().to_vec(), |
| 63 | + in_section.name().unwrap_or("").as_bytes().to_vec(), |
| 64 | + in_section.kind(), |
| 65 | + ); |
| 66 | + let out_section = out_object.section_mut(section_id); |
| 67 | + if out_section.is_bss() { |
| 68 | + out_section.append_bss(in_section.size(), in_section.align()); |
| 69 | + } else { |
| 70 | + out_section.set_data(in_section.uncompressed_data().into(), in_section.align()); |
| 71 | + } |
| 72 | + out_sections.insert(in_section.index(), section_id); |
| 73 | + } |
| 74 | + |
| 75 | + let mut out_symbols = HashMap::new(); |
| 76 | + for (symbol_index, in_symbol) in in_object.symbols() { |
| 77 | + if in_symbol.kind() == SymbolKind::Null { |
| 78 | + continue; |
| 79 | + } |
| 80 | + let (section, value) = match in_symbol.section_index() { |
| 81 | + Some(index) => ( |
| 82 | + Some(*out_sections.get(&index).unwrap()), |
| 83 | + in_symbol.address() - in_object.section_by_index(index).unwrap().address(), |
| 84 | + ), |
| 85 | + None => (None, in_symbol.address()), |
| 86 | + }; |
| 87 | + let in_sym_name = in_symbol.name().unwrap_or(""); |
| 88 | + let sym_name = if !in_sym_name.starts_with("$") |
| 89 | + && in_sym_name.contains("PyInit_") |
| 90 | + && !in_sym_name.contains(name_prefix) |
| 91 | + { |
| 92 | + "PyInit_".to_string() + &name.replace(".", "_") |
| 93 | + } else { |
| 94 | + String::from(in_sym_name) |
| 95 | + }; |
| 96 | + if sym_name != in_sym_name { |
| 97 | + warn!( |
| 98 | + logger, |
| 99 | + "rewrote object symbol name {} to {}", in_sym_name, sym_name, |
| 100 | + ); |
| 101 | + |
| 102 | + rewritten = true; |
| 103 | + } |
| 104 | + |
| 105 | + let out_symbol = write::Symbol { |
| 106 | + name: sym_name.as_bytes().to_vec(), |
| 107 | + value, |
| 108 | + size: in_symbol.size(), |
| 109 | + kind: in_symbol.kind(), |
| 110 | + scope: in_symbol.scope(), |
| 111 | + weak: in_symbol.is_weak(), |
| 112 | + section, |
| 113 | + }; |
| 114 | + let symbol_id = out_object.add_symbol(out_symbol); |
| 115 | + out_symbols.insert(symbol_index, symbol_id); |
| 116 | + } |
| 117 | + |
| 118 | + if !rewritten { |
| 119 | + info!(logger, "no symbol name rewriting occurred for {}", name); |
| 120 | + return Err(NoRewriteError); |
| 121 | + } |
| 122 | + |
| 123 | + for in_section in in_object.sections() { |
| 124 | + if in_section.kind() == SectionKind::Metadata { |
| 125 | + continue; |
| 126 | + } |
| 127 | + let out_section = *out_sections.get(&in_section.index()).unwrap(); |
| 128 | + for (offset, in_relocation) in in_section.relocations() { |
| 129 | + let symbol = match in_relocation.target() { |
| 130 | + RelocationTarget::Symbol(symbol) => *out_symbols.get(&symbol).unwrap(), |
| 131 | + RelocationTarget::Section(section) => { |
| 132 | + out_object.section_symbol(*out_sections.get(§ion).unwrap()) |
| 133 | + } |
| 134 | + }; |
| 135 | + let out_relocation = write::Relocation { |
| 136 | + offset, |
| 137 | + size: in_relocation.size(), |
| 138 | + kind: in_relocation.kind(), |
| 139 | + encoding: in_relocation.encoding(), |
| 140 | + symbol, |
| 141 | + addend: in_relocation.addend(), |
| 142 | + }; |
| 143 | + out_object |
| 144 | + .add_relocation(out_section, out_relocation) |
| 145 | + .unwrap(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + Ok(out_object.write().unwrap()) |
| 150 | +} |
0 commit comments