1 /* Copyright 2013-2014 Free Software Foundation, Inc.
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 3 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #ifndef __SYM_FILE_LOADER__
17 #define __SYM_FILE_LOADER__
21 #include <elf/common.h>
22 #include <elf/external.h>
26 typedef Elf64_External_Phdr Elf_External_Phdr
;
27 typedef Elf64_External_Ehdr Elf_External_Ehdr
;
28 typedef Elf64_External_Shdr Elf_External_Shdr
;
29 typedef Elf64_External_Sym Elf_External_Sym
;
30 typedef uint64_t Elf_Addr
;
32 #elif defined TARGET_ILP32
34 typedef Elf32_External_Phdr Elf_External_Phdr
;
35 typedef Elf32_External_Ehdr Elf_External_Ehdr
;
36 typedef Elf32_External_Shdr Elf_External_Shdr
;
37 typedef Elf32_External_Sym Elf_External_Sym
;
38 typedef uint32_t Elf_Addr
;
42 #define GET(hdr, field) (\
43 sizeof ((hdr)->field) == 1 ? (uint64_t) (hdr)->field[0] : \
44 sizeof ((hdr)->field) == 2 ? (uint64_t) *(uint16_t *) (hdr)->field : \
45 sizeof ((hdr)->field) == 4 ? (uint64_t) *(uint32_t *) (hdr)->field : \
46 sizeof ((hdr)->field) == 8 ? *(uint64_t *) (hdr)->field : \
49 #define GETADDR(hdr, field) (\
50 sizeof ((hdr)->field) == sizeof (Elf_Addr) ? *(Elf_Addr *) (hdr)->field : \
56 Elf_External_Phdr
*phdr
;
60 /* Mini shared library loader. No reallocation is performed
61 for the sake of simplicity. */
64 load_shlib (const char *file
, Elf_External_Ehdr
**ehdr_out
,
65 struct segment
**seg_out
);
67 /* Return the section-header table. */
69 Elf_External_Shdr
*find_shdrtab (Elf_External_Ehdr
*ehdr
);
71 /* Return the string table of the section headers. */
73 const char *find_shstrtab (Elf_External_Ehdr
*ehdr
, uint64_t *size
);
75 /* Return the string table named SECTION. */
77 const char *find_strtab (Elf_External_Ehdr
*ehdr
,
78 const char *section
, uint64_t *strtab_size
);
80 /* Return the section header named SECTION. */
82 Elf_External_Shdr
*find_shdr (Elf_External_Ehdr
*ehdr
, const char *section
);
84 /* Return the symbol table. */
86 Elf_External_Sym
*find_symtab (Elf_External_Ehdr
*ehdr
,
87 uint64_t *symtab_size
);
89 /* Translate a file offset to an address in a loaded segment. */
91 int translate_offset (uint64_t file_offset
, struct segment
*seg
, void **addr
);
93 /* Lookup the address of FUNC. */
96 lookup_function (const char *func
, Elf_External_Ehdr
* ehdr
,
97 struct segment
*seg
, void **addr
);