Difference between the linker flags

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm adding c++ runtime and exception support to the Linux kernel. For that, I need to provide my own lib/gcc and lib/libstdc++instead of the standard libraries provided by the compiler.



So, I am confused with the flags that are to be passed to the linker. In a normal kernel's top-level Makefile, LD = $(CROSS_COMPILE)ld which enables the kernel to use the default standard libraries and startup files. For my kernel I'm using LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles as said in a documentation. What I understood from gcc documentation is that passing -nostdlib to the linker is that of passing both -nodefaultlibs -nostartfiles. What's actually the difference between these flags?







share|improve this question

























    up vote
    1
    down vote

    favorite












    I'm adding c++ runtime and exception support to the Linux kernel. For that, I need to provide my own lib/gcc and lib/libstdc++instead of the standard libraries provided by the compiler.



    So, I am confused with the flags that are to be passed to the linker. In a normal kernel's top-level Makefile, LD = $(CROSS_COMPILE)ld which enables the kernel to use the default standard libraries and startup files. For my kernel I'm using LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles as said in a documentation. What I understood from gcc documentation is that passing -nostdlib to the linker is that of passing both -nodefaultlibs -nostartfiles. What's actually the difference between these flags?







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm adding c++ runtime and exception support to the Linux kernel. For that, I need to provide my own lib/gcc and lib/libstdc++instead of the standard libraries provided by the compiler.



      So, I am confused with the flags that are to be passed to the linker. In a normal kernel's top-level Makefile, LD = $(CROSS_COMPILE)ld which enables the kernel to use the default standard libraries and startup files. For my kernel I'm using LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles as said in a documentation. What I understood from gcc documentation is that passing -nostdlib to the linker is that of passing both -nodefaultlibs -nostartfiles. What's actually the difference between these flags?







      share|improve this question













      I'm adding c++ runtime and exception support to the Linux kernel. For that, I need to provide my own lib/gcc and lib/libstdc++instead of the standard libraries provided by the compiler.



      So, I am confused with the flags that are to be passed to the linker. In a normal kernel's top-level Makefile, LD = $(CROSS_COMPILE)ld which enables the kernel to use the default standard libraries and startup files. For my kernel I'm using LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles as said in a documentation. What I understood from gcc documentation is that passing -nostdlib to the linker is that of passing both -nodefaultlibs -nostartfiles. What's actually the difference between these flags?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 27 at 10:48









      SivaPrasath

      3,88611737




      3,88611737









      asked Jun 27 at 10:15









      jaya shankar reddy kommuru

      84




      84




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          These flags are defined in GCC’s spec files, so the best way to determine the differences between them is to look there:



          gcc -dumpspecs


          The relevant part is the link_command definition. This shows that -nostdlib, -nodefaultlibs and -nostartfiles have the following impact:




          • %!nostdlib:%!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence)) — this adds libgcc, libpthread, libc, libieee as necessary, using a macro and the lib and libgcc spec strings;


          • %!nostdlib:%!nostartfiles:%S — this adds the startfile spec string, , which specifies the object files to add to handle startup (crti.o etc.)


          • %!nostdlib:%fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end %fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end — this adds virtual table verification using libvtv


          • %!nostdlib:%!nodefaultlibs:%mmpx:%fcheck-pointer-bounds: %static:--whole-archive -lmpx --no-whole-archive %:include(libmpx.spec)%(link_libmpx) %!static:%static-libmpx:-Bstatic --whole-archive %!static-libmpx:--push-state --no-as-needed -lmpx %!static-libmpx:--pop-state %static-libmpx:--no-whole-archive -Bdynamic %:include(libmpx.spec)%(link_libmpx)%mmpx:%fcheck-pointer-bounds:%!fno-chkp-use-wrappers: %static:-lmpxwrappers %!static:%static-libmpxwrappers:-Bstatic -lmpxwrappers %static-libmpxwrappers: -Bdynamic — this handles libmpx


          • %!nostdlib:%!nodefaultlibs:%%:sanitize(address): %static-libasan:%:include(libsanitizer.spec)%(link_libasan) %static:%ecannot specify -static with -fsanitize=address %%:sanitize(thread): %static-libtsan:%:include(libsanitizer.spec)%(link_libtsan) %static:%ecannot specify -static with -fsanitize=thread %%:sanitize(undefined):%static-libubsan:-Bstatic -lubsan %static-libubsan:-Bdynamic %static-libubsan:%:include(libsanitizer.spec)%(link_libubsan) %%:sanitize(leak): %static-liblsan:%:include(libsanitizer.spec)%(link_liblsan) — this handles the various sanitisation options


          • %!nostdlib:%!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence) — this adds the stack protection options and repeats the C link sequence (whose libraries were already specified at the start)


          • %!nostdlib:%!nostartfiles:%E — this adds the endfile spec string, which specifies the object files to add to handle left-overs (crtfastmath.o, crtend.o etc.)

          As you understood from the documentation, -nostdlib is a superset of -nodefaultlibs and -nostartfiles. It also disables virtual table verification.



          So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don’t add anything to it. (But it doesn’t hurt to mention them too.)






          share|improve this answer





















          • OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
            – jaya shankar reddy kommuru
            Jun 27 at 12:56











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452187%2fdifference-between-the-linker-flags%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          These flags are defined in GCC’s spec files, so the best way to determine the differences between them is to look there:



          gcc -dumpspecs


          The relevant part is the link_command definition. This shows that -nostdlib, -nodefaultlibs and -nostartfiles have the following impact:




          • %!nostdlib:%!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence)) — this adds libgcc, libpthread, libc, libieee as necessary, using a macro and the lib and libgcc spec strings;


          • %!nostdlib:%!nostartfiles:%S — this adds the startfile spec string, , which specifies the object files to add to handle startup (crti.o etc.)


          • %!nostdlib:%fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end %fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end — this adds virtual table verification using libvtv


          • %!nostdlib:%!nodefaultlibs:%mmpx:%fcheck-pointer-bounds: %static:--whole-archive -lmpx --no-whole-archive %:include(libmpx.spec)%(link_libmpx) %!static:%static-libmpx:-Bstatic --whole-archive %!static-libmpx:--push-state --no-as-needed -lmpx %!static-libmpx:--pop-state %static-libmpx:--no-whole-archive -Bdynamic %:include(libmpx.spec)%(link_libmpx)%mmpx:%fcheck-pointer-bounds:%!fno-chkp-use-wrappers: %static:-lmpxwrappers %!static:%static-libmpxwrappers:-Bstatic -lmpxwrappers %static-libmpxwrappers: -Bdynamic — this handles libmpx


          • %!nostdlib:%!nodefaultlibs:%%:sanitize(address): %static-libasan:%:include(libsanitizer.spec)%(link_libasan) %static:%ecannot specify -static with -fsanitize=address %%:sanitize(thread): %static-libtsan:%:include(libsanitizer.spec)%(link_libtsan) %static:%ecannot specify -static with -fsanitize=thread %%:sanitize(undefined):%static-libubsan:-Bstatic -lubsan %static-libubsan:-Bdynamic %static-libubsan:%:include(libsanitizer.spec)%(link_libubsan) %%:sanitize(leak): %static-liblsan:%:include(libsanitizer.spec)%(link_liblsan) — this handles the various sanitisation options


          • %!nostdlib:%!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence) — this adds the stack protection options and repeats the C link sequence (whose libraries were already specified at the start)


          • %!nostdlib:%!nostartfiles:%E — this adds the endfile spec string, which specifies the object files to add to handle left-overs (crtfastmath.o, crtend.o etc.)

          As you understood from the documentation, -nostdlib is a superset of -nodefaultlibs and -nostartfiles. It also disables virtual table verification.



          So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don’t add anything to it. (But it doesn’t hurt to mention them too.)






          share|improve this answer





















          • OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
            – jaya shankar reddy kommuru
            Jun 27 at 12:56















          up vote
          0
          down vote



          accepted










          These flags are defined in GCC’s spec files, so the best way to determine the differences between them is to look there:



          gcc -dumpspecs


          The relevant part is the link_command definition. This shows that -nostdlib, -nodefaultlibs and -nostartfiles have the following impact:




          • %!nostdlib:%!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence)) — this adds libgcc, libpthread, libc, libieee as necessary, using a macro and the lib and libgcc spec strings;


          • %!nostdlib:%!nostartfiles:%S — this adds the startfile spec string, , which specifies the object files to add to handle startup (crti.o etc.)


          • %!nostdlib:%fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end %fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end — this adds virtual table verification using libvtv


          • %!nostdlib:%!nodefaultlibs:%mmpx:%fcheck-pointer-bounds: %static:--whole-archive -lmpx --no-whole-archive %:include(libmpx.spec)%(link_libmpx) %!static:%static-libmpx:-Bstatic --whole-archive %!static-libmpx:--push-state --no-as-needed -lmpx %!static-libmpx:--pop-state %static-libmpx:--no-whole-archive -Bdynamic %:include(libmpx.spec)%(link_libmpx)%mmpx:%fcheck-pointer-bounds:%!fno-chkp-use-wrappers: %static:-lmpxwrappers %!static:%static-libmpxwrappers:-Bstatic -lmpxwrappers %static-libmpxwrappers: -Bdynamic — this handles libmpx


          • %!nostdlib:%!nodefaultlibs:%%:sanitize(address): %static-libasan:%:include(libsanitizer.spec)%(link_libasan) %static:%ecannot specify -static with -fsanitize=address %%:sanitize(thread): %static-libtsan:%:include(libsanitizer.spec)%(link_libtsan) %static:%ecannot specify -static with -fsanitize=thread %%:sanitize(undefined):%static-libubsan:-Bstatic -lubsan %static-libubsan:-Bdynamic %static-libubsan:%:include(libsanitizer.spec)%(link_libubsan) %%:sanitize(leak): %static-liblsan:%:include(libsanitizer.spec)%(link_liblsan) — this handles the various sanitisation options


          • %!nostdlib:%!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence) — this adds the stack protection options and repeats the C link sequence (whose libraries were already specified at the start)


          • %!nostdlib:%!nostartfiles:%E — this adds the endfile spec string, which specifies the object files to add to handle left-overs (crtfastmath.o, crtend.o etc.)

          As you understood from the documentation, -nostdlib is a superset of -nodefaultlibs and -nostartfiles. It also disables virtual table verification.



          So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don’t add anything to it. (But it doesn’t hurt to mention them too.)






          share|improve this answer





















          • OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
            – jaya shankar reddy kommuru
            Jun 27 at 12:56













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          These flags are defined in GCC’s spec files, so the best way to determine the differences between them is to look there:



          gcc -dumpspecs


          The relevant part is the link_command definition. This shows that -nostdlib, -nodefaultlibs and -nostartfiles have the following impact:




          • %!nostdlib:%!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence)) — this adds libgcc, libpthread, libc, libieee as necessary, using a macro and the lib and libgcc spec strings;


          • %!nostdlib:%!nostartfiles:%S — this adds the startfile spec string, , which specifies the object files to add to handle startup (crti.o etc.)


          • %!nostdlib:%fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end %fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end — this adds virtual table verification using libvtv


          • %!nostdlib:%!nodefaultlibs:%mmpx:%fcheck-pointer-bounds: %static:--whole-archive -lmpx --no-whole-archive %:include(libmpx.spec)%(link_libmpx) %!static:%static-libmpx:-Bstatic --whole-archive %!static-libmpx:--push-state --no-as-needed -lmpx %!static-libmpx:--pop-state %static-libmpx:--no-whole-archive -Bdynamic %:include(libmpx.spec)%(link_libmpx)%mmpx:%fcheck-pointer-bounds:%!fno-chkp-use-wrappers: %static:-lmpxwrappers %!static:%static-libmpxwrappers:-Bstatic -lmpxwrappers %static-libmpxwrappers: -Bdynamic — this handles libmpx


          • %!nostdlib:%!nodefaultlibs:%%:sanitize(address): %static-libasan:%:include(libsanitizer.spec)%(link_libasan) %static:%ecannot specify -static with -fsanitize=address %%:sanitize(thread): %static-libtsan:%:include(libsanitizer.spec)%(link_libtsan) %static:%ecannot specify -static with -fsanitize=thread %%:sanitize(undefined):%static-libubsan:-Bstatic -lubsan %static-libubsan:-Bdynamic %static-libubsan:%:include(libsanitizer.spec)%(link_libubsan) %%:sanitize(leak): %static-liblsan:%:include(libsanitizer.spec)%(link_liblsan) — this handles the various sanitisation options


          • %!nostdlib:%!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence) — this adds the stack protection options and repeats the C link sequence (whose libraries were already specified at the start)


          • %!nostdlib:%!nostartfiles:%E — this adds the endfile spec string, which specifies the object files to add to handle left-overs (crtfastmath.o, crtend.o etc.)

          As you understood from the documentation, -nostdlib is a superset of -nodefaultlibs and -nostartfiles. It also disables virtual table verification.



          So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don’t add anything to it. (But it doesn’t hurt to mention them too.)






          share|improve this answer













          These flags are defined in GCC’s spec files, so the best way to determine the differences between them is to look there:



          gcc -dumpspecs


          The relevant part is the link_command definition. This shows that -nostdlib, -nodefaultlibs and -nostartfiles have the following impact:




          • %!nostdlib:%!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence)) — this adds libgcc, libpthread, libc, libieee as necessary, using a macro and the lib and libgcc spec strings;


          • %!nostdlib:%!nostartfiles:%S — this adds the startfile spec string, , which specifies the object files to add to handle startup (crti.o etc.)


          • %!nostdlib:%fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end %fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end — this adds virtual table verification using libvtv


          • %!nostdlib:%!nodefaultlibs:%mmpx:%fcheck-pointer-bounds: %static:--whole-archive -lmpx --no-whole-archive %:include(libmpx.spec)%(link_libmpx) %!static:%static-libmpx:-Bstatic --whole-archive %!static-libmpx:--push-state --no-as-needed -lmpx %!static-libmpx:--pop-state %static-libmpx:--no-whole-archive -Bdynamic %:include(libmpx.spec)%(link_libmpx)%mmpx:%fcheck-pointer-bounds:%!fno-chkp-use-wrappers: %static:-lmpxwrappers %!static:%static-libmpxwrappers:-Bstatic -lmpxwrappers %static-libmpxwrappers: -Bdynamic — this handles libmpx


          • %!nostdlib:%!nodefaultlibs:%%:sanitize(address): %static-libasan:%:include(libsanitizer.spec)%(link_libasan) %static:%ecannot specify -static with -fsanitize=address %%:sanitize(thread): %static-libtsan:%:include(libsanitizer.spec)%(link_libtsan) %static:%ecannot specify -static with -fsanitize=thread %%:sanitize(undefined):%static-libubsan:-Bstatic -lubsan %static-libubsan:-Bdynamic %static-libubsan:%:include(libsanitizer.spec)%(link_libubsan) %%:sanitize(leak): %static-liblsan:%:include(libsanitizer.spec)%(link_liblsan) — this handles the various sanitisation options


          • %!nostdlib:%!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence) — this adds the stack protection options and repeats the C link sequence (whose libraries were already specified at the start)


          • %!nostdlib:%!nostartfiles:%E — this adds the endfile spec string, which specifies the object files to add to handle left-overs (crtfastmath.o, crtend.o etc.)

          As you understood from the documentation, -nostdlib is a superset of -nodefaultlibs and -nostartfiles. It also disables virtual table verification.



          So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don’t add anything to it. (But it doesn’t hurt to mention them too.)







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 27 at 11:31









          Stephen Kitt

          139k22299361




          139k22299361











          • OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
            – jaya shankar reddy kommuru
            Jun 27 at 12:56

















          • OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
            – jaya shankar reddy kommuru
            Jun 27 at 12:56
















          OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
          – jaya shankar reddy kommuru
          Jun 27 at 12:56





          OK I got it. But I cannot understand why there occurs a misalignment of load segments of vmlinux when the line LD = $(CROSS_COMPILE)ld -nostdlib is changed to LD = $(CROSS_COMPILE)ld -nostdlib -nodefaultlibs -nostartfiles. Instead of aligning at a mutiple of 2MB, they align at 1KB or something like that.
          – jaya shankar reddy kommuru
          Jun 27 at 12:56













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452187%2fdifference-between-the-linker-flags%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay