[llvm-bugs] [Bug 35931] New: hwasan does not link with lld due to pc-relative relocations to .text

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jan 12 09:19:17 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=35931

            Bug ID: 35931
           Summary: hwasan does not link with lld due to pc-relative
                    relocations to .text
           Product: compiler-rt
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: compiler-rt
          Assignee: unassignedbugs at nondot.org
          Reporter: peter.smith at linaro.org
                CC: llvm-bugs at lists.llvm.org

When lld is used as the linker the compiler-rt fails when linking
libclang_rt.hwasan-aarch64.so

ld.lld: error: relocation R_AARCH64_ADR_PREL_PG_HI21 cannot be used against
symbol __interceptor_signal; recompile with -fPIC
>>> defined in lib/hwasan/CMakeFiles/RTHwasan_dynamic.aarch64.dir/hwasan_interceptors.cc.o
>>> referenced by sanitizer_signal_interceptors.inc:85 (/linaro/crosslibcxx/llvm/projects/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_signal_interceptors.inc:85)
>>>               lib/hwasan/CMakeFiles/RTHwasan_dynamic.aarch64.dir/hwasan_interceptors.cc.o:(__hwasan::InitializeInterceptors())

The object files for libclang_rt.hwasan-aarch64.so appear to be compiled with
-fPIE and not with -fPIC. As no symbol is preemptible in an executable the
compiler will use static non GOT-generating relocations for code like:

typedef int fptr(void);

int func(void) {
    return 0;
}

fptr* func2(void) {
    return &func;
}

In the context of a shared library the symbols are preemptible and therefore it
is not safe to resolve the relocations at static link-time as the destination
can move at run-time.

As it happens ld.bfd and ld.gold will silently resolve the relocations at
static link time, which lets the link succeed. In theory the library will cease
to work if a symbol is preempted but I'm guessing that the use-case of hwasan
makes that extremely unlikely. Unfortunately the linker can't know that so I
think lld is right to give an error here.

I've raised PR35929 on lld to see whether it should handle this case with
-znotext. However I think it is worth not producing these relocations in
hwasan. I think that this could be achieved by either giving the symbols
STV_PROTECTED visibility or creating a STV_HIDDEN alias at the same address
that internal callers/address takers can use. If neither lld or hwasan will
change it will be worth removing the -fPIE flag if lld is detected as the
linker.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180112/b7779838/attachment.html>


More information about the llvm-bugs mailing list