# Linked Libraries and Functions

### Imports

* Functions used by a program that are stored in a different program, such as library
* Connected to the main EXE by **Linking**
* Can be linked three ways
  * **Statically**
  * At **Runtime**
  * **Dynamically**

### Unix and Linux: Turning C into Object Code

* Code in files p1.c p2.c
* Compile with command: gcc -O p1.c p2.c -o p
* Use optimizations (-O)
* Put resulting binary in file p

![](/files/-MF7yiqs082OpaeNTHpB)

### Static Linking

* Common in Unix and Linux
* Rarely used for Windows executables
* All code from the library is copied into the executable
* Makes executable large in size

### Runtime Linking

* Unpopular in friendly programs
* Common in malware, especially packed or obfuscated malware
* Connect to libraries only when needed, not when the program starts
* Most commonly done with the LoadLibrary and GetProcAddress functions

### Dynamic Linking

* Most common method
* Host OS searches for necessary libraries when the program is loaded

### Clues in Libraries

* The PE header lists every library and function that will be loaded
* Their names can reveal what the program does
* **URLDownloadToFile** indicates that the program downloads something

### Dependency Walker - Shows Dynamically Linked Functions

* Normal programs have a lot of DLLs
* Malware often has very few DLLs

![](/files/-MF7zYwj22tra5dGpkDH)

![](/files/-MF7zbP2HQzbGGTQrUGW)

### Exports

* DLLs **export** functions
* EXEs **import** functions
* Both exports and imports are listed in the PE header\
  \ <br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.zacheller.dev/exploit-dev-analysis/static-analysis/portable-executable-file-format-pe/linked-libraries-and-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
