cleaning up old files

master
Allen Webster 2017-07-08 22:50:26 -04:00
parent 98fc2c7252
commit 7393c881a6
2 changed files with 0 additions and 819 deletions

View File

@ -1,378 +0,0 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 20.11.2015
*
* DLL loader declarations for 4coder
*
*/
// TOP
// TODO(allen):
// Check the relocation table, if it contains anything that
// is platform specific generate an error to avoid calling
// into invalid code.
i32
dll_compare(char *a, char *b, i32 len){
i32 result;
char *e;
result = 0;
e = a + len;
for (;a < e && *a == *b; ++a, ++b);
if (a < e){
if (*a < *b) result = -1;
else result = 1;
}
return(result);
}
enum DLL_Error{
dll_err_too_small_for_header = 1,
dll_err_wrong_MZ_signature,
dll_err_wrong_DOS_error,
dll_err_wrong_PE_signature,
dll_err_unrecognized_bit_signature,
};
b32
dll_parse_headers(Data file, DLL_Data *dll, i32 *error){
b32 result;
i32 pe_offset;
i32 read_pos;
result = 1;
if (file.size <= sizeof(DOS_Header) + DOS_error_size){
if (error) *error = dll_err_too_small_for_header;
result = 0;
goto dll_parse_end;
}
dll->dos_header = (DOS_Header*)file.data;
if (dll_compare(dll->dos_header->signature, "MZ", 2) != 0){
if (error) *error = dll_err_wrong_MZ_signature;
result = 0;
goto dll_parse_end;
}
if (file.size <= DOS_error_offset + DOS_error_size){
if (error) *error = dll_err_too_small_for_header;
result = 0;
goto dll_parse_end;
}
if (dll_compare((char*)(file.data + DOS_error_offset), DOS_error_message,
sizeof(DOS_error_message) - 1) != 0){
if (error) *error = dll_err_wrong_DOS_error;
result = 0;
goto dll_parse_end;
}
pe_offset = dll->dos_header->e_lfanew;
read_pos = pe_offset;
if (file.size <= read_pos + PE_header_size){
if (error) *error = dll_err_too_small_for_header;
result = 0;
goto dll_parse_end;
}
if (dll_compare((char*)(file.data + read_pos),
PE_header, PE_header_size) != 0){
if (error) *error = dll_err_wrong_PE_signature;
result = 0;
goto dll_parse_end;
}
read_pos += PE_header_size;
if (file.size <= read_pos + sizeof(COFF_Header)){
if (error) *error = dll_err_too_small_for_header;
result = 0;
goto dll_parse_end;
}
dll->coff_header = (COFF_Header*)(file.data + read_pos);
read_pos += sizeof(COFF_Header);
if (file.size <= read_pos + dll->coff_header->size_of_optional_header){
if (error) *error = dll_err_too_small_for_header;
result = 0;
goto dll_parse_end;
}
dll->opt_header_32 = (PE_Opt_Header_32Bit*)(file.data + read_pos);
dll->opt_header_64 = (PE_Opt_Header_64Bit*)(file.data + read_pos);
read_pos += dll->coff_header->size_of_optional_header;
if (dll->opt_header_32->signature != bitsig_32bit &&
dll->opt_header_32->signature != bitsig_64bit){
if (error) *error = dll_err_unrecognized_bit_signature;
result = 0;
goto dll_parse_end;
}
if (dll->opt_header_32->signature == bitsig_32bit) dll->is_64bit = 0;
else dll->is_64bit = 1;
dll->section_defs = (PE_Section_Definition*)(file.data + read_pos);
dll_parse_end:
return(result);
}
i32
dll_total_loaded_size(DLL_Data *dll){
COFF_Header *coff_header;
PE_Section_Definition *section_def;
i32 result, section_end, i;
coff_header = dll->coff_header;
section_def = dll->section_defs;
result = 0;
for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){
section_end = section_def->loaded_location + section_def->loaded_size;
if (section_end > result){
result = section_end;
}
}
return(result);
}
b32
dll_perform_reloc(DLL_Loaded *loaded){
Data img;
byte *base;
Relocation_Block_Header *header;
Relocation_Block_Entry *entry;
Data_Directory *data_directory;
u32 cursor;
u32 bytes_in_table;
u32 block_end;
u32 type;
u32 offset;
b32 result;
b32 highadj_stage;
u64 dif64;
result = 1;
img = loaded->img;
if (loaded->is_64bit){
data_directory = loaded->opt_header_64->data_directory;
dif64 = ((u64)img.data - (u64)loaded->opt_header_64->image_base);
}
else{
data_directory = loaded->opt_header_32->data_directory;
dif64 = ((u64)img.data - (u64)loaded->opt_header_32->image_base);
}
data_directory += image_dir_base_reloc_table;
base = img.data + data_directory->virtual_address;
bytes_in_table = data_directory->size;
highadj_stage = 1;
for (cursor = 0; cursor < bytes_in_table;){
header = (Relocation_Block_Header*)(base + cursor);
block_end = cursor + header->block_size;
cursor += sizeof(Relocation_Block_Header);
for (;cursor < block_end;){
entry = (Relocation_Block_Entry*)(base + cursor);
cursor += sizeof(Relocation_Block_Entry);
type = (u32)(entry->entry & reloc_entry_type_mask) >> reloc_entry_type_shift;
offset = (u32)(entry->entry & reloc_entry_offset_mask) + header->page_base_offset;
switch (type){
case image_base_absolute: break;
case image_base_high:
case image_base_low:
case image_base_highlow:
case image_base_highadj:
case image_base_arm_mov32a:
case image_base_arm_mov32t:
case image_base_mips_jmpaddr16:
result = 0;
goto dll_reloc_end;
case image_base_dir64:
*(u64*)(img.data + offset) += dif64;
break;
}
}
}
dll_reloc_end:
return(result);
}
b32
dll_load_sections(Data img, DLL_Loaded *loaded,
Data file, DLL_Data *dll){
COFF_Header *coff_header;
PE_Section_Definition *section_def;
u32 header_size;
u32 size;
u32 i;
coff_header = dll->coff_header;
section_def = dll->section_defs;
header_size =
(u32)((byte*)(section_def + coff_header->number_of_sections) - file.data);
memcpy(img.data, file.data, header_size);
memset(img.data + header_size, 0, img.size - header_size);
for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){
size = section_def->loaded_size;
if (size > section_def->disk_size)
size = section_def->disk_size;
memcpy(img.data + section_def->loaded_location,
file.data + section_def->disk_location,
size);
if (dll_compare(section_def->name, ".text", 5) == 0){
loaded->text_start = section_def->loaded_location;
loaded->text_size = section_def->loaded_size;
}
}
return(1);
}
void
dll_load(Data img, DLL_Loaded *loaded, Data file, DLL_Data *dll){
Data_Directory *export_dir;
dll_load_sections(img, loaded, file, dll);
loaded->img = img;
loaded->dos_header = (DOS_Header*)((byte*)img.data + ((byte*)dll->dos_header - file.data));
loaded->coff_header = (COFF_Header*)((byte*)img.data + ((byte*)dll->coff_header - file.data));
loaded->opt_header_32 = (PE_Opt_Header_32Bit*)
((byte*)img.data + ((byte*)dll->opt_header_32 - file.data));
loaded->opt_header_64 = (PE_Opt_Header_64Bit*)
((byte*)img.data + ((byte*)dll->opt_header_64 - file.data));
loaded->section_defs = (PE_Section_Definition*)
((byte*)img.data + ((byte*)dll->section_defs - file.data));
loaded->is_64bit = dll->is_64bit;
if (dll->is_64bit){
export_dir = dll->opt_header_64->data_directory;
}
else{
export_dir = dll->opt_header_32->data_directory;
}
export_dir += image_dir_entry_export;
loaded->export_start = export_dir->virtual_address;
dll_perform_reloc(loaded);
}
void*
dll_load_function(DLL_Loaded *dll, char *func_name, i32 size){
Data img;
DLL_Export_Directory_Table *export_dir;
DLL_Export_Address *address_ptr;
DLL_Export_Name *name_ptr;
void *result;
u32 count, i;
u32 result_offset;
u32 ordinal;
img = dll->img;
export_dir = (DLL_Export_Directory_Table*)(img.data + dll->export_start);
count = export_dir->number_of_name_pointers;
name_ptr = (DLL_Export_Name*)(img.data + export_dir->name_pointer_offset);
result = 0;
for (i = 0; i < count; ++i, ++name_ptr){
if (dll_compare((char*)img.data + name_ptr->name_offset,
func_name, size) == 0){
ordinal = ((u16*)(img.data + export_dir->ordinal_offset))[i];
#if 0
// NOTE(allen): The MS docs say to do this, but
// it appears to just be downright incorrect.
ordinal -= export_dir->ordinal_base;
#endif
address_ptr = (DLL_Export_Address*)(img.data + export_dir->address_offset);
address_ptr += ordinal;
result_offset = address_ptr->export_offset;
result = (img.data + result_offset);
break;
}
}
return(result);
}
#define MachineCase(x) case x: result = #x; *len = sizeof(#x) - 1; break
char*
dll_machine_type_str(u16 machine, i32 *len){
char *result;
i32 extra;
if (!len) len = &extra;
result = 0;
switch (machine){
MachineCase(intel_i386);
MachineCase(intel_i860);
MachineCase(mips_r3000);
MachineCase(mips_little_endian);
MachineCase(mips_r10000);
MachineCase(old_alpha_axp);
MachineCase(alpha_axp);
MachineCase(hitachi_sh3);
MachineCase(hitachi_sh3_dsp);
MachineCase(hitachi_sh4);
MachineCase(hitachi_sh5);
MachineCase(arm_little_endian);
MachineCase(thumb);
MachineCase(matsushita_am33);
MachineCase(power_pc_little_endian);
MachineCase(power_pc_with_floating);
MachineCase(intel_ia64);
MachineCase(mips16);
MachineCase(motorola_68000_series);
MachineCase(alpha_axp_64_bit);
MachineCase(mips_with_fpu);
MachineCase(mips16_with_fpu);
MachineCase(eft_byte_code);
MachineCase(amd_amd64);
MachineCase(mitsubishi_m32r_little_endian);
MachineCase(clr_pure_msil);
}
return(result);
}
#undef MachineCase
// BOTTOM

View File

@ -1,441 +0,0 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 20.11.2015
*
* DLL loader declarations for 4coder
*
*/
// TOP
struct DOS_Header {
char signature[2];
i16 lastsize;
i16 nblocks;
i16 nreloc;
i16 hdrsize;
i16 minalloc;
i16 maxalloc;
i16 ss;
i16 sp;
i16 checksum;
i16 ip;
i16 cs;
i16 relocpos;
i16 noverlay;
i16 reserved1[4];
i16 oem_id;
i16 oem_info;
i16 reserved2[10];
i32 e_lfanew;
};
enum Target_Machine_Code{
intel_i386 = 0x14C,
intel_i860 = 0x14D,
mips_r3000 = 0x162,
mips_little_endian = 0x166,
mips_r10000 = 0x168,
old_alpha_axp = 0x183,
alpha_axp = 0x184,
hitachi_sh3 = 0x1a2,
hitachi_sh3_dsp = 0x1a3,
hitachi_sh4 = 0x1a6,
hitachi_sh5 = 0x1a8,
arm_little_endian = 0x1c0,
thumb = 0x1c2,
matsushita_am33 = 0x1d3,
power_pc_little_endian = 0x1f0,
power_pc_with_floating = 0x1f1,
intel_ia64 = 0x200,
mips16 = 0x266,
motorola_68000_series = 0x268,
alpha_axp_64_bit = 0x284,
mips_with_fpu = 0x366,
mips16_with_fpu = 0x466,
eft_byte_code = 0xebc,
amd_amd64 = 0x8664,
mitsubishi_m32r_little_endian = 0x9041,
clr_pure_msil = 0xc0ee
};
#define file_is_exe 0x2
#define file_is_non_reloctable 0x200
#define file_is_dll 0x2000
struct COFF_Header{
u16 machine;
u16 number_of_sections;
u32 time_date_stamp;
u32 pointer_to_symbol_table;
u32 number_of_symbols;
u16 size_of_optional_header;
u16 characteristics;
};
struct Data_Directory{
u32 virtual_address;
u32 size;
};
// This version is untested
struct PE_Opt_Header_32Bit{
// Universal Portion
i16 signature;
i8 major_linker_version;
i8 minor_linker_version;
i32 size_of_code;
i32 size_of_initialized_data;
i32 size_of_uninitialized_data;
i32 address_of_entry_point;
i32 base_of_code;
i32 base_of_data;
// Windows Portion
i32 image_base;
i32 section_alignment;
i32 file_alignment;
i16 major_OS_version;
i16 minor_OS_version;
i16 major_image_version;
i16 minor_image_version;
i16 major_subsystem_version;
i16 minor_subsystem_version;
i32 reserved;
i32 size_of_image;
i32 size_of_headers;
i32 checksum;
i16 subsystem;
i16 DLL_characteristics;
i32 size_of_stack_reserve;
i32 size_of_stack_commit;
i32 size_of_heap_reserve;
i32 size_of_heap_commit;
i32 loader_flags;
i32 number_of_rva_and_sizes;
Data_Directory data_directory[16];
};
struct PE_Opt_Header_64Bit{
// Universal Portion
u16 signature;
u8 major_linker_version;
u8 minor_linker_version;
u32 size_of_code;
u32 size_of_initialized_data;
u32 size_of_uninitialized_data;
u32 address_of_entry_point;
u32 base_of_code;
// Windows Portion
u64 image_base;
u32 section_alignment;
u32 file_alignment;
u16 major_OS_version;
u16 minor_OS_version;
u16 major_image_version;
u16 minor_image_version;
u16 major_subsystem_version;
u16 minor_subsystem_version;
u32 reserved;
u32 size_of_image;
u32 size_of_headers;
u32 checksum;
u16 subsystem;
u16 DLL_characteristics;
u64 size_of_stack_reserve;
u64 size_of_stack_commit;
u64 size_of_heap_reserve;
u64 size_of_heap_commit;
u32 loader_flags;
u32 number_of_rva_and_sizes;
Data_Directory data_directory[16];
};
#define bitsig_32bit 267
#define bitsig_64bit 523
#define image_dir_entry_export 0
#define image_dir_entry_import 1
#define image_dir_entry_resource 2
#define image_dir_base_reloc_table 5
#define image_dir_entry_bound_import 11
struct PE_Section_Definition{
char name[8];
u32 loaded_size;
u32 loaded_location;
u32 disk_size;
u32 disk_location;
u32 disk_relocs;
u32 reserved1;
u16 number_of_relocs;
u16 reserved2;
u32 flags;
};
#define image_scn_type_no_pad 0x00000008
#define image_scn_cnt_code 0x00000020
#define image_scn_cnt_initialized_data 0x00000040
#define image_scn_cnt_uninitialized_data 0x00000080
#define image_scn_lnk_other 0x00000100
#define image_scn_lnk_info 0x00000200
#define image_scn_lnk_remove 0x00000800
#define image_scn_lnk_comdat 0x00001000
#define image_scn_no_defer_spec_exc 0x00004000
#define image_scn_gprel 0x00008000
#define image_scn_mem_fardata 0x00008000
#define image_scn_mem_purgeable 0x00020000
#define image_scn_mem_16BIT 0x00020000
#define image_scn_mem_locked 0x00040000
#define image_scn_mem_preload 0x00080000
#define image_scn_align_1bytes 0x00100000
#define image_scn_align_2bytes 0x00200000
#define image_scn_align_4bytes 0x00300000
#define image_scn_align_8bytes 0x00400000
#define image_scn_align_16bytes 0x00500000
#define image_scn_align_32bytes 0x00600000
#define image_scn_align_64bytes 0x00700000
#define image_scn_align_128bytes 0x00800000
#define image_scn_align_256bytes 0x00900000
#define image_scn_align_512bytes 0x00A00000
#define image_scn_align_1024bytes 0x00B00000
#define image_scn_align_2048bytes 0x00C00000
#define image_scn_align_4096bytes 0x00D00000
#define image_scn_align_8192bytes 0x00E00000
#define image_scn_align_mask 0x00F00000
#define image_scn_lnk_nreloc_ovfl 0x01000000
#define image_scn_mem_discardable 0x02000000
#define image_scn_mem_not_cached 0x04000000
#define image_scn_mem_not_paged 0x08000000
#define image_scn_mem_shared 0x10000000
#define image_scn_mem_execute 0x20000000
#define image_scn_mem_read 0x40000000
#define image_scn_mem_write 0x80000000
#pragma pack(push, 1)
struct COFF_Relocation{
u32 virtual_address;
u32 symbol_table_index;
u16 type;
};
#pragma pack(pop)
enum Image_Rel_Amd64{
image_rel_amd64_absolute = 0x00,
image_rel_amd64_addr64 = 0x01,
image_rel_amd64_addr32 = 0x02,
image_rel_amd64_addr32nb = 0x03,
image_rel_amd64_rel32 = 0x04,
image_rel_amd64_rel32_1 = 0x05,
image_rel_amd64_rel32_2 = 0x06,
image_rel_amd64_rel32_3 = 0x07,
image_rel_amd64_rel32_4 = 0x08,
image_rel_amd64_rel32_5 = 0x09,
image_rel_amd64_section = 0x0A,
image_rel_amd64_secrel = 0x0B,
image_rel_amd64_secrel7 = 0x0C,
image_rel_amd64_token = 0x0D,
image_rel_amd64_srel32 = 0x0E,
image_rel_amd64_pair = 0x0F,
image_rel_amd64_sspan32 = 0x10
};
enum Image_Rel_Arm{
image_rel_arm_absolute = 0x0,
image_rel_arm_addr32 = 0x1,
image_rel_arm_addr32nb = 0x2,
image_rel_arm_branch24 = 0x3,
image_rel_arm_branch11 = 0x4,
image_rel_arm_token = 0x5,
image_rel_arm_blx24 = 0x6,
image_rel_arm_blx11 = 0x7,
image_rel_arm_section = 0x8,
image_rel_arm_secrel = 0x9,
image_rel_arm_mov32a = 0xA,
image_rel_arm_mov32t = 0xB,
image_rel_arm_branch20t = 0xC,
image_rel_arm_branch24t = 0xD,
image_rel_arm_blx32t = 0xE
};
enum Image_Rel_Arm64{
image_rel_arm64_absolute = 0x0,
image_rel_arm64_addr32 = 0x1,
image_rel_arm64_addr32nb = 0x2,
image_rel_arm64_branch26 = 0x3,
image_rel_arm64_pagebase_rel21 = 0x4,
image_rel_arm64_rel21 = 0x5,
image_rel_arm64_pageoffset_12a = 0x6,
image_rel_arm64_pageoffset_12l = 0x7,
image_rel_arm64_secrel = 0x8,
image_rel_arm64_secrel_low12a = 0x9,
image_rel_arm64_secrel_high12a = 0xA,
image_rel_arm64_secrel_low12l = 0xB,
image_rel_arm64_token = 0xC,
image_rel_arm64_section = 0xD,
image_rel_arm64_addr64 = 0xE
};
// NOTE(allen):
// skipped Hitachi SuperH
// skiiped IBM PowerPC
enum Image_Rel_i386{
image_rel_i386_absolute = 0x0,
image_rel_i386_dir16 = 0x1,
image_rel_i386_rel16 = 0x2,
image_rel_i386_dir32 = 0x3,
image_rel_i386_dir32nb = 0x4,
image_rel_i386_seg12 = 0x5,
image_rel_i386_section = 0x6,
image_rel_i386_secrel = 0x7,
image_rel_i386_token = 0x8,
image_rel_i386_secrel7 = 0x9,
image_rel_i386_rel32 = 0xA
};
// NOTE(allen):
// skipped ia64
// skipped MIPS
// skiiped Mitsubishi
struct Relocation_Block_Header{
u32 page_base_offset;
u32 block_size;
};
#define reloc_entry_type_mask 0xF000
#define reloc_entry_type_shift 12
#define reloc_entry_offset_mask 0x0FFF
struct Relocation_Block_Entry{
u16 entry;
};
enum DLL_Relocation_Type{
image_base_absolute,
// nothing
image_base_high,
// add high 16 bits of diff to 16 bits at offset
image_base_low,
// add low 16 bits of diff to 16 bits at offset
image_base_highlow,
// adds all 32 bits to 32 bits at offset
image_base_highadj,
// consumes two slots: high 16 bits at location, low 16 bits at next location
image_base_arm_mov32a,
// mips: jump instruction; arm: MOVW+MOVT
image_base_reserved1,
image_base_arm_mov32t,
// MOVW+MOVT in Thumb mode
image_base_reserved2,
image_base_mips_jmpaddr16,
// mips16 jump instruction
image_base_dir64
// adds to 64 bits field
};
struct DLL_Data{
DOS_Header *dos_header;
COFF_Header *coff_header;
PE_Opt_Header_32Bit *opt_header_32;
PE_Opt_Header_64Bit *opt_header_64;
PE_Section_Definition *section_defs;
b32 is_64bit;
};
struct DLL_Loaded{
DOS_Header *dos_header;
COFF_Header *coff_header;
PE_Opt_Header_32Bit *opt_header_32;
PE_Opt_Header_64Bit *opt_header_64;
PE_Section_Definition *section_defs;
b32 is_64bit;
Data img;
u32 export_start;
u32 text_start;
u32 text_size;
};
struct DLL_Export_Directory_Table{
u32 export_flags;
u32 time_date_stamp;
u16 major_version;
u16 minor_version;
u32 name_offset;
u32 ordinal_base;
u32 number_of_addresses;
u32 number_of_name_pointers;
u32 address_offset;
u32 name_pointer_offset;
u32 ordinal_offset;
};
struct DLL_Export_Address{
u32 export_offset;
};
struct DLL_Export_Name{
u32 name_offset;
};
struct DLL_Export_Ordinal{
u16 ordinal;
};
struct DLL_Debug_Entry{
u32 characteristics;
u32 time_date_stamp;
u16 major_version;
u16 minor_version;
u32 type;
u32 size_of_data;
u32 offset_of_data;
u32 disk_offset_of_data;
} thingy;
enum DLL_Debug_Type{
img_dbg_type_unknown,
img_dbg_type_coff,
img_dbg_type_codeview,
img_dbg_type_fpo,
img_dbg_type_misc,
img_dbg_type_exception,
img_dbg_type_fixup,
img_dbg_type_omap_to_src,
img_dbg_type_omap_from_src
};
char DOS_error_message[] = "This program cannot be run in DOS mode.";
i32 DOS_error_offset = 0x4E;
i32 DOS_error_size = sizeof(DOS_error_message) - 1;
char PE_header[] = {'P', 'E', 0, 0};
i32 PE_header_size = 4;
// BOTTOM