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