<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62535>62535</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            RISC-V: explicitly specified and synthesized `.loc` directives are incompatible with -mrelax
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            MaskRay
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          MaskRay
      </td>
    </tr>
</table>

<pre>
    https://maskray.me/blog/2021-03-14-the-dark-side-of-riscv-linker-relaxation#line-number-information-for-assembly-files

The GNU assembler and LLVM integrated assembler both create a `.debug_line` section for assembly files that have `.file` and `.loc` directives.
When `-g` is specified for assembly files without these directives, debug information is synthesized, including line number information.

However, the LLVM integrated assembler has a bug that creates issues for `-mrelax` with `.debug_line` for both explicitly specified and synthesized `.loc` directives.
This is due to `MCDwarfLineTable::emitOne` calling `MCObjectStreamer::emitDwarfAdvanceLineAddr` to emit line table opcodes for a section.

For an assembly file, `createRISCVELFStreamer` is used to create a `MCObjectStreamer` instance with a `MCAssembler` object.
For a C/C++ file, `createRISCVObjectTargetStreamer` is used to create a `MCObjectStreamer` instance without a `MCAssembler` object.

When a `MCAssembler` object is used, label differences in the line number information are incorrectly foldable.
The LLVM integrated assembler will emit special opcodes that hard code instruction offsets, which can become incorrect when the instruction offset changes due to linker relaxation.

```sh
#!/bin/sh -e
cat > x.c <<eof
void f();
void _start() {
  f();
  f();
  f();
}
eof
# C to object file: correct DW_LNS_fixed_advance_pc
clang --target=riscv64 -g -c x.c
llvm-dwarfdump --debug-line -v x.o | grep \ DW_LNS_fixed_advance_pc

# Assembly to object file with synthesized line number information: incorrect special opcodes
clang --target=riscv64 -S x.c && clang --target=riscv64 -g -c x.s
llvm-dwarfdump --debug-line -v x.o | grep \ DW_LNS_fixed_advance_pc; test $? -eq 1

# Assembly with .loc to object file: incorrect special opcodes
clang --target=riscv64 -S -g x.c && clang --target=riscv64 -c x.s
llvm-dwarfdump --debug-line -v x.o | grep \ DW_LNS_fixed_advance_pc; test $? -eq 1
```

Using `-S` and `-c` on one source file is not commonly used to build projects.
However, `clang --target=riscv64 -g -c x.s` is somewhat common. My suggestion is to remove `-g` for now.
Synthesized debug information is not very useful anyway.

This can be worked around in several places, but that would further complicate the brittle part. I'd like to take some time figuring out the logic and coming up with a patch.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V11v2zoS_TX0y4CCTEV28uAHx6l3CyQt0KTtY0CJI4kbSvSSlB3vr18MJSdKmzTFvcUFiiYSh5o5Z858RHqv6w5xxfJLJsSN9A9f5JEJwfKrmexDY91qfDkrrDqumhB2nmVrJrZMbFvpH5w8Ji0ysS2MrZnYilTMeZrx-RkPDXIl3QP3WiG3FXfal3tudPeAjjs08lEGbTsmMqM75F3fFui47irr2njCK-u49B7bwhx5pQ16ll6xdD38f9cg_OvTVxgt0IHsFFxff7sB3QWsnQyoJqeFDQ2UDmVAkMAWaaKw6Ot78s4WKXgsyStU1p1uHSF6hdDIAI3cY7xG7-gCuaNnY0t6VNrRF_bokyHA7w12ZMBrOtYe_A5LXWlUr_k46NDYPkBo0OPkY0xsIAYKE2ri144d2er_oSIb3ZWmV7qrgQDBQOf0TjIl79_2gHt0dDE0-AvWGulBArmPJAz8edDe9-gjDkLYxnwSTILxCrlkGBOAjzujSx3McUIHMTmB80tW7xpN7kH1CMGS6c3m6iBdda07vJOFQVJotsZWh8-D81IaQ8RE28_Ff7AMt8GhbNE928ZvrNVediXSp9ZKObocLNDxwGqg74PdlVaN6OVJNy_o3dJJ9zLFxDVbpAODXz7ebr59uN4-xTEopPeoyONUpj-FTKadDxToQPdotj4ljSxsvJNMwoENE9sNE5dMXL4R0ODoTroaw5-IjPT8bnCTYnnb9hQCxWxkgQaUrip02JWkxi6q-A3hg3RI5WEdSYmyYY2iRCbPjeTtAjhoYwYFRL1K85T-sSs4BfQckbt-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