https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87936

            Bug ID: 87936
           Summary: gnatlink fails with -pie
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

Consider the following testcase:
...
$ cat hello.adb 
with Ada.Text_IO;

procedure Hello is
begin
   Ada.Text_IO.Put_Line("Hello, world!");
end Hello;
...

It compiles without problems:
...
$ rm -f hello.ali hello.o hello ; gnatmake hello.adb
gcc -c hello.adb
gnatbind -x hello.ali
gnatlink hello.ali
...

and works fine:
...
delia:~ # ./hello 
Hello, world!
...

However, when we try to compile with -fPIE -pie, we get:
...
$ rm -f hello.ali hello.o hello ; gnatmake -fPIE hello.adb -largs -pie 
gcc -c -fPIE hello.adb
gnatbind -x hello.ali
gnatlink hello.ali -fPIE -pie
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld:
/usr/lib64/gcc/x86_64-suse-linux/8/adalib/libgnat.a(a-contai.o): relocation
R_X86_64_32 against symbol `ada__containers__capacity_error' can not be used
when making a PIE object; recompile with -fPIC
   ...
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: final
link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
gnatlink: error when calling /usr/bin/gcc
gnatmake: *** link failed.
...

The problem is that libgnat.a is picked up instead of libgnat_pic.a, so a
workaround is to add -lgnat_pic:
...
$ rm -f hello.ali hello.o hello ; gnatmake -fPIE hello.adb -largs -pie
-lgnat_pic
$ ./hello 
Hello, world!
...

AFAIU, gnatlink should include libgnat_pic.a instead of libgnat.a when passed
the -pie option.

Reply via email to