LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 35720 - Link failure with -z notext (it changes behaviour, not just allows DT_TEXTREL)
Summary: Link failure with -z notext (it changes behaviour, not just allows DT_TEXTREL)
Status: RESOLVED FIXED
Alias: None
Product: lld
Classification: Unclassified
Component: ELF (show other bugs)
Version: unspecified
Hardware: PC FreeBSD
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: 23214
  Show dependency tree
 
Reported: 2017-12-21 11:41 PST by emaste
Modified: 2017-12-26 23:44 PST (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments
FreeBSD 'hello world' -Wl,-z,notext build failure reproducer (738.86 KB, application/x-xz)
2017-12-21 14:14 PST, emaste
Details

Note You need to log in before you can comment on or make changes to this bug.
Description emaste 2017-12-21 11:41:06 PST
-z notext should allow relocations in a read-only (i.e., the .text) segment, but lld changes behaviour when -z notext is specified and linking a trivial application on FreeBSD fails:

% cc -Wl,-z,notext hello.c
/usr/bin/ld: error: relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC
>>> defined in /lib/libc.so.7
>>> referenced by crt1.c:63 (/usr/home/emaste/src/freebsd/lib/csu/amd64/crt1.c:63)
>>>               /usr/lib/crt1.o:(_start)
cc: error: linker command failed with exit code 1 (use -v to see invocation)

I do not have a reduced test case, but can attach a reproducer for the hello world link if desired. However, I observe that RelExpr() bypasses the special case for Sym.isFunc() if !Config->ZText. There's a comment in RelExpr() that describes this same issue occurring with glibc.
Comment 1 emaste 2017-12-21 14:14:50 PST
Created attachment 19588 [details]
FreeBSD 'hello world' -Wl,-z,notext build failure reproducer
Comment 2 emaste 2017-12-21 16:44:30 PST
Also I think the sense of this comment is backwards:

  // Or, if we are allowed to create dynamic relocations against
  // read-only sections (i.e. unless "-z notext" is given),
  // we can create a dynamic relocation as we want, too.
  if (!Config->ZText)
    return Expr;

That should be "i.e., if "-z notext" is given"
Comment 3 George Rimar 2017-12-22 00:39:43 PST
(In reply to emaste from comment #2)
> Also I think the sense of this comment is backwards:
> 
>   // Or, if we are allowed to create dynamic relocations against
>   // read-only sections (i.e. unless "-z notext" is given),
>   // we can create a dynamic relocation as we want, too.
>   if (!Config->ZText)
>     return Expr;
> 
> That should be "i.e., if "-z notext" is given"

Looking at this. 

But you also helped to find bug in google translate :)
It translates "unless" to russian "if", and not to "if ... not":
https://translate.google.ru/#en/ru/unless
notice, when pressing switch message button, backward translation from 
russian is "if" then.
Comment 4 George Rimar 2017-12-22 02:14:05 PST
Reduced test case is following.

main.s:
.text
_start:
callq atexit

dso.s:
.text
.global atexit
atexit:
 nop

llvm-mc -filetype=obj -triple=x86_64-pc-linux main.s -o main.o
llvm-mc -filetype=obj -triple=x86_64-pc-linux dso.s -o dso.o
ld.lld -shared dso.o -o dso.so

ld.lld test.o -o out dso.so -z notext
> error: relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC
Comment 5 George Rimar 2017-12-22 03:46:50 PST
Fix: https://reviews.llvm.org/D41541
Comment 6 George Rimar 2017-12-23 01:02:01 PST
First fix for 'hello world' was committed as r321400.
Comment 7 George Rimar 2017-12-23 03:32:33 PST
Second fix is https://reviews.llvm.org/D41551,
I checked that 'hello world' provided links fine with it.
Comment 8 George Rimar 2017-12-26 23:44:16 PST
Last piece: r321473.