ld.h (3680B)
1 /* ld: shared types and declarations for the static linker 2 * included by ld.c and per-arch relocation handlers */ 3 4 #ifndef LD_LD_H 5 #define LD_LD_H 6 7 #include <elf.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 /* section order: text, data, bss */ 13 #define I_CS 0 14 #define I_DS 1 15 #define I_BSS 2 16 17 #define MAXSECS (1 << 13) 18 #define MAXOBJS (1 << 11) 19 #define MAXSYMS (1 << 16) 20 #define MAXSYMSTR (1 << 20) 21 #define PAGE_SIZE (1 << 12) 22 #define PAGE_MASK (PAGE_SIZE - 1) 23 #define MAXFILES (1 << 10) 24 #define MAXPHDRS 5 25 #define MAXLIBDIRS 32 26 #define PATHLEN 256 27 #define MAXGOT (1 << 16) 28 29 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 30 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 31 32 /* 64-bit ELF by default, switch to 32-bit on __i386__ */ 33 #ifdef __x86_64__ 34 #define ELFCLASS ELFCLASS64 35 #define Elf_Ehdr Elf64_Ehdr 36 #define Elf_Phdr Elf64_Phdr 37 #define Elf_Shdr Elf64_Shdr 38 #define Elf_Sym Elf64_Sym 39 #define Elf_Rela Elf64_Rela 40 #define SHT_REL_ SHT_RELA 41 #define REL_ADDEND(r) ((r)->r_addend) 42 #define ELF_ST_BIND ELF64_ST_BIND 43 #define ELF_ST_TYPE ELF64_ST_TYPE 44 #define ELF_R_SYM ELF64_R_SYM 45 #define ELF_R_TYPE ELF64_R_TYPE 46 #define ELF_ST_INFO ELF64_ST_INFO 47 #else 48 #define ELFCLASS ELFCLASS32 49 #define Elf_Ehdr Elf32_Ehdr 50 #define Elf_Phdr Elf32_Phdr 51 #define Elf_Shdr Elf32_Shdr 52 #define Elf_Sym Elf32_Sym 53 #define Elf_Rela Elf32_Rel 54 #define SHT_REL_ SHT_REL 55 #define REL_ADDEND(r) (0) 56 #define ELF_ST_BIND ELF32_ST_BIND 57 #define ELF_ST_TYPE ELF32_ST_TYPE 58 #define ELF_R_SYM ELF32_R_SYM 59 #define ELF_R_TYPE ELF32_R_TYPE 60 #define ELF_ST_INFO ELF32_ST_INFO 61 #endif 62 63 struct obj { 64 char *mem; 65 Elf_Ehdr *ehdr; 66 Elf_Shdr *shdr; 67 Elf_Sym *syms; 68 int nsyms; 69 char *symstr; 70 char *shstr; 71 }; 72 73 struct secmap { 74 Elf_Shdr *o_shdr; 75 struct obj *obj; 76 unsigned long vaddr; 77 unsigned long faddr; 78 }; 79 80 struct bss_sym { 81 Elf_Sym *sym; 82 int off; 83 }; 84 85 struct got_entry { 86 int sym_idx; 87 int obj_idx; 88 }; 89 90 struct outelf { 91 Elf_Ehdr ehdr; 92 Elf_Phdr phdr[MAXPHDRS]; 93 int nph; 94 struct secmap secs[MAXSECS]; 95 int nsecs; 96 struct obj objs[MAXOBJS]; 97 int nobjs; 98 99 unsigned long code_addr; 100 101 struct bss_sym bss_syms[MAXSYMS]; 102 int nbss_syms; 103 unsigned long bss_vaddr; 104 int bss_len; 105 106 /* got: one entry per GOTPCREL/GOTTPOFF relocation. the entry stores 107 * the resolved symbol address (or, for a tls symbol, its tp-relative 108 * offset). the relocation displacement points here */ 109 struct got_entry got[MAXGOT]; 110 int ngot; 111 unsigned long got_vaddr; 112 unsigned long got_faddr; 113 114 /* tls template: .tdata/.tbss from every object, merged into one 115 * block. a tls section's secmap.vaddr holds its offset from the 116 * start of this block (0-based), not a runtime address, since tls 117 * variables are addressed tp-relative, not absolutely */ 118 unsigned long tls_size; 119 unsigned long tls_filesz; 120 121 /* the notes of every object, laid out together after the code, which 122 * the PT_NOTE segment covers */ 123 unsigned long note_faddr; 124 unsigned long note_vaddr; 125 unsigned long note_len; 126 127 Elf_Shdr shdr[MAXSECS]; 128 int nsh; 129 char symstr[MAXSYMSTR]; 130 Elf_Sym syms[MAXSYMS]; 131 int nsyms; 132 int nsymstr; 133 unsigned long shdr_faddr; 134 unsigned long syms_faddr; 135 unsigned long symstr_faddr; 136 }; 137 138 void die(char *msg); 139 void die_undef(char *name); 140 void die_reloc(char *name, unsigned long value); 141 void die_dup(char *name); 142 143 struct secmap *outelf_mapping(struct outelf *oe, Elf_Shdr *shdr); 144 145 #endif /* LD_LD_H */