LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2019, 07:31 AM   #1
user35735
LQ Newbie
 
Registered: Jul 2019
Posts: 3

Rep: Reputation: Disabled
Symbol relocation from one compiler version to another


I'm compiling a static library, let's call it static.a which is later linked by a shared library shared.so and by a final executable binary file (shared.so uses just a few functions from static.a maybe later this can be further splited). If I try to compile it suing gcc 7.4 I get this linker error:

Code:
 /usr/bin/ld: ../../static.a(file.cpp.o): relocation R_X86_64_TPOFF32 against symbol `_ZGVZN6spdlog7details2os9thread_idEvE3tid' can not be used when making a shared object; recompile with -fPIC
I decided to try also gcc 9.1 and this error doesn't apear anymore.

1. should I always use -fpic when building a static library that will be used in a shared library? I know fpic adds some overhead.
2. how come a newer version of gcc can relocate the symbols of the static.a inside the shared library? Is this safe?

Thank you.
 
Old 07-18-2019, 02:38 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Yes, shared code should be compiled with '-fPIC'. Also it might be the default in some gcc-version.
 
3 members found this post helpful.
Old 07-28-2019, 03:17 PM   #3
user35735
LQ Newbie
 
Registered: Jul 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Yes, shared code should be compiled with '-fPIC'. Also it might be the default in some gcc-version.
What if I know for sure that only one process will load the .so? (I know many consider this a stupid thing but I need to be able to switch between different algorithms that are known only at run time and new algorithms, in the form of a .so, can be added at run time) So there is no need to share the .text section with other processes, do I still need to use fpic?

Last edited by user35735; 07-28-2019 at 03:20 PM.
 
Old 07-28-2019, 10:56 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
It's not the number of processes, but the fact that it will be loaded at a not-preset address by 'ld.so' or 'dlopen', so it has to be position-independent.
(See also: https://en.wikipedia.org/wiki/Addres..._randomization )

Last edited by NevemTeve; 07-28-2019 at 10:58 PM.
 
2 members found this post helpful.
Old 07-29-2019, 02:24 AM   #5
user35735
LQ Newbie
 
Registered: Jul 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
It's not the number of processes, but the fact that it will be loaded at a not-preset address by 'ld.so' or 'dlopen', so it has to be position-independent.
(See also: https://en.wikipedia.org/wiki/Addres..._randomization )
When you link a binary, the linker does a second pass through the binary and updates the addresses of the functions so that they are called correctly when running the binary, using the relocation section '.rela.text' information found in the .o file. Why isn't in possible that 'ld.so' or 'dlopen' do the same thing when you load a shared library, it should go through the loaded code and patch every call to the correct address? Sure this takes longer to start but I don't mind a small delay at startup time. If I understand this correctly, when it comes to call instructions, the problem appears when using absolute addresses and not relative ones when doing a call or a jump ?

Also I found this article article which at page 7 it states this "So, if performanceis important for a library or dynamically loadable module, youcan compile it as non-PIC code. The primary downside tocompiling the module as non-PIC is that loading time in-creases because the dynamic linker must make a large numberof code patches when binding symbols."
 
Old 07-29-2019, 12:20 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Incidentally, that's how Windows32/x86 DLLs work: they are relocated loading time, as they aren't position-independent.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the differences between the normal symbol table, the dynamic symbol table, and the debugging symbol table? watchintv Linux - Software 5 10-22-2016 08:38 AM
gtkpod error: relocation error: gtkpod: undefined symbol: gtk_tool_button_new dlj2 Linux - Software 6 01-29-2005 07:51 AM
Relocation error for symbol but it is being exported nukkel Programming 0 12-04-2004 03:23 AM
rpm error only says"rpm: relocation error: rpm: undefined symbol: poptAliasOptions" dlrsims Linux - Newbie 2 11-14-2003 09:22 PM
dcgui-qt: relocation error: /usr/lib/libqt-mt.so.3: undefined symbol: glXGetConfig akqw Linux - Software 0 10-05-2003 10:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration