clang-9 (MacOS 10.13) linker crash and optimization issues

First post on this mailing report a crash-bug in clang-9.0.0
(x86_64-apple-darwin17.0.0) on MacOS 10.13 (High Sierra). The clang-9
was installed from the pre-built binary on (not XCode).

There're two related issues uncovered:

1) clang-9 ld64.lld crashed when trying to link a default compiled
sample .c file, which has some switch statement cases. Hello-world
kind of .c test file is linking and running ok. The same crashing link
is successful when using clang-8 ld64.lld instead.

2) Executable of the sample file (same as above) produced with clang-9
(or clang-8, or even clang-7) results in "Segmentation Fault: 11",
when built with default compiler flags (apart from MacOS SDK
includes). The code appears to build and run ok with -O1 or -O2 or -O3
flags (-O0 yields SEGV in run-time). However, when bulding any real
application beyond the sample still results in some SEGV when trying
to run the built binaries at point or the other.

It took me some painful debugging to figure out at least one remedy
which appears to clear both of the issues listed above. Adding
"-fno-jump-tables" compiler/optimizer option seems to result in
correct run-time behavior (for all of the clang versions tested:
7,8,9). This seems applicable to all optimization levels -O0 to -O3.

I wonder if at least the "-fno-jump-tables" option should be made part
of defaults in MacOS build, or maybe add some line in docs to alert
anyone that faces these issues.

Below is the sample code that triggered the linker crash and SEGV in
runtime (in the pre-built clang versions 7 through 9). The resulting
run-time SEGV output (without -fno-jump-tables option) is shown after
that. Finally, the clang-9 linker crash dump is shown at the very end.

I also attached these outputs, just in case it gets wrapped on the mail-list.

I hope this could get fixed or maybe there're some other details about
these issues that I don't know.
I also tried the bugpoint way, but it seems to be quite challenging to
figure out given a rather terse documentation on how to use it.
Instead I had to resort to eyeballing the resulting asm files to see
the difference in switch handling between -O0 (crashing) and -O1
(somewhat ok) codes.



clang-9-macos-10.13-crash-bug.txt (7.18 KB)