📄 cv.c
字号:
/*
Copyright 1994-2003 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
You may contact the author at:
mailto::camille@bluegrass.net
or by snail mail at:
David Lindauer
850 Washburn Ave Apt 99
Louisville, KY 40222
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <dos.h>
#include "langext.h"
#include "defines.h"
#include "types.h"
#include "subs.h"
#include "globals.h"
#include "cvinfo.h"
#include "cvexefmt.h"
#undef Mod
void linnum_new_module(bit_32 filenum, char *name)BeginDeclarations module_ptr
mod;
#define Mod (*mod)
EndDeclarations BeginCode
mod = (module_ptr)allocate_memory(sizeof(module_type) + strlen(name) + 1);
If filenum Is 1 Then Mod.debtypseg = cvdebtyp_lseg;
Mod.debsymseg = cvdebsym_lseg;
Mod.cseg = cseg_lseg;
Mod.dseg = dseg_lseg;
Mod.bseg = bseg_lseg;
Mod.constseg = constseg_lseg;
Mod.stringseg = stringseg_lseg;
If cseg_lseg IsNotNull Then(*cseg_lseg).hassyms = True;
EndIf;
If dseg_lseg IsNotNull Then(*dseg_lseg).hassyms = True;
EndIf;
If bseg_lseg IsNotNull Then(*bseg_lseg).hassyms = True;
EndIf;
EndIf
strcpy(Mod.name, name);
If n_modules NotLessThan max_modules Then max_modules += 64;
modules = (module_ptr_array)reallocate_memory(modules, (Bit_32(max_modules) +
1L) *Bit_32(sizeof(module_ptr)));
EndIf
modules[n_modules++] = current_module = mod;
EndCode
#undef Mod
void linnum_new_data(byte_ptr data, bit_32 len)BeginDeclarations
#define Current_module (*current_module)
EndDeclarations BeginCode If current_module IsNotNull Then
Current_module.linnum_data = (module_ptr)reallocate_memory
(Current_module.linnum_data, Current_module.linnum_len + len);
memcpy(Current_module.linnum_data + Current_module.linnum_len, data, len);
Current_module.linnum_len += len;
EndIf EndCode
#undef Current_module
void cv_globaltypes_data(byte_ptr *type_data, bit_32_ptr type_len, byte_ptr
*offset_data, bit_32_ptr offset_len)BeginDeclarations bit_32 i;
int_32 length;
bit_32 pos;
int_32 fieldslen;
bit_32 reclen;
bit_32 typeofs;
bit_32 typecount;
byte_ptr dragofs;
byte_ptr field_ptr;
byte_ptr typedat;
bit_32_ptr offsets;
bit_32 max_offsets;
bit_32 n_offsets;
module_ptr mod;
#define Mod (*mod)
#define NEWTYPE(x) (x) = ((x) >= CV_FIRST_NONPRIM ? (x) + typeofs -
CV_FIRST_NONPRIM : (x))
#define LEAFSIZE(x) ((*(bit_16_ptr)(x)) > 32767 ? 6 : 2) /* only two types of
numeric leaf allowed */
EndDeclarations BeginCode length = 0;
offsets = Null;
max_offsets = n_offsets = 0;
For i = 0;
i < n_modules;
i++BeginFor If modules[i]->debtypseg IsNotNull Then length += (*modules[i]
->debtypseg).length - 4;
EndIf EndFor
typeofs = CV_FIRST_NONPRIM;
typedat = (byte_ptr)allocate_memory(length);
pos = 0;
For i = 0;
i < n_modules;
i++BeginFor mod = modules[i];
If Mod.debtypseg IsNotNull Then length = (*modules[i]->debtypseg).length - 4;
dragofs = (*modules[i]->debtypseg).data + 4;
Mod.type_offset = typeofs;
typecount = 0;
While length GreaterThan 0 BeginWhile reclen = (*(bit_16_ptr)dragofs) + 2;
memcpy(typedat + pos, dragofs, reclen);
If n_offsets >= max_offsets Then max_offsets += 2048;
offsets = (bit_32_ptr)reallocate_memory(offsets, max_offsets *4);
EndIf offsets[n_offsets++] = pos;
dragofs += reclen;
length -= reclen;
typecount++;
Using *(bit_16_ptr)(typedat + pos + 2)BeginCase When LF_POINTER:
{
lfPointer *p = typedat + pos + 2;
NEWTYPE(p->u.utype);
}
//-------------------------------------------------------------------------
break;
When LF_ARRAY:
{
lfArray *p = typedat + pos + 2;
NEWTYPE(p->idxtype);
NEWTYPE(p->elemtype);
}
//-------------------------------------------------------------------------
break;
When LF_UNION:
{
lfUnion *p = typedat + pos + 2;
NEWTYPE(p->field);
}
//-------------------------------------------------------------------------
break;
When LF_CLASS: When LF_STRUCTURE:
{
lfClass *p = typedat + pos + 2;
NEWTYPE(p->field);
NEWTYPE(p->derived);
NEWTYPE(p->vshape);
}
//-------------------------------------------------------------------------
break;
When LF_ENUM:
{
lfEnum *p = typedat + pos + 2;
NEWTYPE(p->utype);
NEWTYPE(p->field);
}
//-------------------------------------------------------------------------
break;
When LF_BITFIELD:
{
lfBitfield *p = typedat + pos + 2;
NEWTYPE(p->type);
}
//-------------------------------------------------------------------------
break;
When LF_FIELDLIST: fieldslen = reclen - 4;
field_ptr = typedat + pos + 4;
While fieldslen GreaterThan 0 BeginWhile Using *(bit_16_ptr)field_ptr BeginCase
When LF_ENUMERATE:
{
lfEnumerate *p = field_ptr;
bit_32 len = LEAFSIZE(&p->value);
len += p->value[len] + sizeof(lfEnumerate);
field_ptr += len;
fieldslen -= len;
}
//-------------------------------------------------------------------------
break;
When LF_MEMBER:
{
lfMember *p = field_ptr;
bit_32 len = LEAFSIZE(&p->offset);
len += p->offset[len] + sizeof(lfMember);
NEWTYPE(p->index);
field_ptr += len;
fieldslen -= len;
}
//-------------------------------------------------------------------------
break;
When LF_INDEX:
{
lfIndex *p = field_ptr;
NEWTYPE(p->index);
}
//-------------------------------------------------------------------------
field_ptr += sizeof(lfIndex);
fieldslen -= sizeof(lfIndex);
break;
Otherwise: printf("CV - unknown debtype fieldlist %X in module %s\n", *
(bit_16_ptr)field_ptr, Mod.name);
field_ptr += sizeof(lfIndex);
fieldslen -= sizeof(lfIndex);
EndCase If fieldslen GreaterThan 0 AndIf((*field_ptr And 0xF0)IsEqualTo 0xF0)
Then fieldslen -= *field_ptr And 15;
field_ptr += *field_ptr And 15;
EndIf EndWhile break;
Otherwise: printf("CV - unknown debtyp field %X in module %s\n", *(bit_16_ptr)
(typedat + pos + 2), Mod.name);
EndCase
pos += reclen;
EndWhile
typeofs += typecount;
EndIf EndFor For i = 0;
i < n_offsets;
i++BeginFor offsets[i] += n_offsets * sizeof(bit_32) + sizeof(bit_32);
EndFor
*type_data = typedat;
*type_len = pos;
*offset_data = offsets;
*offset_len = n_offsets * sizeof(bit_32);
EndCode
#undef Mod
#undef NEWTYPE
#undef LEAFSIZE
void GlobalAddress(module_ptr mod, bit_32_ptr ofs, bit_16_ptr seg)
BeginDeclarations
#define Mod (*mod)
EndDeclarations Using *seg BeginCase When 1: *ofs += Mod.cseg->address;
If pefile.val IsFalse Then*seg = 0;
Else*ofs -= imageBase.val;
*seg = PE_code_seg + 1;
EndIf break;
When 2: *ofs += Mod.dseg->address;
If pefile.val IsFalse Then*seg = 0;
Else*seg = PE_data_seg + 1;
*ofs -= imageBase.val;
EndIf break;
When 3: *ofs += Mod.bseg->address;
If pefile.val IsFalse Then*seg = 0;
Else*seg = PE_bss_seg + 1;
*ofs -= imageBase.val;
EndIf break;
When 4: *ofs += Mod.constseg->address;
If pefile.val IsFalse Then*seg = 0;
Else*ofs -= imageBase.val;
*seg = PE_code_seg + 1;
EndIf break;
When 5: *ofs += Mod.stringseg->address;
If pefile.val IsFalse Then*seg = 0;
Else*ofs -= imageBase.val;
*seg = PE_code_seg + 1;
EndIf break;
EndCase BeginCode EndCode
#undef Mod
void cv_globalsyms_data(byte_ptr *globals, bit_32_ptr globallen)
BeginDeclarations byte align_copy[512];
bit_32 align_base;
bit_32 align_len;
byte_ptr align_ptr;
bit_32 blocklevel;
bit_32 copy_to_global;
byte_ptr cp;
bit_32 i;
Generic_Element_ptr generic;
byte global_copy[512];
bit_32 global_len;
bit_32 global_max;
byte_ptr global_ptr;
bit_32 length;
module_ptr mod;
bit_32 global_base;
bit_32 l;
LABELSYM32 *lab;
bit_32_ptr nextptr_global;
bit_32_ptr nextptr_align;
bit_32_ptr oldptr_global;
public_entry_ptr pub;
lseg_ptr lseg;
SEARCHSYM *search_ptr_align;
SEARCHSYM *search_ptr_global;
bit_32 symlen;
byte_ptr symptr;
#define Mod (*mod)
#define Pub (*pub)
#define Lseg (*lseg)
#define Lab (*lab)
#define NEWTYPE(x) (x) = ((x) >= CV_FIRST_NONPRIM ? (x) + Mod.type_offset -
CV_FIRST_NONPRIM : (x))
EndDeclarations BeginCode blocklevel = 0;
global_max = 65536;
global_ptr = (byte_ptr)allocate_memory(global_max);
search_ptr_global = global_ptr;
search_ptr_global->reclen = sizeof(SEARCHSYM);
search_ptr_global->reclen = ((search_ptr_global->reclen + 3)And ~3) - 2;
search_ptr_global->rectyp = S_SSEARCH;
global_len = search_ptr_global->reclen + 2;
global_base = global_len - 4; /* makes up for the 0002 in the obj file */
nextptr_align = Null;
nextptr_global = Null;
For i = 0;
i < n_modules;
i++BeginFor mod = modules[i];
lseg = Mod.debsymseg;
If lseg IsNull Then LoopIf(Mod.publics.first IsNull);
Mod.align_len = 65536;
Mod.align_data = (byte_ptr)allocate_memory(Mod.align_len);
align_base = - 4;
align_len = 0;
search_ptr_align = align_ptr = Mod.align_data;
search_ptr_align->reclen = sizeof(SEARCHSYM);
search_ptr_align->reclen = ((search_ptr_align->reclen + 3)And ~3) - 2;
search_ptr_align->rectyp = S_SSEARCH;
search_ptr_align->startsym = 0;
search_ptr_align->seg = 0;
align_len += search_ptr_align->reclen + 2;
search_ptr_align = NULL;
align_base += align_len;
TraverseList(Mod.publics, generic)BeginTraverse pub = (public_entry_ptr)(
(byte_ptr)generic - PUB_OFFSET_MODPUBS);
lab = (LABELSYM32*)align_copy;
Lab.reclen = sizeof(LABELSYM32) + (l = Pub.length);
cp = Pub.symbol;
If cp[0] Is '_'Then Lab.reclen--;
l--;
cp++;
EndIf;
If Lab.reclen % 4 Then Lab.reclen = Lab.reclen + 4-Lab.reclen % 4;
EndIf Lab.reclen -= 2;
Lab.rectyp = S_LABEL32;
memset(&Lab.flags, 0, sizeof(Lab.flags));
Lab.name[0] = l;
memcpy(Lab.name + 1, cp, l);
Lab.seg = Pub.Internal.segment_index;
Lab.off = Pub.Internal.offset;
GlobalAddress(mod, &Lab.off, &Lab.seg);
length = Lab.reclen + 2;
If global_len + length > global_max Then global_max += 65536;
oldptr_global = global_ptr;
global_ptr = (byte_ptr)reallocate_memory(global_ptr, global_max);
If nextptr_global IsNotNull Then nextptr_global = (bit_32_ptr)((bit_32)
nextptr_global + (bit_32)global_ptr - (bit_32)oldptr_global);
EndIf If search_ptr_global IsNotNull Then search_ptr_global = (bit_32_ptr)(
(bit_32)search_ptr_global + (bit_32)global_ptr - (bit_32)oldptr_global);
EndIf EndIf memcpy(global_ptr + global_len, align_copy, length);
global_len += length;
If align_len + length > Mod.align_len Then Mod.align_len += 65536;
align_ptr = (byte_ptr)reallocate_memory(align_ptr, Mod.align_len);
EndIf memcpy(align_ptr + align_len, align_copy, length);
align_len += length;
EndTraverse Else Mod.align_len = Lseg.length;
Mod.align_data = (byte_ptr)allocate_memory(Mod.align_len + sizeof(SEARCHSYM));
symptr = Lseg.data + 4;
symlen = Lseg.length - 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -