TEMU  4.4
The Terma Emulator
Loader.h
Go to the documentation of this file.
1 //===-- temu-c/Loader.h - Binary image loading ------------------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2015
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 #include "temu-c/Support/Attributes.h"
9 #include <stdint.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*!
16  Loads a binary image to the memory object.
17 
18  The function will autodetect the file type (based on extensions and
19  magic headers in the file).
20 
21  Binary images will be placed at address 0.
22 
23  The image can be one of the supported file formats.
24 
25  \param mem Memory object, must conform to the mem interface.
26  \param file Path to file to load.
27  \result 0 on success, other values indicates errors.
28  */
29 TEMU_API int temu_loadImage(void *mem, const char *file);
30 
31 /*!
32  Loads a binary image to the memory object.
33 
34  The function will autodetect the file type (based on extensions and
35  magic headers in the file).
36 
37  Binary images will be placed at address 0.
38 
39  The image can be one of the supported file formats.
40 
41  This specific variant, will return the start address of the binary,
42  the StartAddr is typically embedded in the ELF or SREC files. The
43  function therefore allows you to set the program counter to the
44  correct start address after the function has been called.
45 
46  \param Mem Memory object, must conform to the mem interface.
47  \param FileName Path to file to load.
48  \param StartAddr Pointer to location to store the start address.
49  \result 0 on success, 1 for successful load but without valid StartAddr,
50  negative values indicate error.
51  */
52 
53 TEMU_API int temu_loadImageAndStartAddr(void *Mem, const char *FileName,
55 
56 /*!
57  Loads a raw binary image to the memory object at the given address.
58 
59  The function will assume that the file is a raw binary.
60  This way you can load an ELF file as is to memory
61  (e.g. where it is expected by the boot loader).
62 
63  \param mem Memory object, must conform to the mem interface.
64  \param file Path to file to load.
65  \param pa Physical address where to load the image.
66  \result 0 on success, other values indicates errors.
67  */
68 TEMU_API int temu_loadBinaryImage(void *mem, const char *file, uint64_t pa);
69 
70 typedef void temu_Symtab;
71 
72 /*!
73  Loads an ELF file without going through filetype detection
74 
75  \param mem Memory object, must conform to the mem interface.
76  \param file Path to file to load.
77  \param pa Unused argument
78  \result 0 on success, other values indicates errors
79  */
80 TEMU_API int temu_loadElfImage(void *mem, const char *file, uint64_t pa);
81 
82 /*!
83  Loads an ELF file without going through filetype detection
84 
85  \param Mem Memory object, must conform to the mem interface.
86  \param FileName Path to file to load.
87  \param StartAddr Pointer where to place the start address of the elf
88  file
89  \result 0 on success, 1 for successful load but without valid StartAddr,
90  negative values indicate error.
91  */
92 
93 TEMU_API int temu_loadElfImageAndStartAddr(void *Mem, const char *FileName,
95 /*!
96  Return non-zero if file is an ELF file
97 
98  \param file Path to file to check for ELFiness.
99  \result 0 if file is not an ELF file, 1 if the file is an ELF file.
100  */
101 TEMU_API int temu_imageIsELF(const char *file);
102 
103 /*!
104  Return non-zero if file has DWARF data
105 
106  \param file Path to file to check for DWARF-info.
107  \result 0 if file does not contain DWARF, 1 if the file contains DWARF.
108  */
109 TEMU_API int temu_elfHasDwarf(const char *file);
110 
111 /*!
112  Loads the symbol table from a given ELF file.
113 
114  The symbol table is allocated and the pointer of the allocated
115  symbol table is placed in Sym. The user is responsible for
116  disposing the symbol table with temu_disposeSymtab().
117 
118  \param FileName Name of ELF file
119  \param Sym Address of your symtab pointer.
120  \result 0 on success, other values indicates errors.
121  */
122 TEMU_API int temu_loadSymtab(const char *FileName,
124 
125 /*!
126  Get the function name associated with the given address
127 
128  The address can be any address within the range of the function,
129  i.e. from the start to othe end of the function. Thus this serves as
130  a way to find the name of the current function given a PC value.
131 
132  The function first searches for local symbols (e.g. static symbols
133  in C). It then searches for global symbols and last for weak
134  symbols.
135 
136  If a local symbol is found, the LocalFile pointer will point at a
137  string with the file name it is associated to, otherwise the local
138  file pointer will be set to null.
139 
140  The returned pointers are valid until the symbol table is disposed.
141 
142  \param Sym Symboltable
143 
144  \param LocalFile The pointer it refers to will be set to NULL if the
145  symbol found is global.
146 
147  \param Symbol The pointer will be set to NULL if a function cannot
148  be found at the address.
149 
150  \param Addr Virtual address to find a function name for.
151 */
153  const char **LocalFile TEMU_NONNULL,
154  const char **Symbol TEMU_NONNULL,
155  uint64_t Addr);
156 
157 /*!
158  Get the object name associated with the given address
159 
160  The address can be any address within the range of the object. Thus
161  this serves as a way to find the name of a global or static object
162  given an address.
163 
164  The function first searches for local symbols (e.g. static symbols
165  in C). It then searches for global symbols and last for weak
166  symbols.
167 
168  If a local symbol is found, the LocalFile pointer will point at a
169  string with the file name it is associated to, otherwise the local
170  file pointer will be set to NULL.
171 
172  The returned pointers are valid until the symbol table is disposed.
173 
174  \param Sym Symboltable
175 
176  \param LocalFile The pointer it refers to will be set to NULL if the
177  symbol found is global.
178 
179  \param Symbol The pointer will be set to NULL if an object cannot
180  be found at the address.
181 
182  \param Addr Virtual address to find an object name for.
183  */
184 
186  const char **LocalFile TEMU_NONNULL,
187  const char **Symbol TEMU_NONNULL,
188  uint64_t Addr);
189 
190 /*!
191  Get the range of a global function
192 
193  On success, the Addr and the Size references will be set to the starting
194  virtual address of the function and the size in bytes respectively.
195 
196  \param Sym The Symbol table, from which the function is to be retrieved
197  \param FuncName The function, whose range is required
198  \param Addr On success, gives the address of the function
199  \param Size On success, gives the size of the function start from Addr
200  \result 0 on success (the function exists), other values indicate falures.
201  */
203  const char *FuncName TEMU_NONNULL,
205  uint64_t *Size);
206 
207 /*!
208  Get the range of a local/static function
209 
210  On success, the Addr and the Size references will be set to the starting
211  virtual address of the function and the size in bytes respectively.
212 
213  \param Sym The Symbol table, from which the function to be retrieved
214  \param FileName The file/compilation unit that contains the function
215  \param FuncName The function, whose range is required
216  \param Addr On success, gives the address of the function
217  \param Size On success, gives the size of the function start from Addr
218 
219  \result 0 on success (the function exists in the given file), other
220  values indicate falures
221  */
223  const char *FileName TEMU_NONNULL,
224  const char *FuncName TEMU_NONNULL,
227 
228 /*!
229  Get the range of a global object
230 
231  On success, the Addr and the Size references will be set to the starting
232  virtual address of the object and the size in bytes respecivelly.
233 
234  \param Sym The Symbol table, from which the object to be retrieved
235  \param ObjectName Name of the object, whose address range is to be obtained
236  \param Addr On success, gives the address of the object
237  \param Size On success, gives the size of the object start from Addr
238 
239  \result 0 on success (the object exists), other values indicate falures
240  */
242  const char *ObjectName TEMU_NONNULL,
245 
246 /*!
247  Get the range of a local/static object
248 
249  On success, the Addr and the Size references will be set to the starting
250  virtual address of the object and the size in bytes respecivelly.
251 
252  \param Sym The Symbol table, from which the object to be retrieved
253  \param FileName The file/compilation unit that contains the function
254  \param ObjectName The object, whose range is required
255  \param Addr On success, gives the address of the object
256  \param Size On success, gives the size of the object start from Addr
257 
258  \result 0 on success (the object exists), other values indicate falures
259  */
261  const char *FileName TEMU_NONNULL,
262  const char *ObjectName TEMU_NONNULL,
265 
266 /*!
267  Dispose (deallocate) the symbol table
268  \param Sym Symbol table pointer to free.
269  */
271 
272 /*!
273  Loads an SREC file without going through filetype detection.
274 
275  \param mem Memory object, must conform to the mem interface.
276  \param file Path to file to load.
277  \param pa Unused argument
278  \result 0 on success, other values indicates errors
279  */
280 TEMU_API int temu_loadSrecImage(void *mem, const char *file, uint64_t pa);
281 
282 /*!
283  Loads an SREC file without going through filetype detection.
284 
285  \param mem Memory object, must conform to the mem interface.
286  \param fileName Path to file to load.
287  \param startAddr Start address, will be set if result is 0
288  \result 0 on success, 1 for successful load but without valid StartAddr,
289  negative values indicate error.
290  */
291 TEMU_API int temu_loadSrecImageAndStartAddr(void *mem, const char *fileName,
293 #ifdef __cplusplus
294 }
295 #endif
temu_loadSymtab
TEMU_API int temu_loadSymtab(const char *FileName, temu_Symtab **Sym TEMU_NONNULL)
temu_loadSrecImageAndStartAddr
TEMU_API int temu_loadSrecImageAndStartAddr(void *mem, const char *fileName, uint32_t *startAddr TEMU_NONNULL)
temu_disposeSymtab
TEMU_API void temu_disposeSymtab(temu_Symtab *Sym)
temu_imageIsELF
TEMU_API int temu_imageIsELF(const char *file)
temu_loadBinaryImage
TEMU_API int temu_loadBinaryImage(void *mem, const char *file, uint64_t pa)
temu_symtabGetFuncName
TEMU_API void temu_symtabGetFuncName(temu_Symtab *Sym TEMU_NONNULL, const char **LocalFile TEMU_NONNULL, const char **Symbol TEMU_NONNULL, uint64_t Addr)
temu_symtabGetLocalObjRange
TEMU_API int temu_symtabGetLocalObjRange(temu_Symtab *Sym TEMU_NONNULL, const char *FileName TEMU_NONNULL, const char *ObjectName TEMU_NONNULL, uint64_t *Addr TEMU_NONNULL, uint64_t *Size TEMU_NONNULL)
temu_loadElfImage
TEMU_API int temu_loadElfImage(void *mem, const char *file, uint64_t pa)
temu_loadImage
TEMU_API int temu_loadImage(void *mem, const char *file)
temu_symtabGetLocalFuncRange
TEMU_API int temu_symtabGetLocalFuncRange(temu_Symtab *Sym TEMU_NONNULL, const char *FileName TEMU_NONNULL, const char *FuncName TEMU_NONNULL, uint64_t *Addr TEMU_NONNULL, uint64_t *Size TEMU_NONNULL)
temu_elfHasDwarf
TEMU_API int temu_elfHasDwarf(const char *file)
temu_loadImageAndStartAddr
TEMU_API int temu_loadImageAndStartAddr(void *Mem, const char *FileName, uint64_t *StartAddr TEMU_NONNULL)
temu_loadElfImageAndStartAddr
TEMU_API int temu_loadElfImageAndStartAddr(void *Mem, const char *FileName, uint64_t *StartAddr TEMU_NONNULL)
temu_symtabGetGlobalObjRange
TEMU_API int temu_symtabGetGlobalObjRange(temu_Symtab *Sym TEMU_NONNULL, const char *ObjectName TEMU_NONNULL, uint64_t *Addr TEMU_NONNULL, uint64_t *Size TEMU_NONNULL)
temu_Symtab
void temu_Symtab
Definition: Loader.h:70
temu_loadSrecImage
TEMU_API int temu_loadSrecImage(void *mem, const char *file, uint64_t pa)
temu_symtabGetGlobalFuncRange
TEMU_API int temu_symtabGetGlobalFuncRange(temu_Symtab *Sym TEMU_NONNULL, const char *FuncName TEMU_NONNULL, uint64_t *Addr TEMU_NONNULL, uint64_t *Size)
temu_symtabGetObjName
TEMU_API void temu_symtabGetObjName(temu_Symtab *Sym TEMU_NONNULL, const char **LocalFile TEMU_NONNULL, const char **Symbol TEMU_NONNULL, uint64_t Addr)