-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
link.rs
932 lines (819 loc) · 33.1 KB
/
link.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::prelude::*;
use back::rpath;
use driver::session::Session;
use driver::session;
use lib::llvm::llvm;
use lib::llvm::{ModuleRef, mk_pass_manager, mk_target_data, True, False};
use lib;
use metadata::common::LinkMeta;
use metadata::{encoder, csearch, cstore};
use middle::trans::common::CrateContext;
use middle::ty;
use util::ppaux;
use core::char;
use core::hash::Streaming;
use core::hash;
use core::io::{Writer, WriterUtil};
use core::libc::{c_int, c_uint, c_char};
use core::os::consts::{macos, freebsd, linux, android, win32};
use core::os;
use core::ptr;
use core::run;
use core::str;
use core::vec;
use syntax::ast;
use syntax::ast_map::{path, path_mod, path_name};
use syntax::attr;
use syntax::print::pprust;
#[deriving_eq]
pub enum output_type {
output_type_none,
output_type_bitcode,
output_type_assembly,
output_type_llvm_assembly,
output_type_object,
output_type_exe,
}
pub fn llvm_err(sess: Session, +msg: ~str) -> ! {
unsafe {
let cstr = llvm::LLVMRustGetLastError();
if cstr == ptr::null() {
sess.fatal(msg);
} else {
sess.fatal(msg + ~": " + str::raw::from_c_str(cstr));
}
}
}
pub fn WriteOutputFile(sess: Session,
PM: lib::llvm::PassManagerRef, M: ModuleRef,
Triple: *c_char,
// FIXME: When #2334 is fixed, change
// c_uint to FileType
Output: *c_char, FileType: c_uint,
OptLevel: c_int,
EnableSegmentedStacks: bool) {
unsafe {
let result = llvm::LLVMRustWriteOutputFile(
PM,
M,
Triple,
Output,
FileType,
OptLevel,
EnableSegmentedStacks);
if (!result) {
llvm_err(sess, ~"Could not write output");
}
}
}
pub mod jit {
use back::link::llvm_err;
use driver::session::Session;
use lib::llvm::llvm;
use lib::llvm::{ModuleRef, PassManagerRef, mk_target_data};
use metadata::cstore;
use core::cast;
use core::libc::c_int;
use core::ptr;
use core::str;
pub mod rusti {
#[nolink]
#[abi = "rust-intrinsic"]
pub extern {
pub fn morestack_addr() -> *();
}
}
pub struct Closure {
code: *(),
env: *(),
}
pub fn exec(sess: Session,
pm: PassManagerRef,
m: ModuleRef,
opt: c_int,
stacks: bool) {
unsafe {
let manager = llvm::LLVMRustPrepareJIT(rusti::morestack_addr());
// We need to tell JIT where to resolve all linked
// symbols from. The equivalent of -lstd, -lcore, etc.
// By default the JIT will resolve symbols from the std and
// core linked into rustc. We don't want that,
// incase the user wants to use an older std library.
let cstore = sess.cstore;
for cstore::get_used_crate_files(cstore).each |cratepath| {
let path = cratepath.to_str();
debug!("linking: %s", path);
let _: () = str::as_c_str(
path,
|buf_t| {
if !llvm::LLVMRustLoadCrate(manager, buf_t) {
llvm_err(sess, ~"Could not link");
}
debug!("linked: %s", path);
});
}
// The execute function will return a void pointer
// to the _rust_main function. We can do closure
// magic here to turn it straight into a callable rust
// closure. It will also cleanup the memory manager
// for us.
let entry = llvm::LLVMRustExecuteJIT(manager,
pm, m, opt, stacks);
if ptr::is_null(entry) {
llvm_err(sess, ~"Could not JIT");
} else {
let closure = Closure {
code: entry,
env: ptr::null()
};
let func: &fn(++argv: ~[~str]) = cast::transmute(closure);
func(~[/*bad*/copy sess.opts.binary]);
}
}
}
}
pub mod write {
use back::link::jit;
use back::link::{WriteOutputFile, output_type};
use back::link::{output_type_assembly, output_type_bitcode};
use back::link::{output_type_exe, output_type_llvm_assembly};
use back::link::{output_type_object};
use driver::session::Session;
use driver::session;
use lib::llvm::llvm;
use lib::llvm::{False, True, ModuleRef, mk_pass_manager, mk_target_data};
use lib;
use core::libc::{c_char, c_int, c_uint};
use core::path::Path;
use core::str;
pub fn is_object_or_assembly_or_exe(ot: output_type) -> bool {
if ot == output_type_assembly || ot == output_type_object ||
ot == output_type_exe {
return true;
}
return false;
}
pub fn run_passes(sess: Session, llmod: ModuleRef, output: &Path) {
unsafe {
let opts = sess.opts;
if sess.time_llvm_passes() { llvm::LLVMRustEnableTimePasses(); }
let mut pm = mk_pass_manager();
let td = mk_target_data(
/*bad*/copy sess.targ_cfg.target_strs.data_layout);
llvm::LLVMAddTargetData(td.lltd, pm.llpm);
// FIXME (#2812): run the linter here also, once there are llvm-c
// bindings for it.
// Generate a pre-optimization intermediate file if -save-temps
// was specified.
if opts.save_temps {
match opts.output_type {
output_type_bitcode => {
if opts.optimize != session::No {
let filename = output.with_filetype("no-opt.bc");
str::as_c_str(filename.to_str(), |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf)
});
}
}
_ => {
let filename = output.with_filetype("bc");
str::as_c_str(filename.to_str(), |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf)
});
}
}
}
if !sess.no_verify() { llvm::LLVMAddVerifierPass(pm.llpm); }
// FIXME (#2396): This is mostly a copy of the bits of opt's -O2
// that are available in the C api.
// Also: We might want to add optimization levels like -O1, -O2,
// -Os, etc
// Also: Should we expose and use the pass lists used by the opt
// tool?
if opts.optimize != session::No {
let fpm = mk_pass_manager();
llvm::LLVMAddTargetData(td.lltd, fpm.llpm);
let FPMB = llvm::LLVMPassManagerBuilderCreate();
llvm::LLVMPassManagerBuilderSetOptLevel(FPMB, 2u as c_uint);
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(
FPMB, fpm.llpm);
llvm::LLVMPassManagerBuilderDispose(FPMB);
llvm::LLVMRunPassManager(fpm.llpm, llmod);
let mut threshold = 225;
if opts.optimize == session::Aggressive { threshold = 275; }
let MPMB = llvm::LLVMPassManagerBuilderCreate();
llvm::LLVMPassManagerBuilderSetOptLevel(MPMB,
opts.optimize as
c_uint);
llvm::LLVMPassManagerBuilderSetSizeLevel(MPMB, False);
llvm::LLVMPassManagerBuilderSetDisableUnitAtATime(MPMB,
False);
llvm::LLVMPassManagerBuilderSetDisableUnrollLoops(MPMB,
False);
llvm::LLVMPassManagerBuilderSetDisableSimplifyLibCalls(MPMB,
False);
if threshold != 0u {
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold
(MPMB, threshold as c_uint);
}
llvm::LLVMPassManagerBuilderPopulateModulePassManager(
MPMB, pm.llpm);
llvm::LLVMPassManagerBuilderDispose(MPMB);
}
if !sess.no_verify() { llvm::LLVMAddVerifierPass(pm.llpm); }
if is_object_or_assembly_or_exe(opts.output_type) || opts.jit {
let LLVMOptNone = 0 as c_int; // -O0
let LLVMOptLess = 1 as c_int; // -O1
let LLVMOptDefault = 2 as c_int; // -O2, -Os
let LLVMOptAggressive = 3 as c_int; // -O3
let mut CodeGenOptLevel = match opts.optimize {
session::No => LLVMOptNone,
session::Less => LLVMOptLess,
session::Default => LLVMOptDefault,
session::Aggressive => LLVMOptAggressive
};
if opts.jit {
// If we are using JIT, go ahead and create and
// execute the engine now.
// JIT execution takes ownership of the module,
// so don't dispose and return.
jit::exec(sess, pm.llpm, llmod, CodeGenOptLevel, true);
if sess.time_llvm_passes() {
llvm::LLVMRustPrintPassTimings();
}
return;
}
let mut FileType;
if opts.output_type == output_type_object ||
opts.output_type == output_type_exe {
FileType = lib::llvm::ObjectFile;
} else { FileType = lib::llvm::AssemblyFile; }
// Write optimized bitcode if --save-temps was on.
if opts.save_temps {
// Always output the bitcode file with --save-temps
let filename = output.with_filetype("opt.bc");
llvm::LLVMRunPassManager(pm.llpm, llmod);
str::as_c_str(filename.to_str(), |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf)
});
pm = mk_pass_manager();
// Save the assembly file if -S is used
if opts.output_type == output_type_assembly {
let _: () = str::as_c_str(
sess.targ_cfg.target_strs.target_triple,
|buf_t| {
str::as_c_str(output.to_str(), |buf_o| {
WriteOutputFile(
sess,
pm.llpm,
llmod,
buf_t,
buf_o,
lib::llvm::AssemblyFile as c_uint,
CodeGenOptLevel,
true)
})
});
}
// Save the object file for -c or --save-temps alone
// This .o is needed when an exe is built
if opts.output_type == output_type_object ||
opts.output_type == output_type_exe {
let _: () = str::as_c_str(
sess.targ_cfg.target_strs.target_triple,
|buf_t| {
str::as_c_str(output.to_str(), |buf_o| {
WriteOutputFile(
sess,
pm.llpm,
llmod,
buf_t,
buf_o,
lib::llvm::ObjectFile as c_uint,
CodeGenOptLevel,
true)
})
});
}
} else {
// If we aren't saving temps then just output the file
// type corresponding to the '-c' or '-S' flag used
let _: () = str::as_c_str(
sess.targ_cfg.target_strs.target_triple,
|buf_t| {
str::as_c_str(output.to_str(), |buf_o| {
WriteOutputFile(
sess,
pm.llpm,
llmod,
buf_t,
buf_o,
FileType as c_uint,
CodeGenOptLevel,
true)
})
});
}
// Clean up and return
llvm::LLVMDisposeModule(llmod);
if sess.time_llvm_passes() {
llvm::LLVMRustPrintPassTimings();
}
return;
}
if opts.output_type == output_type_llvm_assembly {
// Given options "-S --emit-llvm": output LLVM assembly
str::as_c_str(output.to_str(), |buf_o| {
llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o)});
} else {
// If only a bitcode file is asked for by using the
// '--emit-llvm' flag, then output it here
llvm::LLVMRunPassManager(pm.llpm, llmod);
str::as_c_str(output.to_str(),
|buf| llvm::LLVMWriteBitcodeToFile(llmod, buf) );
}
llvm::LLVMDisposeModule(llmod);
if sess.time_llvm_passes() { llvm::LLVMRustPrintPassTimings(); }
}
}
}
/*
* Name mangling and its relationship to metadata. This is complex. Read
* carefully.
*
* The semantic model of Rust linkage is, broadly, that "there's no global
* namespace" between crates. Our aim is to preserve the illusion of this
* model despite the fact that it's not *quite* possible to implement on
* modern linkers. We initially didn't use system linkers at all, but have
* been convinced of their utility.
*
* There are a few issues to handle:
*
* - Linkers operate on a flat namespace, so we have to flatten names.
* We do this using the C++ namespace-mangling technique. Foo::bar
* symbols and such.
*
* - Symbols with the same name but different types need to get different
* linkage-names. We do this by hashing a string-encoding of the type into
* a fixed-size (currently 16-byte hex) cryptographic hash function (CHF:
* we use SHA1) to "prevent collisions". This is not airtight but 16 hex
* digits on uniform probability means you're going to need 2**32 same-name
* symbols in the same process before you're even hitting birthday-paradox
* collision probability.
*
* - Symbols in different crates but with same names "within" the crate need
* to get different linkage-names.
*
* So here is what we do:
*
* - Separate the meta tags into two sets: exported and local. Only work with
* the exported ones when considering linkage.
*
* - Consider two exported tags as special (and mandatory): name and vers.
* Every crate gets them; if it doesn't name them explicitly we infer them
* as basename(crate) and "0.1", respectively. Call these CNAME, CVERS.
*
* - Define CMETA as all the non-name, non-vers exported meta tags in the
* crate (in sorted order).
*
* - Define CMH as hash(CMETA + hashes of dependent crates).
*
* - Compile our crate to lib CNAME-CMH-CVERS.so
*
* - Define STH(sym) as hash(CNAME, CMH, type_str(sym))
*
* - Suffix a mangled sym with ::STH@CVERS, so that it is unique in the
* name, non-name metadata, and type sense, and versioned in the way
* system linkers understand.
*
*/
pub fn build_link_meta(sess: Session, c: &ast::crate, output: &Path,
symbol_hasher: &hash::State) -> LinkMeta {
struct ProvidedMetas {
name: Option<@str>,
vers: Option<@str>,
cmh_items: ~[@ast::meta_item]
}
fn provided_link_metas(sess: Session, c: &ast::crate) ->
ProvidedMetas {
let mut name = None;
let mut vers = None;
let mut cmh_items = ~[];
let linkage_metas = attr::find_linkage_metas(c.node.attrs);
attr::require_unique_names(sess.diagnostic(), linkage_metas);
for linkage_metas.each |meta| {
if *attr::get_meta_item_name(*meta) == ~"name" {
match attr::get_meta_item_value_str(*meta) {
// Changing attr would avoid the need for the copy
// here
Some(v) => { name = Some(v.to_managed()); }
None => cmh_items.push(*meta)
}
} else if *attr::get_meta_item_name(*meta) == ~"vers" {
match attr::get_meta_item_value_str(*meta) {
Some(v) => { vers = Some(v.to_managed()); }
None => cmh_items.push(*meta)
}
} else { cmh_items.push(*meta); }
}
ProvidedMetas {
name: name,
vers: vers,
cmh_items: cmh_items
}
}
// This calculates CMH as defined above
fn crate_meta_extras_hash(symbol_hasher: &hash::State,
-cmh_items: ~[@ast::meta_item],
dep_hashes: ~[~str]) -> @str {
fn len_and_str(s: &str) -> ~str {
fmt!("%u_%s", s.len(), s)
}
fn len_and_str_lit(l: ast::lit) -> ~str {
len_and_str(pprust::lit_to_str(@l))
}
let cmh_items = attr::sort_meta_items(cmh_items);
fn hash(symbol_hasher: &hash::State, m: &@ast::meta_item) {
match m.node {
ast::meta_name_value(key, value) => {
symbol_hasher.write_str(len_and_str(*key));
symbol_hasher.write_str(len_and_str_lit(value));
}
ast::meta_word(name) => {
symbol_hasher.write_str(len_and_str(*name));
}
ast::meta_list(name, ref mis) => {
symbol_hasher.write_str(len_and_str(*name));
for mis.each |m_| {
hash(symbol_hasher, m_);
}
}
}
}
symbol_hasher.reset();
for cmh_items.each |m| {
hash(symbol_hasher, m);
}
for dep_hashes.each |dh| {
symbol_hasher.write_str(len_and_str(*dh));
}
// tjc: allocation is unfortunate; need to change core::hash
return truncated_hash_result(symbol_hasher).to_managed();
}
fn warn_missing(sess: Session, name: &str, default: &str) {
if !*sess.building_library { return; }
sess.warn(fmt!("missing crate link meta `%s`, using `%s` as default",
name, default));
}
fn crate_meta_name(sess: Session, output: &Path, -opt_name: Option<@str>)
-> @str {
return match opt_name {
Some(v) => v,
None => {
// to_managed could go away if there was a version of
// filestem that returned an @str
let name = session::expect(sess,
output.filestem(),
|| fmt!("output file name `%s` doesn't\
appear to have a stem",
output.to_str())).to_managed();
warn_missing(sess, ~"name", name);
name
}
};
}
fn crate_meta_vers(sess: Session, opt_vers: Option<@str>) -> @str {
return match opt_vers {
Some(v) => v,
None => {
let vers = @"0.0";
warn_missing(sess, ~"vers", vers);
vers
}
};
}
let ProvidedMetas {
name: opt_name,
vers: opt_vers,
cmh_items: cmh_items
} = provided_link_metas(sess, c);
let name = crate_meta_name(sess, output, opt_name);
let vers = crate_meta_vers(sess, opt_vers);
let dep_hashes = cstore::get_dep_hashes(sess.cstore);
let extras_hash =
crate_meta_extras_hash(symbol_hasher, cmh_items,
dep_hashes);
LinkMeta {
name: name,
vers: vers,
extras_hash: extras_hash
}
}
pub fn truncated_hash_result(symbol_hasher: &hash::State) -> ~str {
unsafe {
symbol_hasher.result_str()
}
}
// This calculates STH for a symbol, as defined above
pub fn symbol_hash(tcx: ty::ctxt, symbol_hasher: &hash::State, t: ty::t,
link_meta: LinkMeta) -> @str {
// NB: do *not* use abbrevs here as we want the symbol names
// to be independent of one another in the crate.
symbol_hasher.reset();
symbol_hasher.write_str(link_meta.name);
symbol_hasher.write_str(~"-");
symbol_hasher.write_str(link_meta.extras_hash);
symbol_hasher.write_str(~"-");
symbol_hasher.write_str(encoder::encoded_ty(tcx, t));
let mut hash = truncated_hash_result(symbol_hasher);
// Prefix with _ so that it never blends into adjacent digits
str::unshift_char(&mut hash, '_');
// tjc: allocation is unfortunate; need to change core::hash
hash.to_managed()
}
pub fn get_symbol_hash(ccx: @CrateContext, t: ty::t) -> @str {
match ccx.type_hashcodes.find(&t) {
Some(h) => h,
None => {
let hash = symbol_hash(ccx.tcx, ccx.symbol_hasher, t, ccx.link_meta);
ccx.type_hashcodes.insert(t, hash);
hash
}
}
}
// Name sanitation. LLVM will happily accept identifiers with weird names, but
// gas doesn't!
pub fn sanitize(s: &str) -> ~str {
let mut result = ~"";
for str::chars_each(s) |c| {
match c {
'@' => result += ~"_sbox_",
'~' => result += ~"_ubox_",
'*' => result += ~"_ptr_",
'&' => result += ~"_ref_",
',' => result += ~"_",
'{' | '(' => result += ~"_of_",
'a' .. 'z'
| 'A' .. 'Z'
| '0' .. '9'
| '_' => result.push_char(c),
_ => {
if c > 'z' && char::is_XID_continue(c) {
result.push_char(c);
}
}
}
}
// Underscore-qualify anything that didn't start as an ident.
if result.len() > 0u &&
result[0] != '_' as u8 &&
! char::is_XID_start(result[0] as char) {
return ~"_" + result;
}
return result;
}
pub fn mangle(sess: Session, ss: path) -> ~str {
// Follow C++ namespace-mangling style
let mut n = ~"_ZN"; // Begin name-sequence.
for ss.each |s| {
match *s { path_name(s) | path_mod(s) => {
let sani = sanitize(*sess.str_of(s));
n += fmt!("%u%s", str::len(sani), sani);
} }
}
n += ~"E"; // End name-sequence.
n
}
pub fn exported_name(sess: Session,
+path: path,
hash: &str,
vers: &str) -> ~str {
return mangle(sess,
vec::append_one(
vec::append_one(path, path_name(sess.ident_of(hash.to_owned()))),
path_name(sess.ident_of(vers.to_owned()))));
}
pub fn mangle_exported_name(ccx: @CrateContext,
+path: path,
t: ty::t) -> ~str {
let hash = get_symbol_hash(ccx, t);
return exported_name(ccx.sess, path,
hash,
ccx.link_meta.vers);
}
pub fn mangle_internal_name_by_type_only(ccx: @CrateContext,
t: ty::t,
name: &str) -> ~str {
let s = ppaux::ty_to_short_str(ccx.tcx, t);
let hash = get_symbol_hash(ccx, t);
return mangle(ccx.sess,
~[path_name(ccx.sess.ident_of(name.to_owned())),
path_name(ccx.sess.ident_of(s)),
path_name(ccx.sess.ident_of(hash.to_owned()))]);
}
pub fn mangle_internal_name_by_path_and_seq(ccx: @CrateContext,
+path: path,
+flav: ~str) -> ~str {
return mangle(ccx.sess,
vec::append_one(path, path_name((ccx.names)(flav))));
}
pub fn mangle_internal_name_by_path(ccx: @CrateContext, +path: path) -> ~str {
return mangle(ccx.sess, path);
}
pub fn mangle_internal_name_by_seq(ccx: @CrateContext, +flav: ~str) -> ~str {
return fmt!("%s_%u", flav, (ccx.names)(flav).repr);
}
pub fn output_dll_filename(os: session::os, lm: LinkMeta) -> ~str {
let libname = fmt!("%s-%s-%s", lm.name, lm.extras_hash, lm.vers);
let (dll_prefix, dll_suffix) = match os {
session::os_win32 => (win32::DLL_PREFIX, win32::DLL_SUFFIX),
session::os_macos => (macos::DLL_PREFIX, macos::DLL_SUFFIX),
session::os_linux => (linux::DLL_PREFIX, linux::DLL_SUFFIX),
session::os_android => (android::DLL_PREFIX, android::DLL_SUFFIX),
session::os_freebsd => (freebsd::DLL_PREFIX, freebsd::DLL_SUFFIX),
};
return str::from_slice(dll_prefix) + libname +
str::from_slice(dll_suffix);
}
// If the user wants an exe generated we need to invoke
// cc to link the object file with some libs
pub fn link_binary(sess: Session,
obj_filename: &Path,
out_filename: &Path,
lm: LinkMeta) {
// Converts a library file-stem into a cc -l argument
fn unlib(config: @session::config, +stem: ~str) -> ~str {
if stem.starts_with("lib") &&
config.os != session::os_win32 {
stem.slice(3, stem.len())
} else {
stem
}
}
let output = if *sess.building_library {
let long_libname = output_dll_filename(sess.targ_cfg.os, lm);
debug!("link_meta.name: %s", lm.name);
debug!("long_libname: %s", long_libname);
debug!("out_filename: %s", out_filename.to_str());
debug!("dirname(out_filename): %s", out_filename.dir_path().to_str());
out_filename.dir_path().push(long_libname)
} else {
/*bad*/copy *out_filename
};
log(debug, ~"output: " + output.to_str());
// The default library location, we need this to find the runtime.
// The location of crates will be determined as needed.
let stage: ~str = ~"-L" + sess.filesearch.get_target_lib_path().to_str();
// In the future, FreeBSD will use clang as default compiler.
// It would be flexible to use cc (system's default C compiler)
// instead of hard-coded gcc.
// For win32, there is no cc command,
// so we add a condition to make it use gcc.
let cc_prog: ~str = if sess.targ_cfg.os == session::os_android {
match &sess.opts.android_cross_path {
&Some(copy path) => {
fmt!("%s/bin/arm-linux-androideabi-gcc", path)
}
&None => {
sess.fatal(~"need Android NDK path for linking \
(--android-cross-path)")
}
}
} else if sess.targ_cfg.os == session::os_win32 { ~"gcc" }
else { ~"cc" };
// The invocations of cc share some flags across platforms
let mut cc_args =
vec::append(~[stage], sess.targ_cfg.target_strs.cc_args);
cc_args.push(~"-o");
cc_args.push(output.to_str());
cc_args.push(obj_filename.to_str());
let mut lib_cmd;
let os = sess.targ_cfg.os;
if os == session::os_macos {
lib_cmd = ~"-dynamiclib";
} else {
lib_cmd = ~"-shared";
}
// # Crate linking
let cstore = sess.cstore;
for cstore::get_used_crate_files(cstore).each |cratepath| {
if cratepath.filetype() == Some(~".rlib") {
cc_args.push(cratepath.to_str());
loop;
}
let dir = cratepath.dirname();
if dir != ~"" { cc_args.push(~"-L" + dir); }
let libarg = unlib(sess.targ_cfg, cratepath.filestem().get());
cc_args.push(~"-l" + libarg);
}
let ula = cstore::get_used_link_args(cstore);
for ula.each |arg| { cc_args.push(/*bad*/copy *arg); }
// Add all the link args for external crates.
do cstore::iter_crate_data(cstore) |crate_num, _| {
let link_args = csearch::get_link_args_for_crate(cstore, crate_num);
do vec::consume(link_args) |_, link_arg| {
// Linker arguments that don't begin with - are likely file names,
// so they should not be necessary.
if link_arg.starts_with("-") {
cc_args.push(link_arg);
}
}
}
// # Extern library linking
// User-supplied library search paths (-L on the cammand line) These are
// the same paths used to find Rust crates, so some of them may have been
// added already by the previous crate linking code. This only allows them
// to be found at compile time so it is still entirely up to outside
// forces to make sure that library can be found at runtime.
let addl_paths = /*bad*/copy sess.opts.addl_lib_search_paths;
for addl_paths.each |path| { cc_args.push(~"-L" + path.to_str()); }
// The names of the extern libraries
let used_libs = cstore::get_used_libraries(cstore);
for used_libs.each |l| { cc_args.push(~"-l" + *l); }
if *sess.building_library {
cc_args.push(lib_cmd);
// On mac we need to tell the linker to let this library
// be rpathed
if sess.targ_cfg.os == session::os_macos {
cc_args.push(~"-Wl,-install_name,@rpath/"
+ output.filename().get());
}
}
// On linux librt and libdl are an indirect dependencies via rustrt,
// and binutils 2.22+ won't add them automatically
if sess.targ_cfg.os == session::os_linux {
cc_args.push_all(~[~"-lrt", ~"-ldl"]);
// LLVM implements the `frem` instruction as a call to `fmod`,
// which lives in libm. Similar to above, on some linuxes we
// have to be explicit about linking to it. See #2510
cc_args.push(~"-lm");
}
else if sess.targ_cfg.os == session::os_android {
cc_args.push_all(~[~"-ldl", ~"-llog", ~"-lsupc++",
~"-lgnustl_shared"]);
cc_args.push(~"-lm");
}
if sess.targ_cfg.os == session::os_freebsd {
cc_args.push_all(~[~"-pthread", ~"-lrt",
~"-L/usr/local/lib", ~"-lexecinfo",
~"-L/usr/local/lib/gcc46",
~"-L/usr/local/lib/gcc44", ~"-lstdc++",
~"-Wl,-z,origin",
~"-Wl,-rpath,/usr/local/lib/gcc46",
~"-Wl,-rpath,/usr/local/lib/gcc44"]);
}
// OS X 10.6 introduced 'compact unwind info', which is produced by the
// linker from the dwarf unwind info. Unfortunately, it does not seem to
// understand how to unwind our __morestack frame, so we have to turn it
// off. This has impacted some other projects like GHC.
if sess.targ_cfg.os == session::os_macos {
cc_args.push(~"-Wl,-no_compact_unwind");
}
// Stack growth requires statically linking a __morestack function
cc_args.push(~"-lmorestack");
// Always want the runtime linked in
cc_args.push(~"-lrustrt");
// FIXME (#2397): At some point we want to rpath our guesses as to where
// extern libraries might live, based on the addl_lib_search_paths
cc_args.push_all(rpath::get_rpath_flags(sess, &output));
debug!("%s link args: %s", cc_prog, str::connect(cc_args, ~" "));
// We run 'cc' here
let prog = run::program_output(cc_prog, cc_args);
if 0 != prog.status {
sess.err(fmt!("linking with `%s` failed with code %d",
cc_prog, prog.status));
sess.note(fmt!("%s arguments: %s",
cc_prog, str::connect(cc_args, ~" ")));
sess.note(prog.err + prog.out);
sess.abort_if_errors();
}
// Clean up on Darwin
if sess.targ_cfg.os == session::os_macos {
run::run_program(~"dsymutil", ~[output.to_str()]);
}
// Remove the temporary object file if we aren't saving temps
if !sess.opts.save_temps {
if ! os::remove_file(obj_filename) {
sess.warn(fmt!("failed to delete object file `%s`",
obj_filename.to_str()));
}
}
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//