On 2019-07-21 1:01 a.m., Yangfl wrote:
> John David Anglin <dave.ang...@bell.net> 于2019年7月21日周日 上午5:03写道:
>> Source: libminini
>> Version: 1.2.a+ds-2
>> Severity: normal
>>
>> Dear Maintainer,
>>
>> The build fails in test1 with a segmentation fault:
>>
>> LD_LIBRARY_PATH=. ./test1
>> Segmentation fault
>>
>> This is caused by the fact that objects linked into shared libraries need
>> to be compiled with the -fPIC option.  This option also needs to be added
>> when linking shared libraries.  With this problem fixed, test1 now runs
>> correctly but test2 fails at the following assert:
> Hmm, I'm still confused why this option is not needed on any other arch?
It's definitely a problem on architectures that aren't PIC by default.

On 32-bit hppa, register r19 is the PIC register when PIC code is generated.  
In non-PIC code,
r19 is a general register.  The PIC register for a shared library is set on 
entry by long branch
stubs set up by the linker.  If a shared library contains non PIC code, r19 may 
be clobbered.

That's what happens here in test1:

(gdb) c
Continuing.

Breakpoint 1, 0xfabcca28 in ini_gets (Section=0x11878 "first",
    Key=0x11870 "string", DefValue=0x11868 "dummy",
    Buffer=0xf8f022d4 "\372Wl\200", BufferSize=<optimized out>,
    Filename=<optimized out>) at minIni.cc:374
374       if (ini_openread(Filename, &fp)) {
(gdb) stepi
0xfabcca2c      374       if (ini_openread(Filename, &fp)) {
(gdb) disass $pc-16,$pc+16
Dump of assembler code from 0xfabcca1c to 0xfabcca3c:
   0xfabcca1c <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+44>:    stw r4,-68(sp)
   0xfabcca20 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+48>:    ldw -b4(sp),r4
   0xfabcca24 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+52>:    stw r3,-64(sp)
   0xfabcca28 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+56>:    b,l 0xfabcb91c,rp
=> 0xfabcca2c <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+60>:    copy r23,r3
   0xfabcca30 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+64>:    cmpib,= 0,ret0,0xfabcca70 <ini_gets(char const*,
char const*, char const*, char*, int, char const*)+128>
   0xfabcca34 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+68>:    stw ret0,-78(sp)
   0xfabcca38 <ini_gets(char const*, char const*, char const*, char*, int, char 
const*)+72>:    ldi -1,ret0
End of assembler dump.
(gdb) stepi
0xfabcb91c in ?? ()
   from /home/dave/debian/libminini-x/libminini-1.2.a+ds/libminIni.so.1
(gdb) x/4i $pc
=> 0xfabcb91c:  addil L%-800,r19,r1
   0xfabcb920:  ldw 794(r1),r21
   0xfabcb924:  bv r0(r21)
   0xfabcb928:  ldw 798(r1),r19
(gdb) p/x $r19
$1 = 0x0
(gdb) stepi
0xfabcb920 in ?? ()
   from /home/dave/debian/libminini-x/libminini-1.2.a+ds/libminIni.so.1
(gdb) bt
#0  0xfabcb920 in ?? ()
   from /home/dave/debian/libminini-x/libminini-1.2.a+ds/libminIni.so.1
#1  0xfabcca30 in ini_gets (Section=0x11878 "first", Key=0x11870 "string",
    DefValue=0x11868 "dummy", Buffer=0xf8f022d4 "\372Wl\200",
    BufferSize=<optimized out>, Filename=<optimized out>) at minIni.cc:374
#2  0x000108b4 in main ()
(gdb) stepi

Program received signal SIGSEGV, Segmentation fault.
0xfabcb920 in ?? ()
   from /home/dave/debian/libminini-x/libminini-1.2.a+ds/libminIni.so.1

Non-PIC code also uses different relocations.  Some are not valid in PIC code.

The reason to pass "-fPIC" in shared link commands is in some circumstances 
(e.g., lto) the
linker may perform compilations and generate objects that are linked into the 
shared object.

Dave

-- 
John David Anglin  dave.ang...@bell.net

Reply via email to