ReactOS  0.4.14-dev-317-g96040ec
image.c
Go to the documentation of this file.
1 /*
2  * ReactOS log2lines
3  * Written by Jan Roeloffzen
4  *
5  * - Image functions for symbol info
6  */
7 
8 #include <errno.h>
9 #include <string.h>
10 #include <rsym.h>
11 
12 #include "compat.h"
13 #include "util.h"
14 #include "options.h"
15 #include "log2lines.h"
16 
19 {
20  size_t i;
21  for (i = 0; i < PEFileHeader->NumberOfSections; i++)
22  {
23  if (0 == strcmp((char *)PESectionHeaders[i].Name, ".rossym"))
24  return &PESectionHeaders[i];
25  }
26  return NULL;
27 }
28 
29 size_t
30 fixup_offset(size_t ImageBase, size_t offset)
31 {
32  if (offset > ABS_TRESHOLD)
33  offset -= ImageBase;
34  return offset;
35 }
36 
38 find_offset(void *data, size_t offset)
39 {
41  PROSSYM_ENTRY Entries = (PROSSYM_ENTRY)((char *)data + RosSymHeader->SymbolsOffset);
42  size_t symbols = RosSymHeader->SymbolsLength / sizeof(ROSSYM_ENTRY);
43  size_t i;
44 
45  for (i = 0; i < symbols; i++)
46  {
47  if (Entries[i].Address > offset)
48  {
49  if (!i--)
50  return NULL;
51  else
52  return &Entries[i];
53  }
54  }
55  return NULL;
56 }
57 
60 {
61  PIMAGE_DOS_HEADER PEDosHeader;
62  PIMAGE_FILE_HEADER PEFileHeader;
63  PIMAGE_OPTIONAL_HEADER PEOptHeader;
64  PIMAGE_SECTION_HEADER PESectionHeaders;
65  PIMAGE_SECTION_HEADER PERosSymSectionHeader;
66  size_t ImageBase;
67 
68  /* Check if MZ header exists */
69  PEDosHeader = (PIMAGE_DOS_HEADER)FileData;
70  if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
71  {
72  l2l_dbg(0, "Input file is not a PE image.\n");
74  return NULL;
75  }
76 
77  /* Locate PE file header */
78  /* sizeof(ULONG) = sizeof(MAGIC) */
79  PEFileHeader = (PIMAGE_FILE_HEADER)((char *)FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
80 
81  /* Locate optional header */
82  PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
83  ImageBase = PEOptHeader->ImageBase;
84 
85  /* Locate PE section headers */
86  PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *)PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
87 
88  /* find rossym section */
89  PERosSymSectionHeader = find_rossym_section(PEFileHeader, PESectionHeaders);
90  if (!PERosSymSectionHeader)
91  {
92  l2l_dbg(0, "Couldn't find rossym section in executable\n");
94  return NULL;
95  }
96 
97  return PERosSymSectionHeader;
98 }
99 
100 int
101 get_ImageBase(char *fname, size_t *ImageBase)
102 {
103  IMAGE_DOS_HEADER PEDosHeader;
104  IMAGE_FILE_HEADER PEFileHeader;
105  IMAGE_OPTIONAL_HEADER PEOptHeader;
106 
107  FILE *fr;
108  off_t readLen;
109  int res;
110 
111  *ImageBase = INVALID_BASE;
112  fr = fopen(fname, "rb");
113  if (!fr)
114  {
115  l2l_dbg(3, "get_ImageBase, cannot open '%s' (%s)\n", fname, strerror(errno));
116  return 1;
117  }
118 
119  readLen = fread(&PEDosHeader, sizeof(IMAGE_DOS_HEADER), 1, fr);
120  if (1 != readLen)
121  {
122  l2l_dbg(1, "get_ImageBase %s, read error IMAGE_DOS_HEADER (%s)\n", fname, strerror(errno));
123  fclose(fr);
124  return 2;
125  }
126 
127  /* Check if MZ header exists */
128  if (PEDosHeader.e_magic != IMAGE_DOS_MAGIC || PEDosHeader.e_lfanew == 0L)
129  {
130  l2l_dbg(2, "get_ImageBase %s, MZ header missing\n", fname);
131  fclose(fr);
132  return 3;
133  }
134 
135  /* Locate PE file header */
136  res = fseek(fr, PEDosHeader.e_lfanew + sizeof(ULONG), SEEK_SET);
137  readLen = fread(&PEFileHeader, sizeof(IMAGE_FILE_HEADER), 1, fr);
138  if (1 != readLen)
139  {
140  l2l_dbg(1, "get_ImageBase %s, read error IMAGE_FILE_HEADER (%s)\n", fname, strerror(errno));
141  fclose(fr);
142  return 4;
143  }
144 
145  /* Locate optional header */
146  readLen = fread(&PEOptHeader, sizeof(IMAGE_OPTIONAL_HEADER), 1, fr);
147  if (1 != readLen)
148  {
149  l2l_dbg(1, "get_ImageBase %s, read error IMAGE_OPTIONAL_HEADER (%s)\n", fname, strerror(errno));
150  fclose(fr);
151  return 5;
152  }
153 
154  /* Check if it's really an IMAGE_OPTIONAL_HEADER we are interested in */
155  if (PEOptHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
156  PEOptHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC)
157  {
158  l2l_dbg(2, "get_ImageBase %s, not an IMAGE_NT_OPTIONAL_HDR 32/64 bit\n", fname);
159  fclose(fr);
160  return 6;
161  }
162 
163  *ImageBase = PEOptHeader.ImageBase;
164  fclose(fr);
165  return 0;
166 }
167 
168 /* EOF */
static FILEDATA FileData[MAX_FDS]
Definition: fs.c:51
int offset_errors
Definition: stat.h:22
__kernel_off_t off_t
Definition: linux.h:201
GLintptr offset
Definition: glext.h:5920
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:376
int errno
Definition: rossym.h:26
const char * strerror(int err)
Definition: compat_str.c:23
size_t fixup_offset(size_t ImageBase, size_t offset)
Definition: image.c:30
struct _SYMBOLFILE_HEADER * PSYMBOLFILE_HEADER
struct _ROSSYM_ENTRY * PROSSYM_ENTRY
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
#define l2l_dbg(level,...)
Definition: util.h:35
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
struct _IMAGE_OPTIONAL_HEADER * PIMAGE_OPTIONAL_HEADER
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
#define IMAGE_DOS_MAGIC
Definition: pecoff.h:6
#define SEEK_SET
Definition: jmemansi.c:26
struct _ROSSYM_ENTRY ROSSYM_ENTRY
struct _IMAGE_FILE_HEADER * PIMAGE_FILE_HEADER
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
static PIMAGE_SECTION_HEADER find_rossym_section(PIMAGE_FILE_HEADER PEFileHeader, PIMAGE_SECTION_HEADER PESectionHeaders)
Definition: image.c:18
static const WCHAR L[]
Definition: oid.c:1250
PIMAGE_SECTION_HEADER get_sectionheader(const void *FileData)
Definition: image.c:59
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
ULONG SymbolsOffset
Definition: rsym.h:32
struct _IMAGE_SECTION_HEADER * PIMAGE_SECTION_HEADER
LIST_ENTRY Entries[5]
Definition: ExDoubleList.c:8
#define INVALID_BASE
Definition: config.h:5
ULONG SymbolsLength
Definition: rsym.h:33
PROSSYM_ENTRY find_offset(void *data, size_t offset)
Definition: image.c:38
GLuint res
Definition: glext.h:9613
unsigned int ULONG
Definition: retypes.h:1
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define ABS_TRESHOLD
Definition: config.h:4
SUMM summ
Definition: log2lines.c:36
int get_ImageBase(char *fname, size_t *ImageBase)
Definition: image.c:101