Compiling GAS code doesn't detect -fPIC option












0















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?










share|improve this question





























    0















    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?










    share|improve this question



























      0












      0








      0








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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).






          share|improve this answer


























          • 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











          • 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






          • 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



















          1














          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






          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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).






            share|improve this answer


























            • 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











            • 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






            • 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
















            1














            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).






            share|improve this answer


























            • 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











            • 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






            • 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














            1












            1








            1







            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).






            share|improve this answer















            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).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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











            • 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






            • 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











            • @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











            • @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













            1














            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






            share|improve this answer




























              1














              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






              share|improve this answer


























                1












                1








                1







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 4:57









                Peter CordesPeter Cordes

                122k17184312




                122k17184312






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()