Thread: linker

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    linker

    Hello everyone,


    Here are two conflicting documents from MSDN. About whether linker will modify code generated by compiler. I quote both of them here.

    Document (1) mentioned linker can not modify any code generated by compiler, but document (2) mentioned linker will replace address code generated by compiler during fixup process.

    Which one is correct?

    1.

    http://blogs.msdn.com/oldnewthing/ar...21/673830.aspx

    --------------------
    But the linker can't rewrite the code generated by the compiler.
    --------------------

    2.

    http://www.microsoft.com/msj/0797/hood0797.aspx

    --------------------
    Consider a call to a function named Foo in C++:


    //...
    Foo();
    //...



    The exact bytes emitted from a 32-bit C++ compiler will be this:


    E8 00 00 00 00



    The 0xE8 is the CALL instruction opcode. The next DWORD should contain the offset to the Foo function (relative to the CALL instruction). It's pretty clear that Foo probably isn't zero bytes away from the CALL instruction. Simply put, this code wouldn't work as expected if you were to execute it. The code is broken, and needs to be fixed up. In the above example, the linker needs to replace the DWORD following the CALL opcode with the correct address of Foo.
    --------------------


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I couldn't be bothered to read all the text in the pages you linked to, but I know how it works from experience.

    The linker WILL change the code generated by the compiler when the compiler is producing function calls outside of the current compile unit [a compile unit is normally one .c/.cpp file], where the linker finds the referred function in a different object file (the result of a different compile unit being compiled). If it can't find the function, it will generate an error.

    The loader, when doing "fixup" will also modify these entries, when linking in the DLL's that the executable is using, e.g. C runtime library or Kernel32.dll.

    In the case of DLL's being loaded into the system, these will potentially need to be relocated too - as the default DLL address isn't always available in the system, and thus the DLL will have to be located at a different (virtual) address, and all the addresses within the DLL that refer to absolute (virtual) addresses within that DLL will have to be "fixed up".

    So, yes, the linker will definitely change what the compiler generated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Your reply is great! My question is answered.

    Quote Originally Posted by matsp View Post
    I couldn't be bothered to read all the text in the pages you linked to, but I know how it works from experience.

    The linker WILL change the code generated by the compiler when the compiler is producing function calls outside of the current compile unit [a compile unit is normally one .c/.cpp file], where the linker finds the referred function in a different object file (the result of a different compile unit being compiled). If it can't find the function, it will generate an error.

    The loader, when doing "fixup" will also modify these entries, when linking in the DLL's that the executable is using, e.g. C runtime library or Kernel32.dll.

    In the case of DLL's being loaded into the system, these will potentially need to be relocated too - as the default DLL address isn't always available in the system, and thus the DLL will have to be located at a different (virtual) address, and all the addresses within the DLL that refer to absolute (virtual) addresses within that DLL will have to be "fixed up".

    So, yes, the linker will definitely change what the compiler generated.

    --
    Mats

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    blogs.msdn.com contains personal opinions of various MS-associated people and does NOT constitute official documentation. In case of conflict, assume the core MSDN to be correct unless the article specifically mentions an error in the official docs.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    blogs.msdn.com contains personal opinions of various MS-associated people and does NOT constitute official documentation. In case of conflict,
    assume the core MSDN to be correct unless the article specifically mentions an error in the official docs.
    I agree. What is your comments and ideas on my original question?


    regards,
    George

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I posted them in the other thread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Do you mean #12 of topic dllimport?

    http://cboard.cprogramming.com/showt...219#post721219

    If yes, I think you mean you agree with (2) in my original question. Right?

    Quote Originally Posted by CornedBee View Post
    I posted them in the other thread.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  2. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  3. Linker errors when compiling
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 10-07-2006, 12:55 PM
  4. Linker error using system(*.*)
    By Winston4u in forum C Programming
    Replies: 5
    Last Post: 05-09-2003, 05:54 PM
  5. MSVis-Studio C++ libraries and linker errors
    By kellydj in forum Windows Programming
    Replies: 10
    Last Post: 03-12-2002, 02:03 PM