Compiling GAS code doesn't detect -fPIC option
I am trying to compile some GAS code for a project using the GCC gnu compiler. Here is how I am compiling it:
gcc -c boot.s -o boot.o -fPIC
After I compile my kernel.c file with the -fPIC
argument, I try to link it with this command:
gcc -N -T linker.ld -o Slack Berry.bin -ffreestanding -nostdlib kernel.o boot.o -lgcc
It comes up with:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC
This leads me to think that it is not compiling my GAS code with -fPIC
. How can I fix this?
gcc assembly gas position-independent-code
add a comment |
I am trying to compile some GAS code for a project using the GCC gnu compiler. Here is how I am compiling it:
gcc -c boot.s -o boot.o -fPIC
After I compile my kernel.c file with the -fPIC
argument, I try to link it with this command:
gcc -N -T linker.ld -o Slack Berry.bin -ffreestanding -nostdlib kernel.o boot.o -lgcc
It comes up with:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC
This leads me to think that it is not compiling my GAS code with -fPIC
. How can I fix this?
gcc assembly gas position-independent-code
add a comment |
I am trying to compile some GAS code for a project using the GCC gnu compiler. Here is how I am compiling it:
gcc -c boot.s -o boot.o -fPIC
After I compile my kernel.c file with the -fPIC
argument, I try to link it with this command:
gcc -N -T linker.ld -o Slack Berry.bin -ffreestanding -nostdlib kernel.o boot.o -lgcc
It comes up with:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC
This leads me to think that it is not compiling my GAS code with -fPIC
. How can I fix this?
gcc assembly gas position-independent-code
I am trying to compile some GAS code for a project using the GCC gnu compiler. Here is how I am compiling it:
gcc -c boot.s -o boot.o -fPIC
After I compile my kernel.c file with the -fPIC
argument, I try to link it with this command:
gcc -N -T linker.ld -o Slack Berry.bin -ffreestanding -nostdlib kernel.o boot.o -lgcc
It comes up with:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC
This leads me to think that it is not compiling my GAS code with -fPIC
. How can I fix this?
gcc assembly gas position-independent-code
gcc assembly gas position-independent-code
edited Nov 14 '18 at 4:40
yugr
6,99221342
6,99221342
asked Nov 13 '18 at 23:47
Sushila Jyothi LévêqueSushila Jyothi Lévêque
4410
4410
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
First of all you probly need -fPIE
rather than -fPIC
. -fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).
Now both -fPIC
and -fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead of
movq $bar, %rdx
use
movq bar@GOTPCREL(%rip), %rdx
(normally to get the syntax I need I just run gcc -fPIE -S -o-
on matching C snippet).
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed islea bar(%rip), %rdx
, a RIP-relative LEA instead of amov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)
– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has-ffreestanding -nostdlib
, and has names likeboot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)
– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
|
show 1 more comment
Recompile with -fPIC
only applies if the asm was generated by a compiler, not written by hand. It has no effect on how asm is assembled into machine code.
The problem is that your PIE executable can't be linked with 32-bit absolute addresses. (Did you mean to make a PIE instead of a static position-dependent executable)?
You don't need the full shared-library stuff for referencing symbols in another library or the main executable (like @yugr's answer shows how to do). Your freestanding kernel may not even have a GOT or PLT, and definitely shouldn't use them for internal symbols.
The only change needed is lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r/m64
. (movabs would work to, but be larger and usually slower.)
Or, if you actually meant to build with -static
and create an executable that will be loaded at a fixed address in the low 32 bits of address space, you should use mov $bar, %edx
to get a 5-byte mov $imm32, %r32
encoding instead of 7-byte mov $sign_extended_imm32, %r/m64
or a 7-byte LEA. See also Difference between movq and movabsq in x86-64
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%2f53291129%2fcompiling-gas-code-doesnt-detect-fpic-option%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all you probly need -fPIE
rather than -fPIC
. -fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).
Now both -fPIC
and -fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead of
movq $bar, %rdx
use
movq bar@GOTPCREL(%rip), %rdx
(normally to get the syntax I need I just run gcc -fPIE -S -o-
on matching C snippet).
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed islea bar(%rip), %rdx
, a RIP-relative LEA instead of amov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)
– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has-ffreestanding -nostdlib
, and has names likeboot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)
– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
|
show 1 more comment
First of all you probly need -fPIE
rather than -fPIC
. -fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).
Now both -fPIC
and -fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead of
movq $bar, %rdx
use
movq bar@GOTPCREL(%rip), %rdx
(normally to get the syntax I need I just run gcc -fPIE -S -o-
on matching C snippet).
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed islea bar(%rip), %rdx
, a RIP-relative LEA instead of amov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)
– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has-ffreestanding -nostdlib
, and has names likeboot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)
– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
|
show 1 more comment
First of all you probly need -fPIE
rather than -fPIC
. -fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).
Now both -fPIC
and -fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead of
movq $bar, %rdx
use
movq bar@GOTPCREL(%rip), %rdx
(normally to get the syntax I need I just run gcc -fPIE -S -o-
on matching C snippet).
First of all you probly need -fPIE
rather than -fPIC
. -fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).
Now both -fPIC
and -fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead of
movq $bar, %rdx
use
movq bar@GOTPCREL(%rip), %rdx
(normally to get the syntax I need I just run gcc -fPIE -S -o-
on matching C snippet).
edited Nov 14 '18 at 5:26
answered Nov 14 '18 at 4:20
yugryugr
6,99221342
6,99221342
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed islea bar(%rip), %rdx
, a RIP-relative LEA instead of amov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)
– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has-ffreestanding -nostdlib
, and has names likeboot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)
– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
|
show 1 more comment
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed islea bar(%rip), %rdx
, a RIP-relative LEA instead of amov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)
– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has-ffreestanding -nostdlib
, and has names likeboot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)
– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed is
lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)– Peter Cordes
Nov 14 '18 at 4:52
The OP is making a PIE, not a shared library; they don't need the GOT or PLT for internal static symbols. The only change needed is
lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r64
. (movabs would work to, but be larger and usually slower.)– Peter Cordes
Nov 14 '18 at 4:52
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
@PeterCordes Thanks, I'll make an update. I think the question should be updated as well.
– yugr
Nov 14 '18 at 5:22
Why, what do you think is missing in the question? It has
-ffreestanding -nostdlib
, and has names like boot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)– Peter Cordes
Nov 14 '18 at 11:50
Why, what do you think is missing in the question? It has
-ffreestanding -nostdlib
, and has names like boot.o
and mentions multiboot. Also the error message mentions making a PIE executable. Seems pretty clear to me that the OP wasn't actually trying to make a shared library. (This is not the first question about this error message when making an executable from asm, either, so I wasn't at all expecting the question to be about actually making shared libs. Usually it's just PIE requiring relocatable-anywhere code catching people by surprise.)– Peter Cordes
Nov 14 '18 at 11:50
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
@PeterCordes Well, why not make things more explicit if possible?
– yugr
Nov 14 '18 at 15:59
1
1
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
@PeterCordes Yup, I developed ShlibVisibilityChecker specifically for that. Sadly I found that package maintainers usually don't care much about getting rid of spurious GOT/PLT references...
– yugr
Nov 14 '18 at 17:08
|
show 1 more comment
Recompile with -fPIC
only applies if the asm was generated by a compiler, not written by hand. It has no effect on how asm is assembled into machine code.
The problem is that your PIE executable can't be linked with 32-bit absolute addresses. (Did you mean to make a PIE instead of a static position-dependent executable)?
You don't need the full shared-library stuff for referencing symbols in another library or the main executable (like @yugr's answer shows how to do). Your freestanding kernel may not even have a GOT or PLT, and definitely shouldn't use them for internal symbols.
The only change needed is lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r/m64
. (movabs would work to, but be larger and usually slower.)
Or, if you actually meant to build with -static
and create an executable that will be loaded at a fixed address in the low 32 bits of address space, you should use mov $bar, %edx
to get a 5-byte mov $imm32, %r32
encoding instead of 7-byte mov $sign_extended_imm32, %r/m64
or a 7-byte LEA. See also Difference between movq and movabsq in x86-64
add a comment |
Recompile with -fPIC
only applies if the asm was generated by a compiler, not written by hand. It has no effect on how asm is assembled into machine code.
The problem is that your PIE executable can't be linked with 32-bit absolute addresses. (Did you mean to make a PIE instead of a static position-dependent executable)?
You don't need the full shared-library stuff for referencing symbols in another library or the main executable (like @yugr's answer shows how to do). Your freestanding kernel may not even have a GOT or PLT, and definitely shouldn't use them for internal symbols.
The only change needed is lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r/m64
. (movabs would work to, but be larger and usually slower.)
Or, if you actually meant to build with -static
and create an executable that will be loaded at a fixed address in the low 32 bits of address space, you should use mov $bar, %edx
to get a 5-byte mov $imm32, %r32
encoding instead of 7-byte mov $sign_extended_imm32, %r/m64
or a 7-byte LEA. See also Difference between movq and movabsq in x86-64
add a comment |
Recompile with -fPIC
only applies if the asm was generated by a compiler, not written by hand. It has no effect on how asm is assembled into machine code.
The problem is that your PIE executable can't be linked with 32-bit absolute addresses. (Did you mean to make a PIE instead of a static position-dependent executable)?
You don't need the full shared-library stuff for referencing symbols in another library or the main executable (like @yugr's answer shows how to do). Your freestanding kernel may not even have a GOT or PLT, and definitely shouldn't use them for internal symbols.
The only change needed is lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r/m64
. (movabs would work to, but be larger and usually slower.)
Or, if you actually meant to build with -static
and create an executable that will be loaded at a fixed address in the low 32 bits of address space, you should use mov $bar, %edx
to get a 5-byte mov $imm32, %r32
encoding instead of 7-byte mov $sign_extended_imm32, %r/m64
or a 7-byte LEA. See also Difference between movq and movabsq in x86-64
Recompile with -fPIC
only applies if the asm was generated by a compiler, not written by hand. It has no effect on how asm is assembled into machine code.
The problem is that your PIE executable can't be linked with 32-bit absolute addresses. (Did you mean to make a PIE instead of a static position-dependent executable)?
You don't need the full shared-library stuff for referencing symbols in another library or the main executable (like @yugr's answer shows how to do). Your freestanding kernel may not even have a GOT or PLT, and definitely shouldn't use them for internal symbols.
The only change needed is lea bar(%rip), %rdx
, a RIP-relative LEA instead of a mov $imm32, %r/m64
. (movabs would work to, but be larger and usually slower.)
Or, if you actually meant to build with -static
and create an executable that will be loaded at a fixed address in the low 32 bits of address space, you should use mov $bar, %edx
to get a 5-byte mov $imm32, %r32
encoding instead of 7-byte mov $sign_extended_imm32, %r/m64
or a 7-byte LEA. See also Difference between movq and movabsq in x86-64
answered Nov 14 '18 at 4:57
Peter CordesPeter Cordes
122k17184312
122k17184312
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.
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%2f53291129%2fcompiling-gas-code-doesnt-detect-fpic-option%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