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
arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 
19 #include "log.h"
20 #include "arch.h"
21 
22 extern struct arch arch_x86;
23 extern struct arch arch_x86_64;
24 
25 struct arch *arch_get(arch_type_t at) {
26  if (at == ARCH_X86)
27  return &arch_x86;
28  else if (at == ARCH_X86_64)
29  return &arch_x86_64;
30  else {
31  verror("unsupported arch %d!\n",at);
32  return NULL;
33  }
34 }
35 
36 int arch_has_reg(struct arch *arch,REG reg) {
37  if (reg < 0 || reg >= arch->regcount
38  || !arch->reg_names[reg] || arch->reg_sizes[reg] == 0)
39  return 0;
40  else
41  return 1;
42 }
43 
44 unsigned int arch_regsize(struct arch *arch,REG reg) {
45  if (!arch_has_reg(arch,reg))
46  return 0;
47  else
48  return arch->reg_sizes[reg];
49 }
50 
51 const char *arch_regname(struct arch *arch,REG reg) {
52  if (!arch_has_reg(arch,reg))
53  return 0;
54  else
55  return arch->reg_names[reg];
56 }
57 
58 int arch_regno(struct arch *arch,char *name,REG *reg) {
59  int i;
60 
61  for (i = 0; i <= arch->regcount; ++i) {
62  if (!arch->reg_names[i])
63  continue;
64  else if (strcmp(name,arch->reg_names[i]) == 0) {
65  *reg = i;
66  return 0;
67  }
68  }
69 
70  return 1;
71 }
72 
73 int arch_cregno(struct arch *arch,common_reg_t creg,REG *reg) {
74  if (creg >= COMMON_REG_COUNT)
75  return -1;
76  else if (arch->common_to_arch[creg] == -1)
77  return -1;
78  else {
79  if (reg)
80  *reg = arch->common_to_arch[creg];
81  return 0;
82  }
83 }
struct arch arch_x86_64
Definition: arch_x86_64.c:92
common_reg_t
Definition: arch.h:73
static uint64_t unsigned int i
unsigned int arch_regsize(struct arch *arch, REG reg)
Definition: arch.c:44
int regcount
Definition: arch.h:128
#define verror(format,...)
Definition: log.h:30
const char * arch_regname(struct arch *arch, REG reg)
Definition: arch.c:51
struct arch arch_x86
Definition: arch_x86.c:79
uint8_t * reg_sizes
Definition: arch.h:133
arch_type_t
Definition: arch.h:100
REG * common_to_arch
Definition: arch.h:138
Definition: arch.h:102
char ** reg_names
Definition: arch.h:143
struct arch * arch_get(arch_type_t at)
Definition: arch.c:25
int8_t REG
Definition: common.h:93
#define COMMON_REG_COUNT
Definition: arch.h:80
Definition: arch.h:116
int arch_cregno(struct arch *arch, common_reg_t creg, REG *reg)
Definition: arch.c:73
int arch_has_reg(struct arch *arch, REG reg)
Definition: arch.c:36
int arch_regno(struct arch *arch, char *name, REG *reg)
Definition: arch.c:58
const char * name
Definition: arch.h:118