aboutsummaryrefslogtreecommitdiff
blob: 2b21ab2ff365ae2dbc6997915dfe76a8ce276b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright 2013 Free Software Foundation, Inc.
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __SYM_FILE_LOADER__
#define __SYM_FILE_LOADER__

#include <inttypes.h>
#include <ansidecl.h>
#include <elf/common.h>
#include <elf/external.h>

#ifdef TARGET_LP64

typedef Elf64_External_Phdr Elf_External_Phdr;
typedef Elf64_External_Ehdr Elf_External_Ehdr;
typedef Elf64_External_Shdr Elf_External_Shdr;
typedef Elf64_External_Sym Elf_External_Sym;
typedef uint64_t Elf_Addr;

#elif defined TARGET_ILP32

typedef Elf32_External_Phdr Elf_External_Phdr;
typedef Elf32_External_Ehdr Elf_External_Ehdr;
typedef Elf32_External_Shdr Elf_External_Shdr;
typedef Elf32_External_Sym Elf_External_Sym;
typedef uint32_t Elf_Addr;

#endif

#define GET(hdr, field) (\
sizeof ((hdr)->field) == 1 ? (uint64_t) (hdr)->field[0] : \
sizeof ((hdr)->field) == 2 ? (uint64_t) *(uint16_t *) (hdr)->field : \
sizeof ((hdr)->field) == 4 ? (uint64_t) *(uint32_t *) (hdr)->field : \
sizeof ((hdr)->field) == 8 ? *(uint64_t *) (hdr)->field : \
*(uint64_t *) NULL)

#define GETADDR(hdr, field) (\
sizeof ((hdr)->field) == sizeof (Elf_Addr) ? *(Elf_Addr *) (hdr)->field : \
*(Elf_Addr *) NULL)

struct segment
{
  uint8_t *mapped_addr;
  Elf_External_Phdr *phdr;
  struct segment *next;
};

/* Mini shared library loader.  No reallocation is performed
   for the sake of simplicity.  */

int
load_shlib (const char *file, Elf_External_Ehdr **ehdr_out,
	    struct segment **seg_out);

/* Return the section-header table.  */

Elf_External_Shdr *find_shdrtab (Elf_External_Ehdr *ehdr);

/* Return the string table of the section headers.  */

const char *find_shstrtab (Elf_External_Ehdr *ehdr, uint64_t *size);

/* Return the string table named SECTION.  */

const char *find_strtab (Elf_External_Ehdr *ehdr,
			 const char *section, uint64_t *strtab_size);

/* Return the section header named SECTION.  */

Elf_External_Shdr *find_shdr (Elf_External_Ehdr *ehdr, const char *section);

/* Return the symbol table.  */

Elf_External_Sym *find_symtab (Elf_External_Ehdr *ehdr,
			       uint64_t *symtab_size);

/* Translate a file offset to an address in a loaded segment.   */

int translate_offset (uint64_t file_offset, struct segment *seg, void **addr);

/* Lookup the address of FUNC.  */

int
lookup_function (const char *func, Elf_External_Ehdr* ehdr,
		 struct segment *seg, void **addr);

#endif