Discussion:
Linkage order
Hauke Krüger(IND)
2014-08-19 07:05:44 UTC
Permalink
Hi everyone,

I have the following problem:

I use gcc 4.8 for compiling c++ source code - Linux and Mac platform. In
my cpp file main.cpp,
there is a function called "a" and another function called "b". In a
static library, there is a function
"c" and a function with the same name "b" as there was in the cpp file.
However, "b" in main.cpp and
in the static library have different behavior BUT the function call name
as well as the calling conventions are identical.

main.cpp
Library
=============================== =========================
|--> function "a" : calls function "c"| | --> function "c" : calls
"b" |
| (from library) and "b" (local) | | (local)
|
|--> function "b" | | --> function
"b" |
=============================== ==========================

The idea is that when calling "b" from within "a", it is the function
"b" which is
in main.cpp whereas when calling "b" from within "c", the function "b"
within
the library shall be called.

When linking, it seems that the symbols are linked as if all symbols are
equivalent. Hence, when in "a", "b" from the library may be called by
accident or
when in "c", function "b" may be called which is part of main,cpp. In
both cases,
this is not intended and leads to a segmentation fault.

Question: Can I somehow tell the linker to prefer "local" function calls
such that
this problem is solved? It seems that the Visual Studio linker does it
this way..

Thank you for any help.

Best regards

Hauke
Florian Weimer
2014-08-19 09:11:37 UTC
Permalink
Post by Hauke Krüger(IND)
Question: Can I somehow tell the linker to prefer "local" function calls
such that this problem is solved?
This is more of a binutils question. One way to do this reliability
with static linking is to use "ld -r --retain-symbols-file".
--
Florian Weimer / Red Hat Product Security
Hauke Krüger(IND)
2014-08-19 09:18:33 UTC
Permalink
Hi,
thank you for your answer.
Post by Florian Weimer
Post by Hauke Krüger(IND)
Question: Can I somehow tell the linker to prefer "local" function calls
such that this problem is solved?
This is more of a binutils question. One way to do this reliability
with static linking is to use "ld -r --retain-symbols-file".
I can see with nm that the "--retain-symbols-file" option leads to a
library file that only exposes those symbols specified in the file
given as argument. Is that the list of symbols which also the ld tool
really sees? Why the option -r?
Florian Weimer
2014-08-19 09:21:31 UTC
Permalink
Post by Hauke Krüger(IND)
Hi,
thank you for your answer.
Post by Florian Weimer
Post by Hauke Krüger(IND)
Question: Can I somehow tell the linker to prefer "local" function calls
such that this problem is solved?
This is more of a binutils question. One way to do this reliability
with static linking is to use "ld -r --retain-symbols-file".
I can see with nm that the "--retain-symbols-file" option leads to a
library file that only exposes those symbols specified in the file
given as argument. Is that the list of symbols which also the ld tool
really sees?
Yes, if you look at "nm -D" output (or "readelf -W -s").
Post by Hauke Krüger(IND)
Why the option -r?
To produce an object which can be linked again (statically or even
dynamically, if the code is position-independent).
--
Florian Weimer / Red Hat Product Security
Loading...