]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/lib.rs
codegen_llvm: Simplify logic for relaxing PIC into PIE
[rust.git] / src / librustc_codegen_ssa / lib.rs
1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
2 #![feature(bool_to_option)]
3 #![feature(box_patterns)]
4 #![feature(try_blocks)]
5 #![feature(in_band_lifetimes)]
6 #![feature(nll)]
7 #![feature(or_patterns)]
8 #![feature(trusted_len)]
9 #![feature(associated_type_bounds)]
10 #![recursion_limit = "256"]
11
12 //! This crate contains codegen code that is used by all codegen backends (LLVM and others).
13 //! The backend-agnostic functions of this crate use functions defined in various traits that
14 //! have to be implemented by each backends.
15
16 #[macro_use]
17 extern crate log;
18 #[macro_use]
19 extern crate rustc_middle;
20
21 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22 use rustc_data_structures::svh::Svh;
23 use rustc_data_structures::sync::Lrc;
24 use rustc_hir::def_id::CrateNum;
25 use rustc_hir::LangItem;
26 use rustc_middle::dep_graph::WorkProduct;
27 use rustc_middle::middle::cstore::{CrateSource, LibSource, NativeLibrary};
28 use rustc_middle::middle::dependency_format::Dependencies;
29 use rustc_middle::ty::query::Providers;
30 use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
31 use rustc_span::symbol::Symbol;
32 use std::path::{Path, PathBuf};
33
34 pub mod back;
35 pub mod base;
36 pub mod common;
37 pub mod debuginfo;
38 pub mod glue;
39 pub mod meth;
40 pub mod mir;
41 pub mod mono_item;
42 pub mod traits;
43
44 pub struct ModuleCodegen<M> {
45     /// The name of the module. When the crate may be saved between
46     /// compilations, incremental compilation requires that name be
47     /// unique amongst **all** crates. Therefore, it should contain
48     /// something unique to this crate (e.g., a module path) as well
49     /// as the crate name and disambiguator.
50     /// We currently generate these names via CodegenUnit::build_cgu_name().
51     pub name: String,
52     pub module_llvm: M,
53     pub kind: ModuleKind,
54 }
55
56 // FIXME(eddyb) maybe include the crate name in this?
57 pub const METADATA_FILENAME: &str = "lib.rmeta";
58 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
59
60 impl<M> ModuleCodegen<M> {
61     pub fn into_compiled_module(
62         self,
63         emit_obj: bool,
64         emit_bc: bool,
65         emit_bc_compressed: bool,
66         outputs: &OutputFilenames,
67     ) -> CompiledModule {
68         let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
69         let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
70         let bytecode_compressed = emit_bc_compressed.then(|| {
71             outputs
72                 .temp_path(OutputType::Bitcode, Some(&self.name))
73                 .with_extension(RLIB_BYTECODE_EXTENSION)
74         });
75
76         CompiledModule {
77             name: self.name.clone(),
78             kind: self.kind,
79             object,
80             bytecode,
81             bytecode_compressed,
82         }
83     }
84 }
85
86 #[derive(Debug, RustcEncodable, RustcDecodable)]
87 pub struct CompiledModule {
88     pub name: String,
89     pub kind: ModuleKind,
90     pub object: Option<PathBuf>,
91     pub bytecode: Option<PathBuf>,
92     pub bytecode_compressed: Option<PathBuf>,
93 }
94
95 pub struct CachedModuleCodegen {
96     pub name: String,
97     pub source: WorkProduct,
98 }
99
100 #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
101 pub enum ModuleKind {
102     Regular,
103     Metadata,
104     Allocator,
105 }
106
107 bitflags::bitflags! {
108     pub struct MemFlags: u8 {
109         const VOLATILE = 1 << 0;
110         const NONTEMPORAL = 1 << 1;
111         const UNALIGNED = 1 << 2;
112     }
113 }
114
115 /// Misc info we load from metadata to persist beyond the tcx.
116 ///
117 /// Note: though `CrateNum` is only meaningful within the same tcx, information within `CrateInfo`
118 /// is self-contained. `CrateNum` can be viewed as a unique identifier within a `CrateInfo`, where
119 /// `used_crate_source` contains all `CrateSource` of the dependents, and maintains a mapping from
120 /// identifiers (`CrateNum`) to `CrateSource`. The other fields map `CrateNum` to the crate's own
121 /// additional properties, so that effectively we can retrieve each dependent crate's `CrateSource`
122 /// and the corresponding properties without referencing information outside of a `CrateInfo`.
123 #[derive(Debug, RustcEncodable, RustcDecodable)]
124 pub struct CrateInfo {
125     pub panic_runtime: Option<CrateNum>,
126     pub compiler_builtins: Option<CrateNum>,
127     pub profiler_runtime: Option<CrateNum>,
128     pub is_no_builtins: FxHashSet<CrateNum>,
129     pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
130     pub crate_name: FxHashMap<CrateNum, String>,
131     pub used_libraries: Lrc<Vec<NativeLibrary>>,
132     pub link_args: Lrc<Vec<String>>,
133     pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
134     pub used_crates_static: Vec<(CrateNum, LibSource)>,
135     pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
136     pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
137     pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
138     pub dependency_formats: Lrc<Dependencies>,
139 }
140
141 #[derive(RustcEncodable, RustcDecodable)]
142 pub struct CodegenResults {
143     pub crate_name: Symbol,
144     pub modules: Vec<CompiledModule>,
145     pub allocator_module: Option<CompiledModule>,
146     pub metadata_module: Option<CompiledModule>,
147     pub crate_hash: Svh,
148     pub metadata: rustc_middle::middle::cstore::EncodedMetadata,
149     pub windows_subsystem: Option<String>,
150     pub linker_info: back::linker::LinkerInfo,
151     pub crate_info: CrateInfo,
152 }
153
154 pub fn provide(providers: &mut Providers<'_>) {
155     crate::back::symbol_export::provide(providers);
156     crate::base::provide_both(providers);
157 }
158
159 pub fn provide_extern(providers: &mut Providers<'_>) {
160     crate::back::symbol_export::provide_extern(providers);
161     crate::base::provide_both(providers);
162 }
163
164 /// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`
165 /// uses for the object files it generates.
166 pub fn looks_like_rust_object_file(filename: &str) -> bool {
167     let path = Path::new(filename);
168     let ext = path.extension().and_then(|s| s.to_str());
169     if ext != Some(OutputType::Object.extension()) {
170         // The file name does not end with ".o", so it can't be an object file.
171         return false;
172     }
173
174     // Strip the ".o" at the end
175     let ext2 = path.file_stem().and_then(|s| Path::new(s).extension()).and_then(|s| s.to_str());
176
177     // Check if the "inner" extension
178     ext2 == Some(RUST_CGU_EXT)
179 }