rustc_codegen_ssa/back/link/
raw_dylib.rs

1use std::fs;
2use std::io::{BufWriter, Write};
3use std::path::{Path, PathBuf};
4
5use rustc_abi::Endian;
6use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN};
7use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
8use rustc_data_structures::stable_hasher::StableHasher;
9use rustc_hashes::Hash128;
10use rustc_hir::attrs::NativeLibKind;
11use rustc_session::Session;
12use rustc_session::cstore::DllImport;
13use rustc_span::Symbol;
14use rustc_target::spec::Arch;
15
16use crate::back::archive::ImportLibraryItem;
17use crate::back::link::ArchiveBuilderBuilder;
18use crate::errors::ErrorCreatingImportLibrary;
19use crate::{NativeLib, common, errors};
20
21/// Extract all symbols defined in raw-dylib libraries, collated by library name.
22///
23/// If we have multiple extern blocks that specify symbols defined in the same raw-dylib library,
24/// then the CodegenResults value contains one NativeLib instance for each block. However, the
25/// linker appears to expect only a single import library for each library used, so we need to
26/// collate the symbols together by library name before generating the import libraries.
27fn collate_raw_dylibs_windows<'a>(
28    sess: &Session,
29    used_libraries: impl IntoIterator<Item = &'a NativeLib>,
30) -> Vec<(String, Vec<DllImport>)> {
31    // Use index maps to preserve original order of imports and libraries.
32    let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
33
34    for lib in used_libraries {
35        if let NativeLibKind::RawDylib { .. } = lib.kind {
36            let ext = if lib.verbatim { "" } else { ".dll" };
37            let name = format!("{}{}", lib.name, ext);
38            let imports = dylib_table.entry(name.clone()).or_default();
39            for import in &lib.dll_imports {
40                if let Some(old_import) = imports.insert(import.name, import) {
41                    // FIXME: when we add support for ordinals, figure out if we need to do anything
42                    // if we have two DllImport values with the same name but different ordinals.
43                    if import.calling_convention != old_import.calling_convention {
44                        sess.dcx().emit_err(errors::MultipleExternalFuncDecl {
45                            span: import.span,
46                            function: import.name,
47                            library_name: &name,
48                        });
49                    }
50                }
51            }
52        }
53    }
54    sess.dcx().abort_if_errors();
55    dylib_table
56        .into_iter()
57        .map(|(name, imports)| {
58            (name, imports.into_iter().map(|(_, import)| import.clone()).collect())
59        })
60        .collect()
61}
62
63pub(super) fn create_raw_dylib_dll_import_libs<'a>(
64    sess: &Session,
65    archive_builder_builder: &dyn ArchiveBuilderBuilder,
66    used_libraries: impl IntoIterator<Item = &'a NativeLib>,
67    tmpdir: &Path,
68    is_direct_dependency: bool,
69) -> Vec<PathBuf> {
70    collate_raw_dylibs_windows(sess, used_libraries)
71        .into_iter()
72        .map(|(raw_dylib_name, raw_dylib_imports)| {
73            let name_suffix = if is_direct_dependency { "_imports" } else { "_imports_indirect" };
74            let output_path = tmpdir.join(format!("{raw_dylib_name}{name_suffix}.lib"));
75
76            let mingw_gnu_toolchain = common::is_mingw_gnu_toolchain(&sess.target);
77
78            let items: Vec<ImportLibraryItem> = raw_dylib_imports
79                .iter()
80                .map(|import: &DllImport| {
81                    if sess.target.arch == Arch::X86 {
82                        ImportLibraryItem {
83                            name: common::i686_decorated_name(
84                                import,
85                                mingw_gnu_toolchain,
86                                false,
87                                false,
88                            ),
89                            ordinal: import.ordinal(),
90                            symbol_name: import.is_missing_decorations().then(|| {
91                                common::i686_decorated_name(
92                                    import,
93                                    mingw_gnu_toolchain,
94                                    false,
95                                    true,
96                                )
97                            }),
98                            is_data: !import.is_fn,
99                        }
100                    } else {
101                        ImportLibraryItem {
102                            name: import.name.to_string(),
103                            ordinal: import.ordinal(),
104                            symbol_name: None,
105                            is_data: !import.is_fn,
106                        }
107                    }
108                })
109                .collect();
110
111            archive_builder_builder.create_dll_import_lib(
112                sess,
113                &raw_dylib_name,
114                items,
115                &output_path,
116            );
117
118            output_path
119        })
120        .collect()
121}
122
123/// Extract all symbols defined in raw-dylib libraries, collated by library name.
124///
125/// If we have multiple extern blocks that specify symbols defined in the same raw-dylib library,
126/// then the CodegenResults value contains one NativeLib instance for each block. However, the
127/// linker appears to expect only a single import library for each library used, so we need to
128/// collate the symbols together by library name before generating the import libraries.
129fn collate_raw_dylibs_elf<'a>(
130    sess: &Session,
131    used_libraries: impl IntoIterator<Item = &'a NativeLib>,
132) -> Vec<(String, Vec<DllImport>, bool)> {
133    // Use index maps to preserve original order of imports and libraries.
134    let mut dylib_table = FxIndexMap::<String, (FxIndexMap<Symbol, &DllImport>, bool)>::default();
135
136    for lib in used_libraries {
137        if let NativeLibKind::RawDylib { as_needed } = lib.kind {
138            let filename = if lib.verbatim {
139                lib.name.as_str().to_owned()
140            } else {
141                let ext = sess.target.dll_suffix.as_ref();
142                let prefix = sess.target.dll_prefix.as_ref();
143                format!("{prefix}{}{ext}", lib.name)
144            };
145
146            let (stub_imports, stub_as_needed) =
147                dylib_table.entry(filename.clone()).or_insert((Default::default(), true));
148            for import in &lib.dll_imports {
149                stub_imports.insert(import.name, import);
150            }
151            *stub_as_needed = *stub_as_needed && as_needed.unwrap_or(true);
152        }
153    }
154    sess.dcx().abort_if_errors();
155    dylib_table
156        .into_iter()
157        .map(|(name, (imports, as_needed))| {
158            (name, imports.into_iter().map(|(_, import)| import.clone()).collect(), as_needed)
159        })
160        .collect()
161}
162
163pub(super) fn create_raw_dylib_elf_stub_shared_objects<'a>(
164    sess: &Session,
165    used_libraries: impl IntoIterator<Item = &'a NativeLib>,
166    raw_dylib_so_dir: &Path,
167) -> Vec<(String, bool)> {
168    collate_raw_dylibs_elf(sess, used_libraries)
169        .into_iter()
170        .map(|(load_filename, raw_dylib_imports, as_needed)| {
171            use std::hash::Hash;
172
173            // `load_filename` is the *target/loader* filename that will end up in NEEDED.
174            // Usually this will be something like `libc.so` or `libc.so.6` but with
175            // verbatim it might also be an absolute path.
176            // To be able to support this properly, we always put this load filename
177            // into the SONAME of the library and link it via a temporary file with a random name.
178            // This also avoids naming conflicts with non-raw-dylib linkage of the same library.
179
180            let shared_object = create_elf_raw_dylib_stub(sess, &load_filename, &raw_dylib_imports);
181
182            let mut file_name_hasher = StableHasher::new();
183            load_filename.hash(&mut file_name_hasher);
184            for raw_dylib in raw_dylib_imports {
185                raw_dylib.name.as_str().hash(&mut file_name_hasher);
186            }
187
188            let library_filename: Hash128 = file_name_hasher.finish();
189            let temporary_lib_name = format!(
190                "{}{}{}",
191                sess.target.dll_prefix,
192                library_filename.as_u128().to_base_fixed_len(CASE_INSENSITIVE),
193                sess.target.dll_suffix
194            );
195            let link_path = raw_dylib_so_dir.join(&temporary_lib_name);
196
197            let file = match fs::File::create_new(&link_path) {
198                Ok(file) => file,
199                Err(error) => sess.dcx().emit_fatal(ErrorCreatingImportLibrary {
200                    lib_name: &load_filename,
201                    error: error.to_string(),
202                }),
203            };
204            if let Err(error) = BufWriter::new(file).write_all(&shared_object) {
205                sess.dcx().emit_fatal(ErrorCreatingImportLibrary {
206                    lib_name: &load_filename,
207                    error: error.to_string(),
208                });
209            };
210
211            (temporary_lib_name, as_needed)
212        })
213        .collect()
214}
215
216/// Create an ELF .so stub file for raw-dylib.
217/// It exports all the provided symbols, but is otherwise empty.
218fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]) -> Vec<u8> {
219    use object::write::elf as write;
220    use object::{AddressSize, Architecture, elf};
221
222    let mut stub_buf = Vec::new();
223
224    // Build the stub ELF using the object crate.
225    // The high-level portable API does not allow for the fine-grained control we need,
226    // so this uses the low-level object::write::elf API.
227    // The low-level API consists of two stages: reservation and writing.
228    // We first reserve space for all the things in the binary and then write them.
229    // It is important that the order of reservation matches the order of writing.
230    // The object crate contains many debug asserts that fire if you get this wrong.
231
232    let Some((arch, sub_arch)) = sess.target.object_architecture(&sess.unstable_target_features)
233    else {
234        sess.dcx().fatal(format!(
235            "raw-dylib is not supported for the architecture `{}`",
236            sess.target.arch
237        ));
238    };
239
240    let endianness = match sess.target.options.endian {
241        Endian::Little => object::Endianness::Little,
242        Endian::Big => object::Endianness::Big,
243    };
244
245    let is_64 = match arch.address_size() {
246        Some(AddressSize::U8 | AddressSize::U16 | AddressSize::U32) => false,
247        Some(AddressSize::U64) => true,
248        _ => sess.dcx().fatal(format!(
249            "raw-dylib is not supported for the architecture `{}`",
250            sess.target.arch
251        )),
252    };
253
254    let mut stub = write::Writer::new(endianness, is_64, &mut stub_buf);
255
256    let mut vers = Vec::new();
257    let mut vers_map = FxHashMap::default();
258    let mut syms = Vec::new();
259
260    for symbol in symbols {
261        let symbol_name = symbol.name.as_str();
262        if let Some((name, version_name)) = symbol_name.split_once('@') {
263            assert!(!version_name.contains('@'));
264            let dynstr = stub.add_dynamic_string(name.as_bytes());
265            let ver = if let Some(&ver_id) = vers_map.get(version_name) {
266                ver_id
267            } else {
268                let id = vers.len();
269                vers_map.insert(version_name, id);
270                let dynstr = stub.add_dynamic_string(version_name.as_bytes());
271                vers.push((version_name, dynstr));
272                id
273            };
274            syms.push((name, dynstr, Some(ver)));
275        } else {
276            let dynstr = stub.add_dynamic_string(symbol_name.as_bytes());
277            syms.push((symbol_name, dynstr, None));
278        }
279    }
280
281    let soname = stub.add_dynamic_string(soname.as_bytes());
282
283    // These initial reservations don't reserve any bytes in the binary yet,
284    // they just allocate in the internal data structures.
285
286    // First, we create the dynamic symbol table. It starts with a null symbol
287    // and then all the symbols and their dynamic strings.
288    stub.reserve_null_dynamic_symbol_index();
289
290    for _ in syms.iter() {
291        stub.reserve_dynamic_symbol_index();
292    }
293
294    // Reserve the sections.
295    // We have the minimal sections for a dynamic SO and .text where we point our dummy symbols to.
296    stub.reserve_shstrtab_section_index();
297    let text_section_name = stub.add_section_name(".text".as_bytes());
298    let text_section = stub.reserve_section_index();
299    stub.reserve_dynsym_section_index();
300    stub.reserve_dynstr_section_index();
301    if !vers.is_empty() {
302        stub.reserve_gnu_versym_section_index();
303        stub.reserve_gnu_verdef_section_index();
304    }
305    stub.reserve_dynamic_section_index();
306
307    // These reservations now determine the actual layout order of the object file.
308    stub.reserve_file_header();
309    stub.reserve_shstrtab();
310    stub.reserve_section_headers();
311    stub.reserve_dynsym();
312    stub.reserve_dynstr();
313    let verdef_count = 1 + vers.len();
314    let mut dynamic_entries = 2; // DT_SONAME, DT_NULL
315    if !vers.is_empty() {
316        stub.reserve_gnu_versym();
317        stub.reserve_gnu_verdef(verdef_count, verdef_count);
318        dynamic_entries += 1; // DT_VERDEFNUM
319    }
320    stub.reserve_dynamic(dynamic_entries);
321
322    // First write the ELF header with the arch information.
323    let e_machine = match (arch, sub_arch) {
324        (Architecture::Aarch64, None) => elf::EM_AARCH64,
325        (Architecture::Aarch64_Ilp32, None) => elf::EM_AARCH64,
326        (Architecture::Arm, None) => elf::EM_ARM,
327        (Architecture::Avr, None) => elf::EM_AVR,
328        (Architecture::Bpf, None) => elf::EM_BPF,
329        (Architecture::Csky, None) => elf::EM_CSKY,
330        (Architecture::E2K32, None) => elf::EM_MCST_ELBRUS,
331        (Architecture::E2K64, None) => elf::EM_MCST_ELBRUS,
332        (Architecture::I386, None) => elf::EM_386,
333        (Architecture::X86_64, None) => elf::EM_X86_64,
334        (Architecture::X86_64_X32, None) => elf::EM_X86_64,
335        (Architecture::Hexagon, None) => elf::EM_HEXAGON,
336        (Architecture::LoongArch32, None) => elf::EM_LOONGARCH,
337        (Architecture::LoongArch64, None) => elf::EM_LOONGARCH,
338        (Architecture::M68k, None) => elf::EM_68K,
339        (Architecture::Mips, None) => elf::EM_MIPS,
340        (Architecture::Mips64, None) => elf::EM_MIPS,
341        (Architecture::Mips64_N32, None) => elf::EM_MIPS,
342        (Architecture::Msp430, None) => elf::EM_MSP430,
343        (Architecture::PowerPc, None) => elf::EM_PPC,
344        (Architecture::PowerPc64, None) => elf::EM_PPC64,
345        (Architecture::Riscv32, None) => elf::EM_RISCV,
346        (Architecture::Riscv64, None) => elf::EM_RISCV,
347        (Architecture::S390x, None) => elf::EM_S390,
348        (Architecture::Sbf, None) => elf::EM_SBF,
349        (Architecture::Sharc, None) => elf::EM_SHARC,
350        (Architecture::Sparc, None) => elf::EM_SPARC,
351        (Architecture::Sparc32Plus, None) => elf::EM_SPARC32PLUS,
352        (Architecture::Sparc64, None) => elf::EM_SPARCV9,
353        (Architecture::Xtensa, None) => elf::EM_XTENSA,
354        _ => {
355            sess.dcx().fatal(format!(
356                "raw-dylib is not supported for the architecture `{}`",
357                sess.target.arch
358            ));
359        }
360    };
361
362    stub.write_file_header(&write::FileHeader {
363        os_abi: crate::back::metadata::elf_os_abi(sess),
364        abi_version: 0,
365        e_type: object::elf::ET_DYN,
366        e_machine,
367        e_entry: 0,
368        e_flags: crate::back::metadata::elf_e_flags(arch, sess),
369    })
370    .unwrap();
371
372    // .shstrtab
373    stub.write_shstrtab();
374
375    // Section headers
376    stub.write_null_section_header();
377    stub.write_shstrtab_section_header();
378    // Create a dummy .text section for our dummy symbols.
379    stub.write_section_header(&write::SectionHeader {
380        name: Some(text_section_name),
381        sh_type: elf::SHT_PROGBITS,
382        sh_flags: 0,
383        sh_addr: 0,
384        sh_offset: 0,
385        sh_size: 0,
386        sh_link: 0,
387        sh_info: 0,
388        sh_addralign: 1,
389        sh_entsize: 0,
390    });
391    stub.write_dynsym_section_header(0, 1);
392    stub.write_dynstr_section_header(0);
393    if !vers.is_empty() {
394        stub.write_gnu_versym_section_header(0);
395        stub.write_gnu_verdef_section_header(0);
396    }
397    stub.write_dynamic_section_header(0);
398
399    // .dynsym
400    stub.write_null_dynamic_symbol();
401    for (_name, dynstr, _ver) in syms.iter().copied() {
402        stub.write_dynamic_symbol(&write::Sym {
403            name: Some(dynstr),
404            st_info: (elf::STB_GLOBAL << 4) | elf::STT_NOTYPE,
405            st_other: elf::STV_DEFAULT,
406            section: Some(text_section),
407            st_shndx: 0, // ignored by object in favor of the `section` field
408            st_value: 0,
409            st_size: 0,
410        });
411    }
412
413    // .dynstr
414    stub.write_dynstr();
415
416    // ld.bfd is unhappy if these sections exist without any symbols, so we only generate them when necessary.
417    if !vers.is_empty() {
418        // .gnu_version
419        stub.write_null_gnu_versym();
420        for (_name, _dynstr, ver) in syms.iter().copied() {
421            stub.write_gnu_versym(if let Some(ver) = ver {
422                assert!((2 + ver as u16) < elf::VERSYM_HIDDEN);
423                elf::VERSYM_HIDDEN | (2 + ver as u16)
424            } else {
425                1
426            });
427        }
428
429        // .gnu_version_d
430        stub.write_align_gnu_verdef();
431        stub.write_gnu_verdef(&write::Verdef {
432            version: elf::VER_DEF_CURRENT,
433            flags: elf::VER_FLG_BASE,
434            index: 1,
435            aux_count: 1,
436            name: soname,
437        });
438        for (ver, (_name, dynstr)) in vers.into_iter().enumerate() {
439            stub.write_gnu_verdef(&write::Verdef {
440                version: elf::VER_DEF_CURRENT,
441                flags: 0,
442                index: 2 + ver as u16,
443                aux_count: 1,
444                name: dynstr,
445            });
446        }
447    }
448
449    // .dynamic
450    // the DT_SONAME will be used by the linker to populate DT_NEEDED
451    // which the loader uses to find the library.
452    stub.write_align_dynamic();
453    stub.write_dynamic_string(elf::DT_SONAME, soname);
454    // LSB section "2.7. Symbol Versioning" requires `DT_VERDEFNUM` to be reliable.
455    if verdef_count > 1 {
456        stub.write_dynamic(elf::DT_VERDEFNUM, verdef_count as u64);
457    }
458    // DT_NULL terminates the .dynamic table.
459    stub.write_dynamic(elf::DT_NULL, 0);
460
461    stub_buf
462}