C++ objcopy to make class method weak
I have this header file:
weather.h
#ifndef _WEATHER_H_
#define _WEATHER_H_
#include <string>
using namespace std;
class Weather {
private:
int temp;
public:
Weather();
string announce();
};
#endif
When I compile the related source file, I want to make announce and Weather (the default constructor) weak during compile time.
To do so, the flow looks something like:
g++ -std=c++11 -g -Wall -c weather.cpp
objcopy --weaken-symbol=announce --weaken-symbol=Weather weather.o weather.o
However, when I then compile another version of weather without the weakening? I still get a duplicate symbol error.
I know --weaken exists, but this is just a sample and I do not want to blanket weaken every method in the class.
c++ g++ clang
add a comment |
I have this header file:
weather.h
#ifndef _WEATHER_H_
#define _WEATHER_H_
#include <string>
using namespace std;
class Weather {
private:
int temp;
public:
Weather();
string announce();
};
#endif
When I compile the related source file, I want to make announce and Weather (the default constructor) weak during compile time.
To do so, the flow looks something like:
g++ -std=c++11 -g -Wall -c weather.cpp
objcopy --weaken-symbol=announce --weaken-symbol=Weather weather.o weather.o
However, when I then compile another version of weather without the weakening? I still get a duplicate symbol error.
I know --weaken exists, but this is just a sample and I do not want to blanket weaken every method in the class.
c++ g++ clang
Can you post the relevantreadelf
lines from both weather.o files to confirm one is weak and the other is not? Does using--weaken-symbol announce
without the equals sign make any difference?
– Paul
Nov 26 '18 at 0:23
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.
– Pete Becker
Nov 26 '18 at 3:48
add a comment |
I have this header file:
weather.h
#ifndef _WEATHER_H_
#define _WEATHER_H_
#include <string>
using namespace std;
class Weather {
private:
int temp;
public:
Weather();
string announce();
};
#endif
When I compile the related source file, I want to make announce and Weather (the default constructor) weak during compile time.
To do so, the flow looks something like:
g++ -std=c++11 -g -Wall -c weather.cpp
objcopy --weaken-symbol=announce --weaken-symbol=Weather weather.o weather.o
However, when I then compile another version of weather without the weakening? I still get a duplicate symbol error.
I know --weaken exists, but this is just a sample and I do not want to blanket weaken every method in the class.
c++ g++ clang
I have this header file:
weather.h
#ifndef _WEATHER_H_
#define _WEATHER_H_
#include <string>
using namespace std;
class Weather {
private:
int temp;
public:
Weather();
string announce();
};
#endif
When I compile the related source file, I want to make announce and Weather (the default constructor) weak during compile time.
To do so, the flow looks something like:
g++ -std=c++11 -g -Wall -c weather.cpp
objcopy --weaken-symbol=announce --weaken-symbol=Weather weather.o weather.o
However, when I then compile another version of weather without the weakening? I still get a duplicate symbol error.
I know --weaken exists, but this is just a sample and I do not want to blanket weaken every method in the class.
c++ g++ clang
c++ g++ clang
asked Nov 26 '18 at 0:18
jajabarrjajabarr
8111
8111
Can you post the relevantreadelf
lines from both weather.o files to confirm one is weak and the other is not? Does using--weaken-symbol announce
without the equals sign make any difference?
– Paul
Nov 26 '18 at 0:23
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.
– Pete Becker
Nov 26 '18 at 3:48
add a comment |
Can you post the relevantreadelf
lines from both weather.o files to confirm one is weak and the other is not? Does using--weaken-symbol announce
without the equals sign make any difference?
– Paul
Nov 26 '18 at 0:23
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.
– Pete Becker
Nov 26 '18 at 3:48
Can you post the relevant
readelf
lines from both weather.o files to confirm one is weak and the other is not? Does using --weaken-symbol announce
without the equals sign make any difference?– Paul
Nov 26 '18 at 0:23
Can you post the relevant
readelf
lines from both weather.o files to confirm one is weak and the other is not? Does using --weaken-symbol announce
without the equals sign make any difference?– Paul
Nov 26 '18 at 0:23
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (
_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.– Pete Becker
Nov 26 '18 at 3:48
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (
_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.– Pete Becker
Nov 26 '18 at 3:48
add a comment |
1 Answer
1
active
oldest
votes
Weakening the symbols in the one file and linking all the objects together works for me. Make sure you are relinking all the objects (including all constructors).
weather1.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather");
}
weather2.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather 2");
}
build.sh
g++ weather1.cpp -c -o weather1.o
g++ weather2.cpp -c -o weather2.o
g++ test.cpp -c -o test.o
objcopy --weaken-symbol=_ZN7Weather8announceEv --weaken-symbol=_ZN7WeatherC2Ev --weaken-symbol=_ZN7WeatherC1Ev weather2.o weather2.o
g++ *.o -o test.out
Depending on whether I weaken weather1.o or weather2.o, I see different outputs from my test main function:
int main()
{
Weather w;
std::cout << w.announce() << "n";
return 0;
}
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473333%2fc-objcopy-to-make-class-method-weak%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Weakening the symbols in the one file and linking all the objects together works for me. Make sure you are relinking all the objects (including all constructors).
weather1.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather");
}
weather2.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather 2");
}
build.sh
g++ weather1.cpp -c -o weather1.o
g++ weather2.cpp -c -o weather2.o
g++ test.cpp -c -o test.o
objcopy --weaken-symbol=_ZN7Weather8announceEv --weaken-symbol=_ZN7WeatherC2Ev --weaken-symbol=_ZN7WeatherC1Ev weather2.o weather2.o
g++ *.o -o test.out
Depending on whether I weaken weather1.o or weather2.o, I see different outputs from my test main function:
int main()
{
Weather w;
std::cout << w.announce() << "n";
return 0;
}
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
add a comment |
Weakening the symbols in the one file and linking all the objects together works for me. Make sure you are relinking all the objects (including all constructors).
weather1.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather");
}
weather2.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather 2");
}
build.sh
g++ weather1.cpp -c -o weather1.o
g++ weather2.cpp -c -o weather2.o
g++ test.cpp -c -o test.o
objcopy --weaken-symbol=_ZN7Weather8announceEv --weaken-symbol=_ZN7WeatherC2Ev --weaken-symbol=_ZN7WeatherC1Ev weather2.o weather2.o
g++ *.o -o test.out
Depending on whether I weaken weather1.o or weather2.o, I see different outputs from my test main function:
int main()
{
Weather w;
std::cout << w.announce() << "n";
return 0;
}
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
add a comment |
Weakening the symbols in the one file and linking all the objects together works for me. Make sure you are relinking all the objects (including all constructors).
weather1.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather");
}
weather2.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather 2");
}
build.sh
g++ weather1.cpp -c -o weather1.o
g++ weather2.cpp -c -o weather2.o
g++ test.cpp -c -o test.o
objcopy --weaken-symbol=_ZN7Weather8announceEv --weaken-symbol=_ZN7WeatherC2Ev --weaken-symbol=_ZN7WeatherC1Ev weather2.o weather2.o
g++ *.o -o test.out
Depending on whether I weaken weather1.o or weather2.o, I see different outputs from my test main function:
int main()
{
Weather w;
std::cout << w.announce() << "n";
return 0;
}
Weakening the symbols in the one file and linking all the objects together works for me. Make sure you are relinking all the objects (including all constructors).
weather1.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather");
}
weather2.cpp
Weather::Weather() {}
string Weather::announce()
{
return string("Bad weather 2");
}
build.sh
g++ weather1.cpp -c -o weather1.o
g++ weather2.cpp -c -o weather2.o
g++ test.cpp -c -o test.o
objcopy --weaken-symbol=_ZN7Weather8announceEv --weaken-symbol=_ZN7WeatherC2Ev --weaken-symbol=_ZN7WeatherC1Ev weather2.o weather2.o
g++ *.o -o test.out
Depending on whether I weaken weather1.o or weather2.o, I see different outputs from my test main function:
int main()
{
Weather w;
std::cout << w.announce() << "n";
return 0;
}
answered Nov 26 '18 at 3:31
PaulPaul
3606
3606
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
add a comment |
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Similarly, it also works to weaken only certain symbols and redefine those in the other file.
– Paul
Nov 26 '18 at 3:35
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
Interesting to note that if both functions are marked weak, the first one in the final link command will be the one used.
– Paul
Nov 26 '18 at 3:52
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
This is what I ended up finding - Thank you! Funnily enough I had the code for this written but it wasn't working on mac. I found a bug thread saying objcopy had some issues with macOS and x86 architecture - I ended up having to run it in a linux VM to see any progress.
– jajabarr
Nov 27 '18 at 1:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473333%2fc-objcopy-to-make-class-method-weak%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you post the relevant
readelf
lines from both weather.o files to confirm one is weak and the other is not? Does using--weaken-symbol announce
without the equals sign make any difference?– Paul
Nov 26 '18 at 0:23
Your C++ compiler, like every other C++ compiler in existence, uses name mangling. You need to figure out the mangled names of the constructor and announce. I can guarantee you, with 100% certainty, that it's not "Weather" and "announce".
– Sam Varshavchik
Nov 26 '18 at 0:31
@Paul Objdump isn't showing any weak symbols, and I'm seeing different behavior than when I manually insert weak attributes in the header file (which I don't want to do) SamVarshavchik even when I specify the mangled symbol name it does not work
– jajabarr
Nov 26 '18 at 3:07
@jajabarr Seems to work fine for me. I'll add my solution in an answer
– Paul
Nov 26 '18 at 3:26
This doesn’t address the question, but names that begin with an underscore followed by a capital letter (
_WEATHER_H_
) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code.– Pete Becker
Nov 26 '18 at 3:48