Solved

C++ Text segment relocation error - writing to read only memory

Posted on 2010-09-10
4
744 Views
Last Modified: 2013-11-17
Hey guys,

So... On a pretty beefy project (~15,000 files), I had to start using a new static library.  The static library uses other libraries, etc.  Anyway, everything is in, compliling, etc.

Here's my problem now.  When I run my unit tests on the module using the new library I get a text segment relocation warning followed by a symbol reference and a note of it trying to write to read only memory.

The unit tests literally have nothing whatsoever to do with the portion of the module using the new library (in fact that part isnt even instantiated, the tests are run on a singleton with no dependencies on the part using the library).

Anyway, I'm guessing something about the static libraries is crashing the executable prior to getting to my code, but I don't understand what.  So...

(1) what is a text relocation warning/error?
(2) why would a reference and inclusion to a static library possibly cause one?
(3) why is my executable doing anything with the library when none of its functions or objeects are referenced in the test whatsoever?
(4) why do I ge this error at run instead of compile/link time.

I know that I didn't provide enough information to get specific solutions, but I"m hopeing getting some answers to those 4 questions will help me work this out.  Thanks :)

-w00te
0
Comment
Question by:w00te
  • 2
4 Comments
 
LVL 12

Author Comment

by:w00te
ID: 33647998
PS: The makefile for the test executable does include everything the makefile for the real executable includes, whether or not its used.  That's why the library is part of the build in the first place.

-w00te
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 400 total points
ID: 33652615
>> (1) what is a text relocation warning/error?

It indicates that you're linking to a shared library (loaded at runtime) that is non PIC (position independent code).

In other words : the shared library was not created with -fPIC, so on load time, the dynamic linker has to resolve all the text relocations, by patching the addresses in the code. This involves quite a bit of work (and a slower start of the executable), hence the warning.


>> (2) why would a reference and inclusion to a static library possibly cause one?

Assuming that it is the inclusion of that static library that causes this, I'd say that either :

a) the library is actually a non PIC shared library
b) the library loads in some other shared library that is non PIC


>> (3) why is my executable doing anything with the library when none of its functions or objeects are referenced in the test whatsoever?

If you link to it, then there's a good chance it will be included into the executable (for static libraries) or loaded at runtime (for shared libraries). Static globals eg.

If you could show the exact error message/warning you get, we can be more precise.


>> (4) why do I ge this error at run instead of compile/link time.

Because a shared library is loaded at runtime.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 100 total points
ID: 33652679
Never seen this before. But here's a link that may offer more ideas:
    See Displacement Relocations
         http://docs.sun.com/app/docs/doc/817-1984/chapter2-10?l=en&a=view
0
 
LVL 12

Author Closing Comment

by:w00te
ID: 33653464
Thanks guys.  I've been going through the libraries and looking for fPIC compiler options and it seems to be everywhere (I ran into into suggestion online yesterday). Our code base and makefiles makes it a little tough to track everywhere so it may take a while assuming that really is the problem. Were cross compiling to a special unix like system so I'm hoping it's not something strange about the system itself. Anyway, I think this is a god solution for the text relocation issue in general so I'm accepting it.  Thanks for the thoughts :)

-w00te
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now