consequences of multiple weak symbols(C Linker)
up vote
-1
down vote
favorite
I just have a question about potential problem of multiple weak symbols, this question is from my textbook:
One module A:
int x;
int y;
p1() ...
the other module B:
double x;
p2() ...
and my textbook says that 'write to x in p2 might overwrite y'
I can kind of get the idea of the textbook( double x is twice the size of int x, and int y is placed right after int x, here comes the problem), but still lost in details, I know when there are multiple weak symbols, the linker will just randomly pick one, so my question is, which x of module that the linker choose will result in writing to x in p2 will overwrite y.
This is my understanding :if the linker choose the int x of module A will result in the consequence, because in that way x,y are both 4 bytes and the p2(image after compilation there is one assembly code movq
compared by movl
in p1 )will change 8 bytes therefore overwrite y.
But my instructor said if only the linker choose double x of module B, that will result in overwriting y, how come, am I correct or my instructor is correct?
c linker
add a comment |
up vote
-1
down vote
favorite
I just have a question about potential problem of multiple weak symbols, this question is from my textbook:
One module A:
int x;
int y;
p1() ...
the other module B:
double x;
p2() ...
and my textbook says that 'write to x in p2 might overwrite y'
I can kind of get the idea of the textbook( double x is twice the size of int x, and int y is placed right after int x, here comes the problem), but still lost in details, I know when there are multiple weak symbols, the linker will just randomly pick one, so my question is, which x of module that the linker choose will result in writing to x in p2 will overwrite y.
This is my understanding :if the linker choose the int x of module A will result in the consequence, because in that way x,y are both 4 bytes and the p2(image after compilation there is one assembly code movq
compared by movl
in p1 )will change 8 bytes therefore overwrite y.
But my instructor said if only the linker choose double x of module B, that will result in overwriting y, how come, am I correct or my instructor is correct?
c linker
3
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I just have a question about potential problem of multiple weak symbols, this question is from my textbook:
One module A:
int x;
int y;
p1() ...
the other module B:
double x;
p2() ...
and my textbook says that 'write to x in p2 might overwrite y'
I can kind of get the idea of the textbook( double x is twice the size of int x, and int y is placed right after int x, here comes the problem), but still lost in details, I know when there are multiple weak symbols, the linker will just randomly pick one, so my question is, which x of module that the linker choose will result in writing to x in p2 will overwrite y.
This is my understanding :if the linker choose the int x of module A will result in the consequence, because in that way x,y are both 4 bytes and the p2(image after compilation there is one assembly code movq
compared by movl
in p1 )will change 8 bytes therefore overwrite y.
But my instructor said if only the linker choose double x of module B, that will result in overwriting y, how come, am I correct or my instructor is correct?
c linker
I just have a question about potential problem of multiple weak symbols, this question is from my textbook:
One module A:
int x;
int y;
p1() ...
the other module B:
double x;
p2() ...
and my textbook says that 'write to x in p2 might overwrite y'
I can kind of get the idea of the textbook( double x is twice the size of int x, and int y is placed right after int x, here comes the problem), but still lost in details, I know when there are multiple weak symbols, the linker will just randomly pick one, so my question is, which x of module that the linker choose will result in writing to x in p2 will overwrite y.
This is my understanding :if the linker choose the int x of module A will result in the consequence, because in that way x,y are both 4 bytes and the p2(image after compilation there is one assembly code movq
compared by movl
in p1 )will change 8 bytes therefore overwrite y.
But my instructor said if only the linker choose double x of module B, that will result in overwriting y, how come, am I correct or my instructor is correct?
c linker
c linker
asked Nov 12 at 2:44
amjad
3468
3468
3
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53
add a comment |
3
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53
3
3
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*
"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.
If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y;
and the other has double x
, that a write through the double x
alias will clobber y
. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union
between two structures or some such.
Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read
function to replace the POSIX one, for instance. The library itself will not break not matter how read
is redefined; when it needs to call read
, it doesn't use the weak symbol read
but some internal alias like __libc_read
.
This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read
as an external name.
* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."
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%2f53255357%2fconsequences-of-multiple-weak-symbolsc-linker%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
up vote
0
down vote
According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*
"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.
If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y;
and the other has double x
, that a write through the double x
alias will clobber y
. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union
between two structures or some such.
Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read
function to replace the POSIX one, for instance. The library itself will not break not matter how read
is redefined; when it needs to call read
, it doesn't use the weak symbol read
but some internal alias like __libc_read
.
This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read
as an external name.
* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."
add a comment |
up vote
0
down vote
According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*
"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.
If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y;
and the other has double x
, that a write through the double x
alias will clobber y
. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union
between two structures or some such.
Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read
function to replace the POSIX one, for instance. The library itself will not break not matter how read
is redefined; when it needs to call read
, it doesn't use the weak symbol read
but some internal alias like __libc_read
.
This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read
as an external name.
* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."
add a comment |
up vote
0
down vote
up vote
0
down vote
According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*
"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.
If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y;
and the other has double x
, that a write through the double x
alias will clobber y
. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union
between two structures or some such.
Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read
function to replace the POSIX one, for instance. The library itself will not break not matter how read
is redefined; when it needs to call read
, it doesn't use the weak symbol read
but some internal alias like __libc_read
.
This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read
as an external name.
* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."
According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*
"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.
If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y;
and the other has double x
, that a write through the double x
alias will clobber y
. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union
between two structures or some such.
Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read
function to replace the POSIX one, for instance. The library itself will not break not matter how read
is redefined; when it needs to call read
, it doesn't use the weak symbol read
but some internal alias like __libc_read
.
This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read
as an external name.
* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."
edited Nov 12 at 7:18
answered Nov 12 at 6:32
Kaz
37.6k765103
37.6k765103
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53255357%2fconsequences-of-multiple-weak-symbolsc-linker%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
3
Why is an instructor teaching nonsense like this? These are not weak symbols and it hardly matters what happens unless you're writing exploits for software with such a bug, since otherwise it's simply undefined behavior and you just don't do it.
– R..
Nov 12 at 3:53