1 /* 2 ** libelfsh.h for libelfsh 3 ** 4 ** Started on Mon Jul 23 15:47:12 2001 mayhem 5 ** Last update Sat Aug 16 22:27:30 2003 jv 6 */ 7 8 #ifndef __LIBELFSH_H_ 9 #define __LIBELFSH_H_ 10 11 #include <stdlib.h> 12 #include <stdio.h> 13 #include <sys/types.h> 14 #include <string.h> 15 #include <unistd.h> 16 #include <fcntl.h> 17 #include <errno.h> 18 #include <sys/stat.h> 19 #include <libelfsh-compat.h> 20 21 /* Configure the DEBUG modes for various part of the code */ 22 #define __DEBUG_MAP__ 0 23 #define __DEBUG_SECTS__ 0 24 #define __DEBUG_BLOCKS__ 0 25 #define __DEBUG_SORT__ 0 26 #define __DEBUG_RELADD__ 0 27 #define __DEBUG_COPYPLT__ 0 28 #define __DEBUG_BSS__ 0 29 30 31 /* libelfsh block hijack types */ 32 /* lower byte is OS, middle byte is CPU type and the highest is hijack type */ 33 34 #define ELFSH_HIJACK_CPU_MASK 0x0000ff 35 #define ELFSH_HIJACK_CPU_ANY EM_NONE 36 #define ELFSH_HIJACK_CPU_i86 EM_386 37 #define ELFSH_HIJACK_CPU_SPARC EM_SPARC 38 39 #define ELFSH_HIJACK_OS_MASK 0x00ff00 40 #define ELFSH_HIJACK_OS_ANY 0x000000 41 #define ELFSH_HIJACK_OS_LINUX 0x000100 42 #define ELFSH_HIJACK_OS_SOLARIS 0x000200 43 44 #define ELFSH_HIJACK_TYPE_MASK 0xff0000 45 #define ELFSH_HIJACK_TYPE_NONE 0x000000 46 #define ELFSH_HIJACK_TYPE_GOT 0x010000 47 #define ELFSH_HIJACK_TYPE_PLT 0x020000 48 49 /* Some defined values */ 50 #define ELFSH_SECTION_NAME_MAPPED ".mapped" 51 #define ELFSH_SECTION_NAME_UNMAPPED ".unmapped" 52 #define ELFSH_SECTION_NAME_EXTRAREL ".elfsh.rel" 53 #define ELFSH_SECTION_NAME_ALTPLT ".orig.plt" 54 55 /* We need this for string based research, and for sht reconstruction */ 56 #define ELFSH_SECTION_NAME_PLT ".plt" 57 #define ELFSH_SECTION_NAME_DTORS ".dtors" 58 #define ELFSH_SECTION_NAME_CTORS ".ctors" 59 #define ELFSH_SECTION_NAME_GOT ".got" 60 #define ELFSH_SECTION_NAME_INTERP ".interp" 61 #define ELFSH_SECTION_NAME_SYMTAB ".symtab" 62 #define ELFSH_SECTION_NAME_STRTAB ".strtab" 63 #define ELFSH_SECTION_NAME_DYNSTR ".dynstr" 64 #define ELFSH_SECTION_NAME_SHSTRTAB ".shstrtab" 65 #define ELFSH_SECTION_NAME_BSS ".bss" 66 #define ELFSH_SECTION_NAME_TEXT ".text" 67 #define ELFSH_SECTION_NAME_DATA ".data" 68 #define ELFSH_SECTION_NAME_NOTES ".notes" 69 #define ELFSH_SECTION_NAME_NOTES_ABI ".notes.ABI-tag" 70 #define ELFSH_SECTION_NAME_DYNAMIC ".dynamic" 71 #define ELFSH_SECTION_NAME_COMMENT ".comment" 72 #define ELFSH_SECTION_NAME_RODATA ".rodata" 73 #define ELFSH_SECTION_NAME_STAB ".stab" 74 75 76 /* Section index in the secthash */ 77 #define ELFSH_SECTION_NULL 0 78 #define ELFSH_SECTION_INTERP 1 79 #define ELFSH_SECTION_NOTE 2 80 #define ELFSH_SECTION_HASH 3 81 #define ELFSH_SECTION_DYNSYM 4 82 #define ELFSH_SECTION_DYNSTR 5 83 #define ELFSH_SECTION_GNUVER 6 84 #define ELFSH_SECTION_INIT 10 85 #define ELFSH_SECTION_PLT 11 86 #define ELFSH_SECTION_TEXT 12 87 #define ELFSH_SECTION_FINI 13 88 #define ELFSH_SECTION_RODATA 14 89 #define ELFSH_SECTION_DATA 15 90 #define ELFSH_SECTION_CTORS 17 91 #define ELFSH_SECTION_DTORS 18 92 #define ELFSH_SECTION_GOT 19 93 #define ELFSH_SECTION_DYNAMIC 20 94 #define ELFSH_SECTION_BSS 21 95 #define ELFSH_SECTION_STAB 22 96 #define ELFSH_SECTION_COMMENT 23 97 #define ELFSH_SECTION_SHSTRTAB 24 98 #define ELFSH_SECTION_SYMTAB 25 99 #define ELFSH_SECTION_STRTAB 26 100 #define ELFSH_SECTION_STABSTR 27 101 #define ELFSH_SECTION_ALTPLT 28 102 #define ELFSH_SECTION_UNKNOWN 255 103 #define ELFSH_SECTION_MAX 256 104 105 /* Some constants */ 106 #define ELFSH_PLT_ENTRY_SIZE 16 107 #define ELFSH_SYMTAB_ENTRY_SIZE sizeof(Elf32_Sym) 108 #define ELFSH_REL_ENTRY_SIZE sizeof(Elf32_Rel) 109 110 #define ELFSH_STARTSYM "_start" 111 #define ELFSH_NULL_STRING "(NULL)" 112 #define ELFSH_SORT_BY_ADDR 'a' 113 #define ELFSH_SORT_BY_SIZE 's' 114 115 /* Dedicated to you know who */ 116 #define ELFSH_MEANING 42 117 118 #define ELFSH_SECTION_FIRST 0 119 #define ELFSH_SECTION_LAST 0xFFFF 120 #define ELFSH_BLOCK_FIRST 0 121 #define ELFSH_BLOCK_LAST 0xFFFF 122 123 #define ELFSH_SHIFTING_ABSENT 0 124 #define ELFSH_SHIFTING_NONE 1 125 #define ELFSH_SHIFTING_PARTIAL 2 126 #define ELFSH_SHIFTING_COMPLETE 3 127 128 #define ELFSH_UNSET_OFFSET (1 << 30) 129 130 #define ELFSH_LOWSYM 1 131 #define ELFSH_HIGHSYM 2 132 #define ELFSH_EXACTSYM 3 133 134 #define ELFSH_DATA_INJECTION 0 135 #define ELFSH_CODE_INJECTION 1 136 137 #define ELFSH_PAGESIZE (getpagesize()) 138 #define STT_BLOCK 6 139 140 141 #define DUMPABLE(sym) (elfsh_get_symbol_type(sym) == STT_FUNC || \ 142 elfsh_get_symbol_type(sym) == STT_OBJECT || \ 143 elfsh_get_symbol_type(sym) == STT_COMMON || \ 144 elfsh_get_symbol_type(sym) == STT_SECTION) 145 146 147 148 #define FILE_IS_SPARC(obj) (((elfsh_get_arch((obj)->hdr) == EM_SPARC) || \ 149 (elfsh_get_arch((obj)->hdr) == EM_SPARC32PLUS) || \ 150 (elfsh_get_arch((obj)->hdr) == EM_SPARCV9)) ? 1 : 0) 151 152 153 #define IS_REL(scn) (FILE_IS_SPARC((scn)->parent) ? 0 : 1) 154 155 156 157 158 159 /* Pax flags (to be read in elfhdr.e_flags) */ 160 #define ELFSH_PAX_PAGEEXEC 1 /* Paging based non-exec pages */ 161 #define ELFSH_PAX_EMULTRAMP 2 /* Emulate trampolines */ 162 #define ELFSH_PAX_MPROTECT 4 /* Restrict mprotect() */ 163 #define ELFSH_PAX_RANDMMAP 8 /* Randomize mmap() base */ 164 #define ELFSH_PAX_RANDEXEC 16 /* Randomize ET_EXEC base */ 165 #define ELFSH_PAX_SEGMEXEC 32 /* Segmentation based non-exec pages */ 166 #define EI_PAX 14 /* Index in e_ident[] where to read flags */ 167 168 169 /* ELFsh private stab entry */ 170 typedef struct s_stab 171 { 172 unsigned long strindex; 173 unsigned char type; 174 unsigned char other; 175 unsigned short desc; 176 unsigned int value; 177 } elfshstabent_t; 178 179 180 181 /* ELFsh private .notes entry format */ 182 typedef struct s_nentry 183 { 184 int namesz; 185 int descsz; 186 int type; 187 char *note; 188 char *desc; 189 struct s_nentry *next; 190 struct s_nentry *prev; 191 } elfshnotent_t; 192 193 194 /* ELFsh private relocation format */ 195 typedef struct s_rel 196 { 197 u_int idx_src; 198 u_int off_src; 199 u_int idx_dst; 200 u_int off_dst; 201 202 #define ELFSH_RELOC_SECTBASE 0 /* The dword is to be patched depending on section's base */ 203 #define ELFSH_RELOC_FP 0xFFFFFFFF /* This entry is a false positive : do not relocate */ 204 u_int type; 205 } elfshrel_t; 206 207 /* forward declaration */ 208 struct s_sect; 209 struct s_obj; 210 211 212 /* Block type */ 213 typedef struct s_block 214 { 215 Elf32_Sym *sym; 216 struct s_sect *section; /* section the block belongs to */ 217 int offset; /* Section offset for this block */ 218 int len; /* block real lenght */ 219 struct s_block *next; 220 } elfshblock_t; 221 222 223 /* Also use the same format for PLT entries */ 224 typedef elfshblock_t elfshpltent_t; 225 226 227 228 /* BSS Zone type */ 229 typedef struct s_zone 230 { 231 char *modname; /* Relocatable object name */ 232 u_int off; /* Offset from the start of the section for this zone */ 233 u_int size; /* Zone size */ 234 struct s_zone *next; /* Next zone */ 235 } elfshzone_t; 236 237 238 239 /* Section data type */ 240 typedef struct s_sect 241 { 242 243 /* Filled at creation */ 244 char *name; /* Cached name */ 245 struct s_obj *parent; /* Parent Object */ 246 Elf32_Phdr *phdr; /* the first parent segment */ 247 248 /* Filled at creation, can be modified when SHT is sorted */ 249 Elf32_Shdr *shdr; /* section header for this section */ 250 int index; /* Section index in sht */ 251 struct s_sect *next; /* Next section in the list */ 252 struct s_sect *prev; /* Prev section in the list */ 253 254 /* ELFsh section state */ 255 #define ELFSH_SECTION_ANALYZED (1 << 0) 256 #define ELFSH_SECTION_INSERTED (1 << 1) 257 #define ELFSH_SECTION_MOVED (1 << 2) 258 char flags; /* Analysed/Raw, Orphelin/Inserted, Moved/Notmoved */ 259 260 /* Filled at loading */ 261 void *data; /* Section's data cache */ 262 263 /* Filled at analysing */ 264 void *altdata; /* Type dependant internal format */ 265 void *terdata; /* Alternate type dependant internal format */ 266 267 /* Filled at relocation */ 268 elfshrel_t *rel; /* ELFsh private relocation table for this section */ 269 u_int srcref; /* Number of absolute reference FROM this section : sizeof(rel) */ 270 u_int dstref; /* Number of absolute reference TO this section */ 271 272 /* Filled at merging (BSS only for the moment) */ 273 elfshzone_t *modzone; /* Extra zone list, one zone per object */ 274 275 } elfshsect_t; 276 277 278 /* ELF object structure */ 279 typedef struct s_obj 280 { 281 Elf32_Ehdr *hdr; /* Elf header */ 282 Elf32_Shdr *sht; /* Section header table */ 283 Elf32_Phdr *pht; /* Program Header table */ 284 elfshsect_t *sectlist; /* Section linked list */ 285 elfshsect_t *secthash[ELFSH_SECTION_MAX]; /* Section hash table */ 286 287 int fd; /* File descriptor for the original file */ 288 char *name; /* Object path */ 289 int type; /* ELFSH_OBJECT_CORE, ELFSH_OBJECT_SHARED, ELFSH_OBJECT_RELOC or ELFSH_OBJECT_EXEC */ 290 int rights; /* 0 = RO, 1 = WR */ 291 time_t loadtime; /* Time of Loading */ 292 u_int id; /* Object ID */ 293 char scanned; /* Has the object already been block scanned ? */ 294 char read; /* Has the object already been read ? */ 295 char shtrm; /* Mark SHT and Unmapped sections as stripped ? */ 296 char strip; /* Mark file as stripped */ 297 char shtrb; /* Reconstruct the SHT if non present */ 298 char buff[ELFSH_MEANING]; /* Internal buffer, sometimes used to avoid a malloc */ 299 const char *error; /* Last error string */ 300 struct s_obj *next; /* The list is simply linked */ 301 } elfshobj_t; 302 303 304 305 306 307 /* 308 ** 309 ** Libelfsh API prototypes 310 ** 311 ** Check elfsh/doc/libelfsh-api.txt for a complete description 312 ** 313 ** XXX: libelfsh-api.txt not updated to 0.5 (it is for 0.43b) 314 ** 315 */ 316 317 318 /* dynamic.c */ 319 Elf32_Dyn *elfsh_get_dynamic(elfshobj_t *file, int *num); 320 Elf32_Dyn *elfsh_get_dynamic_entry_by_type(elfshobj_t *file, char type); 321 Elf32_Sword elfsh_get_dynentry_type(Elf32_Dyn *d); 322 int elfsh_set_dynentry_type(Elf32_Dyn *d, Elf32_Sword tag); 323 Elf32_Word elfsh_get_dynentry_val(Elf32_Dyn *d); 324 int elfsh_set_dynentry_val(Elf32_Dyn *d, Elf32_Word val); 325 Elf32_Dyn *elfsh_get_dynamic_entry_by_index(Elf32_Dyn *dynamic, u_int index); 326 char *elfsh_get_dynentry_string(elfshobj_t *file, Elf32_Dyn *ent); 327 328 /* symbol.c */ 329 Elf32_Sym *elfsh_get_symbol_by_name(elfshobj_t *file, char *name); 330 char *elfsh_reverse_symbol(elfshobj_t *file, u_int sym_value, int *offset); 331 char *elfsh_get_symbol_name(elfshobj_t *file, Elf32_Sym *s); 332 void *elfsh_get_symtab(elfshobj_t *file, int *num); 333 Elf32_Sym *elfsh_get_symbol_by_value(elfshobj_t *file, u_int vaddr, int *off, int mode); 334 int elfsh_strip(elfshobj_t *file); 335 int elfsh_set_symbol_name(elfshobj_t *file, Elf32_Sym *s, char *name); 336 int elfsh_shift_symtab(elfshobj_t *file, u_long lim, int inc); 337 int elfsh_insert_sectsym(elfshobj_t *file, elfshsect_t *sect); 338 int elfsh_get_symbol_foffset(elfshobj_t *file, Elf32_Sym *sym); 339 340 /* dynsym.c */ 341 Elf32_Sym *elfsh_get_dynsymbol_by_name(elfshobj_t *file, char *name); 342 void *elfsh_get_dynsymtab(elfshobj_t *file, int *num); 343 char *elfsh_reverse_dynsymbol(elfshobj_t *file, u_int sym_value, int *offset); 344 char *elfsh_get_dynsymbol_name(elfshobj_t *file, Elf32_Sym *s); 345 int elfsh_set_dynsymbol_name(elfshobj_t *file, Elf32_Sym *s, char *name); 346 347 /* sym_common.c */ 348 Elf32_Sym elfsh_create_symbol(int value, int size, int type, int binding, int vis, int idx); 349 Elf32_Sym *elfsh_copy_symtab(void *addr, int size); 350 Elf32_Sym *elfsh_merge_symtabs(Elf32_Sym *one, Elf32_Sym *two, int size_one, int size_two); 351 Elf32_Sym *elfsh_get_sym_by_value(Elf32_Sym *sym, int num, u_int vaddr, int *off, int mode); 352 int elfsh_insert_symbol(elfshsect_t *sect, Elf32_Sym *sym, char *name); 353 Elf32_Word elfsh_get_symbol_size(Elf32_Sym *s); 354 int elfsh_set_symbol_size(Elf32_Sym *s, Elf32_Word size); 355 Elf32_Addr elfsh_get_symbol_value(Elf32_Sym *s); 356 int elfsh_set_symbol_value(Elf32_Sym *s, Elf32_Addr value); 357 u_char elfsh_get_symbol_bind(Elf32_Sym *s); 358 u_char elfsh_set_symbol_bind(Elf32_Sym *s, char opt); 359 u_char elfsh_get_symbol_type(Elf32_Sym *s); 360 u_char elfsh_set_symbol_type(Elf32_Sym *s, char type); 361 u_char elfsh_get_symbol_visibility(Elf32_Sym *s); 362 u_int elfsh_set_symbol_visibility(Elf32_Sym *s, char vis); 363 u_int elfsh_get_symbol_link(Elf32_Sym *s); 364 u_int elfsh_set_symbol_link(Elf32_Sym *s, u_int val); 365 366 Elf32_Sym *elfsh_get_symbol_by_index(Elf32_Sym *symtab, u_int index); 367 Elf32_Sym *elfsh_get_metasym_by_name(elfshobj_t *file, char *name); 368 char *elfsh_reverse_metasym(elfshobj_t *file, u_int vaddr, int *off); 369 370 /* obj.c */ 371 elfshobj_t *elfsh_load_obj(char *name); 372 void elfsh_unload_obj(elfshobj_t *file); 373 374 /* elf.c */ 375 int elfsh_load_hdr(elfshobj_t *file); 376 void *elfsh_get_hdr(elfshobj_t *file); 377 Elf32_Off elfsh_get_shtoff(Elf32_Ehdr *e); 378 Elf32_Half elfsh_get_shtnbr(Elf32_Ehdr *e); 379 Elf32_Off elfsh_get_phtoff(Elf32_Ehdr *e); 380 Elf32_Half elfsh_get_phtnbr(Elf32_Ehdr *e); 381 Elf32_Addr elfsh_get_entrypoint(Elf32_Ehdr *e); 382 Elf32_Half elfsh_get_arch(Elf32_Ehdr *e); 383 Elf32_Half elfsh_get_objtype(Elf32_Ehdr *e); 384 Elf32_Half elfsh_get_shstrtab_index(Elf32_Ehdr *e); 385 Elf32_Word elfsh_get_version(Elf32_Ehdr *e); 386 Elf32_Half elfsh_get_ehsize(Elf32_Ehdr *e); 387 Elf32_Half elfsh_get_phentsize(Elf32_Ehdr *e); 388 Elf32_Half elfsh_get_shentsize(Elf32_Ehdr *e); 389 int elfsh_set_shtoff(Elf32_Ehdr *e, Elf32_Off off); 390 int elfsh_set_shtnbr(Elf32_Ehdr *e, Elf32_Half num); 391 int elfsh_set_phtoff(Elf32_Ehdr *e, Elf32_Off num); 392 int elfsh_set_phtnbr(Elf32_Ehdr *e, Elf32_Half num); 393 int elfsh_set_entrypoint(Elf32_Ehdr *e, Elf32_Addr addr); 394 u_int elfsh_set_arch(Elf32_Ehdr *e, Elf32_Half val); 395 u_int elfsh_set_objtype(Elf32_Ehdr *e, Elf32_Half val); 396 u_int elfsh_set_shstrtab_index(Elf32_Ehdr *e, Elf32_Half val); 397 u_int elfsh_set_version(Elf32_Ehdr *e, Elf32_Word val); 398 u_int elfsh_set_ehsize(Elf32_Ehdr *e, Elf32_Half val); 399 u_int elfsh_set_phentsize(Elf32_Ehdr *e, Elf32_Half val); 400 u_int elfsh_set_shentsize(Elf32_Ehdr *e, Elf32_Half val); 401 int elfsh_get_encoding(Elf32_Ehdr *hdr); 402 int elfsh_set_encoding(Elf32_Ehdr *hdr, u_char type); 403 int elfsh_set_flags(Elf32_Ehdr *hdr, Elf32_Word flags); 404 Elf32_Word elfsh_get_flags(Elf32_Ehdr *hdr); 405 int elfsh_set_magic(Elf32_Ehdr *hdr, u_int mag); 406 int elfsh_get_magic(Elf32_Ehdr *hdr); 407 int elfsh_set_class(Elf32_Ehdr *hdr, u_char class); 408 int elfsh_get_class(Elf32_Ehdr *hdr); 409 410 /* pax.c */ 411 int elfsh_set_paxflags(Elf32_Ehdr *hdr, Elf32_Word flags); 412 Elf32_Word elfsh_get_paxflags(Elf32_Ehdr *hdr); 413 char elfsh_set_pax_pageexec(Elf32_Ehdr *hdr, u_short off); 414 char elfsh_get_pax_pageexec(Elf32_Ehdr *hdr); 415 char elfsh_set_pax_emultramp(Elf32_Ehdr *hdr, u_short off); 416 char elfsh_get_pax_emultramp(Elf32_Ehdr *hdr); 417 char elfsh_set_pax_mprotect(Elf32_Ehdr *hdr, u_short off); 418 char elfsh_get_pax_mprotect(Elf32_Ehdr *hdr); 419 char elfsh_set_pax_randmmap(Elf32_Ehdr *hdr, u_short off); 420 char elfsh_get_pax_randmmap(Elf32_Ehdr *hdr); 421 char elfsh_set_pax_randexec(Elf32_Ehdr *hdr, u_short off); 422 char elfsh_get_pax_randexec(Elf32_Ehdr *hdr); 423 char elfsh_set_pax_segmexec(Elf32_Ehdr *hdr, u_short off); 424 char elfsh_get_pax_segmexec(Elf32_Ehdr *hdr); 425 426 427 /* stab.c */ 428 void *elfsh_get_stab(elfshobj_t *file, int *num); 429 void *elfsh_get_stab_offset(elfshstabent_t *s); 430 char *elfsh_get_stab_name(elfshobj_t *file, elfshstabent_t *s); 431 u_int elfsh_get_stab_type(elfshstabent_t *s); 432 433 434 /* sht.c */ 435 Elf32_Shdr *elfsh_get_shtentry_from_sym(elfshobj_t *file, Elf32_Sym *sym); 436 void *elfsh_get_sht(elfshobj_t *file, int *num); 437 int elfsh_load_sht(elfshobj_t *file); 438 Elf32_Sym *elfsh_get_sym_from_shtentry(elfshobj_t *file, Elf32_Shdr *hdr); 439 Elf32_Word elfsh_get_section_info(Elf32_Shdr *s); 440 Elf32_Word elfsh_get_section_entsize(Elf32_Shdr *s); 441 Elf32_Word elfsh_get_section_link(Elf32_Shdr *s); 442 Elf32_Off elfsh_get_section_foffset(Elf32_Shdr *s); 443 Elf32_Addr elfsh_get_section_addr(Elf32_Shdr *s); 444 Elf32_Word elfsh_get_section_align(Elf32_Shdr *s); 445 Elf32_Word elfsh_get_section_size(Elf32_Shdr *s); 446 Elf32_Word elfsh_get_section_type(Elf32_Shdr *s); 447 int elfsh_set_section_info(Elf32_Shdr *s, Elf32_Word info); 448 int elfsh_set_section_entsize(Elf32_Shdr *s, Elf32_Word entsize); 449 int elfsh_set_section_link(Elf32_Shdr *s, Elf32_Word link); 450 int elfsh_set_section_foffset(Elf32_Shdr *s, Elf32_Off offset); 451 int elfsh_set_section_addr(Elf32_Shdr *s, Elf32_Addr addr); 452 int elfsh_set_section_align(Elf32_Shdr *s, Elf32_Word align); 453 int elfsh_set_section_size(Elf32_Shdr *s, Elf32_Word size); 454 int elfsh_set_section_type(Elf32_Shdr *s, Elf32_Word type); 455 char elfsh_get_section_execflag(Elf32_Shdr *s); 456 char elfsh_get_section_writableflag(Elf32_Shdr *s); 457 char elfsh_get_section_allocflag(Elf32_Shdr *s); 458 char elfsh_get_section_mergeableflag(Elf32_Shdr *s); 459 char elfsh_get_section_strflag(Elf32_Shdr *s); 460 char elfsh_get_section_linkflag(Elf32_Shdr *s); 461 char elfsh_get_section_orderflag(Elf32_Shdr *s); 462 char elfsh_set_section_execflag(Elf32_Shdr *s, char val); 463 char elfsh_set_section_writableflag(Elf32_Shdr *s, char val); 464 char elfsh_set_section_allocflag(Elf32_Shdr *s, char val); 465 char elfsh_set_section_mergeableflag(Elf32_Shdr *s, char val); 466 char elfsh_set_section_strflag(Elf32_Shdr *s, char val); 467 char elfsh_set_section_linkflag(Elf32_Shdr *s, char val); 468 char elfsh_set_section_orderflag(Elf32_Shdr *s, char val); 469 int elfsh_insert_section_header(elfshobj_t *file, Elf32_Shdr hdr, u_int range, char *name); 470 void elfsh_remove_sht(elfshobj_t *file); 471 int elfsh_set_section_name(elfshobj_t *file, elfshsect_t *sect, char *name); 472 char *elfsh_get_section_name(elfshobj_t *file, elfshsect_t *s); 473 Elf32_Shdr elfsh_create_shdr(Elf32_Word name, Elf32_Word type, Elf32_Word flags, 474 Elf32_Addr addr, Elf32_Off offset, Elf32_Word size, 475 Elf32_Word link, Elf32_Word info, Elf32_Word align, 476 Elf32_Word entsize); 477 int elfsh_sort_sht(elfshobj_t *file); 478 479 480 481 Elf32_Shdr *elfsh_get_sht_entry_by_index(Elf32_Shdr *s, u_int index); 482 Elf32_Shdr *elfsh_get_sht_entry_by_name(elfshobj_t *file, char *name); 483 484 485 486 /* pht.c */ 487 void *elfsh_get_pht(elfshobj_t *file, int *num); 488 Elf32_Phdr *elfsh_get_segment_by_type(elfshobj_t *file, int type, int range); 489 Elf32_Phdr *elfsh_get_parent_segment(elfshobj_t *file, elfshsect_t *new); 490 int elfsh_segment_is_parent(elfshsect_t *new, Elf32_Phdr *p); 491 int elfsh_load_pht(elfshobj_t *file); 492 int elfsh_segment_is_writable(Elf32_Phdr *p); 493 int elfsh_segment_is_readable(Elf32_Phdr *p); 494 int elfsh_segment_is_executable(Elf32_Phdr *p); 495 int elfsh_set_segment_flags(Elf32_Phdr *p, Elf32_Word flags); 496 int elfsh_set_segment_align(Elf32_Phdr *p, Elf32_Word align); 497 int elfsh_set_segment_memsz(Elf32_Phdr *p, Elf32_Word memsz); 498 int elfsh_set_segment_filesz(Elf32_Phdr *p, Elf32_Word filesz); 499 int elfsh_set_segment_paddr(Elf32_Phdr *p, Elf32_Addr paddr); 500 int elfsh_set_segment_vaddr(Elf32_Phdr *p, Elf32_Addr vaddr); 501 int elfsh_set_segment_type(Elf32_Phdr *p, Elf32_Word type); 502 int elfsh_set_segment_offset(Elf32_Phdr *p, Elf32_Off offset); 503 Elf32_Word elfsh_get_segment_flags(Elf32_Phdr *p); 504 Elf32_Word elfsh_get_segment_align(Elf32_Phdr *p); 505 Elf32_Word elfsh_get_segment_memsz(Elf32_Phdr *p); 506 Elf32_Word elfsh_get_segment_filesz(Elf32_Phdr *p); 507 Elf32_Addr elfsh_get_segment_paddr(Elf32_Phdr *p); 508 Elf32_Addr elfsh_get_segment_vaddr(Elf32_Phdr *p); 509 Elf32_Word elfsh_get_segment_type(Elf32_Phdr *p); 510 Elf32_Off elfsh_get_segment_offset(Elf32_Phdr *p); 511 Elf32_Phdr *elfsh_get_pht_entry_by_index(Elf32_Phdr *pht, u_int index); 512 Elf32_Addr elfsh_get_object_baseaddr(elfshobj_t *file); 513 514 515 /* hash.c */ 516 void *elfsh_get_hashtable(elfshobj_t *file); 517 int elfsh_get_symbol_hash(char *sym_name); 518 int elfsh_get_dynsymbol_by_hash(elfshobj_t *file, char *sym_name); 519 520 /* got.c */ 521 u_long *elfsh_get_got(elfshobj_t *file, int *num); 522 u_long *elfsh_get_got_entry_by_index(u_long *got, u_int index); 523 u_long *elfsh_get_got_entry_by_name(elfshobj_t *file, char *name); 524 int elfsh_set_got_entry_by_index(elfshobj_t *file, int index, u_long a); 525 int elfsh_set_got_entry_by_name(elfshobj_t *file, char *name, u_long a); 526 int elfsh_set_got_entry(u_long *got, u_long vaddr); 527 int elfsh_get_got_entry(u_long *got); 528 529 /* dtors.c */ 530 u_long *elfsh_get_dtors(elfshobj_t *file, int *num); 531 u_long *elfsh_get_dtors_entry_by_index(u_long *dtors, u_int index); 532 u_long *elfsh_get_dtors_entry_by_name(elfshobj_t *file, char *name); 533 int elfsh_get_dtors_entry(u_long *dtors); 534 int elfsh_set_dtors_entry_by_index(elfshobj_t *file, int index, u_long a); 535 int elfsh_set_dtors_entry_by_name(elfshobj_t *file, char *name, u_long a); 536 int elfsh_set_dtors_entry(u_long *dtors, u_long vaddr); 537 538 /* ctors.c */ 539 u_long *elfsh_get_ctors(elfshobj_t *file, int *num); 540 u_long *elfsh_get_ctors_entry_by_index(u_long *ctors, u_int index); 541 u_long *elfsh_get_ctors_entry_by_name(elfshobj_t *file, char *name); 542 int elfsh_get_ctors_entry(u_long *ctors); 543 int elfsh_set_ctors_entry_by_name(elfshobj_t *file, char *name, u_long a); 544 int elfsh_set_ctors_entry_by_index(elfshobj_t *file, int index, u_long a); 545 int elfsh_set_ctors_entry(u_long *ctors, u_long vaddr); 546 547 548 549 /* section.c */ 550 elfshsect_t *elfsh_get_section_by_type(elfshobj_t *, u_int type, int range, int *, int *, int *); 551 elfshsect_t *elfsh_get_section_by_index(elfshobj_t *, int index, int *, int *); 552 elfshsect_t *elfsh_get_section_by_name(elfshobj_t *, char *name, int *, int *, int *); 553 elfshsect_t *elfsh_get_parent_section(elfshobj_t *file, u_int addr, int *offset); 554 elfshsect_t *elfsh_get_parent_section_by_foffset(elfshobj_t *file, u_int foff, int *offset); 555 elfshsect_t *elfsh_create_section(char *name); 556 elfshsect_t *elfsh_get_section_from_sym(elfshobj_t *file, Elf32_Sym *sym); 557 void *elfsh_load_section(elfshobj_t *file, Elf32_Shdr *shdr); 558 void *elfsh_get_anonymous_section(elfshobj_t *file, elfshsect_t *sect); 559 int elfsh_append_data_to_section(elfshsect_t *sect, void *input, u_int len); 560 char elfsh_shift_section(elfshsect_t *sct, elfshsect_t *tmp, u_char mode); 561 562 int elfsh_insert_unmapped_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data); 563 int elfsh_insert_mapped_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data, int mode); 564 int elfsh_insert_data_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data); 565 int elfsh_insert_code_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data); 566 int elfsh_insert_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data, u_int index); 567 568 elfshsect_t *elfsh_get_section_list(elfshobj_t *file, int *num); 569 elfshsect_t *elfsh_get_tail_section(elfshobj_t *file); 570 571 elfshsect_t *elfsh_get_section_by_idx(elfshsect_t *list, int index); 572 void *elfsh_get_section_data(elfshsect_t *obj, u_int off, u_int sizelem); 573 int elfsh_add_section(elfshobj_t *file, elfshsect_t *sect, u_int index, void *data, int shift); 574 int elfsh_insert_code_section(elfshobj_t *file, elfshsect_t *sect, Elf32_Shdr hdr, void *data); 575 int elfsh_write_section_data(elfshsect_t *sect, u_int off, char *data, u_int size, u_int sizelem); 576 elfshsect_t *elfsh_get_section_by_nam(elfshobj_t *file, char *name); 577 578 579 /* reloc.c */ 580 elfshsect_t *elfsh_get_reloc(elfshobj_t *file, u_int range, u_int *num); 581 void elfsh_free_reloc_list(elfshobj_t *file); 582 u_int elfsh_set_reltype(Elf32_Rel *r, Elf32_Word type); 583 u_int elfsh_set_relsym(Elf32_Rel *r, Elf32_Word sym); 584 int elfsh_set_reloffset(Elf32_Rel *r, Elf32_Addr off); 585 u_int elfsh_get_reltype(Elf32_Rel *r); 586 u_int elfsh_get_relsym(Elf32_Rel *r); 587 Elf32_Addr elfsh_get_reloffset(Elf32_Rel *r); 588 Elf32_Rel *elfsh_get_relent_by_index(Elf32_Rel *table, u_int index); 589 elfshrel_t *elfsh_find_rel(elfshsect_t *sect); 590 int elfsh_insert_relent(elfshsect_t *sect, Elf32_Rel *rel); 591 char *elfsh_get_symname_from_reloc(elfshobj_t *file, Elf32_Rel *r); 592 Elf32_Sym *elfsh_get_symbol_from_reloc(elfshobj_t *file, Elf32_Rel *r); 593 Elf32_Rel *elfsh_get_relent_by_name(elfshobj_t *file, char *name); 594 Elf32_Sword elfsh_get_reladdend(Elf32_Rela *r); 595 int elfsh_set_reladdend(Elf32_Rela *r, Elf32_Sword val); 596 597 598 /* interp.c */ 599 char *elfsh_get_interp(elfshobj_t *file); 600 int elfsh_write_interp(elfshobj_t *file, char *interp); 601 602 /* notes */ 603 elfshsect_t *elfsh_get_notes(elfshobj_t *file, u_int range); 604 605 void elfsh_free_notes_list(elfshobj_t *file); 606 607 /* blocks.c */ 608 elfshblock_t *elfsh_get_block_by_addr(elfshobj_t *file, int addr); 609 elfshblock_t *elfsh_get_block_by_name(elfshobj_t *file, char *name); 610 Elf32_Addr elfsh_get_block_addr(elfshblock_t *func); 611 u_int elfsh_get_block_len(elfshblock_t *func); 612 int elfsh_insert_block(elfshsect_t *sct, elfshblock_t *fct, int range); 613 int elfsh_scan_blocks(elfshobj_t *file); 614 615 int elfsh_print_blocks(elfshsect_t *sect); 616 617 /* plt.c */ 618 elfshsect_t *elfsh_get_plt(elfshobj_t *file, int *num); 619 int elfsh_is_pltentry(elfshobj_t *file, Elf32_Sym *sym); 620 int elfsh_is_plt(elfshobj_t *file, elfshsect_t *s); 621 int elfsh_get_pltentsz(elfshobj_t *file); 622 int elfsh_copy_plt(elfshobj_t *file); 623 624 /* raw.c */ 625 int elfsh_raw_write(elfshobj_t *file, u_int dst, void *src, int len); 626 int elfsh_raw_read(elfshobj_t *file, u_int dst, void *src, int len); 627 int elfsh_get_foffset_from_vaddr(elfshobj_t *file, u_int vaddr); 628 int elfsh_get_vaddr_from_foffset(elfshobj_t *file, u_int foffset); 629 630 /* remap.c */ 631 int elfsh_reloc_pht(elfshobj_t *file, u_long diff); 632 int elfsh_reloc_sht(elfshobj_t *file, u_long diff); 633 int elfsh_relocate_section(elfshsect_t *sect, u_long diff); 634 635 int elfsh_reloc_raw(elfshsect_t *cur, u_long diff); 636 int elfsh_reloc_symtab(elfshsect_t *s, u_long diff); 637 int elfsh_reloc_rel(elfshsect_t *sect, u_long diff); 638 int elfsh_reloc_got(elfshsect_t *sect, u_long diff); 639 int elfsh_reloc_ctors(elfshsect_t *sect, u_long diff); 640 int elfsh_reloc_dtors(elfshsect_t *sect, u_long diff); 641 int elfsh_reloc_hash(elfshsect_t *sect, u_long diff); 642 int elfsh_reloc_array(elfshobj_t *file, u_long *array, u_int size, u_long diff); 643 int elfsh_relocate(elfshobj_t *file); 644 645 /* map.c */ 646 int elfsh_read_obj(elfshobj_t *file); 647 elfshobj_t *elfsh_map_obj(char *name); 648 649 /* save.c */ 650 int elfsh_save_obj(elfshobj_t *file, char *name); 651 652 /* copy.c */ 653 elfshobj_t *elfsh_copy_obj(elfshobj_t *file); 654 655 /* strtab.c */ 656 int elfsh_insert_in_strtab(elfshobj_t *file, char *name); 657 int elfsh_insert_in_shstrtab(elfshobj_t *file, char *name); 658 int elfsh_insert_in_dynstr(elfshobj_t *file, char *name); 659 elfshsect_t *elfsh_get_strtab(elfshobj_t *file, int index); 660 elfshsect_t *elfsh_rebuild_strtab(elfshobj_t *file); 661 662 /* error.c */ 663 void elfsh_error(); 664 665 /* fixup.c */ 666 elfshsect_t *elfsh_fixup_symtab(elfshobj_t *file, int *strindex); 667 668 /* sanitize.c */ 669 void elfsh_update_symlinkidx_equ(elfshobj_t *file, int idx, int diff); 670 void elfsh_update_linkidx_equ(elfshobj_t *file, int idx, int diff); 671 void elfsh_update_linkidx(elfshobj_t *file, int low_index, int diff); 672 void elfsh_update_nameidx(elfshobj_t *file, int offset, int len); 673 674 /* sht_rebuild.c */ 675 int elfsh_rebuild_sht(elfshobj_t *file); 676 677 /* comment.c */ 678 elfshsect_t *elfsh_get_comments(elfshobj_t *file); 679 char *elfsh_get_comments_entry(elfshobj_t *file, u_int range); 680 681 /* hijack.c */ 682 int elfsh_hijack_function_by_name(elfshobj_t *file, 683 uint32_t type, 684 char *name, 685 Elf32_Addr addr); 686 int elfsh_hijack_plt_sparc(elfshobj_t *file, 687 Elf32_Sym *symbol, 688 Elf32_Addr addr); 689 int elfsh_hijack_plt_i86(elfshobj_t *file, 690 Elf32_Sym *symbol, 691 Elf32_Addr addr); 692 int elfsh_hijack_plt_sparc_g2(elfshobj_t *file, 693 Elf32_Sym *symbol, 694 Elf32_Addr addr); 695 696 /* debug.c */ 697 int elfsh_print_sectlist(elfshobj_t *obj, char *label); 698 699 /* bss.c */ 700 elfshsect_t *elfsh_fixup_bss(elfshobj_t *file); 701 elfshsect_t *elfsh_add_bss(elfshobj_t *file); 702 elfshzone_t *elfsh_create_bsszone(char *name, u_int off, u_int size); 703 elfshzone_t *elfsh_find_bsszone(elfshsect_t *bss, char *name); 704 int elfsh_add_bsszone(elfshsect_t *bss, elfshzone_t *zone); 705 706 /* int elfsh_find_bsslen(elfshobj_t *host, elfshsect_t *relbss); */ 707 int elfsh_find_bsslen(elfshobj_t *host, elfshobj_t *rel); 708 709 /* relinject.c */ 710 int elfsh_inject_etrel(elfshobj_t *file, elfshobj_t *rel); 711 712 /* sort.c */ 713 int elfsh_sync_sorted_symtab(elfshsect_t *sect); 714 int elfsh_sort_symtab(Elf32_Sym *symtab, int size, int type); 715 716 #define R_386_NONE 0 /* No reloc */ 717 #define R_386_32 1 /* Direct 32 bit */ 718 #define R_386_PC32 2 /* PC relative 32 bit */ 719 #define R_386_GOT32 3 /* 32 bit GOT entry */ 720 #define R_386_PLT32 4 /* 32 bit PLT address */ 721 #define R_386_COPY 5 /* Copy symbol at runtime */ 722 #define R_386_GLOB_DAT 6 /* Create GOT entry */ 723 #define R_386_JMP_SLOT 7 /* Create PLT entry */ 724 #define R_386_RELATIVE 8 /* Adjust by program base */ 725 #define R_386_GOTOFF 9 /* 32 bit offset to GOT */ 726 #define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */ 727 728 729 #endif /* __LIBELFSH_H_ */ 730