Skip to content

Commit

Permalink
[clang] fix generation of .debug_aranges with LTO
Browse files Browse the repository at this point in the history
Right now in case of LTO the section is not emited:

    $ cat test.c
    void __attribute__((optnone)) bar()
    {
    }
    void __attribute__((optnone)) foo()
    {
            bar();
    }
    int main()
    {
            foo();
    }

    $ clang -flto=thin -gdwarf-aranges -g -O3 test.c
    $ eu-readelf -waranges a.out  | fgrep -c -e foo -e bar
    0

    $ clang -gdwarf-aranges -g -O3 test.c
    $ eu-readelf -waranges a.out  | fgrep -c -e foo -e bar
    2

Fix this by passing explicitly -mllvm -generate-arange-section.

P.S. although this looks like a hack, since none of -mllvm was passed to
the lld before.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Suggested-by: OCHyams <orlando.hyams@sony.com>

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D133092
  • Loading branch information
azat authored and dwblaikie committed Sep 13, 2022
1 parent 13f1bc4 commit 6bf6730
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,19 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
Suffix,
Plugin);
CmdArgs.push_back(Args.MakeArgString(Plugin));
} else {
// NOTE:
// - it is not possible to use lld for PS4
// - addLTOOptions() is not used for PS5
// Hence no need to handle SCE (like in Clang.cpp::renderDebugOptions()).
//
// But note, this solution is far from perfect, better to encode it into IR
// metadata, but this may not be worth it, since it looks like aranges is
// on the way out.
if (Args.hasArg(options::OPT_gdwarf_aranges)) {
CmdArgs.push_back(Args.MakeArgString("-mllvm"));
CmdArgs.push_back(Args.MakeArgString("-generate-arange-section"));
}
}

// Try to pass driver level flags relevant to LTO code generation down to
Expand Down
8 changes: 7 additions & 1 deletion clang/test/Driver/debug-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@
// RUN: %clang -### -c -glldb %s 2>&1 | FileCheck -check-prefix=NOPUB %s
// RUN: %clang -### -c -glldb -gno-pubnames %s 2>&1 | FileCheck -check-prefix=NOPUB %s
//
// RUN: %clang -### -c -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=GARANGE %s
// RUN: %clang -### -target x86_64-unknown-linux -c -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=GARANGE %s
// RUN: %clang -### -target x86_64-unknown-linux -flto -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=LDGARANGE %s
// RUN: %clang -### -target x86_64-unknown-linux -flto=thin -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=LDGARANGE %s
// RUN: %clang -### -target x86_64-unknown-linux -fuse-ld=lld -flto -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=LLDGARANGE %s
// RUN: %clang -### -target x86_64-unknown-linux -fuse-ld=lld -flto=thin -gdwarf-aranges %s 2>&1 | FileCheck -check-prefix=LLDGARANGE %s
//
// RUN: %clang -### -fdebug-types-section -target x86_64-unknown-linux %s 2>&1 \
// RUN: | FileCheck -check-prefix=FDTS %s
Expand Down Expand Up @@ -371,6 +375,8 @@
// NORNGBSE-NOT: -fdebug-ranges-base-address
//
// GARANGE-DAG: -generate-arange-section
// LDGARANGE-NOT: {{".*lld.*"}} {{.*}} "-generate-arange-section"
// LLDGARANGE: {{".*lld.*"}} {{.*}} "-generate-arange-section"
//
// FDTS: "-mllvm" "-generate-type-units"
// FDTSE: error: unsupported option '-fdebug-types-section' for target 'x86_64-apple-darwin'
Expand Down

0 comments on commit 6bf6730

Please sign in to comment.