834 changes: 0 additions & 834 deletions src/libmscoff.c

This file was deleted.

758 changes: 758 additions & 0 deletions src/libmscoff.d

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,18 @@ SRC = win32.mak posix.mak osmodel.mak aggregate.h aliasthis.h arraytypes.h \
attrib.h complex_t.h cond.h ctfe.h ctfe.h declaration.h dsymbol.h \
enum.h errors.h expression.h globals.h hdrgen.h identifier.h idgen.d \
import.h init.h intrange.h json.h lexer.h lib.h macro.h \
mars.h module.h mtype.h nspace.h objc.h parse.h scanmscoff.c scanomf.c \
mars.h module.h mtype.h nspace.h objc.h parse.h \
scope.h statement.h staticassert.h target.h template.h tokens.h utf.h \
version.h visitor.h libomf.d scanomf.d $(DMD_SRCS)
version.h visitor.h libomf.d scanomf.d libmscoff.d scanmscoff.d \
$(DMD_SRCS)

ROOT_SRC = $(addprefix $(ROOT)/,aav.h array.h file.h filename.h \
longdouble.h newdelete.c object.h outbuffer.h port.h rmem.h \
root.h speller.h stringtable.h)

GLUE_SRC = glue.c msc.c s2ir.c todt.c e2ir.c tocsym.c \
toobj.c toctype.c tocvdebug.c toir.h toir.c \
libmscoff.c scanmscoff.c irstate.h irstate.c iasm.c \
irstate.h irstate.c iasm.c \
toelfdebug.c libelf.c scanelf.c libmach.c scanmach.c \
tk.c eh.c gluestub.c objc_glue.c objc_glue_stubs.c

Expand Down
199 changes: 0 additions & 199 deletions src/scanmscoff.c

This file was deleted.

249 changes: 249 additions & 0 deletions src/scanmscoff.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2015 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt

module ddmd.scanmscoff;

import core.stdc.string, core.stdc.stdlib, core.sys.windows.windows;
import ddmd.globals, ddmd.errors;

enum LOG = false;

/*****************************************
* Reads an object module from base[0..buflen] and passes the names
* of any exported symbols to (*pAddSymbol)().
* Input:
* pctx context pointer, pass to *pAddSymbol
* pAddSymbol function to pass the names to
* base[0..buflen] contains contents of object module
* module_name name of the object module (used for error messages)
* loc location to use for error printing
*/
extern (C++) void scanMSCoffObjModule(void* pctx, void function(void* pctx, char* name, int pickAny) pAddSymbol, void* base, size_t buflen, const(char)* module_name, Loc loc)
{
static if (LOG)
{
printf("scanMSCoffObjModule(%s)\n", module_name);
}
ubyte* buf = cast(ubyte*)base;
int reason;
/* First do sanity checks on object file
*/
if (buflen < BIGOBJ_HEADER.sizeof)
{
reason = __LINE__;
Lcorrupt:
error(loc, "MS-Coff object module %s is corrupt, %d", module_name, reason);
return;
}
BIGOBJ_HEADER* header = cast(BIGOBJ_HEADER*)buf;
char is_old_coff = false;
if (header.Sig2 != 0xFFFF && header.Version != 2)
{
is_old_coff = true;
IMAGE_FILE_HEADER* header_old;
header_old = cast(IMAGE_FILE_HEADER*)malloc(IMAGE_FILE_HEADER.sizeof);
memcpy(header_old, buf, IMAGE_FILE_HEADER.sizeof);
header = cast(BIGOBJ_HEADER*)malloc(BIGOBJ_HEADER.sizeof);
memset(header, 0, BIGOBJ_HEADER.sizeof);
header.Machine = header_old.Machine;
header.NumberOfSections = header_old.NumberOfSections;
header.TimeDateStamp = header_old.TimeDateStamp;
header.PointerToSymbolTable = header_old.PointerToSymbolTable;
header.NumberOfSymbols = header_old.NumberOfSymbols;
free(header_old);
}
switch (header.Machine)
{
case IMAGE_FILE_MACHINE_UNKNOWN:
case IMAGE_FILE_MACHINE_I386:
case IMAGE_FILE_MACHINE_AMD64:
break;
default:
if (buf[0] == 0x80)
error(loc, "Object module %s is 32 bit OMF, but it should be 64 bit MS-Coff", module_name);
else
error(loc, "MS-Coff object module %s has magic = %x, should be %x", module_name, header.Machine, IMAGE_FILE_MACHINE_AMD64);
return;
}
// Get string table: string_table[0..string_len]
size_t off = header.PointerToSymbolTable;
if (off == 0)
{
error(loc, "MS-Coff object module %s has no string table", module_name);
return;
}
off += header.NumberOfSymbols * (is_old_coff ? SymbolTable.sizeof : SymbolTable32.sizeof);
if (off + 4 > buflen)
{
reason = __LINE__;
goto Lcorrupt;
}
uint string_len = *cast(uint*)(buf + off);
char* string_table = cast(char*)(buf + off + 4);
if (off + string_len > buflen)
{
reason = __LINE__;
goto Lcorrupt;
}
string_len -= 4;
for (int i = 0; i < header.NumberOfSymbols; i++)
{
SymbolTable32* n;
char[8 + 1] s;
char* p;
static if (LOG)
{
printf("Symbol %d:\n", i);
}
off = header.PointerToSymbolTable + i * (is_old_coff ? SymbolTable.sizeof : SymbolTable32.sizeof);
if (off > buflen)
{
reason = __LINE__;
goto Lcorrupt;
}
n = cast(SymbolTable32*)(buf + off);
if (is_old_coff)
{
SymbolTable* n2;
n2 = cast(SymbolTable*)malloc(SymbolTable.sizeof);
memcpy(n2, (buf + off), SymbolTable.sizeof);
n = cast(SymbolTable32*)malloc(SymbolTable32.sizeof);
memcpy(n, n2, (n2.Name).sizeof);
n.Value = n2.Value;
n.SectionNumber = n2.SectionNumber;
n.Type = n2.Type;
n.StorageClass = n2.StorageClass;
n.NumberOfAuxSymbols = n2.NumberOfAuxSymbols;
free(n2);
}
if (n.Zeros)
{
strncpy(s.ptr, cast(const(char)*)n.Name, 8);
s[SYMNMLEN] = 0;
p = s.ptr;
}
else
p = string_table + n.Offset - 4;
i += n.NumberOfAuxSymbols;
static if (LOG)
{
printf("n_name = '%s'\n", p);
printf("n_value = x%08lx\n", n.Value);
printf("n_scnum = %d\n", n.SectionNumber);
printf("n_type = x%04x\n", n.Type);
printf("n_sclass = %d\n", n.StorageClass);
printf("n_numaux = %d\n", n.NumberOfAuxSymbols);
}
switch (n.SectionNumber)
{
case IMAGE_SYM_DEBUG:
continue;
case IMAGE_SYM_ABSOLUTE:
if (strcmp(p, "@comp.id") == 0)
continue;
break;
case IMAGE_SYM_UNDEFINED:
// A non-zero value indicates a common block
if (n.Value)
break;
continue;
default:
break;
}
switch (n.StorageClass)
{
case IMAGE_SYM_CLASS_EXTERNAL:
break;
case IMAGE_SYM_CLASS_STATIC:
if (n.Value == 0) // if it's a section name
continue;
continue;
case IMAGE_SYM_CLASS_FUNCTION:
case IMAGE_SYM_CLASS_FILE:
case IMAGE_SYM_CLASS_LABEL:
continue;
default:
continue;
}
(*pAddSymbol)(pctx, p, 1);
}
}

align(1)
struct BIGOBJ_HEADER
{
WORD Sig1; // IMAGE_FILE_MACHINE_UNKNOWN
WORD Sig2; // 0xFFFF
WORD Version; // 2
WORD Machine; // identifies type of target machine
DWORD TimeDateStamp; // creation date, number of seconds since 1970
BYTE UUID[16]; // { '\xc7', '\xa1', '\xba', '\xd1', '\xee', '\xba', '\xa9', '\x4b',
// '\xaf', '\x20', '\xfa', '\xf6', '\x6a', '\xa4', '\xdc', '\xb8' };
DWORD unused[4]; // { 0, 0, 0, 0 }
DWORD NumberOfSections; // number of sections
DWORD PointerToSymbolTable; // file offset of symbol table
DWORD NumberOfSymbols; // number of entries in the symbol table
};

align(1)
struct IMAGE_FILE_HEADER
{
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
}

enum SYMNMLEN = 8;

enum IMAGE_FILE_MACHINE_UNKNOWN = 0; // applies to any machine type
enum IMAGE_FILE_MACHINE_I386 = 0x14C; // x86
enum IMAGE_FILE_MACHINE_AMD64 = 0x8664; // x86_64

enum IMAGE_SYM_DEBUG = -2;
enum IMAGE_SYM_ABSOLUTE = -1;
enum IMAGE_SYM_UNDEFINED = 0;

enum IMAGE_SYM_CLASS_EXTERNAL = 2;
enum IMAGE_SYM_CLASS_STATIC = 3;
enum IMAGE_SYM_CLASS_LABEL = 6;
enum IMAGE_SYM_CLASS_FUNCTION = 101;
enum IMAGE_SYM_CLASS_FILE = 103;

align(1)
struct SymbolTable32
{
union
{
BYTE[SYMNMLEN] Name;
struct
{
DWORD Zeros;
DWORD Offset;
};
};
DWORD Value;
DWORD SectionNumber;
WORD Type;
BYTE StorageClass;
BYTE NumberOfAuxSymbols;
}

align(1)
struct SymbolTable
{
BYTE[SYMNMLEN] Name;
DWORD Value;
WORD SectionNumber;
WORD Type;
BYTE StorageClass;
BYTE NumberOfAuxSymbols;
}
13 changes: 5 additions & 8 deletions src/win32.mak
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ DMD_SRCS=access.d aggregate.d aliasthis.d apply.d argtypes.d arrayop.d \
impcnvtab.d init.d inline.d intrange.d json.d lexer.d lib.d link.d \
mars.d mtype.d nogc.d nspace.d objc_stubs.d opover.d optimize.d parse.d \
sapply.d sideeffect.d statement.d staticassert.d target.d tokens.d \
traits.d utf.d visitor.d libomf.d scanomf.d typinf.d
traits.d utf.d visitor.d libomf.d scanomf.d typinf.d \
libmscoff.d scanmscoff.d

# Glue layer
GLUEOBJ=glue.obj msc.obj s2ir.obj todt.obj e2ir.obj tocsym.obj \
toobj.obj toctype.obj tocvdebug.obj toir.obj \
libmscoff.obj scanmscoff.obj irstate.obj \
iasm.obj objc_glue_stubs.obj
irstate.obj iasm.obj objc_glue_stubs.obj

# D back end
BACKOBJ= go.obj gdag.obj gother.obj gflow.obj gloop.obj var.obj el.obj \
Expand All @@ -179,14 +179,14 @@ SRCS = win32.mak posix.mak osmodel.mak aggregate.h aliasthis.h arraytypes.h \
attrib.h complex_t.h cond.h ctfe.h ctfe.h declaration.h dsymbol.h \
enum.h errors.h expression.h globals.h hdrgen.h identifier.h idgen.d \
import.h init.h intrange.h json.h lexer.h lib.h macro.h \
mars.h module.h mtype.h nspace.h objc.h parse.h scanmscoff.c scanomf.c \
mars.h module.h mtype.h nspace.h objc.h parse.h \
scope.h statement.h staticassert.h target.h template.h tokens.h utf.h \
version.h visitor.h $(DMD_SRCS)

# Glue layer
GLUESRC= glue.c msc.c s2ir.c todt.c e2ir.c tocsym.c \
toobj.c toctype.c tocvdebug.c toir.h toir.c \
libmscoff.c scanmscoff.c irstate.h irstate.c iasm.c \
irstate.h irstate.c iasm.c \
toelfdebug.c libelf.c scanelf.c libmach.c scanmach.c \
tk.c eh.c objc_glue_stubs.c objc_glue.c

Expand Down Expand Up @@ -528,9 +528,6 @@ ptrntab.obj : $C\iasm.h $C\ptrntab.c
rtlsym.obj : $C\rtlsym.h $C\rtlsym.c
$(CC) -c $(MFLAGS) $C\rtlsym

scanmscoff.obj : $(TOTALH) $C\mscoff.h scanmscoff.c
$(CC) -c $(CFLAGS) -I.;$(ROOT);$C scanmscoff.c

ti_achar.obj : $C\tinfo.h $C\ti_achar.c
$(CC) -c $(MFLAGS) -I. $C\ti_achar

Expand Down