Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
binfile.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
23 #include "arch.h"
24 #include "dwdebug_priv.h"
25 #include "dwdebug.h"
26 
27 /*
28  * For now, these just match ELF types that we support. As we add more
29  * backends, we might need to add more types.
30  */
31 typedef enum {
38 static inline const char *BINFILE_TYPE(int n) {
39  switch (n) {
40  case BINFILE_TYPE_NONE: return "none";
41  case BINFILE_TYPE_REL: return "relocatable";
42  case BINFILE_TYPE_EXEC: return "executable";
43  case BINFILE_TYPE_DYN: return "dynamic";
44  case BINFILE_TYPE_CORE: return "core";
45  default: return NULL;
46  }
47 }
48 
49 /*
50  * Library init routines.
51  */
52 void binfile_init(void);
53 
54 struct binfile *binfile_create(char *filename,struct binfile_ops *bfops,
55  void *priv);
56 int binfile_cache_clean(void);
57 /*
58  * Tries all backends, or the one referred to by @bfinst, to open
59  * @filename. @returns a struct binfile if successful; NULL otherwise.
60  * If @root_prefix is set, any other filenames with absolute paths this
61  * library tries to open because of this binfile will be prefixed with
62  * @root_prefix prior to opening. This supports the target API; if you
63  * are not using that, you should have no reason to use @root_prefix.
64  *
65  * If successful, and if the resulting binfile is shareable (not created
66  * based on an instance), the returned binfile has a ref taken on the
67  * caller's behalf, so the caller must call RPUT(binfile) to release
68  * (and free) it.
69  *
70  * If @DFPATH is set, it should be a NULL-terminated array of char *
71  * that specify dirs to look into for files containing debuginfo. This
72  * is right now only used by the ELF backend to load debuginfo for files
73  * that include a build-id and use that as a means to identify which
74  * file contains debuginfo (i.e., the path is often something like
75  * /usr/lib/debug/.build-id/xx/xx...xx.debug -- where the xx's are the
76  * hex string repr of the build id). So, the default DFPATH is
77  * /usr/lib/debug,/usr/local/lib/debug,NULL.
78  *
79  * Note also that even if you specify DFPATH, you should *not* prefix it
80  * with @root_prefix; @root_prefix will be *prepended* to the entries in
81  * DFPATH if they are searched.
82  */
83 struct binfile *binfile_open(char *filename,char *root_prefix,
84  struct binfile_instance *bfinst);
86  struct binfile_instance *bfinst,
87  const char *DFPATH[]);
88 
89 /*
90  * Loads @filename as a binfile, then uses that binfile backend's ops to
91  * infer a default program layout, informed by the @base load address,
92  * and any key/value pairs in @config. This function holds a ref to the
93  * return value; you must free it with binfile_instance_release().
94  *
95  * @param filename A path to a binary file.
96  * @param root_prefix A path to prepend to any other file opens this
97  * library might need; should be the same prefix @filename has.
98  * @param base The base load address as the program image is or was
99  * constructed.
100  * @param config A simple char *->char * hash of key/value pairs; its use
101  * is backend-specific.
102  *
103  * @returns A binfile_instance that represents a program image.
104  */
106  char *root_prefix,
107  ADDR base,GHashTable *config);
108 /*
109  * @param binfile A loaded binfile.
110  * @returns The name of the backend that loaded @binfile.
111  */
112 const char *binfile_get_backend_name(struct binfile *binfile);
113 /*
114  * @param binfile A loaded binfile.
115  * @returns The type of @binfile.
116  */
118 
120  int *named,int *duplicated,int *anon,
121  int *numscopes);
122 /*
123  * Closes all resources associated with loading @binfile, but does not
124  * free the result of loading the binfile -- i.e., any symbols or
125  * metadata that were cached into internal structures after "load".
126  *
127  * @param binfile A loaded binfile.
128  *
129  * @returns A result code.
130  */
131 int binfile_close(struct binfile *binfile);
132 /*
133  * Releases a reference @binfile. If this is the last reference, the
134  * binfile is freed. At this point, the caller must not use any symbols
135  * or data obtained from the binfile.
136  */
138 /*
139  * Releases a reference @bfi. If this is the last reference, the
140  * binfile_instance is freed. At this point, the caller must not use
141  * the binfile_instance.
142  */
144 /*
145  * Frees a binfile_instance. These are not refcnt'd, so can be freed
146  * directly (no release).
147  */
148 REFCNT binfile_instance_free(struct binfile_instance *bfi,int force);
149 
150 struct binfile *binfile_lookup(char *filename);
151 int binfile_cache(struct binfile *binfile);
152 int binfile_uncache(struct binfile *binfile);
153 /*
154  * Internal versions; do not hold a ref to their return values!
155  */
156 struct binfile *binfile_open__int(char *filename,char *root_prefix,
157  struct binfile_instance *bfinst);
159  struct binfile_instance *bfinst,
160  const char *DFPATH[]);
161 REFCNT binfile_free(struct binfile *binfile,int force);
162 
163 /*
164  * Each binfile supports a simple per-backend lifecycle. @open (invoked
165  * via binfile_open) creates the binfile datastructures, opens a
166  * binfile, loads its metadata and symbols). @close closes any open
167  * files and releases any resources related to processing the file.
168  * @free release any other releases that were independent of processing
169  * -- such as symbols, metadata, etc. binfile backends must keep enough
170  * state around to support their symbol table until @free is called.
171  */
172 struct binfile_ops {
173  const char *(*get_backend_name)(void);
174  struct binfile *(*open)(char *filename,char *root_prefix,
175  struct binfile_instance *bfinst);
176  struct binfile *(*open_debuginfo)(struct binfile *binfile,
177  struct binfile_instance *bfinst,
178  const char *DFPATH[]);
179  struct binfile_instance *(*infer_instance)(struct binfile *binfile,
180  ADDR base,GHashTable *config);
181  int (*close)(struct binfile *bfile);
182  void (*free)(struct binfile *bfile);
183  void (*free_instance)(struct binfile_instance *bfi);
184 };
185 
186 /*
187  * binfiles store basic information about compiled binary files. They
188  * provide basic string table, symbol table, and address range
189  * abstractions. Any binary file that is the result of a compilation
190  * will have such tables; some binary files might have more information
191  * regarding loading and layout, and/or relocation. For now, this
192  * information must be stored in the backend-specific @priv field.
193  */
194 struct binfile {
195  /* Our reference count. */
197  /* Our weak reference count. */
198  //REFCNT refcntw;
199 
201 
202  uint8_t is_dynamic:1,
203  has_debuginfo:1;
204 
205  /* If @has_debuginfo, which type. */
207 
208  /*
209  * Opened binfiles have either @fd > 0, or non-NULL @image (right
210  * now, image is used when the binfile has to be loaded into memory
211  * for whatever reason -- initially, relocation in the backend).
212  */
213  char *image;
214  int fd;
215 
216  struct arch *arch;
217 
218  /*
219  * The string table for this file. All binfile string pointers are
220  * checked for presence in this table before freeing.
221  *
222  * This table persists until the binfile is freed.
223  */
224  unsigned int strtablen;
225  char *strtab;
226 
227  /*
228  * The dynamic string table for this file. All binfile string pointers are
229  * checked for presence in this table before freeing.
230  *
231  * This table persists until the binfile is freed.
232  */
233  unsigned int dynstrtablen;
234  char *dynstrtab;
235 
236  /*
237  * This must be an absolute path; binfile_create will try to resolve
238  * its @filename argument and place the result here; but if the
239  * backend updates it, the backend must enforce this constraint.
240  *
241  * This is a unique ID used for caching.
242  */
243  char *filename;
244 
245  /*
246  * binfile_open does a best-effort pass at these fields via regexps;
247  * however, backends are free to update these fields via free() and
248  * malloc() during @binfile_ops->open.
249  *
250  * @name is the name of the binfile, minus any path and version info.
251  * For shared libs, this is "libz"; for the kernel, it
252  * is literally just the name "vmlinux"; for kernel modules,
253  * it is the module name; for programs, it is the executable name.
254  *
255  * @version: the kernel, kmods, and shared libs should all have versions.
256  * Programs probably won't have versions.
257  */
258  char *name;
259  char *version;
260 
261  /*
262  * Backend info.
263  */
264  struct binfile_ops *ops;
265  void *priv;
266 
267  /*
268  * The root for this binfile. All symbols contained in it are
269  * per-backend symbols, not DWARF symbols. They cannot be expanded
270  * into fully-loaded symbols.
271  */
272  struct symbol *root;
273 
274  /*
275  * We keep a separate range structure for binfile symbols, because
276  * the normal debugfile->ranges range structure contains symtabs,
277  * not symbols. So they can't be mixed... unfortunate.
278  */
280 
281  /*
282  * These are the minimum phys/virt address pairs that we learn from
283  * looking at the program headers.
284  */
287 
288  /*
289  * The binfile_instance that was used to load and relocate this
290  * binfile.
291  *
292  * If @binfile_instance is set, this binfile cannot be shared
293  * (because we relocated bits in the binfile using the instance
294  * info).
295  *
296  * If we loaded a binfile that does not depend on the instance,
297  * this field should NOT be set.
298  *
299  * The backend is in charge of setting this field correctly!
300  */
302 
303  /*
304  * This is an alternate prefix that will be prepended to any files
305  * that the binfile code attempts to open. This helps us load
306  * binary file information for filenames whose binaries are not
307  * actually at that location in the / filesystem that the library
308  * code is running on. For instance, this is useful when we use the
309  * target library to open ELF/debuginfo for files that are really
310  * inside a VM that we are inspecting; it helps us look for them in
311  * this prefix instead of our root.
312  *
313  * Why put this here? Because the library attempts to infer new
314  * files to open sometimes (i.e. as it loads debuginfo), so we have
315  * to make sure it looks in the right place. When the user says to
316  * open a binary file as a debugfile, they must use the real path
317  * (i.e., including @root_prefix below). But, those files
318  * themselves may have embedded links to other files -- and if those
319  * links are absolute, we have to prepend our @root_prefix.
320  */
321  char *root_prefix;
322 
323  /*
324  * Currently unused.
325  */
326  time_t load_time;
327  time_t mod_time;
328 };
329 
330 struct binfile_elf {
331  int class;
332  size_t shstrndx;
333  char *buildid;
336 
337  unsigned int num_symbols;
338 
339  /*
340  * We save off full copies of this stuff so that even if the ELF
341  * file is closed, we still have it.
342  */
343  GElf_Ehdr ehdr;
344  GElf_Phdr *phdrs;
345  GElf_Shdr *shdrs;
346 
347  /*
348  * Save off the elfutils info until we're done with it.
349  */
350  Elf *elf;
351  Ebl *ebl;
352  Dwfl *dwfl;
353  int dwfl_fd;
354 };
355 
356 /*
357  * Instances are simple. They map a binfile filename to a loaded
358  * instance of it. We do not necessarily map struct binfile to the
359  * instance, because we might not have loaded the binfile yet! In other
360  * words, the instance might be used to load a (relocated) version of
361  * the binfile; or it might be simply used to load a shareable
362  * (non-relocated) version of the binfile, and then discarded.
363  */
365  /* Our reference count. */
367 
368  char *filename;
369  /*
370  * Since the binfile library is capable of opening files on behalf
371  * of loaded binfiles or binfile_instances, it needs to know if the
372  * real place those files are to be loaded from is a fake (or
373  * alternate) root filesystem. See the comments in struct debugfile
374  * for more information.
375  */
376  char *root_prefix;
377 
381  struct binfile_ops *ops;
382  void *priv;
383 };
384 
386  unsigned int num_sections;
387  unsigned int num_symbols;
388  /* A map of ELF section index to an address in the instance. */
390  /* A map of ELF symtab index to an address in the instance. */
392 
393  /*
394  * When loading an instance, we might modify the section headers.
395  * If we do, this has the mods for us to apply.
396  */
397  GElf_Shdr *shdrs;
398 };
ADDR base_virt_addr
Definition: binfile.h:286
REFCNT binfile_free(struct binfile *binfile, int force)
Definition: binfile.c:358
char * filename
Definition: binfile.h:243
void binfile_init(void)
Definition: binfile.c:103
struct binfile * binfile_lookup(char *filename)
Definition: binfile.c:162
const char * binfile_get_backend_name(struct binfile *binfile)
Definition: binfile.c:318
struct binfile * binfile_open_debuginfo__int(struct binfile *binfile, struct binfile_instance *bfinst, const char *DFPATH[])
Definition: binfile.c:297
int fd
Definition: binfile.h:214
struct binfile_instance * binfile_infer_instance(char *filename, char *root_prefix, ADDR base, GHashTable *config)
Definition: binfile.c:242
clrange_t ranges
Definition: binfile.h:279
struct binfile * binfile_open_debuginfo(struct binfile *binfile, struct binfile_instance *bfinst, const char *DFPATH[])
Definition: binfile.c:306
unsigned int num_symbols
Definition: binfile.h:337
char * version
Definition: binfile.h:259
char * image
Definition: binfile.h:213
Ebl * ebl
Definition: binfile.h:351
binfile_type_t type
Definition: binfile.h:200
unsigned int strtablen
Definition: binfile.h:224
char * strtab
Definition: binfile.h:225
unsigned int dynstrtablen
Definition: binfile.h:233
struct binfile * binfile_open(char *filename, char *root_prefix, struct binfile_instance *bfinst)
Definition: binfile.c:286
void(* free)(struct binfile *bfile)
Definition: binfile.h:182
Dwfl * dwfl
Definition: binfile.h:352
time_t load_time
Definition: binfile.h:326
Pvoid_t clrange_t
Definition: clfit.h:34
uint32_t gnu_debuglinkfile_crc
Definition: binfile.h:335
uint8_t is_dynamic
Definition: binfile.h:202
char * root_prefix
Definition: binfile.h:376
int(* close)(struct binfile *bfile)
Definition: binfile.h:181
char * name
Definition: binfile.h:258
int binfile_close(struct binfile *binfile)
Definition: binfile.c:339
struct arch * arch
Definition: binfile.h:216
void * priv
Definition: binfile.h:265
struct symbol * root
Definition: binfile.h:272
size_t shstrndx
Definition: binfile.h:332
int binfile_uncache(struct binfile *binfile)
Definition: binfile.c:189
debugfile_type_t
Definition: dwdebug.h:770
int dwfl_fd
Definition: binfile.h:353
char * gnu_debuglinkfile
Definition: binfile.h:334
unsigned int num_symbols
Definition: binfile.h:387
uint8_t has_debuginfo
Definition: binfile.h:202
GElf_Shdr * shdrs
Definition: binfile.h:397
binfile_type_t
Definition: binfile.h:31
struct binfile * binfile_open__int(char *filename, char *root_prefix, struct binfile_instance *bfinst)
Definition: binfile.c:254
REFCNT binfile_instance_free(struct binfile_instance *bfi, int force)
Definition: binfile.c:437
GElf_Ehdr ehdr
Definition: binfile.h:343
char * buildid
Definition: binfile.h:333
time_t mod_time
Definition: binfile.h:327
struct binfile_ops * ops
Definition: binfile.h:264
REFCNT refcnt
Definition: binfile.h:366
struct binfile_ops * ops
Definition: binfile.h:381
int binfile_cache(struct binfile *binfile)
Definition: binfile.c:180
uint32_t ADDR
Definition: common.h:64
Definition: arch.h:116
struct binfile * binfile_create(char *filename, struct binfile_ops *bfops, void *priv)
Definition: binfile.c:124
GElf_Shdr * shdrs
Definition: binfile.h:345
GElf_Phdr * phdrs
Definition: binfile.h:344
uint32_t REFCNT
Definition: common.h:124
struct binfile_instance * instance
Definition: binfile.h:301
void(* free_instance)(struct binfile_instance *bfi)
Definition: binfile.h:183
char * dynstrtab
Definition: binfile.h:234
char * root_prefix
Definition: binfile.h:321
binfile_type_t binfile_get_type(struct binfile *binfile)
REFCNT binfile_release(struct binfile *binfile)
Definition: binfile.c:352
Elf * elf
Definition: binfile.h:350
int binfile_get_root_scope_sizes(struct binfile *binfile, int *named, int *duplicated, int *anon, int *numscopes)
Definition: binfile.c:326
ADDR base_phys_addr
Definition: binfile.h:285
int binfile_cache_clean(void)
Definition: binfile.c:197
REFCNT refcnt
Definition: binfile.h:196
REFCNT binfile_instance_release(struct binfile_instance *bfi)
Definition: binfile.c:431
unsigned int num_sections
Definition: binfile.h:386
struct spf_config * config
Definition: spf.c:156
char * filename
Definition: binfile.h:368
debugfile_type_t has_debuginfo_type
Definition: binfile.h:206