Elf linking

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
(ARM Thumb insanity)
(Extended Linker Format (elf))
Line 18: Line 18:
 
Many schemes exist for symbol table implementation, some use a hash table for quicker lookups, some use linked lists while others have a finite array. They all do the same thing, which is, to provide a database relating addresses to names. My implementation is based of VxWork's symLib. But anyways, what about linking?
 
Many schemes exist for symbol table implementation, some use a hash table for quicker lookups, some use linked lists while others have a finite array. They all do the same thing, which is, to provide a database relating addresses to names. My implementation is based of VxWork's symLib. But anyways, what about linking?
  
==Extended Linker Format (elf)==
+
Tip top stuff. I'll epexct more now.
When the compiler runs it does not know where these sections will be in RAM, so it instead provides references to locations within the object file of data or other functions from this file, and indicates when a symbol's value is unknown and must be provided externally. The format of these outputted files is typically ELF or Extended Linker Format.
+
 
+
The ELF file is divided into a number of parts, first there is the elf header (Elf32_Ehdr) which defines the number and location of the section and program segments. next there are the program segments which contain prelinked absolute data, and section segments. These are both described using program (Elf32_Shdr) and section (Elf32_Phdr) headers respectively. Definitions of these structures is in elf.h which varies from build to build of binutils/gcc toolchain. Because the elf format works equally well with all CPUs, and most operating systems there are many many variants such as 32 and 64 bit, arm, mips, x86 cpu etc.
+
 
+
The specifics of which system a particular elf object file are for as well as which version of the toolchain was used to compile it is are found in the elf header. These should be checked for compatibility before linking.
+
 
+
Sections within the object file contain data of various types. Some is actual data or code to be linked, others contain information about how to link the code or data, even others describe the symbol table, etc etc.
+
 
+
Sections from a typical ELF file (found using objdump -h):
+
<pre>
+
Sections:
+
Idx Name          Size      VMA      LMA      File off  Algn
+
  0 .text        0000003a  00000000  00000000  00000034  2**2
+
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
+
  1 .data        00000000  00000000  00000000  00000070  2**2
+
                  CONTENTS, ALLOC, LOAD, DATA
+
  2 .bss          00000000  00000000  00000000  00000070  2**2
+
                  ALLOC
+
  3 .debug_abbrev 0000007d  00000000  00000000  00000070  2**0
+
                  CONTENTS, READONLY, DEBUGGING
+
  4 .debug_info  0000019f  00000000  00000000  000000ed  2**0
+
                  CONTENTS, RELOC, READONLY, DEBUGGING
+
  5 .debug_line  0000003b  00000000  00000000  0000028c  2**0
+
                  CONTENTS, RELOC, READONLY, DEBUGGING
+
  6 .rodata      00000018  00000000  00000000  000002c7  2**0
+
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
+
  7 .debug_frame  00000054  00000000  00000000  000002e0  2**2
+
                  CONTENTS, RELOC, READONLY, DEBUGGING
+
  8 .debug_loc    00000058  00000000  00000000  00000334  2**0
+
                  CONTENTS, READONLY, DEBUGGING
+
  9 .debug_pubnames 0000002d  00000000  00000000  0000038c  2**0
+
                  CONTENTS, RELOC, READONLY, DEBUGGING
+
10 .debug_aranges 00000020  00000000  00000000  000003b9  2**0
+
                  CONTENTS, RELOC, READONLY, DEBUGGING
+
11 .comment      0000002a  00000000  00000000  000003d9  2**0
+
                  CONTENTS, READONLY
+
12 .note.GNU-stack 00000000  00000000  00000000  00000403  2**0
+
                  CONTENTS, READONLY
+
</pre>
+
 
+
For most applications you can ignore the .debug* sections, these are not normally used for linking but instead for gdb. Sections within the file which are to be linked are marked as "LOAD" (SHF_ALLOC). Sections with an accompanying relocate table are marked as "RELOC" (SHF_ALLOC?), the allocation table seems to always follow in the section immediately following it.
+
 
+
The definitions for headers are system and version specific, however mostly interchangeable. For instance for testing I used my /usr/include/'''elf.h''' from standard Ubuntu Linux distro and just added some ARM specific defines from another file and it worked fine. Another file of interest is elf32-arm.h (or elf32-x86.h) but more on that later... ^^
+
 
+
Wait, I cannot fathom it being so stragihotfwrard.
+
 
+
Hey, that's poewrufl. Thanks for the news.
+

Revision as of 16:22, 31 July 2011

Adventures in dynamic linking

Contents

Well done atricle that. I'll make sure to use it wisely.

Symbols

First it needs some way to identify locations in memory. This is accomplished with symbols. A symbol is a reference to a location in memory and minimally contains location of the symbol, and a textual name for the symbol. It may also contain information about the type of symbol (program, data, global data, etc), who (or which process) owns the symbol, which section of the object file the symbol came from, group it belongs to, etc.

//Typical symbol table
typedef struct _SYMBOL {
	char *name;
	char *value;
	SYM_TYPE  type;
	UINT16    group;
} SYMBOL;

Many schemes exist for symbol table implementation, some use a hash table for quicker lookups, some use linked lists while others have a finite array. They all do the same thing, which is, to provide a database relating addresses to names. My implementation is based of VxWork's symLib. But anyways, what about linking?

Tip top stuff. I'll epexct more now.

Personal tools
irssi scripts
eggdrop scripts