Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

2.7. ld, the GNU Linker

The GNU linker, ld, combines multiple object files together and creates an executable program from them. It does this by resolving references between the different object files, grouping together similar sections in the object files into one place, arranging for these sections to be loaded at the correct addresses in memory, and generating the necessary header information at the start of a file that allows it to be run.

The linker moves blocks of bytes of your program to their load-time addresses. These blocks slide to their addresses as rigid units; their length does not change and neither does the order of the bytes within them. Such a rigid unit is called a section. Assigning runtime addresses to sections is called relocation. It includes the task of adjusting mentions of object-file addresses so they refer to the proper runtime addresses.

Each section in an object file has a name and a size. Most sections also have an associated block of data, known as the section contents. A section may be marked as allocatable, meaning that space should be reserved for it in memory when the executable starts running. A section may also be marked as loadable, meaning that its contents should be loaded into memory when the executable starts. A section which is allocatable but not loadable will have a zero-filled area of memory created for it.

A section, which is neither loadable nor allocatable, typically contains some sort of debugging information. Every loadable or allocatable output section has two addresses associated with it. The first is the virtual memory address (VMA), the address the section will have when the executable is running. The second is the load memory address (LMA), which is the address in memory where the section will loaded. In most cases the two addresses will be the same. An example of when they might be different is when a data section is loaded into ROM, and then copied into RAM when the program starts. This technique is often used to initialize global variables in a ROM-based system. In this case, the ROM address would be the LMA, and the RAM address would be the VMA. To review the sections in an object file, use the objdump binary utility with the -h option.

Every object file also has a list of symbols, known as the symbol table. A symbol may be defined or undefined. Each symbol has a name, and each defined symbol has an address. If you compile a C or C++ program into an object file, you get a defined symbol for every defined function and global or static variable. Every undefined function or global variable, which is referenced in the input file, will become an undefined symbol. You can refer to the symbols in an object file by using the nm binary utility, or by using the objdump binary utility with the -t option.

The linker is controlled by a linker script which is a text file containing commands in a simple language. The main purpose of the script is to describe how sections in the input files should be mapped into sections in the output file and to control memory layout of the output file. When necessary, the linker script also directs the linker to perform other operations.

The linker uses a default script that compiles into the linker executable, if you do not supply one via the -T command line option. Use the --verbose option to display the default linker script. Certain options (such as -r or -N) affect the default linker script. Linker scripts are written in a superset of AT&T's Link Editor Command Language syntax.

For more information, refer to Using ld, the Gnu Linker.

 
 
  Published under the terms of the GNU General Public License Design by Interspire