scor32k

Untitled

Nov 16th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1.  
  2. ## How does a linker know what all libraries to link?
  3.  
  4. enter image description here
  5.  
  6. How does the linker know that it needs to include the standard runtime library and for example the definitions of the functions declared in iostream are not present elsewhere?
  7.  
  8. Is there some mapping that facilitates the linking process?
  9.  
  10. To rephrase - If I include some file with only function declarations, how does any linker figure out where the function definitions are?
  11.  
  12. If you use g++, it will always link the standard c++ library (-lstdc++).
  13.  
  14. To bypass it, you can use gcc and link a different library.
  15.  
  16. Edit: the linker doesn't figure out anything. The standard library is automatically linked, so there's nothing to figure out for the standard functions. If you just declare something that is not in STL and try to use it without the actual definition, the linker will fail. You have to manually link the library/.o.
  17.  
  18. ## What do linkers do?
  19.  
  20. To understand linkers, it helps to first understand what happens "under the hood" when you convert a source file (such as a C or C++ file) into an executable file (an executable file is a file that can be executed on your machine or someone else's machine running the same machine architecture).
  21.  
  22. Under the hood, when a program is compiled, the compiler converts the source file into object byte code. This byte code (sometimes called object code) is mnemonic instructions that only your computer architecture understands. Traditionally, these files have an .OBJ extension.
  23.  
  24. After the object file is created, the linker comes into play. More often than not, a real program that does anything useful will need to reference other files. In C, for example, a simple program to print your name to the screen would consist of:
  25.  
  26. printf("Hello Kristina!\n");
  27. When the compiler compiled your program into an obj file, it simply puts a reference to the printf function. The linker resolves this reference. Most programming languages have a standard library of routines to cover the basic stuff expected from that language. The linker links your OBJ file with this standard library. The linker can also link your OBJ file with other OBJ files. You can create other OBJ files that have functions that can be called by another OBJ file. The linker works almost like a word processor's copy and paste. It "copies" out all the necessary functions that your program references and creates a single executable. Sometimes other libraries that are copied out are dependent on yet other OBJ or library files. Sometimes a linker has to get pretty recursive to do its job.
  28.  
  29. Note that not all operating systems create a single executable. Windows, for example, uses DLLs that keep all these functions together in a single file. This reduces the size of your executable, but makes your executable dependent on these specific DLLs. DOS used to use things called Overlays (.OVL files). This had many purposes, but one was to keep commonly used functions together in 1 file (another purpose it served, in case you're wondering, was to be able to fit large programs into memory. DOS has a limitation in memory and overlays could be "unloaded" from memory and other overlays could be "loaded" on top of that memory, hence the name, "overlays"). Linux has shared libraries, which is basically the same idea as DLLs (hard core Linux guys I know would tell me there are MANY BIG differences).
  30.  
  31.  
  32. ### 3
  33.  
  34. Address relocation minimal example
  35. Address relocation is one of the crucial functions of linking.
  36.  
  37. So let's have a look on how it works with a minimal example.
  38.  
  39. 0) Introduction
  40. Summary: relocation edits the .text section of object files to translate:
  41.  
  42. object file address
  43. into the final address of the executable
  44. This must be done by the linker because the compiler only sees one input file at a time, but we must know about all object files at once to decide how to:
  45.  
  46. resolve undefined symbols like declared undefined functions
  47. not clash multiple .text and .data sections of multiple object files
  48. Prerequisites: minimal understanding of:
  49.  
  50. x86-64 or IA-32 assembly
  51. global structure of an ELF file. I have made a tutorial for that
  52. Linking has nothing to do with C or C++ specifically: compilers just generate the object files. The linker then takes them as input without ever knowing what language compiled them. It might as well be Fortran.
  53.  
  54. So to reduce the crust, let's study a NASM x86-64 ELF Linux hello world:
  55.  
  56. section .data
  57. hello_world db "Hello world!", 10
  58. section .text
  59. global _start
  60. _start:
  61.  
  62. ; sys_write
  63. mov rax, 1
  64. mov rdi, 1
  65. mov rsi, hello_world
  66. mov rdx, 13
  67. syscall
  68.  
  69. ; sys_exit
  70. mov rax, 60
  71. mov rdi, 0
  72. syscall
  73. compiled and assembled with:
  74.  
  75. nasm -o hello_world.o hello_world.asm
  76. ld -o hello_world.out hello_world.o
  77. with NASM 2.10.09.
  78.  
  79. 1) .text of .o
  80. First we decompile the .text section of the object file:
  81.  
  82. objdump -d hello_world.o
  83. which gives:
  84.  
  85. 0000000000000000 <_start>:
  86. 0: b8 01 00 00 00 mov $0x1,%eax
  87. 5: bf 01 00 00 00 mov $0x1,%edi
  88. a: 48 be 00 00 00 00 00 movabs $0x0,%rsi
  89. 11: 00 00 00
  90. 14: ba 0d 00 00 00 mov $0xd,%edx
  91. 19: 0f 05 syscall
  92. 1b: b8 3c 00 00 00 mov $0x3c,%eax
  93. 20: bf 00 00 00 00 mov $0x0,%edi
  94. 25: 0f 05 syscall
  95. the crucial lines are:
  96.  
  97. a: 48 be 00 00 00 00 00 movabs $0x0,%rsi
  98. 11: 00 00 00
  99. which should move the address of the hello world string into the rsi register, which is passed to the write system call.
  100.  
  101. But wait! How can the compiler possibly know where "Hello world!" will end up in memory when the program is loaded?
  102.  
  103. Well, it can't, specially after we link a bunch of .o files together with multiple .data sections.
  104.  
  105. Only the linker can do that since only he will have all those object files.
  106.  
  107. So the compiler just:
  108.  
  109. puts a placeholder value 0x0 on the compiled output
  110. gives some extra information to the linker of how to modify the compiled code with the good addresses
  111. This "extra information" is contained in the .rela.text section of the object file
  112.  
  113. 2) .rela.text
  114. .rela.text stands for "relocation of the .text section".
  115.  
  116. The word relocation is used because the linker will have to relocate the address from the object into the executable.
  117.  
  118. We can disassemble the .rela.text section with:
  119.  
  120. readelf -r hello_world.o
  121. which contains;
  122.  
  123. Relocation section '.rela.text' at offset 0x340 contains 1 entries:
  124. Offset Info Type Sym. Value Sym. Name + Addend
  125. 00000000000c 000200000001 R_X86_64_64 0000000000000000 .data + 0
  126. The format of this section is fixed documented at: http://www.sco.com/developers/gabi/2003-12-17/ch4.reloc.html
  127.  
  128. Each entry tells the linker about one address which needs to be relocated, here we have only one for the string.
  129.  
  130. Simplifying a bit, for this particular line we have the following information:
  131.  
  132. Offset = C: what is the first byte of the .text that this entry changes.
  133.  
  134. If we look back at the decompiled text, it is exactly inside the critical movabs $0x0,%rsi, and those that know x86-64 instruction encoding will notice that this encodes the 64-bit address part of the instruction.
  135.  
  136. Name = .data: the address points to the .data section
  137.  
  138. Type = R_X86_64_64, which specifies what exactly what calculation has to be done to translate the address.
  139.  
  140. This field is actually processor dependent, and thus documented on the AMD64 System V ABI extension section 4.4 "Relocation".
  141.  
  142. That document says that R_X86_64_64 does:
  143.  
  144. Field = word64: 8 bytes, thus the 00 00 00 00 00 00 00 00 at address 0xC
  145.  
  146. Calculation = S + A
  147.  
  148. S is value at the address being relocated, thus 00 00 00 00 00 00 00 00
  149. A is the addend which is 0 here. This is a field of the relocation entry.
  150. So S + A == 0 and we will get relocated to the very first address of the .data section.
  151.  
  152. 3) .text of .out
  153. Now lets look at the text area of the executable ld generated for us:
  154.  
  155. objdump -d hello_world.out
  156. gives:
  157.  
  158. 00000000004000b0 <_start>:
  159. 4000b0: b8 01 00 00 00 mov $0x1,%eax
  160. 4000b5: bf 01 00 00 00 mov $0x1,%edi
  161. 4000ba: 48 be d8 00 60 00 00 movabs $0x6000d8,%rsi
  162. 4000c1: 00 00 00
  163. 4000c4: ba 0d 00 00 00 mov $0xd,%edx
  164. 4000c9: 0f 05 syscall
  165. 4000cb: b8 3c 00 00 00 mov $0x3c,%eax
  166. 4000d0: bf 00 00 00 00 mov $0x0,%edi
  167. 4000d5: 0f 05 syscall
  168. So the only thing that changed from the object file are the critical lines:
  169.  
  170. 4000ba: 48 be d8 00 60 00 00 movabs $0x6000d8,%rsi
  171. 4000c1: 00 00 00
  172. which now point to the address 0x6000d8 (d8 00 60 00 00 00 00 00 in little-endian) instead of 0x0.
  173.  
  174. Is this the right location for the hello_world string?
  175.  
  176. To decide we have to check the program headers, which tell Linux where to load each section.
  177.  
  178. We disassemble them with:
  179.  
  180. readelf -l hello_world.out
  181. which gives:
  182.  
  183. Program Headers:
  184. Type Offset VirtAddr PhysAddr
  185. FileSiz MemSiz Flags Align
  186. LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
  187. 0x00000000000000d7 0x00000000000000d7 R E 200000
  188. LOAD 0x00000000000000d8 0x00000000006000d8 0x00000000006000d8
  189. 0x000000000000000d 0x000000000000000d RW 200000
  190.  
  191. Section to Segment mapping:
  192. Segment Sections...
  193. 00 .text
  194. 01 .data
  195. This tells us that the .data section, which is the second one, starts at VirtAddr = 0x06000d8.
  196.  
  197. And the only thing on the data section is our hello world string.
  198.  
  199.  
  200.  
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment