Since my last question was closed because of bad code style and typo, I reviewed it and ask for help again.
I'm trying to resolve a .exe file with PE format. Here is part of my code
#include "PE_resolve.h"
#define SIZEOF_SECTION_HEADER 0x28
/*load filebuffer into Imagebuffer*/
int Read_2_ImageBuffer(void **p_filebuffer, void **p_Imagebuffer,long filesize);
/*helper function*/
inline void * Get_NT_POS(void **p_filebuffer);
inline void * Get_FileHeader_POS(void **p_filebuffer);
inline void * Get_Opt_FileHeader_POS(void **p_filebuffer);
int main(){
return 0;
}
int Read_2_ImageBuffer(void **p_filebuffer, void **p_Imagebuffer,long filesize){
/*allocate memory for imagebuffer*/
void *Opt_PE_Header = Get_Opt_FileHeader_POS(p_filebuffer); //THE ERROR LINE
DWORD SizeOfImage = *(DWORD*) ((BYTE *)Opt_PE_Header + 0x38);
*p_Imagebuffer = malloc(SizeOfImage);
if(*p_Imagebuffer == NULL){
printf("can't allocate enough heap memory\n");
exit(EXIT_FAILURE);
}
memset(*p_Imagebuffer,0,SizeOfImage);
/*.....something deal the memory.....*/
return 0;
}
inline void * Get_NT_POS(void **p_filebuffer){
/*return void * point to NT_header in filebuffer*/
DWORD offset = *(DWORD*)((BYTE *)*p_filebuffer + 0x3c);
return (BYTE *)*p_filebuffer + offset;
}
inline void * Get_FileHeader_POS(void **p_filebuffer){
/*return void * point to PE_header in filebuffer*/
void *nt_pos = Get_NT_POS(p_filebuffer);
return (BYTE *)nt_pos + sizeof(DWORD);
}
inline void * Get_Opt_FileHeader_POS(void **p_filebuffer){
/*return void * point to option_PE_header in filebuffer*/
void *fileheader_pos = Get_FileHeader_POS(p_filebuffer);
return (BYTE *)fileheader_pos + 0x14;
}
And this is PE_resolve.h
#ifndef __PE_resolve_header
#define __PE_resolve_header
#define IMAGE_SIZEOF_SHORT_NAME 8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char BYTE;
typedef short WORD;
typedef int DWORD;
typedef struct _IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
} _IMAGE_SECTION_HEADER;
#endif
when I compile it with MinGW, it follows with undefined reference to `Get_Opt_FileHeader_POS'
C:\MinGW\bin\gcc.exe -g D:\Compile\PE_resolve\PE_loader.c -o D:\Compile\PE_resolve\PE_loader.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\15205\AppData\Local\Temp\ccadB6tt.o: in function `Read_2_ImageBuffer':
D:/Compile/PE_resolve/PE_loader.c:52: undefined reference to `Get_Opt_FileHeader_POS'
As you see, I implement all functions in this .c file with prototype at the beginning. I'm a little frustrated. Could you please tell me the reason that cause the ERROR ? Thx :(
You can add the static
keyword before every inline function.
By doing that you force the linker to include the function symbol in the symbol table:
static inline void *Get_Opt_FileHeader_POS(void **p_filebuffer);
Another option is to simply remove the inline
keyword.