📄 coffimpl.c
字号:
/**************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Emulate long format of import library members in memory.
* It is derived from short format of the import library members
* in source .LIB file for each entry.
* Emulated data is saved into coff_file_hnd structure.
*
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "walloca.h"
#include "coffimpl.h"
/* note before you use this for anything make sure that the values below
are large enough for your purposes (particularly the num sections and symbols.
I put in some saftey code in the string table because if the user declares
long function names (very long like > 512) it is possible that 1k will not
suffice for the string table size in import libraries. the number of sections
and symbols however are fixed and suffice for import libraries */
#define MAX_NUM_COFF_LIB_SECTIONS 8
#define MAX_NUM_COFF_LIB_SYMBOLS 32
#define INIT_MAX_SIZE_COFF_STRING_TABLE 1024
typedef struct {
coff_file_header header;
coff_section_header section[MAX_NUM_COFF_LIB_SECTIONS];
coff_symbol symbol[MAX_NUM_COFF_LIB_SYMBOLS];
unsigned_32 string_table_size;
unsigned_32 max_string_table_size;
char *string_table;
} coff_lib_file;
typedef enum {
IMPORT_DESCRIPTOR,
NULL_IMPORT_DESCRIPTOR,
NULL_THUNK_DATA,
ORDINAL, // ordinal and name
NAMED, // name only
ELF, // name only or name and ordinal
ELFRENAMED, // renamed entry,
} importType;
typedef struct {
importType type;
short processor;
char *DLLName;
long ordinal;
char *symName;
char *exportedName;
uint_32 time_date_stamp;
} import_sym;
#pragma pack ( push, 1 )
//IMPORT_DESCRIPT optional header
static char CoffImportAxpText[] = {
0x00,0x00,0x7F,0x27,0x00,0x00,0x7B,0xA3,0x00,0x00,0xFB,0x6B
};
static char CoffImportPpcText[] = {
0x00,0x00,0x62,0x81,0x00,0x00,0x8B,0x81,0x04,0x00,0x41,0x90,0xA6,0x03,0x89,0x7D,
0x04,0x00,0x4B,0x80,0x20,0x04,0x80,0x4E
};
static char CoffImportPpcPdata[] = {
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
0x0D,0x00,0x00,0x00
};
static char CoffImportX86Text[] = {
0xFF,0x25,0x00,0x00,0x00,0x00
};
static void InitCoffFile( coff_lib_file *c_file )
{
c_file->string_table = malloc( INIT_MAX_SIZE_COFF_STRING_TABLE );
c_file->max_string_table_size = INIT_MAX_SIZE_COFF_STRING_TABLE;
}
static void SetCoffFile( coff_lib_file *c_file, short processor,
unsigned_32 time_stamp, unsigned_16 opt_hdr_size)
{
c_file->header.cpu_type = processor;
c_file->header.num_sections = 0;
c_file->header.time_stamp = time_stamp;
c_file->header.num_symbols = 0;
c_file->header.opt_hdr_size = opt_hdr_size;
c_file->header.flags = IMAGE_FILE_32BIT_MACHINE;
c_file->string_table_size = 0;
}
static void FiniCoffLibFile( coff_lib_file *c_file )
{
free( c_file->string_table );
}
static void AddCoffString( coff_lib_file *c_file, char *name, int len )
{
len++;
if( ( c_file->string_table_size + len ) >= c_file->max_string_table_size ) {
c_file->max_string_table_size *= 2;
c_file->string_table = (char *)realloc( c_file->string_table, c_file->max_string_table_size );
}
memcpy( c_file->string_table + c_file->string_table_size, name, len );
c_file->string_table_size += len;
}
static int AddCoffSection( coff_lib_file *c_file, char *name, unsigned_32 size,
unsigned_16 num_relocs, unsigned_32 flags )
{
coff_section_header *section;
int len;
// Sections numbering has index base 1
section = c_file->section + c_file->header.num_sections;
len = strlen( name );
memset( section, 0, COFF_SECTION_HEADER_SIZE );
if( len > COFF_SEC_NAME_LEN ) {
section->name[0] = '/';
ultoa( c_file->string_table_size + 4, section->name + 1, 10 );
AddCoffString( c_file, name, len );
} else {
memcpy( section->name, name, len );
}
section->size = size;
section->num_relocs = num_relocs;
section->flags = flags;
return ++(c_file->header.num_sections);
}
static int AddCoffSymbol( coff_lib_file *c_file, char *name, unsigned_32 value,
signed_16 sec_num, unsigned_16 type, unsigned_8 class, unsigned_8 num_aux )
{
coff_symbol *sym;
int len;
// Symbols numbering has index base 0
sym = c_file->symbol + c_file->header.num_symbols;
len = strlen( name );
if( len > COFF_SYM_NAME_LEN ) {
sym->name.non_name.zeros = 0;
sym->name.non_name.offset = c_file->string_table_size + 4;
AddCoffString( c_file, name, len );
} else {
memset( sym->name.name_string, 0, COFF_SYM_NAME_LEN );
memcpy( sym->name.name_string, name, len );
}
sym->value = value;
sym->sec_num = sec_num;
sym->type = type;
sym->storage_class = class;
sym->num_aux = num_aux;
return c_file->header.num_symbols++;
}
static int AddCoffSymSec( coff_lib_file *c_file, unsigned_8 selection, int sec )
{
coff_sym_section *sym;
char name[9];
coff_section_header *section;
int symbol_no;
section = c_file->section + sec - 1;
memcpy( name, section->name, 8 );
name[8]='\0';
symbol_no = AddCoffSymbol( c_file, name, 0x0, sec, IMAGE_SYM_TYPE_NULL,
IMAGE_SYM_CLASS_STATIC, 1 );
sym = (coff_sym_section *) (c_file->symbol + c_file->header.num_symbols);
sym->length = section->size;
sym->num_relocs = section->num_relocs;
sym->num_line_numbers = 0;
sym->checksum = 0;
if( selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE ) {
sym->number = sec;
} else {
sym->number = 0;
}
sym->selection = selection;
c_file->header.num_symbols++;
return symbol_no;
}
#define IMPLIB_LEN (*((int*)coff_file_hnd->implib_data))
#define IMPLIB_POS (*((long*)(coff_file_hnd->implib_data + sizeof(int))))
#define IMPLIB_DATA (coff_file_hnd->implib_data + sizeof(int) + sizeof(long))
static int AddDataImpLib( coff_file_handle coff_file_hnd, void * buff, int len )
{
char * x;
if (coff_file_hnd->implib_data == NULL) {
coff_file_hnd->implib_data = _ClientAlloc(coff_file_hnd, sizeof(int) + sizeof(long) + len);
IMPLIB_LEN = sizeof(int) + sizeof(long);
IMPLIB_POS = 0;
} else {
x = _ClientAlloc(coff_file_hnd, IMPLIB_LEN + len);
memcpy(x, coff_file_hnd->implib_data, IMPLIB_LEN);
_ClientFree(coff_file_hnd, coff_file_hnd->implib_data);
coff_file_hnd->implib_data = x;
}
memcpy(coff_file_hnd->implib_data + IMPLIB_LEN, buff, len);
IMPLIB_LEN += len;
return ORL_OKAY;
}
static void * ImportLibRead(void * _coff_file_hnd, size_t len)
{
coff_file_handle coff_file_hnd = _coff_file_hnd;
IMPLIB_POS += len;
return IMPLIB_DATA + IMPLIB_POS - len;
}
static long ImportLibSeek(void * _coff_file_hnd, long pos, int where)
{
coff_file_handle coff_file_hnd = _coff_file_hnd;
if( where == SEEK_SET ) {
IMPLIB_POS = pos;
} else if( where == SEEK_CUR ) {
IMPLIB_POS += pos;
} else {
IMPLIB_POS = IMPLIB_LEN - sizeof(int) - sizeof(long) - pos;
}
return IMPLIB_POS;
}
static void CreateCoffFileHeader( coff_file_handle coff_file_hnd, coff_lib_file *c_file )
{
unsigned i;
unsigned_32 d_ptr;
d_ptr = sizeof(coff_file_header) + c_file->header.opt_hdr_size + c_file->header.num_sections * COFF_SECTION_HEADER_SIZE;
for( i = 0; i < c_file->header.num_sections; i++ ) {
c_file->section[i].rawdata_ptr = d_ptr;
d_ptr += c_file->section[i].size;
c_file->section[i].reloc_ptr = d_ptr;
d_ptr += c_file->section[i].num_relocs * COFF_RELOC_SIZE;
}
c_file->header.sym_table = d_ptr;
AddDataImpLib( coff_file_hnd, &( c_file->header ), sizeof(coff_file_header) );
}
static void CreateCoffSections( coff_file_handle coff_file_hnd, coff_lib_file *c_file )
{
AddDataImpLib( coff_file_hnd, c_file->section, c_file->header.num_sections * COFF_SECTION_HEADER_SIZE );
}
static void CreateCoffSymbols( coff_file_handle coff_file_hnd, coff_lib_file *c_file )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -