C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 02-18-2008, 09:46 PM   #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
George2 is offline  
Old 02-19-2008, 04:14 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline  
Old 02-19-2008, 08:23 PM   #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
George2 is offline  
Old 02-20-2008, 03:20 PM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline  
Old 02-20-2008, 07:54 PM   #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
George2 is offline  
Old 02-21-2008, 04:33 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline  
Old 02-23-2008, 01:25 AM   #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
George2 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22