Elf linking
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?
Extended Linker Format (elf)
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):
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
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.
ARM Thumb insanity
Immediately one problem became clear, while it worked fine on x86 code, even after adding the required relocation handling code for R_ARM_CALL and R_ARM_ABS32 (this was same as x86 abs32 btw :D) the arm9 linking test wasn't working properly. After digging a bit realized that libnds operates mostly in 16 bit Thumb mode. For this mode R_ARM_THM_PC22 relocation handling was needed.
To get an idea how arm-eabi handles R_ARM_THM_PC22 i examined elf32-arm.h which contains the processor specific handling code and macros relating to relocation among other things. From this i immediately realized thumb being 16 bit uses two instructions for 'bl' each containing 11 bits representing the branch address and 5 bits for the branch high or branch low instrunctions. (0xf000 and 0xf800) Perfect sense right @_@)?
Below is the source code that was linked into thlink (see image):
#include <stdio.h> //Just filler so that otherFunc is offset for better testing int otherFunc(int argc, char **argv) { printf("otherFunc!\n"); } int someFunc(void) { printf("Zomg! It Works! ^_^\n"); }
Thank God! Someone with birans speaks!