Linker Scripts: Difference between revisions
[unchecked revision] | [unchecked revision] |
(→External Links: See discussion.) |
m (→External Links) |
||
Line 66: | Line 66: | ||
=== External Links === |
=== External Links === |
||
* [http://sourceware.org/binutils/docs-2.21/ld/index.html gnu ld online |
* [http://sourceware.org/binutils/docs-2.21/ld/index.html gnu ld online manual] |
||
[[Category:Linkers]] |
[[Category:Linkers]] |
Revision as of 15:26, 19 April 2011
Introduction
So what exactly is a linker script, and do I really need one? In all basic cases, you can technically do without, as you can specify a lot of options on the GNU ld command line, but how fun is that to type 100 characters over and over? A linker script is basically a nice little file you can create that ld will parse and link according to it.
Keywords
I won't cover all of the keywords here, but I will cover the ones that will most likely be used.
ENTRY
ENTRY(main) ENTRY(MultibootEntry)
ENTRY takes one argument. That is the symbol name for the entry point of the linked program/kernel. This can be "start" or "__main" or whatever, it's really up to you, but this will be the very first byte of your loaded program(or the first byte of the .text section in ELF and PE binaries).
OUTPUT_FORMAT
OUTPUT_FORMAT(elf64-x86-64) OUTPUT_FORMAT("pe-i386")
OUTPUT_FORMAT also takes one argument. It specifies what the output format(duh) of your executable will be. To find out what formats your binutils and GCC supports, just use 'objdump -i'. Some of the more common formats are
- binary --This is just a flat binary with no formatting at all
- elf32-i386 --This is just the ELF format, usually little endian too.
- elf64-x86-64 --This is the ELF format for 64bit, usually little endian.
- pe-i386 --The PE format
STARTUP
STARTUP(Boot.o) STARTUP(crt0.o)
STARTUP takes on argument. It is which file you want to be linked to the very beginning of the executable. For userland programs, this is usually crt0.o or crtbegin.o. For kernels, it is usually the file that contains your assembly boilerplate that initiates the stack and in some cases GDT and such and then calls your kmain().
SEARCH_DIR
SEARCH_DIR(Directory)
This will add a path to your library search directory. The -nostdlib flag will cause any library found in this path to be effectively ignored. I'm not sure why, it just seems to be how ld works. It treats linker script specified search directories as standard directories, and therefore ignores them with -no-default-libs and such flags
INPUT
INPUT(File1.o File2.o File3.o ...) INPUT ( File1.o File2.o File3.o ... )
INPUT is a 'in-linker script' replacement for adding object files to the command line. Where you would usually specify something like ld File1.o File2.o, the INPUT section can be used to do this inside the linker script instead.
OUTPUT
OUTPUT(Kernel.bin)
OUTPUT specifies the file that has to be output by the linker. This is the name of the executable. For applications on Windows, for example, this is usually .exe.