Skip to content

Commit b037c52

Browse files
committed
Emit DW_AT_main_subprogram
This changes rustc to emit DW_AT_main_subprogram on the "main" program. This lets gdb suitably stop at the user's main in response to "start" (rather than the library's main, which is what happens currently). Fixes #32620 r? michaelwoerister
1 parent d7777ae commit b037c52

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

src/librustc_llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ pub mod debuginfo {
459459
const FlagStaticMember = (1 << 12),
460460
const FlagLValueReference = (1 << 13),
461461
const FlagRValueReference = (1 << 14),
462+
const FlagMainSubprogram = (1 << 21),
462463
}
463464
}
464465
}

src/librustc_trans/debuginfo/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
257257
let function_name = CString::new(name).unwrap();
258258
let linkage_name = CString::new(linkage_name).unwrap();
259259

260+
let mut flags = DIFlags::FlagPrototyped;
261+
match *cx.sess().entry_fn.borrow() {
262+
Some((id, _)) => {
263+
if local_id == Some(id) {
264+
flags = flags | DIFlags::FlagMainSubprogram;
265+
}
266+
}
267+
None => {}
268+
};
269+
260270
let fn_metadata = unsafe {
261271
llvm::LLVMRustDIBuilderCreateFunction(
262272
DIB(cx),
@@ -269,7 +279,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
269279
is_local_to_unit,
270280
true,
271281
scope_line as c_uint,
272-
DIFlags::FlagPrototyped,
282+
flags,
273283
cx.sess().opts.optimize != config::OptLevel::No,
274284
llfn,
275285
template_parameters,

src/rustllvm/RustWrapper.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ enum class LLVMRustDIFlags : uint32_t {
346346
FlagStaticMember = (1 << 12),
347347
FlagLValueReference = (1 << 13),
348348
FlagRValueReference = (1 << 14),
349+
FlagMainSubprogram = (1 << 21),
349350
// Do not add values that are not supported by the minimum LLVM
350351
// version we support!
351352
};
@@ -432,6 +433,11 @@ static unsigned fromRust(LLVMRustDIFlags Flags) {
432433
if (isSet(Flags & LLVMRustDIFlags::FlagRValueReference)) {
433434
Result |= DINode::DIFlags::FlagRValueReference;
434435
}
436+
#if LLVM_RUSTLLVM || LLVM_VERSION_GE(4, 0)
437+
if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) {
438+
Result |= DINode::DIFlags::FlagMainSubprogram;
439+
}
440+
#endif
435441

436442
return Result;
437443
}

src/test/codegen/mainsubprogram.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The minimum LLVM version is set to 3.8, but really this test
12+
// depends on a patch that is was committed to upstream LLVM before
13+
// 4.0; and also backported to the Rust LLVM fork.
14+
15+
// ignore-tidy-linelength
16+
// ignore-windows
17+
// ignore-macos
18+
// min-llvm-version 3.8
19+
20+
// compile-flags: -g -C no-prepopulate-passes
21+
22+
// CHECK-LABEL: @main
23+
// CHECK: {{.*}}DISubprogram{{.*}}name: "main",{{.*}}DIFlagMainSubprogram{{.*}}
24+
25+
pub fn main() {
26+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The minimum LLVM version is set to 3.8, but really this test
12+
// depends on a patch that is was committed to upstream LLVM before
13+
// 4.0; and also backported to the Rust LLVM fork.
14+
15+
// ignore-tidy-linelength
16+
// ignore-windows
17+
// ignore-macos
18+
// min-llvm-version 3.8
19+
20+
// compile-flags: -g -C no-prepopulate-passes
21+
22+
#![feature(start)]
23+
24+
// CHECK-LABEL: @main
25+
// CHECK: {{.*}}DISubprogram{{.*}}name: "start",{{.*}}DIFlagMainSubprogram{{.*}}
26+
27+
#[start]
28+
fn start(_: isize, _: *const *const u8) -> isize {
29+
return 0;
30+
}

0 commit comments

Comments
 (0)