Itay Marom
Itay Marom

Reputation: 863

shared library (PIC) returns a pointer to caller results SEGFAULT

I've built a shared library using gcc on Linux (test.so) the code of course is PIC.

one of test.so API calls returns a pointer to a struct on the shared library stack and another call returns a pointer to a struct on the shared library data segment.

When I am trying to access these pointesr after getting them from the shared library I get a SEGFAULT.

My questions are these:

  1. stack addresses should always be valid (as they are calculated as offests to SP) so why did i get a SEGFAULT?

  2. for global addresses, if I access them inside the library the compiler uses a base value given by the dynamic loader to resolve them, but what happens if i do this:

    return &x

does the compiler returns the resolved address or does it assume because no LOAD/STORE was executed it can remain the non resolved address?

if it does not resolve the address then the caller might get an offset address instead of a valid address. is this possible?

Upvotes: 0

Views: 207

Answers (1)

Employed Russian
Employed Russian

Reputation: 213706

one of test.so API calls returns a pointer to a struct on the shared library stack

There is no such thing as the "shared library stack". There is only one stack (for each thread), and returning an address of a local variable is almost never what you want.

another call returns a pointer to a struct on the shared library data segment.

That's ok, so long as you don't dlclose() the library in question.

When I am trying to access these pointesr after getting them from the shared library I get a SEGFAULT.

It is likely that you didn't tell us the whole story, and that your problem is elsewhere.

In any case, given the information you provided a good answer is unlikely. Please edit your question with more details. A test case that can be built and that demonstrates your problem is best.

Upvotes: 1

Related Questions