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

Posted on 2010-09-10
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 :)

Question by:w00te
  • 2
LVL 12

Author Comment

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.

LVL 53

Accepted Solution

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.
LVL 32

Assisted Solution

phoffric earned 100 total points
ID: 33652679
Never seen this before. But here's a link that may offer more ideas:
    See Displacement Relocations
LVL 12

Author Closing Comment

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 :)


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to become programmer? 12 32
is twain_32.dll cmpatible with windows 10 ? 10 125
Least Squares Curve Fitting 4 60
What is sub-make ? 2 38
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

21 Experts available now in Live!

Get 1:1 Help Now