Repository: rust
Issue: Rust generate erroneous debug line information for non-local panic handlers
Labels: A-debuginfo,T-compiler,E-help-wanted,C-bug

Related to https://github.com/rust-lang/rust/issues/55352, Rustc 1.33 generates incorrect debugging information for panic handlers not in the current crate:

Compiling:

```rust
// Taken from https://github.com/rust-lang/rust/issues/55352

pub struct Person {
    pub name: String,
    pub age: u32
}

pub fn get_age() -> u32 {
    42
}

pub fn create_bob() -> Person {
    Person {
        name: "Bob".to_string(),
        age: get_age()
    }
}


#[cfg(test)]
mod tests {
    use super::create_bob;
    use super::get_age;

    #[test]
    fn test_create_bob() {
        create_bob();
    }

    #[test]
    fn test_get_age() {
        assert_eq!(get_age(), 42);
    }
}
```

as `src/lib.rs`, Rust generates a `.debug_lines` section, which contains the following (from `readelf --debug-dump=decodedline`):

```
CU: /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libtest/lib.rs:
File name                            Line number    Starting address
lib.rs                                       330             0x1e580

lib.rs                                       331             0x1e587
lib.rs                                         0             0x1e590

./<::core::macros::assert_eq macros>:[++]
<::core::macros::assert_eq macros>            14             0x1e597
<::core::macros::assert_eq macros>            15             0x1e5a6
<::core::macros::assert_eq macros>            15             0x1e5b0
<::core::macros::assert_eq macros>            16             0x1e5bd
<::core::macros::assert_eq macros>            16             0x1e5c4
<::core::macros::assert_eq macros>            16             0x1e5cc
<::core::macros::assert_eq macros>            16             0x1e5d2
<::core::macros::assert_eq macros>            16             0x1e5d6
<::core::macros::assert_eq macros>             0             0x1e5de

/rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libtest/lib.rs:
lib.rs                                       335             0x1e5e5

src/lib.rs:
lib.rs                                         1             0x1e5f2
```

This means that the `/rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libtest/lib.rs` compilation unit points a debug line at line 1 of `src/lib.rs`

Reading through the generated assembly (via `objdump -S -d`)

```
000000000001e580 <_ZN4test18assert_test_result17haf3989bfec92a9a7E>:
   1e580:       48 81 ec 68 01 00 00    sub    $0x168,%rsp
   1e587:       e8 54 e4 ff ff          callq  1c9e0 <_ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17h05d35b96322ac0d1E>
   1e58c:       89 44 24 64             mov    %eax,0x64(%rsp)
   1e590:       48 8d 05 25 dd 0a 00    lea    0xadd25(%rip),%rax        # cc2bc <_IO_stdin_used+0xfc>
   1e597:       48 8d 4c 24 64          lea    0x64(%rsp),%rcx
   1e59c:       48 89 4c 24 68          mov    %rcx,0x68(%rsp)
   1e5a1:       48 89 44 24 70          mov    %rax,0x70(%rsp)
   1e5a6:       48 8b 44 24 68          mov    0x68(%rsp),%rax
   1e5ab:       48 89 44 24 78          mov    %rax,0x78(%rsp)
   1e5b0:       48 8b 44 24 70          mov    0x70(%rsp),%rax
   1e5b5:       48 89 84 24 80 00 00    mov    %rax,0x80(%rsp)
   1e5bc:       00 
   1e5bd:       48 8b 44 24 78          mov    0x78(%rsp),%rax
   1e5c2:       8b 10                   mov    (%rax),%edx
   1e5c4:       48 8b 84 24 80 00 00    mov    0x80(%rsp),%rax
   1e5cb:       00 
   1e5cc:       3b 10                   cmp    (%rax),%edx
   1e5ce:       40 0f 94 c6             sete   %sil
   1e5d2:       40 80 f6 ff             xor    $0xff,%sil
   1e5d6:       40 f6 c6 01             test   $0x1,%sil
   1e5da:       75 02                   jne    1e5de <_ZN4test18assert_test_result17haf3989bfec92a9a7E+0x5e>
   1e5dc:       eb 3d                   jmp    1e61b <_ZN4test18assert_test_result17haf3989bfec92a9a7E+0x9b>
   1e5de:       48 8d 35 3b 15 0a 00    lea    0xa153b(%rip),%rsi        # bfb20 <_ZN4core3fmt3num52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17ha3b2ffd94f72c608E>
   1e5e5:       48 8d 44 24 64          lea    0x64(%rsp),%rax
   1e5ea:       48 89 84 24 40 01 00    mov    %rax,0x140(%rsp)
   1e5f1:       00 
// Taken from https://github.com/rust-lang/rust/issues/55352
   1e5f2:       48 8b 84 24 40 01 00    mov    0x140(%rsp),%rax
```

`1ef52` is part of the panic handler if a test fails.

Thus it's not possible to cover the code without having a test fail!