Skip to content

Commit b1d6f12

Browse files
committed
Don't put a copy of main (the C one) in each binary we produce. This is a step
in getting a driver that works on all linux systems. Sorry for the linker hacks, I will remove them after snapshotting a new compiler.
1 parent df9cf0b commit b1d6f12

File tree

9 files changed

+77
-66
lines changed

9 files changed

+77
-66
lines changed

Makefile.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ COMPILER_INPUTS := $(wildcard $(addprefix $(S)src/comp/, \
144144
######################################################################
145145

146146
LREQ := rt/$(CFG_RUNTIME) rustllvm/$(CFG_RUSTLLVM)
147-
SREQ0 := stage0/rustc$(X) $(LREQ) stage1/glue.o stage1/$(CFG_STDLIB)
148-
SREQ1 := stage1/rustc$(X) $(LREQ) stage2/glue.o stage2/$(CFG_STDLIB)
149-
SREQ2 := stage2/rustc$(X) $(LREQ) stage3/glue.o stage3/$(CFG_STDLIB)
147+
SREQ0 := stage0/rustc$(X) $(LREQ) rt/main.a stage1/glue.o stage1/$(CFG_STDLIB)
148+
SREQ1 := stage1/rustc$(X) $(LREQ) rt/main.a stage2/glue.o stage2/$(CFG_STDLIB)
149+
SREQ2 := stage2/rustc$(X) $(LREQ) rt/main.a stage3/glue.o stage3/$(CFG_STDLIB)
150150

151151

152152
######################################################################

mk/clean.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ clean:
2828
$(Q)rm -f stage3/rustc$(X) stage3/$(CFG_STDLIB) stage3/glue*
2929
$(Q)rm -f rustllvm/$(CFG_RUSTLLVM) rustllvm/rustllvmbits.a
3030
$(Q)rm -f rt/$(CFG_RUNTIME)
31+
$(Q)rm -f rt/main.o
32+
$(Q)rm -f rt/main.a
3133
$(Q)rm -Rf $(PKG_NAME)-*.tar.gz dist
3234
$(Q)rm -f $(foreach ext,o a d bc s exe,$(wildcard stage*/*.$(ext)))
3335
$(Q)rm -Rf $(foreach ext,out out.tmp \

mk/rt.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ rt/%.o: rt/%.s $(MKFILES)
7676
@$(call E, compile: $@)
7777
$(Q)$(call CFG_COMPILE_C, $@, $(RUNTIME_INCS)) $<
7878

79+
ifdef CFG_WINDOWSY
80+
rt/main.ll: rt/main.ll.in
81+
sed 's/MAIN/WinMain@16/' < $^ > $@
82+
else
83+
rt/main.ll: rt/main.ll.in
84+
sed 's/MAIN/main/' < $^ > $@
85+
endif
86+
87+
rt/main.a: rt/main.o
88+
rm -f $@
89+
ar crs $@ $^
90+
7991
rt/%.o: rt/%.ll $(MKFILES)
8092
@$(call E, llc: $@)
8193
$(Q)$(LLC) -filetype=obj -relocation-model=pic -march=x86 -o $@ $<

mk/stage1.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ stage1/%.o: stage1/%.s
4444
stage1/%$(X): stage1/%.o $(SREQ0)
4545
@$(call E, link [gcc]: $@)
4646
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage1/glue.o -o $@ $< \
47-
-Lstage1 -Lrustllvm -Lrt -lrustrt -lrustllvm -lstd -lm
47+
-Lstage1 -Lrustllvm -Lrt rt/main.a -lrustrt -lrustllvm -lstd -lm
4848
@# dsymutil sometimes fails or prints a warning, but the
4949
@# program still runs. Since it simplifies debugging other
5050
@# programs, I\'ll live with the noise.

mk/stage2.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ stage2/%.o: stage2/%.s
4444
stage2/%$(X): stage2/%.o $(SREQ1)
4545
@$(call E, link [gcc]: $@)
4646
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage2/glue.o -o $@ $< \
47-
-Lstage2 -Lrustllvm -Lrt -lrustrt -lrustllvm -lstd -lm
47+
-Lstage2 -Lrustllvm -Lrt rt/main.a -lrustrt -lrustllvm -lstd -lm
4848
@# dsymutil sometimes fails or prints a warning, but the
4949
@# program still runs. Since it simplifies debugging other
5050
@# programs, I\'ll live with the noise.

mk/stage3.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ stage3/rustc.o: $(COMPILER_CRATE) $(COMPILER_INPUTS) $(SREQ2)
2323
$(STAGE2) -c -o $@ $<
2424

2525
stage3/glue.o: stage2/rustc$(X) stage2/$(CFG_STDLIB) stage2/intrinsics.bc \
26-
rustllvm/$(CFG_RUSTLLVM) rt/$(CFG_RUNTIME)
26+
rustllvm/$(CFG_RUSTLLVM) rt/$(CFG_RUNTIME)
2727
@$(call E, generate: $@)
2828
$(STAGE2) -c -o $@ --glue
2929

@@ -44,7 +44,7 @@ stage3/%.o: stage3/%.s
4444
stage3/%$(X): stage3/%.o $(SREQ2)
4545
@$(call E, link [gcc]: $@)
4646
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage3/glue.o -o $@ $< \
47-
-Lstage3 -Lrustllvm -Lrt -lrustrt -lrustllvm -lstd -lm
47+
-Lstage3 -Lrustllvm -Lrt rt/main.a -lrustrt -lrustllvm -lstd -lm
4848
@# dsymutil sometimes fails or prints a warning, but the
4949
@# program still runs. Since it simplifies debugging other
5050
@# programs, I\'ll live with the noise.

mk/tests.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ compile-check: tidy \
185185
%.stage0$(X): %.stage0.o $(SREQ0)
186186
@$(call E, link [gcc]: $@)
187187
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage1/glue.o -o $@ $< \
188-
-Lstage1 -Lrt -lrustrt -lstd -lm
188+
-Lstage1 -Lrt rt/main.a -lrustrt -lstd -lm
189189
@# dsymutil sometimes fails or prints a warning, but the
190190
@# program still runs. Since it simplifies debugging other
191191
@# programs, I\'ll live with the noise.
@@ -194,7 +194,7 @@ compile-check: tidy \
194194
%.stage1$(X): %.stage1.o $(SREQ1)
195195
@$(call E, link [gcc]: $@)
196196
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage2/glue.o -o $@ $< \
197-
-Lstage2 -Lrt -lrustrt -lstd -lm
197+
-Lstage2 -Lrt rt/main.a -lrustrt -lstd -lm
198198
@# dsymutil sometimes fails or prints a warning, but the
199199
@# program still runs. Since it simplifies debugging other
200200
@# programs, I\'ll live with the noise.
@@ -203,7 +203,7 @@ compile-check: tidy \
203203
%.stage2$(X): %.stage2.o $(SREQ2)
204204
@$(call E, link [gcc]: $@)
205205
$(Q)gcc $(CFG_GCCISH_CFLAGS) stage3/glue.o -o $@ $< \
206-
-Lstage3 -Lrt -lrustrt -lstd -lm
206+
-Lstage3 -Lrt rt/main.a -lrustrt -lstd -lm
207207
@# dsymutil sometimes fails or prints a warning, but the
208208
@# program still runs. Since it simplifies debugging other
209209
@# programs, I\'ll live with the noise.

src/comp/middle/trans.rs

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ fn get_type_sha1(&@crate_ctxt ccx, &ty::t t) -> str {
338338

339339
fn mangle(&vec[str] ss) -> str {
340340

341+
if (vec::len(ss) > 0u && str::eq(vec::top(ss), "main")) {
342+
ret "_rust_main";
343+
}
341344
// Follow C++ namespace-mangling style
342345

343346
auto n = "_ZN"; // Begin name-sequence.
@@ -369,7 +372,7 @@ fn mangle_name_by_path_and_seq(&@crate_ctxt ccx, &vec[str] path,
369372
ret mangle(path + [ccx.names.next(flav)]);
370373
}
371374

372-
fn mangle_name_by_path(&@crate_ctxt ccx, &vec[str] path) -> str {
375+
fn mangle_name_by_path(&vec[str] path) -> str {
373376
ret mangle(path);
374377
}
375378

@@ -7095,7 +7098,7 @@ fn create_vtbl(@local_ctxt cx,
70957098

70967099
let @local_ctxt mcx = @rec(path = cx.path + ["method",
70977100
m.node.ident] with *cx);
7098-
let str s = mangle_name_by_path(mcx.ccx, mcx.path);
7101+
let str s = mangle_name_by_path(mcx.path);
70997102
let ValueRef llfn = decl_internal_fastcall_fn(cx.ccx.llmod, s,
71007103
llfnty);
71017104
cx.ccx.item_ids.insert(m.node.id, llfn);
@@ -7107,7 +7110,7 @@ fn create_vtbl(@local_ctxt cx,
71077110
methods += [llfn];
71087111
}
71097112
auto vtbl = C_struct(methods);
7110-
auto vtbl_name = mangle_name_by_path(cx.ccx, cx.path + ["vtbl"]);
7113+
auto vtbl_name = mangle_name_by_path(cx.path + ["vtbl"]);
71117114
auto gvar = llvm::LLVMAddGlobal(cx.ccx.llmod, val_ty(vtbl),
71127115
str::buf(vtbl_name));
71137116
llvm::LLVMSetInitializer(gvar, vtbl);
@@ -7124,7 +7127,7 @@ fn trans_dtor(@local_ctxt cx,
71247127
&@ast::method dtor) -> ValueRef {
71257128

71267129
auto llfnty = T_dtor(cx.ccx, dtor.span, llself_ty);
7127-
let str s = mangle_name_by_path(cx.ccx, cx.path + ["drop"]);
7130+
let str s = mangle_name_by_path(cx.path + ["drop"]);
71287131
let ValueRef llfn = decl_internal_fastcall_fn(cx.ccx.llmod, s, llfnty);
71297132
cx.ccx.item_ids.insert(dtor.node.id, llfn);
71307133
cx.ccx.item_symbols.insert(dtor.node.id, s);
@@ -7518,7 +7521,7 @@ fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp,
75187521
}
75197522

75207523
// Declare the function itself.
7521-
let str s = mangle_name_by_path(ccx, path);
7524+
let str s = mangle_name_by_path(path);
75227525
let ValueRef llfn = decl_internal_fastcall_fn(ccx.llmod, s, llfty);
75237526

75247527
// Declare the global constant pair that points to it.
@@ -7532,6 +7535,8 @@ fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp,
75327535
ccx.sess.span_err(sp, "multiple 'main' functions");
75337536
}
75347537
log #fmt("registering %s as main function for crate", ps);
7538+
llvm::LLVMSetLinkage(llfn, lib::llvm::LLVMExternalLinkage
7539+
as llvm::Linkage);
75357540
ccx.main_fn = some(llfn);
75367541
}
75377542
}
@@ -7593,7 +7598,7 @@ fn decl_native_fn_and_pair(&@crate_ctxt ccx,
75937598
// Declare the wrapper.
75947599
auto t = node_ann_type(ccx, ann);
75957600
auto wrapper_type = native_fn_wrapper_type(ccx, sp, num_ty_param, t);
7596-
let str s = mangle_name_by_path(ccx, path);
7601+
let str s = mangle_name_by_path(path);
75977602
let ValueRef wrapper_fn = decl_internal_fastcall_fn(ccx.llmod, s,
75987603
wrapper_type);
75997604

@@ -7996,51 +8001,6 @@ fn create_typedefs(&@crate_ctxt cx) {
79968001
llvm::LLVMAddTypeName(cx.llmod, str::buf("tydesc"), T_tydesc(cx.tn));
79978002
}
79988003

7999-
fn trans_main_fn(@local_ctxt cx, ValueRef crate_map) {
8000-
auto T_main_args = [T_int(), T_int()];
8001-
auto T_rust_start_args = [T_int(), T_int(), T_int(), T_int()];
8002-
8003-
auto main_name;
8004-
if (str::eq(std::os::target_os(), "win32")) {
8005-
main_name = "WinMain@16";
8006-
} else {
8007-
main_name = "main";
8008-
}
8009-
8010-
auto llmain =
8011-
decl_cdecl_fn(cx.ccx.llmod, main_name, T_fn(T_main_args, T_int()));
8012-
8013-
auto llrust_start = decl_cdecl_fn(cx.ccx.llmod, "rust_start",
8014-
T_fn(T_rust_start_args, T_int()));
8015-
8016-
auto llargc = llvm::LLVMGetParam(llmain, 0u);
8017-
auto llargv = llvm::LLVMGetParam(llmain, 1u);
8018-
auto llrust_main = alt (cx.ccx.main_fn) {
8019-
case (none) {
8020-
cx.ccx.sess.err("missing 'main' function");
8021-
// FIXME: shouldn't sess.err's ! result unify with f?
8022-
C_nil()
8023-
}
8024-
case (some(?f)) { f }
8025-
};
8026-
8027-
//
8028-
// Emit the moral equivalent of:
8029-
//
8030-
// main(int argc, char **argv) {
8031-
// rust_start(&_rust.main, argc, argv);
8032-
// }
8033-
//
8034-
8035-
let BasicBlockRef llbb =
8036-
llvm::LLVMAppendBasicBlock(llmain, str::buf(""));
8037-
auto b = new_builder(llbb);
8038-
8039-
auto start_args = [p2i(llrust_main), llargc, llargv, p2i(crate_map)];
8040-
8041-
b.Ret(b.Call(llrust_start, start_args));
8042-
}
8043-
80448004
fn declare_intrinsics(ModuleRef llmod) -> hashmap[str,ValueRef] {
80458005

80468006
let vec[TypeRef] T_memmove32_args = [T_ptr(T_i8()), T_ptr(T_i8()),
@@ -8211,8 +8171,6 @@ fn create_module_map(&@crate_ctxt ccx) -> ValueRef {
82118171
auto maptype = T_array(elttype, ccx.module_data.size() + 1u);
82128172
auto map = llvm::LLVMAddGlobal(ccx.llmod, maptype,
82138173
str::buf("_rust_mod_map"));
8214-
llvm::LLVMSetLinkage(map, lib::llvm::LLVMInternalLinkage
8215-
as llvm::Linkage);
82168174
let vec[ValueRef] elts = [];
82178175
for each (@tup(str, ValueRef) item in ccx.module_data.items()) {
82188176
auto elt = C_struct([p2i(C_cstr(ccx, item._0)), p2i(item._1)]);
@@ -8245,7 +8203,14 @@ fn create_crate_map(&@crate_ctxt ccx) -> ValueRef {
82458203
i += 1;
82468204
}
82478205
vec::push[ValueRef](subcrates, C_int(0));
8248-
auto sym_name = "_rust_crate_map_" + crate_name(ccx, "__none__");
8206+
auto cname = crate_name(ccx, "__none__");
8207+
auto mapname;
8208+
if (ccx.sess.get_opts().shared) {
8209+
mapname = cname;
8210+
} else {
8211+
mapname = "toplevel";
8212+
}
8213+
auto sym_name = "_rust_crate_map_" + mapname;
82498214
auto arrtype = T_array(T_int(), vec::len[ValueRef](subcrates));
82508215
auto maptype = T_struct([T_int(), arrtype]);
82518216
auto map = llvm::LLVMAddGlobal(ccx.llmod, maptype, str::buf(sym_name));
@@ -8322,9 +8287,17 @@ fn trans_crate(&session::session sess, &@ast::crate crate,
83228287
trans_constants(ccx, crate);
83238288
trans_mod(cx, crate.node.module);
83248289
auto crate_map = create_crate_map(ccx);
8290+
83258291
if (!sess.get_opts().shared) {
8326-
trans_main_fn(cx, crate_map);
8327-
}
8292+
auto gvar = llvm::LLVMAddGlobal(cx.ccx.llmod, T_ptr(T_int()),
8293+
str::buf("_rust_fetch_this_object_hack"));
8294+
8295+
auto gvar2 = llvm::LLVMAddGlobal(cx.ccx.llmod, T_ptr(T_ptr(T_int())),
8296+
str::buf("_rust_fetch_this_object_hack2"));
8297+
llvm::LLVMSetInitializer(gvar2, gvar);
8298+
llvm::LLVMSetGlobalConstant(gvar, True);
8299+
llvm::LLVMSetGlobalConstant(gvar2, True);
8300+
}
83288301

83298302
emit_tydescs(ccx);
83308303

src/rt/main.ll.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
%0 = type { i32, [1 x i32] }
2+
%1 = type { i32, i32 }
3+
%2 = type { i32, %3 }
4+
%3 = type { %tydesc*, %4, i1, {} }
5+
%4 = type { i1*, i1* }
6+
%5 = type { i32, i32, i32, i32, [0 x %6*] }
7+
%6 = type { i32, i32, i32, i32, [0 x i8] }
8+
9+
10+
@_rust_crate_map_toplevel = external global %0
11+
12+
declare fastcc void @_rust_main(i1* nocapture, %task*, %2* nocapture, %5*);
13+
declare i32 @rust_start(i32, i32, i32, i32)
14+
15+
%tydesc = type { %tydesc**, i32, i32, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*)*, void (i1*, %task*, i1*, %tydesc**, i8*, i8*, i8)* }
16+
17+
%task = type { i32, i32, i32, i32, i32, i32, i32, i32 }
18+
19+
@_rust_fetch_this_object_hack = global i32 0
20+
21+
define i32 @"MAIN"(i32, i32) {
22+
%3 = tail call i32 @rust_start(i32 ptrtoint (void (i1*, %task*, %2*, %5*)* @_rust_main to i32), i32 %0, i32 %1, i32 ptrtoint (%0* @_rust_crate_map_toplevel to i32))
23+
ret i32 %3
24+
}

0 commit comments

Comments
 (0)